From 05a7d87ccf4e197e745ce7154a2746a0a537db63 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 26 Dec 2017 15:41:38 +0100 Subject: [PATCH] Start moving functions from Releases class to Release model --- Changelog | 1 + app/Models/Release.php | 96 ++++++++++++++++++++ misc/testing/Dev/test-ReleaseCleaner.php | 2 +- nntmux/NZBImport.php | 2 +- nntmux/ReleaseFiles.php | 16 ++-- nntmux/Releases.php | 93 ------------------- nntmux/SphinxSearch.php | 66 +++++++------- nntmux/processing/ProcessReleases.php | 2 +- nntmux/processing/post/ProcessAdditional.php | 5 +- public/admin/release-edit.php | 3 +- 10 files changed, 148 insertions(+), 138 deletions(-) diff --git a/Changelog b/Changelog index 7b5d22fed..068e25901 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2017-12-26 DariusIII + * Chg: Start moving functions from Releases class to Release model * Chg: Update composer.json and .lock files * Fix: Add back command removed by mistake to monitor.php * Chg: Update switch.php and add imdb and tv lookup settings to procesNfoFiles function call diff --git a/app/Models/Release.php b/app/Models/Release.php index e15447520..c4161a3b7 100644 --- a/app/Models/Release.php +++ b/app/Models/Release.php @@ -3,6 +3,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; +use nntmux\SphinxSearch; class Release extends Model { @@ -105,4 +107,98 @@ class Release extends Model { return $this->hasMany(ReleasesGroups::class, 'releases_id'); } + + /** + * Insert a single release returning the ID on success or false on failure. + * + * @param array $parameters Insert parameters, must be escaped if string. + * + * @return bool|int + * @throws \Exception + */ + public static function insertRelease(array $parameters = []) + { + $passwordStatus = ((int) Settings::settingValue('..checkpasswordedrar') === 1 ? -1 : 0); + $parameters['id'] = self::query() + ->insertGetId( + [ + 'name' => $parameters['name'], + 'searchname' => $parameters['searchname'], + 'totalpart' => $parameters['totalpart'], + 'groups_id' => $parameters['groups_id'], + 'adddate' => Carbon::now(), + 'guid' => $parameters['guid'], + 'leftguid' => $parameters['guid'][0], + 'postdate' => $parameters['postdate'], + 'fromname' => $parameters['fromname'], + 'size' => $parameters['size'], + 'passwordstatus' => $passwordStatus, + 'haspreview' => -1, + 'categories_id' => $parameters['categories_id'], + 'nfostatus' => -1, + 'nzbstatus' => $parameters['nzbstatus'], + 'isrenamed' => $parameters['isrenamed'], + 'iscategorized' => 1, + 'predb_id' => $parameters['predb_id'], + ] + ); + + (new SphinxSearch())->insertRelease($parameters); + + return $parameters['id']; + } + + /** + * Used for release edit page on site. + * + * @param int $ID + * @param string $name + * @param string $searchName + * @param string $fromName + * @param int $categoryID + * @param int $parts + * @param int $grabs + * @param int $size + * @param string $postedDate + * @param string $addedDate + * @param $videoId + * @param $episodeId + * @param int $imDbID + * @param int $aniDbID + */ + public static function updateRelease( + $ID, + $name, + $searchName, + $fromName, + $categoryID, + $parts, + $grabs, + $size, + $postedDate, + $addedDate, + $videoId, + $episodeId, + $imDbID, + $aniDbID + ): void { + self::query()->where('id', $ID)->update( + [ + 'name' => $name, + 'searchname' => $searchName, + 'fromname' => $fromName, + 'categories_id' => $categoryID, + 'totalpart' => $parts, + 'grabs' => $grabs, + 'size' => $size, + 'postdate' => $postedDate, + 'adddate' => $addedDate, + 'videos_id' => $videoId, + 'tv_episodes_id' => $episodeId, + 'imdbid' => $imDbID, + 'anidbid' => $aniDbID, + ] + ); + (new SphinxSearch())->updateRelease($ID); + } } diff --git a/misc/testing/Dev/test-ReleaseCleaner.php b/misc/testing/Dev/test-ReleaseCleaner.php index 6c64efcb2..6916a4844 100755 --- a/misc/testing/Dev/test-ReleaseCleaner.php +++ b/misc/testing/Dev/test-ReleaseCleaner.php @@ -64,7 +64,7 @@ foreach ($releases as $release) { if ($rename === true) { $newName = $pdo->escapeString($newName); $pdo->queryExec(sprintf('UPDATE releases SET searchname = %s WHERE id = %d', $newName, $release['id'])); - $sphinx->updateRelease($release['id'], $pdo); + $sphinx->updateRelease($release['id']); } } } diff --git a/nntmux/NZBImport.php b/nntmux/NZBImport.php index f09a76de6..b502d8f08 100755 --- a/nntmux/NZBImport.php +++ b/nntmux/NZBImport.php @@ -416,7 +416,7 @@ class NZBImport if ($dupeCheck === null) { $escapedSearchName = $cleanName; // Insert the release into the DB. - $relID = $this->releases->insertRelease( + $relID = Release::insertRelease( [ 'name' => $escapedSubject, 'searchname' => $escapedSearchName, diff --git a/nntmux/ReleaseFiles.php b/nntmux/ReleaseFiles.php index 22c8ba346..d90b091fe 100755 --- a/nntmux/ReleaseFiles.php +++ b/nntmux/ReleaseFiles.php @@ -58,11 +58,12 @@ class ReleaseFiles * @param $id * * @return bool|\PDOStatement + * @throws \Exception */ public function delete($id) { $res = ReleaseFile::query()->where('releases_id', $id)->delete(); - $this->sphinxSearch->updateRelease($id, $this->pdo); + $this->sphinxSearch->updateRelease($id); return $res; } @@ -70,14 +71,15 @@ class ReleaseFiles /** * Add new files for a release ID. * - * @param int $id The ID of the release. - * @param string $name Name of the file. - * @param string $hash hash_16k of par2 + * @param int $id The ID of the release. + * @param string $name Name of the file. + * @param string $hash hash_16k of par2 * @param int $size Size of the file. - * @param int $createdTime Unix time the file was created. - * @param int $hasPassword Does it have a password (see Releases class constants)? + * @param int $createdTime Unix time the file was created. + * @param int $hasPassword Does it have a password (see Releases class constants)? * * @return mixed + * @throws \Exception */ public function add($id, $name, $hash = '', $size, $createdTime, $hasPassword) { @@ -100,7 +102,7 @@ class ReleaseFiles if (\strlen($hash) === 32) { ParHash::insertIgnore(['releases_id' => $id, 'hash' => $hash]); } - $this->sphinxSearch->updateRelease($id, $this->pdo); + $this->sphinxSearch->updateRelease($id); } return $insert; diff --git a/nntmux/Releases.php b/nntmux/Releases.php index 7a8248e17..e63ffa5ea 100755 --- a/nntmux/Releases.php +++ b/nntmux/Releases.php @@ -77,51 +77,12 @@ class Releases $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); $this->groups = ($options['Groups'] instanceof Groups ? $options['Groups'] : new Groups(['Settings' => $this->pdo])); $this->updateGrabs = ((int) Settings::settingValue('..grabstatus') !== 0); - $this->passwordStatus = ((int) Settings::settingValue('..checkpasswordedrar') === 1 ? -1 : 0); $this->sphinxSearch = new SphinxSearch(); $this->releaseSearch = new ReleaseSearch($this->pdo); $this->category = new Category(['Settings' => $this->pdo]); $this->showPasswords = self::showPasswords(); } - /** - * Insert a single release returning the ID on success or false on failure. - * - * @param array $parameters Insert parameters, must be escaped if string. - * - * @return bool|int - */ - public function insertRelease(array $parameters = []) - { - $parameters['id'] = Release::query() - ->insertGetId( - [ - 'name' => $parameters['name'], - 'searchname' => $parameters['searchname'], - 'totalpart' => $parameters['totalpart'], - 'groups_id' => $parameters['groups_id'], - 'adddate' => Carbon::now(), - 'guid' => $parameters['guid'], - 'leftguid' => $parameters['guid'][0], - 'postdate' => $parameters['postdate'], - 'fromname' => $parameters['fromname'], - 'size' => $parameters['size'], - 'passwordstatus' => $this->passwordStatus, - 'haspreview' => -1, - 'categories_id' => $parameters['categories_id'], - 'nfostatus' => -1, - 'nzbstatus' => $parameters['nzbstatus'], - 'isrenamed' => $parameters['isrenamed'], - 'iscategorized' => 1, - 'predb_id' => $parameters['predb_id'], - ] - ); - - $this->sphinxSearch->insertRelease($parameters); - - return $parameters['id']; - } - /** * Create a GUID for a release. * @return string @@ -723,60 +684,6 @@ class Releases $query->execute(); } - /** - * Used for release edit page on site. - * - * @param int $ID - * @param string $name - * @param string $searchName - * @param string $fromName - * @param int $categoryID - * @param int $parts - * @param int $grabs - * @param int $size - * @param string $postedDate - * @param string $addedDate - * @param $videoId - * @param $episodeId - * @param int $imDbID - * @param int $aniDbID - */ - public function update( - $ID, - $name, - $searchName, - $fromName, - $categoryID, - $parts, - $grabs, - $size, - $postedDate, - $addedDate, - $videoId, - $episodeId, - $imDbID, - $aniDbID - ): void { - Release::query()->where('id', $ID)->update( - [ - 'name' => $name, - 'searchname' => $searchName, - 'fromname' => $fromName, - 'categories_id' => $categoryID, - 'totalpart' => $parts, - 'grabs' => $grabs, - 'size' => $size, - 'postdate' => $postedDate, - 'adddate' => $addedDate, - 'videos_id' => $videoId, - 'tv_episodes_id' => $episodeId, - 'imdbid' => $imDbID, - 'anidbid' => $aniDbID, - ] - ); - $this->sphinxSearch->updateRelease($ID, $this->pdo); - } - /** * @param $guids * @param $category diff --git a/nntmux/SphinxSearch.php b/nntmux/SphinxSearch.php index ed05d54a8..0cde3546e 100755 --- a/nntmux/SphinxSearch.php +++ b/nntmux/SphinxSearch.php @@ -28,13 +28,13 @@ class SphinxSearch define('NN_SPHINXQL_SOCK_FILE', ''); } $this->sphinxQL = new DB( - [ - 'dbname' => '', - 'dbport' => NN_SPHINXQL_PORT, - 'dbhost' => NN_SPHINXQL_HOST_NAME, - 'dbsock' => NN_SPHINXQL_SOCK_FILE, - ] - ); + [ + 'dbname' => '', + 'dbport' => NN_SPHINXQL_PORT, + 'dbhost' => NN_SPHINXQL_HOST_NAME, + 'dbsock' => NN_SPHINXQL_SOCK_FILE, + ] + ); } } @@ -46,15 +46,15 @@ class SphinxSearch { if ($this->sphinxQL !== null && $parameters['id']) { $this->sphinxQL->queryExec( - sprintf( - 'REPLACE INTO releases_rt (id, name, searchname, fromname, filename) VALUES (%d, %s, %s, %s, %s)', - $parameters['id'], - $this->sphinxQL->escapeString($parameters['name']), - $this->sphinxQL->escapeString($parameters['searchname']), - $this->sphinxQL->escapeString($parameters['fromname']), - empty($parameters['filename']) ? "''" : $this->sphinxQL->escapeString($parameters['filename']) - ) - ); + sprintf( + 'REPLACE INTO releases_rt (id, name, searchname, fromname, filename) VALUES (%d, %s, %s, %s, %s)', + $parameters['id'], + $this->sphinxQL->escapeString($parameters['name']), + $this->sphinxQL->escapeString($parameters['searchname']), + $this->sphinxQL->escapeString($parameters['fromname']), + empty($parameters['filename']) ? "''" : $this->sphinxQL->escapeString($parameters['filename']) + ) + ); } } @@ -68,8 +68,8 @@ class SphinxSearch if ($this->sphinxQL !== null) { if ($identifiers['i'] === false) { $identifiers['i'] = $pdo->queryOneRow( - sprintf('SELECT id FROM releases WHERE guid = %s', $pdo->escapeString($identifiers['g'])) - ); + sprintf('SELECT id FROM releases WHERE guid = %s', $pdo->escapeString($identifiers['g'])) + ); if ($identifiers['i'] !== false) { $identifiers['i'] = $identifiers['i']['id']; } @@ -88,15 +88,15 @@ class SphinxSearch public static function escapeString($string) { $from = [ - '\\', '(', ')', '|', '---', '--', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=', "'", - "\x00", "\n", "\r", "\x1a", - ]; + '\\', '(', ')', '|', '---', '--', '-', '!', '@', '~', '"', '&', '/', '^', '$', '=', "'", + "\x00", "\n", "\r", "\x1a", + ]; $to = [ - '\\\\\\\\', '\\\\\\\\(', '\\\\\\\\)', '\\\\\\\\|', '-', '-', '\\\\\\\\-', '\\\\\\\\!', - '\\\\\\\\@', '\\\\\\\\~', - '\\\\\\\\"', '\\\\\\\\&', '\\\\\\\\/', '\\\\\\\\^', '\\\\\\\\$', '\\\\\\\\=', "\\'", - '\\x00', '\\n', '\\r', '\\x1a', - ]; + '\\\\\\\\', '\\\\\\\\(', '\\\\\\\\)', '\\\\\\\\|', '-', '-', '\\\\\\\\-', '\\\\\\\\!', + '\\\\\\\\@', '\\\\\\\\~', + '\\\\\\\\"', '\\\\\\\\&', '\\\\\\\\/', '\\\\\\\\^', '\\\\\\\\$', '\\\\\\\\=', "\\'", + '\\x00', '\\n', '\\r', '\\x1a', + ]; return str_replace($from, $to, $string); } @@ -105,21 +105,23 @@ class SphinxSearch * Update Sphinx Relases index for given releases_id. * * @param int $releaseID - * @param DB $pdo + * @throws \Exception */ - public function updateRelease($releaseID, DB $pdo): void + public function updateRelease($releaseID): void { if ($this->sphinxQL !== null) { + $pdo = new DB(); $new = $pdo->queryOneRow( - sprintf(' + sprintf( + ' SELECT r.id, r.name, r.searchname, r.fromname, IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename FROM releases r LEFT JOIN release_files rf ON (r.id=rf.releases_id) WHERE r.id = %d GROUP BY r.id LIMIT 1', - $releaseID - ) - ); + $releaseID + ) + ); if ($new !== false) { $this->insertRelease($new); } diff --git a/nntmux/processing/ProcessReleases.php b/nntmux/processing/ProcessReleases.php index 41bd4bfc3..6d5f4c662 100755 --- a/nntmux/processing/ProcessReleases.php +++ b/nntmux/processing/ProcessReleases.php @@ -601,7 +601,7 @@ class ProcessReleases } } - $releaseID = $this->releases->insertRelease( + $releaseID = Release::insertRelease( [ 'name' => $cleanRelName, 'searchname' => utf8_encode($cleanedName), diff --git a/nntmux/processing/post/ProcessAdditional.php b/nntmux/processing/post/ProcessAdditional.php index 2f8dc281c..cbda7979f 100755 --- a/nntmux/processing/post/ProcessAdditional.php +++ b/nntmux/processing/post/ProcessAdditional.php @@ -1131,6 +1131,7 @@ class ProcessAdditional * Get a list of all files in the compressed file, add the file info to the DB. * * @return bool + * @throws \Exception */ protected function _processCompressedFileList() { @@ -1187,7 +1188,7 @@ class ProcessAdditional $this->_addFileInfo($file); } if ($this->_addedFileInfo > 0) { - $this->sphinx->updateRelease($this->_release['id'], $this->pdo); + $this->sphinx->updateRelease($this->_release['id']); } return $this->_totalFileInfo > 0; @@ -1788,7 +1789,7 @@ class ProcessAdditional $this->_release['id'] ) ); - $this->sphinx->updateRelease($this->_release['id'], $this->pdo); + $this->sphinx->updateRelease($this->_release['id']); // Echo the changed name. if ($this->_echoCLI) { diff --git a/public/admin/release-edit.php b/public/admin/release-edit.php index 4b74de7fa..1f0b60cbd 100644 --- a/public/admin/release-edit.php +++ b/public/admin/release-edit.php @@ -2,6 +2,7 @@ require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'smarty.php'; +use App\Models\Release; use nntmux\Category; use nntmux\Releases; @@ -15,7 +16,7 @@ $action = ($_REQUEST['action'] ?? 'view'); switch ($action) { case 'submit': - $releases->update( + Release::updateRelease( $_POST['id'], $_POST['name'], $_POST['searchname'],