From 441332fc07def8c21f76cb29ffdc28ff818e77af Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 4 Jun 2015 22:50:38 +0200 Subject: [PATCH] Updated the NZB import and export scripts. --- misc/testing/nzb-export.php | 37 +++++++++ .../tmux/lib => }/testing/nzb-import.php | 16 ++-- misc/update_scripts/import.php | 6 +- newznab/controllers/NZBExport.php | 11 ++- newznab/controllers/NZBImport.php | 67 ++++++++-------- newznab/controllers/Releases.php | 77 ++++++++++++------- newznab/libraries/ForkingImportNZB.php | 2 +- 7 files changed, 136 insertions(+), 80 deletions(-) create mode 100644 misc/testing/nzb-export.php rename misc/{update_scripts/nix_scripts/tmux/lib => }/testing/nzb-import.php (89%) diff --git a/misc/testing/nzb-export.php b/misc/testing/nzb-export.php new file mode 100644 index 000000000..cc386c4d1 --- /dev/null +++ b/misc/testing/nzb-export.php @@ -0,0 +1,37 @@ +beginExport( + array( + // Path. + $argv[1], + // Start time. + (strtolower($argv[2]) === 'false' ? '' : $argv[2]), + // End time. + (strtolower($argv[3]) === 'false' ? '' : $argv[3]), + // Group ID. + (strtolower($argv[4]) === 'false' ? 0 : (int)$argv[4]), + // Gzip. + (strtolower($argv[5]) === 'true' ? true : false) + ) +); diff --git a/misc/update_scripts/nix_scripts/tmux/lib/testing/nzb-import.php b/misc/testing/nzb-import.php similarity index 89% rename from misc/update_scripts/nix_scripts/tmux/lib/testing/nzb-import.php rename to misc/testing/nzb-import.php index d2644da27..475c3e153 100644 --- a/misc/update_scripts/nix_scripts/tmux/lib/testing/nzb-import.php +++ b/misc/testing/nzb-import.php @@ -1,12 +1,12 @@ = $argv[5]) { @@ -72,9 +72,9 @@ if ($i > 1) { $useNzbName = ($argv[4] == 'true') ? true : false; // Create a new instance of NZBImport and send it the file locations. - $NZBImport = new \NZBImport(); + $NZBImport = new NZBImport(); $NZBImport->beginImport($nzbFiles, $useNzbName, $deleteNZB, $deleteFailedNZB); } else { echo 'Nothing found to import!' . $n; -} +} \ No newline at end of file diff --git a/misc/update_scripts/import.php b/misc/update_scripts/import.php index 10597a999..cc20566b3 100644 --- a/misc/update_scripts/import.php +++ b/misc/update_scripts/import.php @@ -97,10 +97,10 @@ foreach ($filestoprocess as $nzbFile) { $relguid = md5(uniqid()); $name = $releases->cleanReleaseName(str_replace(".nzb", "", basename($nzbFile))); $catId = $cat->determineCategory($groupName, $name); - $relid = $releases->insertRelease($name, $nzbInfo->filecount, $groupID, $relguid, $catId, "", date("Y-m-d H:i:s", $nzbInfo->postedlast), $nzbInfo->poster, "", $page->site); + $relid = $releases->insertRelease($name, $nzbInfo->filecount, $groupID, $relguid, $catId, "", date("Y-m-d H:i:s", $nzbInfo->postedlast), $nzbInfo->poster, "", $page->settings); $db->queryExec(sprintf("update releases set totalpart = %d, size = %s, completion = %d, GID=%s where id = %d", $nzbInfo->filecount, $nzbInfo->filesize, $nzbInfo->completion, $db->escapeString($nzbInfo->gid), $relid)); - $nzbfilename = $nzb->getNZBPath($relguid, $page->site->nzbpath, true); + $nzbfilename = $nzb->getNZBPath($relguid, $page->settings->getSetting('nzbpath'), true); $fp = gzopen($nzbfilename, "w"); if ($fp) { gzwrite($fp, $nzbInfo->toNzb()); @@ -154,7 +154,7 @@ foreach ($filestoprocess as $nzbFile) { $numbins++; if (count($postFile['segments']) > 0) { - $sql = "INSERT INTO parts (binaryID, messageID, number, partnumber, size) values "; + $sql = "INSERT INTO parts (binaryid, messageid, number, partnumber, size) values "; foreach ($postFile['segments'] as $fileSegmentNum => $fileSegment) { $sql .= sprintf("(%d, %s, 0, %d, %d),", $binaryId, $db->escapeString($fileSegment), $fileSegmentNum, $postFile['segmentbytes'][$fileSegmentNum]); $numparts++; diff --git a/newznab/controllers/NZBExport.php b/newznab/controllers/NZBExport.php index b7c954f9f..9dff1857d 100644 --- a/newznab/controllers/NZBExport.php +++ b/newznab/controllers/NZBExport.php @@ -51,7 +51,7 @@ class NZBExport * * @access public */ - public function __construct(array $options = array()) + public function __construct(array $options = []) { $defaults = [ 'Browser' => false, // Started from browser? @@ -65,8 +65,8 @@ class NZBExport $this->browser = $options['Browser']; $this->echoCLI = (!$this->browser && NN_ECHOCLI && $options['Echo']); $this->pdo = ($options['Settings'] instanceof Settings ? $options['Setting'] : new Settings()); - $this->releases = ($options['Releases'] instanceof \Releases ? $options['Releases'] : new \Releases(['Settings' => $this->pdo])); - $this->nzb = ($options['NZB'] instanceof \NZB ? $options['NZB'] : new \NZB($this->pdo)); + $this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['Settings' => $this->pdo])); + $this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB($this->pdo)); } /** @@ -140,9 +140,8 @@ class NZBExport // Loop over groups to take less RAM. foreach ($groups as $group) { $currentExport = 0; - $cat = ''; // Get all the releases based on the parameters. - $releases = $this->releases->getForExport($fromDate, $toDate, $group['id'], $cat); + $releases = $this->releases->getForExport($fromDate, $toDate, $group['id']); $totalFound = count($releases); if ($totalFound === 0) { if ($this->echoCLI) { @@ -182,7 +181,7 @@ class NZBExport } continue; } - // If not, decompress it and create a file to store it in. + // If not, decompress it and create a file to store it in. } else { $nzbContents = Utility::unzipGzipFile($nzbFile); if (!$nzbContents) { diff --git a/newznab/controllers/NZBImport.php b/newznab/controllers/NZBImport.php index 470b24216..e990d8a56 100644 --- a/newznab/controllers/NZBImport.php +++ b/newznab/controllers/NZBImport.php @@ -28,7 +28,7 @@ class NZBImport protected $releaseCleaner; /** - * @var bool|stdClass + * @var bool|\stdClass * @access protected */ protected $site; @@ -93,24 +93,24 @@ class NZBImport public function __construct(array $options = []) { $defaults = [ - 'Browser' => false, // Was this started from the browser? - 'Echo' => true, // Echo to CLI? - 'Binaries' => null, - 'Categorize' => null, - 'NZB' => null, - 'ReleaseCleaning' => null, - 'Releases' => null, - 'Settings' => null, + 'Browser' => false, // Was this started from the browser? + 'Echo' => true, // Echo to CLI? + 'Binaries' => null, + 'Categorize' => null, + 'NZB' => null, + 'ReleaseCleaning' => null, + 'Releases' => null, + 'Settings' => null, ]; $options += $defaults; $this->echoCLI = (!$this->browser && NN_ECHOCLI && $options['Echo']); $this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings()); - $this->binaries = ($options['Binaries'] instanceof \Binaries ? $options['Binaries'] : new \Binaries(['Settings' => $this->pdo, 'Echo' => $this->echoCLI])); - $this->category = ($options['Categorize'] instanceof \Categorize ? $options['Categorize'] : new \Categorize(['Settings' => $this->pdo])); - $this->nzb = ($options['NZB'] instanceof \NZB ? $options['NZB'] : new \NZB($this->pdo)); - $this->releaseCleaner = ($options['ReleaseCleaning'] instanceof \ReleaseCleaning ? $options['ReleaseCleaning'] : new \ReleaseCleaning($this->pdo)); - $this->releases = ($options['Releases'] instanceof \Releases ? $options['Releases'] : new \Releases(['settings' => $this->pdo])); + $this->binaries = ($options['Binaries'] instanceof Binaries ? $options['Binaries'] : new Binaries(['Settings' => $this->pdo, 'Echo' => $this->echoCLI])); + $this->category = ($options['Categorize'] instanceof Categorize ? $options['Categorize'] : new Categorize(['Settings' => $this->pdo])); + $this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB($this->pdo)); + $this->releaseCleaner = ($options['ReleaseCleaning'] instanceof ReleaseCleaning ? $options['ReleaseCleaning'] : new ReleaseCleaning($this->pdo)); + $this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['settings' => $this->pdo])); $this->crossPostt = ($this->pdo->getSetting('crossposttime') != '') ? $this->pdo->getSetting('crossposttime') : 2; $this->browser = $options['Browser']; @@ -182,11 +182,11 @@ class NZBImport if ($inserted) { // Try to copy the NZB to the NZB folder. - $path = $this->nzb->getNZBPath($this->relGuid, '', true); + $path = $this->nzb->getNZBPath($this->relGuid, 0, true); // Try to compress the NZB file in the NZB folder. - $fp = gzopen ($path, 'w5'); - gzwrite ($fp, $nzbString); + $fp = gzopen($path, 'w5'); + gzwrite($fp, $nzbString); gzclose($fp); if (!is_file($path)) { @@ -264,25 +264,25 @@ class NZBImport $groupID = -1; // Get the nzb info. - if ($firstName === false ) { - $firstName =(string) $file->attributes()->subject; + if ($firstName === false) { + $firstName = (string)$file->attributes()->subject; } if ($posterName === false) { - $posterName = (string) $file->attributes()->poster; + $posterName = (string)$file->attributes()->poster; } if ($postDate === false) { - $postDate = date("Y-m-d H:i:s", (string) $file->attributes()->date); + $postDate = date("Y-m-d H:i:s", (string)$file->attributes()->date); } // Make a fake message array to use to check the blacklist. - $msg = ["Subject" => (string) $file->attributes()->subject, "From" => (string) $file->attributes()->poster, "Message-ID" => ""]; + $msg = ["Subject" => (string)$file->attributes()->subject, "From" => (string)$file->attributes()->poster, "Message-ID" => ""]; - // Get the group names, groupid, check if it's blacklisted. + // Get the group names, group_id, check if it's blacklisted. $groupArr = []; foreach ($file->groups->group as $group) { - $group = (string) $group; + $group = (string)$group; - // If groupid is -1 try to get a groupid. + // If group_id is -1 try to get a group_id. if ($groupID === -1) { if (array_key_exists($group, $this->allGroups)) { $groupID = $this->allGroups[$group]; @@ -330,7 +330,7 @@ class NZBImport 'useFName' => $useNzbName, 'postDate' => (empty($postDate) ? date("Y-m-d H:i:s") : $postDate), 'from' => (empty($posterName) ? '' : $posterName), - 'groupid' => $groupID, + 'group_id' => $groupID, 'groupName' => $groupName, 'totalFiles' => $totalFiles, 'totalSize' => $totalSize @@ -393,19 +393,16 @@ class NZBImport 'name' => $escapedSubject, 'searchname' => $escapedSearchName, 'totalpart' => $nzbDetails['totalFiles'], - 'groupid' => $nzbDetails['groupid'], + 'groupid' => $nzbDetails['group_id'], 'guid' => $this->pdo->escapeString($this->relGuid), - 'regexid' => NULL, 'postdate' => $this->pdo->escapeString($nzbDetails['postDate']), 'fromname' => $escapedFromName, - 'reqid' => NULL, - 'passwordstatus' => ($this->pdo->getSetting('checkpasswordedrar') > 0 ? -1 : 0), 'size' => $this->pdo->escapeString($nzbDetails['totalSize']), - 'categoryid' => $this->category->determineCategory($nzbDetails['groupid'], $cleanName), + 'categoryid' => $this->category->determineCategory($nzbDetails['group_id'], $cleanName), 'isrenamed' => $renamed, 'reqidstatus' => 0, 'prehashid' => 0, - 'nzbstatus' => \Enzebe::NZB_ADDED + 'nzbstatus' => Enzebe::NZB_ADDED ] ); } else { @@ -419,10 +416,11 @@ class NZBImport } return true; } + /** * Get all groups in the DB. - * @return bool * + * @return bool * @access protected */ protected function getAllGroups() @@ -442,7 +440,8 @@ class NZBImport /** * Echo message to browser or CLI. - * @param $message + * + * @param string $message * * @access protected */ diff --git a/newznab/controllers/Releases.php b/newznab/controllers/Releases.php index fe4c4bb78..0e83c122d 100644 --- a/newznab/controllers/Releases.php +++ b/newznab/controllers/Releases.php @@ -441,38 +441,59 @@ class Releases } /** - * Get a range of releases. Used in nzb export + * Get list of releases available for export. + * + * @param string $postFrom (optional) Date in this format : 01/01/2014 + * @param string $postTo (optional) Date in this format : 01/01/2014 + * @param string $groupID (optional) Group ID. + * + * @return array */ - public function getForExport($postfrom, $postto, $group, $cat) + public function getForExport($postFrom = '', $postTo = '', $groupID = '') { + return $this->pdo->query( + sprintf( + "SELECT searchname, guid, groups.name AS gname, CONCAT(cp.title,'_',category.title) AS catName + FROM releases r + INNER JOIN category ON r.categoryid = category.id + INNER JOIN groups ON r.groupid = groups.id + INNER JOIN category cp ON cp.id = category.parentid + WHERE r.nzbstatus = %d + %s %s %s", + Enzebe::NZB_ADDED, + $this->exportDateString($postFrom), + $this->exportDateString($postTo, false), + (($groupID != '' && $groupID != '-1') ? sprintf(' AND group_id = %d ', $groupID) : '') + ) + ); + } - if ($postfrom != "") { - $dateparts = explode("/", $postfrom); - if (count($dateparts) == 3) - $postfrom = sprintf(" and postdate > %s ", $this->pdo->escapeString($dateparts[2] . "-" . $dateparts[1] . "-" . $dateparts[0] . " 00:00:00")); - else - $postfrom = ""; + /** + * Create a date query string for exporting. + * + * @param string $date + * @param bool $from + * + * @return string + */ + private function exportDateString($date, $from = true) + { + if ($date != '') { + $dateParts = explode('/', $date); + if (count($dateParts) === 3) { + $date = sprintf( + ' AND postdate %s %s ', + ($from ? '>' : '<'), + $this->pdo->escapeString( + $dateParts[2] . '-' . $dateParts[1] . '-' . $dateParts[0] . + ($from ? ' 00:00:00' : ' 23:59:59') + ) + ); + } else { + $date = ''; + } } - - if ($postto != "") { - $dateparts = explode("/", $postto); - if (count($dateparts) == 3) - $postto = sprintf(" and postdate < %s ", $this->pdo->escapeString($dateparts[2] . "-" . $dateparts[1] . "-" . $dateparts[0] . " 23:59:59")); - else - $postto = ""; - } - - if ($group != "" && $group != "-1") - $group = sprintf(" and groupid = %d ", $group); - else - $group = ""; - - if ($cat != "" && $cat != "-1") - $cat = sprintf(" and categoryid = %d ", $cat); - else - $cat = ""; - - return $this->pdo->queryDirect(sprintf("SELECT searchname, guid, CONCAT(cp.title,'_',category.title) AS catName FROM releases INNER JOIN category ON releases.categoryid = category.id LEFT OUTER JOIN category cp ON cp.id = category.parentid WHERE 1 = 1 %s %s %s %s", $postfrom, $postto, $group, $cat)); + return $date; } /** diff --git a/newznab/libraries/ForkingImportNZB.php b/newznab/libraries/ForkingImportNZB.php index 277b5ac20..6d1a3e904 100644 --- a/newznab/libraries/ForkingImportNZB.php +++ b/newznab/libraries/ForkingImportNZB.php @@ -21,7 +21,7 @@ class ForkingImportNZB extends Forking $options += $defaults; parent::__construct($options); - $this->importPath = (PHP_BINARY . ' ' . NN_TMUX . 'lib' . DS . 'testing' . DS . 'nzb-import.php '); + $this->importPath = (PHP_BINARY . ' ' . NN_MISC . 'testing' . DS . 'nzb-import.php '); $this->pdo = $options['settings']; }