Exemple #1
0
 public function setDefaultLabels($forceAllAlbums = FALSE)
 {
     $app = \Slim\Slim::getInstance();
     $this->jobPhase = 7;
     // check config
     if (isset($app->config['label-parent-directories']) === FALSE) {
         cliLog('aborting setDefaultLabels() no label directories configured', 2);
         return;
     }
     // validate config
     $foundValidDirectories = FALSE;
     foreach ($app->config['label-parent-directories'] as $dir) {
         if (is_dir($app->config['mpd']['musicdir'] . $dir) === FALSE) {
             cliLog('WARNING: label-parent-directory ' . $dir . ' does not exist', 2, 'yellow', 2);
         } else {
             $foundValidDirectories = TRUE;
         }
     }
     if ($foundValidDirectories === FALSE) {
         cliLog('aborting setDefaultLabels() no valid label directories configured', 2, 'red');
         return;
     }
     $this->beginJob(array('currentItem' => "fetching all track-labels for inserting into table:album ..."), __FUNCTION__);
     foreach ($app->config['label-parent-directories'] as $labelDir) {
         if (substr($labelDir, -1) !== DS) {
             $labelDir .= DS;
         }
         // append trailingSlash
         $query = "SELECT count(id) as itemCountTotal FROM track WHERE relativePath LIKE \"" . $labelDir . "%\" ";
         $this->itemCountTotal += $app->db->query($query)->fetch_assoc()['itemCountTotal'];
         $msg = "found " . $this->itemCountTotal . " to check";
         cliLog($msg, 2);
     }
     $updatedTracks = 0;
     foreach ($app->config['label-parent-directories'] as $labelDir) {
         if (substr($labelDir, -1) !== DS) {
             $labelDir .= DS;
         }
         // append trailingSlash
         $query = "SELECT id, labelId, relativePath FROM track WHERE relativePath LIKE \"" . $labelDir . "%\"";
         $result = $app->db->query($query);
         $counter = 0;
         $previousLabelString = "";
         $existingLabelIdsInDir = array();
         $updateTrackIds = array();
         while ($t = $result->fetch_assoc()) {
             $labelDirname = explode(DS, substr($t['relativePath'], strlen($labelDir)), 2);
             $labelDirname = $labelDirname[0];
             $counter++;
             $this->itemCountChecked++;
             if ($t['labelId'] == '' || $t['labelId'] == '1') {
                 $updateTrackIds[] = $t['id'];
                 $updatedTracks++;
             }
             if ($counter === 1) {
                 $previousLabelString = $labelDirname;
                 $existingLabelIdsInDir = array_merge($existingLabelIdsInDir, trimExplode(",", $t['labelId'], TRUE));
                 continue;
             }
             if ($labelDirname != $previousLabelString) {
                 // extract the most used label-id
                 $existingLabelIdsInDir = uniqueArrayOrderedByRelevance($existingLabelIdsInDir);
                 $foundMatchingDatabaseLabelId = FALSE;
                 // remove "Unknown Label (id=1)"
                 if (($key = array_search('1', $existingLabelIdsInDir)) !== false) {
                     unset($existingLabelIdsInDir[$key]);
                 }
                 if (count($existingLabelIdsInDir) > 0) {
                     $existingLabelId = array_shift($existingLabelIdsInDir);
                     $databaseLabel = \Slimpd\Label::getInstanceByAttributes(array('id' => $existingLabelId));
                     if (az09($previousLabelString) == $databaseLabel->getAz09()) {
                         $foundMatchingDatabaseLabelId = $databaseLabel->getId();
                         // everything is fine - we already have a label id
                         #$msg = "GOOD: directory: " . $previousLabelString . " matches database-label:" . $databaseLabel->getTitle();
                         #echo $msg . "\n";
                     } else {
                         // try to replace common substitutions
                         $teststring = str_replace(array("_", " and ", " point "), array(" ", " & ", " . "), $previousLabelString);
                         if (az09($teststring) == $databaseLabel->getAz09()) {
                             $foundMatchingDatabaseLabelId = $databaseLabel->getId();
                             #$msg = "GOOD: directory: " . $previousLabelString . " matches database-label:" . $databaseLabel->getTitle();
                         } else {
                             #$msg = "BAD: directory: " . $previousLabelString . " does NOT match database-label:" . $databaseLabel->getTitle();
                             #echo $msg . "\n";
                             #print_r($existingLabelIdsInDir);
                         }
                     }
                 }
                 if ($foundMatchingDatabaseLabelId === FALSE) {
                     $newLabelString = ucwords(str_replace("_", " ", $previousLabelString));
                     $msg = "generating labelstring from dirname: " . $newLabelString;
                     $foundMatchingDatabaseLabelId = join(",", \Slimpd\Label::getIdsByString($newLabelString));
                 } else {
                     $msg = "updating with ID: " . $foundMatchingDatabaseLabelId;
                 }
                 cliLog($msg, 3);
                 // update all tracks with labelId's
                 if (count($updateTrackIds) > 0) {
                     $query = "UPDATE track SET labelId=\"" . $foundMatchingDatabaseLabelId . "\"\n\t\t\t\t\t\t\tWHERE id IN (" . join(",", $updateTrackIds) . ")";
                     $app->db->query($query);
                 }
                 $this->updateJob(array('updatedTracks' => $updatedTracks, 'currentItem' => 'directory: ' . $t['relativePath'] . ' ' . $msg));
                 $previousLabelString = $labelDirname;
                 $existingLabelIdsInDir = trimExplode(",", $t['labelId'], TRUE);
                 $updateTrackIds = array();
             }
             $this->itemCountProcessed++;
         }
     }
     $this->finishJob(array('updatedTracks' => $updatedTracks), __FUNCTION__);
     return;
 }
Exemple #2
0
             case 'artist':
                 $config['itemlist'] = array();
                 foreach ($result['matches'] as $id => $foo) {
                     $config['itemlist'][] = \Slimpd\Artist::getInstanceByAttributes(array('id' => $id));
                 }
                 break;
             case 'genre':
                 $config['itemlist'] = array();
                 foreach ($result['matches'] as $id => $foo) {
                     $config['itemlist'][] = \Slimpd\Genre::getInstanceByAttributes(array('id' => $id));
                 }
                 break;
             case 'label':
                 $config['itemlist'] = array();
                 foreach ($result['matches'] as $id => $foo) {
                     $config['itemlist'][] = \Slimpd\Label::getInstanceByAttributes(array('id' => $id));
                 }
                 break;
         }
     }
 }
 //if($config['search']['all']['total'] == 0) {
 // TODO: suggest query with fuzzy input
 //}
 #echo "<pre>" . print_r($result,1); die();
 $config['action'] = 'searchresult.' . $currenttype;
 #if(isset($result['matches']) === FALSE) {
 #	$app->render('surrounding.twig', $config);
 #	return;
 #}
 $app->render('surrounding.twig', $config);