Exemplo n.º 1
0
 private function scan_releases(&$processed, &$total, $limit = Null)
 {
     // Scan all nzb files whos releases match against data
     // that has no nfo files associated with it.
     //
     // nzb files are further parsed for nfo segments that can
     // be extracted and applied to the release
     $nzb = new NZB();
     $db = new Settings();
     // How many releases to handle at a time
     $batch = Nfo::NNTP_BATCH_COUNT;
     // Build NFO List
     $nfometa = array();
     // Missing NFO Query (oldest first so they don't expire on us)
     $mnfo = "SELECT id,guid, name FROM releases r " . "WHERE r.releasenfoid = " . Nfo::FLAG_NFO_PENDING . " ORDER BY postdate DESC";
     if ($limit !== Null && $limit > 0) {
         $mnfo .= " LIMIT {$limit}";
     }
     $res = $db->query($mnfo);
     if ($res) {
         foreach ($res as $r) {
             $nzbfile = $nzb->getNZBPath($r["guid"]);
             if (!is_file($nzbfile)) {
                 if ($this->verbose) {
                     echo sprintf("%s Missing NZB File: %d/%s ...\n", 'NfoProc', intval($r["id"]), $r["name"]);
                 }
                 $this->setNfoMissing($r["id"]);
                 continue;
             }
             $nzbInfo = new NzbInfo();
             if (!$nzbInfo->loadFromFile($nzbfile)) {
                 if ($this->verbose) {
                     echo sprintf("%s Unable to parse NZB File: %d/%s ...\n", 'NfoProc', intval($r["id"]), $r["name"]);
                 }
                 $this->setNfoMissing($r["id"]);
                 continue;
             }
             $total += 1;
             $filename = basename($nzbfile);
             if ($this->verbose) {
                 echo sprintf("NfoProc : Scanning %s - ", $r["name"]);
             }
             $matches = $this->nfo_scan($nzbInfo);
             unset($nzbInfo);
             if (is_array($matches)) {
                 if (!count($matches)) {
                     if ($this->verbose) {
                         echo "nfo missing.\n";
                     }
                     $this->setNfoMissing($r["id"]);
                     continue;
                 }
             } else {
                 if ($this->verbose) {
                     echo "corrupt nzb.\n";
                 }
                 $this->setNfoMissing($r["id"]);
                 continue;
             }
             if ($this->verbose) {
                 echo count($matches) . " possible nfo(s).\n";
             }
             $processed++;
             // Hash Matches by Release id
             $nfometa[(string) $r["id"]] = $matches;
             if (!($processed % $batch)) {
                 $nfoblob = array();
                 if ($this->verbose) {
                     echo "NfoProc : Retrieval ...";
                 }
                 if ($this->nfo_grab($nfometa, $nfoblob)) {
                     $before = array_keys($nfoblob);
                     $this->parse_blobs($nfometa, $nfoblob);
                     $after = array_keys($nfoblob);
                     $removed = array_diff($before, $after);
                     $this->store_blob($nfometa, $nfoblob, $removed);
                 }
                 if ($this->verbose) {
                     echo "\n";
                 }
                 // Reset nfo list array
                 $nfometa = array();
             }
         }
         if ($processed % $batch) {
             $nfoblob = array();
             if ($this->verbose) {
                 echo "NfoProc : Retrieval ...";
             }
             if ($this->nfo_grab($nfometa, $nfoblob)) {
                 $before = array_keys($nfoblob);
                 $this->parse_blobs($nfometa, $nfoblob);
                 $after = array_keys($nfoblob);
                 $removed = array_diff($before, $after);
                 $this->store_blob($nfometa, $nfoblob, $removed);
             }
             if ($this->verbose) {
                 echo "\n";
             }
         }
     }
     return true;
 }
 public function processGID($limit = 500, $batch = 5000, $delete_broken_releases = false)
 {
     // Process until someone presses cntrl-c
     $db = new DB();
     $nzb = new NZB();
     $processed = 0;
     // We need an offset for tracking unhandled issues
     $offset = 0;
     $fsql = 'SELECT ID, name, guid FROM releases ' . 'WHERE GID IS NULL ORDER BY adddate DESC LIMIT %d,%d';
     $usql = "UPDATE releases SET GID = '%s' WHERE ID = %d";
     while (1) {
         // finish
         if ($limit > 0 && $processed >= $limit) {
             break;
         }
         $batch = $limit > 0 && $batch > $limit ? $limit : $batch;
         $res = $db->query(sprintf($fsql, $offset, $batch));
         if (!$res) {
             break;
         }
         if (count($res) <= 0) {
             break;
         }
         $offset += $batch;
         foreach ($res as $r) {
             $nzbfile = $nzb->getNZBPath($r["guid"]);
             if ($nzbfile === Null) {
                 continue;
             }
             $nzbInfo = new NzbInfo();
             if (!$nzbInfo->loadFromFile($nzbfile)) {
                 if ($delete_broken_releases) {
                     $release = new Releases();
                     $release->delete($r['ID']);
                     // Free the variable in an attempt to recover memory
                     unset($release);
                     echo '-';
                 } else {
                     // Skip over this one for future fetches
                     $offset++;
                 }
                 continue;
             }
             $gid = false;
             if (!empty($nzbInfo->gid)) {
                 $gid = $nzbInfo->gid;
             }
             // Free the variable in an attempt to recover memory
             unset($nzbInfo);
             if (!$gid) {
                 if ($delete_broken_releases) {
                     $release = new Releases();
                     $release->delete($r['ID']);
                     unset($release);
                     echo '-';
                 } else {
                     // Skip over this one for future fetches
                     $offset++;
                 }
                 continue;
             }
             // Update DB With Global Identifer
             $ures = $db->exec(sprintf($usql, $gid, $r['ID']));
             if ($ures < 0) {
                 printf("\nPostPrc : Failed to update: %s\n", $r['name']);
             }
             // make noise...
             echo '.';
             $processed += 1;
         }
     }
     # Batch update for comment table
     $usql = 'UPDATE releasecomment, releases ' . 'SET releasecomment.GID = releases.GID ' . 'WHERE releases.ID = releasecomment.releaseID ' . 'AND releasecomment.GID IS NULL ' . 'AND releases.GID IS NOT NULL ';
     $affected = $db->exec(sprintf($usql));
     if ($affected > 0) {
         $processed += $affected;
     }
     return $processed;
 }