/** * Create releases from complete binaries. * * @param int|string $groupID (optional) * * @return int * @access public */ public function createReleases($groupID) { $startTime = time(); $group = $this->groups->getCBPTableNames($this->tablePerGroup, $groupID); $page = new Page(); $this->pdo->log->doEcho($this->pdo->log->primary('Creating releases from complete binaries')); $this->pdo->ping(true); // // Get out all distinct relname, group from binaries // $categorize = new \Categorize(['Settings' => $this->pdo]); $returnCount = $duplicate = 0; $result = $this->pdo->queryDirect(sprintf("SELECT %s.*, g.name AS group_name, count(%s.id) AS parts FROM %s INNER JOIN groups g ON g.id = %s.groupid WHERE %s procstat = %d AND relname IS NOT NULL GROUP BY relname, g.name, groupid, fromname ORDER BY COUNT(%s.id) DESC LIMIT %d", $group['bname'], $group['bname'], $group['bname'], $group['bname'], !empty($groupID) ? ' groupid = ' . $groupID . ' AND ' : ' ', Releases::PROCSTAT_READYTORELEASE, $group['bname'], $this->releaseCreationLimit)); while ($row = $this->pdo->getAssocArray($result)) { $relguid = $this->createGUID(); // Clean release name $releaseCleaning = new ReleaseCleaning(); $cleanRelName = $this->cleanReleaseName($row['relname']); $cleanedName = $releaseCleaning->releaseCleaner($row['relname'], $row['fromname'], $row['group_name']); if (is_array($cleanedName)) { $properName = $cleanedName['properlynamed']; $prehashID = isset($cleanerName['predb']) ? $cleanerName['predb'] : false; $isReqID = isset($cleanerName['requestid']) ? $cleanerName['requestid'] : false; $cleanedName = $cleanedName['cleansubject']; } else { $properName = true; $isReqID = $prehashID = false; } if ($prehashID === false && $cleanedName !== '') { // try to match the cleaned searchname to predb title or filename here $preHash = new PreHash(); $preMatch = $preHash->matchPre($cleanedName); if ($preMatch !== false) { $cleanedName = $preMatch['title']; $prehashID = $preMatch['prehashid']; $properName = true; } } $relid = $this->insertRelease(['name' => $this->pdo->escapeString($cleanRelName), 'searchname' => $this->pdo->escapeString(utf8_encode($cleanedName)), 'totalpart' => $row["parts"], 'groupid' => $row["groupid"], 'guid' => $this->pdo->escapeString($relguid), 'categoryid' => $categorize->determineCategory($groupID, $cleanedName), 'regexid' => $row["regexid"], 'postdate' => $this->pdo->escapeString($row['date']), 'fromname' => $this->pdo->escapeString($row['fromname']), 'reqid' => $row["reqid"], 'passwordstatus' => $page->settings->getSetting('checkpasswordedrar') > 0 ? -1 : 0, 'nzbstatus' => \Enzebe::NZB_NONE, 'isrenamed' => $properName === true ? 1 : 0, 'reqidstatus' => $isReqID === true ? 1 : 0, 'prehashid' => $prehashID === false ? 0 : $prehashID]); // // Tag every binary for this release with its parent release id // $this->pdo->queryExec(sprintf("UPDATE %s SET procstat = %d, releaseid = %d WHERE relname = %s AND procstat = %d AND %s fromname=%s", $group['bname'], Releases::PROCSTAT_RELEASED, $relid, $this->pdo->escapeString($row["relname"]), Releases::PROCSTAT_READYTORELEASE, !empty($groupID) ? ' groupid = ' . $groupID . ' AND ' : ' ', $this->pdo->escapeString($row["fromname"]))); $cat = new \Categorize(['Settings' => $this->pdo]); // // Write the nzb to disk // $catId = $cat->determineCategory($groupID, $cleanRelName); $nzbfile = $this->nzb->getNZBPath($relguid, $page->settings->getSetting('nzbpath'), true); $this->nzb->writeNZBforreleaseID($relid, $cleanRelName, $catId, $nzbfile, $groupID); // // Remove used binaries // $this->pdo->queryDelete(sprintf("DELETE %s, %s FROM %s JOIN %s ON %s.id = %s.binaryid WHERE releaseid = %d ", $group['pname'], $group['bname'], $group['pname'], $group['bname'], $group['bname'], $group['pname'], $relid)); // // If nzb successfully written, then load it and get size completion from it // $nzbInfo = new NZBInfo(); if (!$nzbInfo->loadFromFile($nzbfile)) { $this->pdo->log->doEcho($this->pdo->log->primary('Failed to write nzb file (bad perms?) ' . $nzbfile . '')); $this->delete($relid); } else { // Check if gid already exists $dupes = $this->pdo->queryOneRow(sprintf("SELECT EXISTS(SELECT 1 FROM releases WHERE gid = %s) AS total", $this->pdo->escapeString($nzbInfo->gid))); if ($dupes['total'] > 0) { $this->pdo->log->doEcho($this->pdo->log->primary('Duplicate - ' . $cleanRelName . '')); $this->delete($relid); $duplicate++; } else { $this->pdo->queryExec(sprintf("UPDATE releases SET totalpart = %d, size = %s, COMPLETION = %d, GID=%s , nzb_guid = %s WHERE id = %d", $nzbInfo->filecount, $nzbInfo->filesize, $nzbInfo->completion, $this->pdo->escapeString($nzbInfo->gid), $this->pdo->escapeString($nzbInfo->gid), $relid)); $this->pdo->log->doEcho($this->pdo->log->primary('Added release ' . $cleanRelName . '')); $returnCount++; if ($this->echoCLI) { $this->pdo->log->doEcho($this->pdo->log->primary('Added ' . $returnCount . 'releases.')); } } } } if ($this->echoCLI) { $this->pdo->log->doEcho($this->pdo->log->primary(PHP_EOL . number_format($returnCount) . ' Releases added and ' . number_format($duplicate) . ' duplicate releases deleted in ' . $this->consoleTools->convertTime(time() - $startTime)), true); } return ['added' => $returnCount, 'dupes' => $duplicate]; }