示例#1
0
    /**
     * Create NZB files from complete releases.
     *
     * @param int|string $groupID (optional)
     *
     * @return int
     * @access public
     */
    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."));
        }
        $releases = $this->pdo->queryDirect(sprintf("\n\t\t\t\tSELECT CONCAT(COALESCE(cp.title,'') , CASE WHEN cp.title IS NULL THEN '' ELSE ' > ' END , c.title) AS title,\n\t\t\t\t\tr.name, r.id, r.guid\n\t\t\t\tFROM releases r\n\t\t\t\tINNER JOIN category c ON r.categoryid = c.id\n\t\t\t\tINNER JOIN category cp ON cp.id = c.parentid\n\t\t\t\tWHERE %s nzbstatus = 0", !empty($groupID) ? ' r.group_id = ' . $groupID . ' AND ' : ' '));
        $deleted = $nzbCount = 0;
        if ($releases && $releases->rowCount()) {
            $total = $releases->rowCount();
            // Init vars for writing the NZB's.
            $this->nzb->initiateForWrite($groupID);
            foreach ($releases as $release) {
                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");
                    }
                }
            }
        }
        $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();
        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))));
        }
        return $nzbCount;
    }