Move CBP deletion.

This commit is contained in:
DariusIII
2015-05-16 03:25:53 +02:00
parent acf4d9138d
commit c20b7856ab
3 changed files with 43 additions and 63 deletions
+35 -27
View File
@@ -8,6 +8,9 @@ use newznab\db\DB;
*/
class NZB
{
const NZB_NONE = 0; // Release has no NZB file yet.
const NZB_ADDED = 1; // Release had an NZB file created.
/**
* Determines if the site setting table per group is enabled.
* @var bool
@@ -76,8 +79,12 @@ class NZB
*/
protected $_nzbHeadString;
const NZB_NONE = 0; // Release has no NZB file yet.
const NZB_ADDED = 1; // Release had an NZB file created.
/**
* Names of CBP tables.
* @var array [string => string]
* @access protected
*/
protected $_tableNames;
/**
* Default constructor.
@@ -111,19 +118,22 @@ class NZB
public function initiateForWrite($groupID)
{
$this->groupID = $groupID;
$site = new Sites();
// Set table names
if ($this->tablePerGroup === true) {
if ($this->groupID == '') {
exit("$this->groupID is missing\n");
}
$cName = 'collections_' .$this->groupID;
$bName = 'binaries_' . $this->groupID;
$pName = 'parts_' . $this->groupID;
$this->_tableNames = [
'cName' => 'collections_' . $this->groupID,
'bName' => 'binaries_' . $this->groupID,
'pName' => 'parts_' . $this->groupID
];
} else {
$cName = 'collections';
$bName = 'binaries';
$pName = 'parts';
$this->_tableNames = [
'cName' => 'collections',
'bName' => 'binaries',
'pName' => 'parts'
];
}
$this->_collectionsQuery = sprintf(
@@ -131,22 +141,23 @@ class NZB
FROM %s
INNER JOIN groups ON %s.group_id = groups.id
WHERE %s.releaseid = ',
$cName,
$cName,
$cName,
$cName,
$cName
$this->_tableNames['cName'],
$this->_tableNames['cName'],
$this->_tableNames['cName'],
$this->_tableNames['cName'],
$this->_tableNames['cName']
);
$this->_binariesQuery = (
'SELECT id, name, totalparts FROM '. $bName .' WHERE collection_id = %d ORDER BY name'
'SELECT id, name, totalparts FROM ' . $this->_tableNames['bName'] . ' WHERE collection_id = %d ORDER BY name'
);
$this->_partsQuery = (
'SELECT DISTINCT(messageid), size, partnumber FROM ' . $pName . ' WHERE binaryid = %d ORDER BY partnumber'
'SELECT DISTINCT(messageid), size, partnumber FROM ' . $this->_tableNames['pName'] .
' WHERE binaryid = %d ORDER BY partnumber'
);
$this->_nzbHeadString = (
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE nzb PUBLIC \"-//newzBin//DTD NZB 1.1//EN\" \"http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd\">\n<!-- NZB Generated by: newznab-tmux " .
$site->version() . ' ' . htmlspecialchars(date('F j, Y, g:i a O'), ENT_QUOTES, 'utf-8') .
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE nzb PUBLIC \"-//newzBin//DTD NZB 1.1//EN\" \"http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd\">\n<!-- NZB Generated by: newznab " .
$this->pdo->version() . ' ' . htmlspecialchars(date('F j, Y, g:i a O'), ENT_QUOTES, 'utf-8') .
" -->\n<nzb xmlns=\"http://www.newzbin.com/DTD/2003/nzb\">\n<head>\n <meta type=\"category\">%s</meta>\n <meta type=\"name\">%s</meta>\n</head>\n\n"
);
}
@@ -154,7 +165,7 @@ class NZB
/**
* Write an NZB to the hard drive for a single release.
*
* @param int $relID The id of the release in the DB.
* @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.
@@ -169,7 +180,6 @@ class NZB
$fp = gzopen($path, 'wb7');
if ($fp) {
$nzb_guid = '';
$gid = '';
gzwrite(
$fp,
sprintf(
@@ -202,9 +212,6 @@ class NZB
if ($nzb_guid === '') {
$nzb_guid = $part['messageid'];
}
if ($gid === '') {
$gid = $part['messageid'];
}
$string .= (
' <segment bytes="' . $part['size']
. '" number="' . $part['partnumber'] . '">'
@@ -230,13 +237,14 @@ class NZB
gzclose($fp);
if (is_file($path)) {
// Mark release as having NZB and delete CBP.
$this->pdo->queryExec(
sprintf('
UPDATE releases SET nzbstatus = %d %s %s WHERE id = %d',
\NZB::NZB_ADDED,
UPDATE releases SET nzbstatus = %d %s WHERE id = %d;
DELETE FROM %s WHERE releaseid = %d',
NZB::NZB_ADDED,
($nzb_guid === '' ? '' : ', nzb_guid = ' . $this->pdo->escapestring(md5($nzb_guid))),
($gid === '' ? '' : ', gid = ' . $this->pdo->escapestring(md5($gid))),
$relID
$relID, $this->_tableNames['cName'], $relID
)
);
@@ -112,7 +112,7 @@ class TmuxOutput extends Tmux
$buffer = '';
$state = ($this->runVar['settings']['is_running'] == 1) ? 'Running' : 'Disabled';
//$version = $this->_tvers . 'r' . $this->_vers;
$tversion = '0.6r0218';
$tversion = '0.6r0221';
$buffer .= sprintf($this->tmpMasks[2],
"Monitor $state v$tversion @ $this->_tvers [" . $this->_vers ."]: ",
@@ -660,7 +660,6 @@ class ProcessReleases
public function createNZBs($groupID)
{
$startTime = time();
$group = $this->groups->getCBPTableNames($this->tablePerGroup, $groupID);
if ($this->echoCLI) {
$this->pdo->log->doEcho($this->pdo->log->header("Process Releases -> Create the NZB, delete collections/binaries/parts."));
@@ -674,11 +673,11 @@ class ProcessReleases
INNER JOIN category c ON r.categoryid = c.id
INNER JOIN category cp ON cp.id = c.parentid
WHERE %s nzbstatus = 0",
(!empty($groupID) ? ' r.groupid = ' . $groupID . ' AND ' : ' ')
(!empty($groupID) ? ' r.group_id = ' . $groupID . ' AND ' : ' ')
)
);
$deleted = $nzbCount = 0;
$nzbCount = 0;
if ($releases && $releases->rowCount()) {
$total = $releases->rowCount();
@@ -689,47 +688,20 @@ class ProcessReleases
if ($this->nzb->writeNZBforReleaseId($release['id'], $release['guid'], $release['name'], $release['title']) === true) {
$nzbCount++;
if ($this->echoCLI) {
echo $this->pdo->log->primaryOver("Creating NZBs:\t" . $nzbCount . '/' . $total . "\r");
echo $this->pdo->log->primaryOver("Creating NZBs and deleting Collections:\t" . $nzbCount . '/' . $total . "\r");
}
}
}
}
$nzbEnd = time();
if ($nzbCount > 0) {
if ($this->echoCLI) {
$this->pdo->log->doEcho(
$this->pdo->log->primary(
PHP_EOL . 'Deleting collections/binaries/parts, be patient.'
)
);
}
$deleteQuery = $this->pdo->queryExec(
sprintf('
DELETE c FROM %s c
INNER JOIN releases r ON r.id = c.releaseid
WHERE r.nzbstatus = %d
AND c.filecheck = %d',
$group['cname'],
\NZB::NZB_ADDED,
self::COLLFC_INSERTED
)
);
if ($deleteQuery !== false) {
$deleted = $deleteQuery->rowCount();
}
}
$deleteEnd = time();
$totalTime = (time() - $startTime);
if ($this->echoCLI) {
$this->pdo->log->doEcho(
$this->pdo->log->primary(
number_format($nzbCount) . ' NZBs created in ' . ($nzbEnd - $startTime) . ' seconds.' . PHP_EOL .
'Deleted ' . number_format($deleted) . ' collections in ' . ($deleteEnd - $nzbEnd) . ' seconds.' . PHP_EOL .
'Total time: ' . $this->pdo->log->primary($this->consoleTools->convertTime(time() - $startTime))
number_format($nzbCount) . ' NZBs created/Collections deleted in ' .
$totalTime . ' seconds.' . PHP_EOL .
'Total time: ' . $this->pdo->log->primary($this->consoleTools->convertTime($totalTime))
)
);
}