From d6a624b30c081c61524158dd318f3fbe4c8ee458 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. (cherry picked from commit 441332f) --- misc/testing/nzb-export.php | 37 ++++++++++ .../tmux/lib => }/testing/nzb-import.php | 16 ++--- misc/update_scripts/import.php | 12 ++-- newznab/controllers/NZBExport.php | 8 +-- newznab/controllers/NZBImport.php | 71 +++++++++---------- newznab/libraries/ForkingImportNZB.php | 2 +- 6 files changed, 89 insertions(+), 57 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 da7ab41ca..cc20566b3 100644 --- a/misc/update_scripts/import.php +++ b/misc/update_scripts/import.php @@ -6,8 +6,8 @@ use newznab\db\Settings; use newznab\utility\Utility; $releases = new Releases(); -$db = new newznab\db\Settings(); -$cat = new Category(); +$db = new Settings(); +$cat = new Categorize(); $releaseRegex = new ReleaseRegex(); $nzb = new NZB(); $page = new Page(); @@ -20,7 +20,7 @@ $categoryoverride = -1; if (empty($argc) || $argc <= 1) { $path = "./"; } else { - $util = new newznab\utility\Utility(); + $util = new Utility(); $path = (!$util->endsWith($argv[1], "/") ? $argv[1] . "/" : $argv[1]); if (isset($argv[2])) $usefilename = strtolower($argv[2]) == 'true'; @@ -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->NZBPath($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 4fe605f1a..f9fb15ad6 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)); } /** @@ -181,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 c37ab4cde..034a3e050 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,28 +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; - $s = new Sites(); - $this->site = $s->get(); $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])); - $s = new Sites(); - $this->site = $s->get(); + $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']; @@ -186,11 +182,11 @@ class NZBImport if ($inserted) { // Try to copy the NZB to the NZB folder. - $path = $this->nzb->NZBPath($this->relGuid, '', true); + $path = $this->nzb->NZBPath($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)) { @@ -268,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]; @@ -334,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 @@ -397,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' => \NZB::NZB_ADDED + 'nzbstatus' => NZB::NZB_ADDED ] ); } else { @@ -423,10 +416,11 @@ class NZBImport } return true; } + /** * Get all groups in the DB. - * @return bool * + * @return bool * @access protected */ protected function getAllGroups() @@ -446,7 +440,8 @@ class NZBImport /** * Echo message to browser or CLI. - * @param $message + * + * @param string $message * * @access protected */ diff --git a/newznab/libraries/ForkingImportNZB.php b/newznab/libraries/ForkingImportNZB.php index cd714f36e..9b9b03083 100644 --- a/newznab/libraries/ForkingImportNZB.php +++ b/newznab/libraries/ForkingImportNZB.php @@ -22,7 +22,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']; }