From e62f479b050d0942172c55cfaf26184e84d7bce6 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Thu, 29 Dec 2016 10:46:54 +0100 Subject: [PATCH] Add new class that expands NZB class, adjust other classes accordingly --- nntmux/Binaries.php | 14 +- nntmux/NZB.php | 66 +++------ nntmux/NZBMultiGroup.php | 188 ++++++++++++++++++++++++++ nntmux/ReleasesMultiGroup.php | 11 +- nntmux/processing/ProcessReleases.php | 2 +- 5 files changed, 221 insertions(+), 60 deletions(-) create mode 100644 nntmux/NZBMultiGroup.php diff --git a/nntmux/Binaries.php b/nntmux/Binaries.php index ce42b74ef..72dc3ee86 100755 --- a/nntmux/Binaries.php +++ b/nntmux/Binaries.php @@ -704,7 +704,7 @@ class Binaries } // Used to group articles together when forming the release/nzb. - if ($multiGroup === true) { + if ($multiGroup) { $header['CollectionKey'] = ( $this->_collectionsCleaning->collectionsCleaner($matches[1], '') . $header['From'] . @@ -737,7 +737,7 @@ class Binaries totalfiles, collectionhash, dateadded) VALUES (%s, %s, FROM_UNIXTIME(%s), %s, %d, %d, '%s', NOW()) ON DUPLICATE KEY UPDATE dateadded = NOW(), noise = '%s'", - ($multiGroup === true ? $tableNames['mgrcname'] : $tableNames['cname']), + ($multiGroup ? $tableNames['mgrcname'] : $tableNames['cname']), $this->_pdo->escapeString(substr(utf8_encode($matches[1]), 0, 255)), $this->_pdo->escapeString(utf8_encode($header['From'])), (is_numeric($header['Date']) ? ($header['Date'] > $now ? $now : $header['Date']) : $now), @@ -767,8 +767,8 @@ class Binaries INSERT INTO %s (binaryhash, name, collection_id, totalparts, currentparts, filenumber, partsize) VALUES (UNHEX('%s'), %s, %d, %d, 1, %d, %d) ON DUPLICATE KEY UPDATE currentparts = currentparts + 1, partsize = partsize + %d", - ($multiGroup === true ? $tableNames['mgrbname'] : $tableNames['bname']), - ($multiGroup === true ? md5($matches[1] . $header['From']) : md5($matches[1] . $header['From'] . $groupMySQL['id'])), + ($multiGroup ? $tableNames['mgrbname'] : $tableNames['bname']), + ($multiGroup ? md5($matches[1] . $header['From']) : md5($matches[1] . $header['From'] . $groupMySQL['id'])), $this->_pdo->escapeString(utf8_encode($matches[1])), $collectionID, $matches[3], @@ -807,7 +807,7 @@ class Binaries '(' . $binaryID . ',' . $header['Number'] . ',' . rtrim($header['Message-ID'], '>') . "'," . $matches[2] . ',' . $header['Bytes'] . '),'; - if ($multiGroup === true) { + if ($multiGroup) { $mgrPartsQuery .= '(' . $binaryID . ',' . $header['Number'] . ',' . rtrim($header['Message-ID'], '>') . "'," . $matches[2] . ',' . $header['Bytes'] . '),'; @@ -823,7 +823,7 @@ class Binaries // End of processing headers. $timeCleaning = number_format($startUpdate - $startCleaning, 2); - $binariesQuery = $binariesCheck = sprintf('INSERT INTO %s (id, partsize, currentparts) VALUES ', ($multiGroup === true ? $tableNames['mgrbname'] : $tableNames['bname'])); + $binariesQuery = $binariesCheck = sprintf('INSERT INTO %s (id, partsize, currentparts) VALUES ', ($multiGroup ? $tableNames['mgrbname'] : $tableNames['bname'])); foreach ($binariesUpdate as $binaryID => $binary) { $binariesQuery .= '(' . $binaryID . ',' . $binary['Size'] . ',' . $binary['Parts'] . '),'; } @@ -849,7 +849,7 @@ class Binaries $this->_pdo->Rollback(); } - if ($multiGroup === true) { + if ($multiGroup) { $this->_pdo->beginTransaction(); if (((strlen($mgrPartsQuery) === strlen($mgrPartsCheck)) ? true : $this->_pdo->queryExec(rtrim($mgrPartsQuery, ',')))) { $this->_pdo->Commit(); diff --git a/nntmux/NZB.php b/nntmux/NZB.php index 376ba4cc8..3dde16278 100755 --- a/nntmux/NZB.php +++ b/nntmux/NZB.php @@ -33,14 +33,14 @@ class NZB * * @var int */ - private $nzbSplitLevel; + protected $nzbSplitLevel; /** * Path to store NZB files. * * @var string */ - private $siteNzbPath; + protected $siteNzbPath; /** * Group id when writing NZBs. @@ -142,18 +142,15 @@ class NZB /** * Initiate class vars when writing NZB's. * - * @param int $groupID - * - * @param bool $multiGroup + * @param int $groupID * * @access public */ - public function initiateForWrite($groupID, $multiGroup = false) + public function initiateForWrite($groupID) { $this->groupID = $groupID; - // Set table names - if ($this->tablePerGroup === true && $multiGroup === false) { + if ($this->tablePerGroup === true) { if ($this->groupID == '') { exit("{$this->groupID} is missing\n"); } @@ -162,44 +159,28 @@ class NZB 'bName' => 'binaries_' . $this->groupID, 'pName' => 'parts_' . $this->groupID ]; - } else if ($this->tablePerGroup === true && $multiGroup === true) { - $this->_tableNames = [ - 'cName' => 'collections_' . $this->groupID, - 'bName' => 'binaries_' . $this->groupID, - 'pName' => 'parts_' . $this->groupID, - 'mgrcName' => 'mgr_collections', - 'mgrbName' => 'mgr_binaries', - 'mgrpName' => 'mgr_parts' - ]; } else { $this->_tableNames = [ 'cName' => 'collections', 'bName' => 'binaries', - 'pName' => 'parts', - 'mgrcName' => 'mgr_collections', - 'mgrbName' => 'mgr_binaries', - 'mgrpName' => 'mgr_parts' + 'pName' => 'parts' ]; } - $tablesc = $multiGroup === true ? $this->_tableNames['mgrcName'] : $this->_tableNames['cName']; - $tablesb = $multiGroup === true ? $this->_tableNames['mgrbName'] : $this->_tableNames['bName']; - $tablesp = $multiGroup === true ? $this->_tableNames['mgrpName'] : $this->_tableNames['pName']; - - $this->_collectionsQuery = " + $this->_collectionsQuery = " SELECT c.*, UNIX_TIMESTAMP(c.date) AS udate, g.name AS groupname - FROM {$tablesc} c + FROM {$this->_tableNames['cName']} c INNER JOIN groups g ON c.group_id = g.id WHERE c.releaseid = "; - $this->_binariesQuery = " + $this->_binariesQuery = " SELECT b.id, b.name, b.totalparts - FROM {$tablesb} b + FROM {$this->_tableNames['bName']} b WHERE b.collection_id = %d ORDER BY b.name ASC"; - $this->_partsQuery = " + $this->_partsQuery = " SELECT DISTINCT(p.messageid), p.size, p.partnumber - FROM {$tablesp} p + FROM {$this->_tableNames['pName']} p WHERE p.binaryid = %d ORDER BY p.partnumber ASC"; @@ -222,7 +203,7 @@ class NZB * * @access public */ - public function writeNZBforReleaseId($relID, $relGuid, $name, $cTitle, $multigroup = false) + public function writeNZBforReleaseId($relID, $relGuid, $name, $cTitle) { $collections = $this->pdo->queryDirect($this->_collectionsQuery . $relID); @@ -320,21 +301,12 @@ class NZB ) ); // Delete CBP for release that has its NZB created. - if ($multigroup === true) { - $this->pdo->queryExec( - sprintf(' + $this->pdo->queryExec( + sprintf(' DELETE c, b, p FROM %s c JOIN %s b ON(c.id=b.collection_id) STRAIGHT_JOIN %s p ON(b.id=p.binaryid) WHERE c.releaseid = %d', - $this->_tableNames['mgrcName'], $this->_tableNames['mgrbName'], $this->_tableNames['mgrpName'], $relID - ) - ); - } else { - $this->pdo->queryExec( - sprintf(' - DELETE c, b, p FROM %s c JOIN %s b ON(c.id=b.collection_id) STRAIGHT_JOIN %s p ON(b.id=p.binaryid) WHERE c.releaseid = %d', - $this->_tableNames['cName'], $this->_tableNames['bName'], $this->_tableNames['pName'], $relID - ) - ); - } + $this->_tableNames['cName'], $this->_tableNames['bName'], $this->_tableNames['pName'], $relID + ) + ); // Chmod to fix issues some users have with file permissions. chmod($path, 0777); @@ -352,7 +324,7 @@ class NZB * * @access public */ - private function buildNZBPath($releaseGuid, $levelsToSplit, $createIfNotExist) + protected function buildNZBPath($releaseGuid, $levelsToSplit, $createIfNotExist) { $nzbPath = ''; diff --git a/nntmux/NZBMultiGroup.php b/nntmux/NZBMultiGroup.php new file mode 100644 index 000000000..ebe73251c --- /dev/null +++ b/nntmux/NZBMultiGroup.php @@ -0,0 +1,188 @@ +_tableNames = [ + 'mgrcName' => 'mgr_collections', + 'mgrbName' => 'mgr_binaries', + 'mgrpName' => 'mgr_parts' + ]; + + $this->_collectionsQuery = " + SELECT c.*, UNIX_TIMESTAMP(c.date) AS udate, + g.name AS groupname + FROM {$this->_tableNames['mgrcName']} c + INNER JOIN groups g ON c.group_id = g.id + WHERE c.releaseid = "; + $this->_binariesQuery = " + SELECT b.id, b.name, b.totalparts + FROM {$this->_tableNames['mgrbName']} b + WHERE b.collection_id = %d + ORDER BY b.name ASC"; + $this->_partsQuery = " + SELECT DISTINCT(p.messageid), p.size, p.partnumber + FROM {$this->_tableNames['mgrpName']} p + WHERE p.binaryid = %d + ORDER BY p.partnumber ASC"; + + $this->_nzbCommentString = sprintf( + "NZB Generated by: NNTmux %s %s", + (new li3Versions())->getGitTagInRepo(), + Utility::htmlfmt(date('F j, Y, g:i a O')) + ); + } + + /** + * Write an NZB to the hard drive for a single release. + * + * @param int $relID The ID of the release in the DB. + * @param string $relGuid The guid of the release. + * @param string $name The name of the release. + * @param string $cTitle The name of the category this release is in. + * + * @return bool Have we successfully written the NZB to the hard drive? + * + * @access public + */ + public function writeMgrNZBforReleaseId($relID, $relGuid, $name, $cTitle) + { + $collections = $this->pdo->queryDirect($this->_collectionsQuery . $relID); + + if (!$collections instanceof \Traversable) { + return false; + } + + $xmlwrtr = new \XMLWriter(); + $xmlwrtr->openMemory(); + $xmlwrtr->setIndent(true); + $xmlwrtr->setIndentString(' '); + + $nzb_guid = ''; + + $xmlwrtr->startDocument('1.0', 'UTF-8'); + $xmlwrtr->startDtd(self::NZB_DTD_NAME, self::NZB_DTD_PUBLIC, self::NZB_DTD_EXTERNAL); + $xmlwrtr->endDtd(); + $xmlwrtr->writeComment($this->_nzbCommentString); + + $xmlwrtr->startElement('nzb'); + $xmlwrtr->writeAttribute('xmlns', self::NZB_XML_NS); + $xmlwrtr->startElement('head'); + $xmlwrtr->startElement('meta'); + $xmlwrtr->writeAttribute('type', 'category'); + $xmlwrtr->text($cTitle); + $xmlwrtr->endElement(); + $xmlwrtr->startElement('meta'); + $xmlwrtr->writeAttribute('type', 'name'); + $xmlwrtr->text($name); + $xmlwrtr->endElement(); + $xmlwrtr->endElement(); //head + + foreach ($collections as $collection) { + $binaries = $this->pdo->queryDirect(sprintf($this->_binariesQuery, $collection['id'])); + if (!$binaries instanceof \Traversable) { + return false; + } + + $poster = $collection['fromname']; + + foreach ($binaries as $binary) { + $parts = $this->pdo->queryDirect(sprintf($this->_partsQuery, $binary['id'])); + if (!$parts instanceof \Traversable) { + return false; + } + + $subject = $binary['name'] . '(1/' . $binary['totalparts'] . ')'; + $xmlwrtr->startElement('file'); + $xmlwrtr->writeAttribute('poster', $poster); + $xmlwrtr->writeAttribute('date', $collection['udate']); + $xmlwrtr->writeAttribute('subject', $subject); + $xmlwrtr->startElement('groups'); + if (preg_match_all('#(\S+):\S+#', $collection['xref'], $matches)) { + foreach ($matches[1] as $group) { + $xmlwrtr->writeElement('group', $group); + } + } + $xmlwrtr->endElement(); //groups + $xmlwrtr->startElement('segments'); + foreach ($parts as $part) { + if ($nzb_guid === '') { + $nzb_guid = $part['messageid']; + } + $xmlwrtr->startElement('segment'); + $xmlwrtr->writeAttribute('bytes', $part['size']); + $xmlwrtr->writeAttribute('number', $part['partnumber']); + $xmlwrtr->text($part['messageid']); + $xmlwrtr->endElement(); + } + $xmlwrtr->endElement(); //segments + $xmlwrtr->endElement(); //file + } + } + $xmlwrtr->endElement(); //nzb + $xmlwrtr->endDocument(); + $path = ($this->buildNZBPath($relGuid, $this->nzbSplitLevel, true) . $relGuid . '.nzb.gz'); + $fp = gzopen($path, 'wb7'); + if (!$fp) { + return false; + } + gzwrite($fp, $xmlwrtr->outputMemory()); + gzclose($fp); + unset($xmlwrtr); + if (!is_file($path)) { + echo "ERROR: $path does not exist.\n"; + + return false; + } + // Mark release as having NZB. + $this->pdo->queryExec( + sprintf(' + UPDATE releases SET nzbstatus = %d %s WHERE id = %d', + NZB::NZB_ADDED, ($nzb_guid === '' ? '' : ', nzb_guid = UNHEX( ' . $this->pdo->escapeString(md5($nzb_guid)) . ' )'), + $relID + ) + ); + // Delete CBP for release that has its NZB created. + $this->pdo->queryExec( + sprintf(' + DELETE c, b, p FROM %s c JOIN %s b ON(c.id=b.collection_id) STRAIGHT_JOIN %s p ON(b.id=p.binaryid) WHERE c.releaseid = %d', + $this->_tableNames['mgrcName'], $this->_tableNames['mgrbName'], $this->_tableNames['mgrpName'], $relID + ) + ); + // Chmod to fix issues some users have with file permissions. + chmod($path, 0777); + + return true; + } +} diff --git a/nntmux/ReleasesMultiGroup.php b/nntmux/ReleasesMultiGroup.php index 41b63c660..a52c222dd 100644 --- a/nntmux/ReleasesMultiGroup.php +++ b/nntmux/ReleasesMultiGroup.php @@ -57,10 +57,11 @@ class ReleasesMultiGroup $this->echoCLI = NN_ECHOCLI; $this->releases = new Releases(['Settings' => $this->_pdo, 'Groups' => $this->_groups]); $this->consoleTools = new ConsoleTools(['ColorCLI' => $this->_pdo->log]); - $this->nzb = new NZB($this->_pdo); + $this->nzb = new NZBMultiGroup(); $this->releaseCleaning = new ReleaseCleaning($this->_pdo); $this->tablePerGroup = (Settings::value('..tablepergroup') == 0 ? false : true); $this->releaseCreationLimit = (Settings::value('..maxnzbsprocessed') != '' ? (int)Settings::value('..maxnzbsprocessed') : 1000); + $this->mgrFromNames = implode(",", self::$mgrPosterNames); } /** @@ -283,7 +284,7 @@ class ReleasesMultiGroup * @return int * @access public */ - public function createNZBs() + public function createMGRNZBs() { $releases = $this->_pdo->queryDirect( @@ -293,7 +294,7 @@ class ReleasesMultiGroup FROM releases r INNER JOIN categories c ON r.categories_id = c.id INNER JOIN categories cp ON cp.id = c.parentid - WHERE nzbstatus = 0" + WHERE r.nzbstatus = 0 AND r.fromname IN (%s)", $this->_pdo->escapeString($this->mgrFromNames) ) ); @@ -302,10 +303,10 @@ class ReleasesMultiGroup if ($releases && $releases->rowCount()) { $total = $releases->rowCount(); // Init vars for writing the NZB's. - $this->nzb->initiateForWrite('', true); + $this->nzb->initiateForMgrWrite(); foreach ($releases as $release) { - if ($this->nzb->writeNZBforReleaseId($release['id'], $release['guid'], $release['name'], $release['title'], true) === true) { + if ($this->nzb->writeMgrNZBforReleaseId($release['id'], $release['guid'], $release['name'], $release['title']) === true) { $nzbCount++; if ($this->echoCLI) { echo $this->_pdo->log->primaryOver("Creating NZBs and deleting Collections:\t" . $nzbCount . '/' . $total . "\r"); diff --git a/nntmux/processing/ProcessReleases.php b/nntmux/processing/ProcessReleases.php index d6e6cfae7..1001d50fe 100755 --- a/nntmux/processing/ProcessReleases.php +++ b/nntmux/processing/ProcessReleases.php @@ -771,7 +771,7 @@ class ProcessReleases $this->pdo->log->doEcho($this->pdo->log->header("Process Releases -> Create the NZB, delete collections/binaries/parts.")); } - (new ReleasesMultiGroup())->createNZBs(); + (new ReleasesMultiGroup())->createMgrNZBs(); $releases = $this->pdo->queryDirect( sprintf("