From 9be1bf4e69dce9c449519ee1b2b4bc21fc40d0a9 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 5 Jan 2018 13:11:56 +0100 Subject: [PATCH] Remove nntmux/PreDb class and move its functions into Predb model --- Changelog | 1 + app/Models/Predb.php | 182 ++++++++++++++++++ misc/update/tmux/bin/postprocess_pre.php | 6 +- nntmux/PreDb.php | 231 ----------------------- nntmux/processing/ProcessReleases.php | 5 +- public/admin/predb.php | 9 +- public/pages/ajax_preinfo.php | 15 +- public/pages/details.php | 5 +- 8 files changed, 201 insertions(+), 253 deletions(-) delete mode 100755 nntmux/PreDb.php diff --git a/Changelog b/Changelog index 046919bce..c4840454a 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-01-05 DariusIII + * Chg: Remove nntmux/PreDb class and move its functions into Predb model * Chg: Update ConsoleTools class and related scripts * Chg: Update Forking and postprocess.php * Chg: Update Movie class and add missing rtrating column to movieinfo table in mysql-ddl.sql diff --git a/app/Models/Predb.php b/app/Models/Predb.php index aea39540f..c4e12b971 100644 --- a/app/Models/Predb.php +++ b/app/Models/Predb.php @@ -3,6 +3,10 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Cache; +use nntmux\ColorCLI; +use nntmux\ConsoleTools; /** * @property mixed $release @@ -10,6 +14,14 @@ use Illuminate\Database\Eloquent\Model; */ class Predb extends Model { + // Nuke status. + public const PRE_NONUKE = 0; // Pre is not nuked. + public const PRE_UNNUKED = 1; // Pre was un nuked. + public const PRE_NUKED = 2; // Pre is nuked. + public const PRE_MODNUKE = 3; // Nuke reason was modified. + public const PRE_RENUKED = 4; // Pre was re nuked. + public const PRE_OLDNUKE = 5; // Pre is nuked for being old. + /** * @var string */ @@ -39,4 +51,174 @@ class Predb extends Model { return $this->hasMany(Release::class, 'predb_id'); } + + /** + * Attempts to match PreDB titles to releases. + * + * @param $dateLimit + * @throws \RuntimeException + */ + public static function checkPre($dateLimit = false): void + { + $consoleTools = new ConsoleTools(); + $updated = 0; + + if (NN_ECHOCLI) { + echo ColorCLI::header('Querying DB for release search names not matched with PreDB titles.'); + } + + $query = self::query() + ->where('releases.predb_id', '<', 1) + ->join('releases', 'predb.title', '=', 'releases.searchname') + ->select(['predb.id as predb_id', 'releases.id as releases_id']); + if ($dateLimit !== false && is_numeric($dateLimit)) { + $query->where('adddate', '>', Carbon::now()->subDays($dateLimit)); + } + + $res = $query->get(); + + if ($res !== null) { + $total = \count($res); + echo ColorCLI::primary(number_format($total).' releases to match.'); + + if ($res instanceof \Traversable) { + foreach ($res as $row) { + Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]); + + if (NN_ECHOCLI) { + $consoleTools->overWritePrimary( + 'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total) + ); + } + } + if (NN_ECHOCLI) { + echo PHP_EOL; + } + } + + if (NN_ECHOCLI) { + echo ColorCLI::header( + 'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.' + ); + } + } + } + + /** + * Try to match a single release to a PreDB title when the release is created. + * + * @param string $cleanerName + * + * @return array|bool Array with title/id from PreDB if found, bool False if not found. + */ + public static function matchPre($cleanerName) + { + if (empty($cleanerName)) { + return false; + } + + $titleCheck = self::query()->where('title', $cleanerName)->first(['id']); + + if ($titleCheck !== null) { + return [ + 'title' => $cleanerName, + 'predb_id' => $titleCheck['id'], + ]; + } + + // Check if clean name matches a PreDB filename. + $fileCheck = self::query()->where('filename', $cleanerName)->first(['id', 'title']); + + if ($fileCheck !== null) { + return [ + 'title' => $fileCheck['title'], + 'predb_id' => $fileCheck['id'], + ]; + } + + return false; + } + + /** + * Get all PRE's in the DB. + * + * + * @param $offset + * @param $offset2 + * @param string|array $search + * @return array + */ + public static function getAll($offset, $offset2, $search = ''): array + { + if ($search !== '') { + $search = explode(' ', trim($search)); + } + + $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_MEDIUM); + if ($search === '') { + $check = Cache::get('predbcount'); + if ($check !== null) { + $count = $check; + } else { + $count = self::count(); + Cache::put('predbcount', $count, $expiresAt); + } + } else { + $sql = self::query()->where(function ($query) use ($search) { + for ($i = 0, $iMax = \count($search); $i < $iMax; $i++) { + $query->where('title', 'like', '%'.$search[$i].'%'); + } + }); + $check = Cache::get(md5(implode(',', $search))); + if ($check !== null) { + $count = $check; + } else { + $count = $sql->count('id'); + Cache::put(md5(implode(',', $search)), $count, $expiresAt); + } + } + + $sql = self::query()->leftJoin('releases', 'predb.id', '=', 'releases.predb_id')->orderBy('predb.predate', 'desc')->limit($offset2)->offset($offset); + if ($search !== '') { + $sql->where(function ($query) use ($search) { + for ($i = 0, $iMax = \count($search); $i < $iMax; $i++) { + $query->where('title', 'like', '%'.$search[$i].'%'); + } + }); + } + $search = $search !== '' ? implode(',', $search) : ''; + $check = Cache::get(md5($offset.$offset2.$search)); + if ($check !== null) { + $parr = $check; + } else { + $parr = $sql->get(); + Cache::put(md5($offset.$offset2.$search), $parr, $expiresAt); + } + + return ['arr' => $parr, 'count' => $count ?? 0]; + } + + /** + * Get all PRE's for a release. + * + * + * @param $preID + * @return \Illuminate\Database\Eloquent\Collection|static[] + */ + public static function getForRelease($preID) + { + return self::query()->where('id', $preID)->get(); + } + + /** + * Return a single PRE for a release. + * + * + * @param $preID + * @return \Illuminate\Database\Eloquent\Model|null|static + */ + public static function getOne($preID) + { + return self::query()->where('id', $preID)->first(); + } } diff --git a/misc/update/tmux/bin/postprocess_pre.php b/misc/update/tmux/bin/postprocess_pre.php index 6cfc15ec4..5712b2d5e 100755 --- a/misc/update/tmux/bin/postprocess_pre.php +++ b/misc/update/tmux/bin/postprocess_pre.php @@ -1,7 +1,7 @@ true]))->checkPre((isset($argv[1]) && is_numeric($argv[1]) ? $argv[1] : false)); +Predb::checkPre((isset($argv[1]) && is_numeric($argv[1]) ? $argv[1] : false)); diff --git a/nntmux/PreDb.php b/nntmux/PreDb.php deleted file mode 100755 index b6d53fa9c..000000000 --- a/nntmux/PreDb.php +++ /dev/null @@ -1,231 +0,0 @@ - false, - 'Settings' => null, - ]; - $options += $defaults; - - $this->echooutput = ($options['Echo'] && NN_ECHOCLI); - $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); - } - - /** - * Attempts to match PreDB titles to releases. - * - * @param $dateLimit - * @throws \RuntimeException - */ - public function checkPre($dateLimit = false): void - { - $this->dateLimit = $dateLimit; - - $consoleTools = new ConsoleTools(); - $updated = 0; - - if ($this->echooutput) { - echo ColorCLI::header('Querying DB for release search names not matched with PreDB titles.'); - } - - $query = PredbModel::query() - ->where('releases.predb_id', '<', 1) - ->join('releases', 'predb.title', '=', 'releases.searchname') - ->select(['predb.id as predb_id', 'releases.id as releases_id']); - if ($this->dateLimit !== false && is_numeric($this->dateLimit)) { - $query->where('adddate', '>', Carbon::now()->subDays($this->dateLimit)); - } - - $res = $query->get(); - - if ($res !== null) { - $total = \count($res); - echo ColorCLI::primary(number_format($total).' releases to match.'); - - if ($res instanceof \Traversable) { - foreach ($res as $row) { - Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]); - - if ($this->echooutput) { - $consoleTools->overWritePrimary( - 'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total) - ); - } - } - if ($this->echooutput) { - echo PHP_EOL; - } - } - - if ($this->echooutput) { - echo ColorCLI::header( - 'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.' - ); - } - } - } - - /** - * Try to match a single release to a PreDB title when the release is created. - * - * @param string $cleanerName - * - * @return array|bool Array with title/id from PreDB if found, bool False if not found. - */ - public function matchPre($cleanerName) - { - if (empty($cleanerName)) { - return false; - } - - $titleCheck = PredbModel::query()->where('title', $cleanerName)->first(['id']); - - if ($titleCheck !== null) { - return [ - 'title' => $cleanerName, - 'predb_id' => $titleCheck['id'], - ]; - } - - // Check if clean name matches a PreDB filename. - $fileCheck = PredbModel::query()->where('filename', $cleanerName)->first(['id', 'title']); - - if ($fileCheck !== null) { - return [ - 'title' => $fileCheck['title'], - 'predb_id' => $fileCheck['id'], - ]; - } - - return false; - } - - /** - * Get all PRE's in the DB. - * - * - * @param $offset - * @param $offset2 - * @param string|array $search - * @return array - */ - public function getAll($offset, $offset2, $search = ''): array - { - if ($search !== '') { - $search = explode(' ', trim($search)); - } - - $expiresAt = Carbon::now()->addSeconds(NN_CACHE_EXPIRY_MEDIUM); - if ($search === '') { - $check = Cache::get('predbcount'); - if ($check !== null) { - $count = $check; - } else { - $count = PredbModel::count(); - Cache::put('predbcount', $count, $expiresAt); - } - } else { - $sql = PredbModel::query()->where(function ($query) use ($search) { - for ($i = 0, $iMax = \count($search); $i < $iMax; $i++) { - $query->where('title', 'like', '%'.$search[$i].'%'); - } - }); - $check = Cache::get(md5(implode(',', $search))); - if ($check !== null) { - $count = $check; - } else { - $count = $sql->count('id'); - Cache::put(md5(implode(',', $search)), $count, $expiresAt); - } - } - - $sql = PredbModel::query()->leftJoin('releases', 'predb.id', '=', 'releases.predb_id')->orderBy('predb.predate', 'desc')->limit($offset2)->offset($offset); - if ($search !== '') { - $sql->where(function ($query) use ($search) { - for ($i = 0, $iMax = \count($search); $i < $iMax; $i++) { - $query->where('title', 'like', '%'.$search[$i].'%'); - } - }); - } - $search = $search !== '' ? implode(',', $search) : ''; - $check = Cache::get(md5($offset.$offset2.$search)); - if ($check !== null) { - $parr = $check; - } else { - $parr = $sql->get(); - Cache::put(md5($offset.$offset2.$search), $parr, $expiresAt); - } - - return ['arr' => $parr, 'count' => $count ?? 0]; - } - - /** - * Get all PRE's for a release. - * - * - * @param $preID - * @return \Illuminate\Database\Eloquent\Collection|static[] - */ - public function getForRelease($preID) - { - return PredbModel::query()->where('id', $preID)->get(); - } - - /** - * Return a single PRE for a release. - * - * - * @param $preID - * @return \Illuminate\Database\Eloquent\Model|null|static - */ - public function getOne($preID) - { - return PredbModel::query()->where('id', $preID)->first(); - } -} diff --git a/nntmux/processing/ProcessReleases.php b/nntmux/processing/ProcessReleases.php index 307626f38..81de4d4a2 100755 --- a/nntmux/processing/ProcessReleases.php +++ b/nntmux/processing/ProcessReleases.php @@ -2,10 +2,10 @@ namespace nntmux\processing; +use App\Models\Predb; use nntmux\NZB; use nntmux\NNTP; use nntmux\db\DB; -use nntmux\PreDb; use nntmux\Genres; use nntmux\Groups; use nntmux\Category; @@ -559,7 +559,6 @@ class ProcessReleases } if ($collections instanceof \Traversable) { - $preDB = new PreDb(['Echo' => $this->echoCLI, 'Settings' => $this->pdo]); foreach ($collections as $collection) { $cleanRelName = utf8_encode(str_replace(['#', '@', '$', '%', '^', '§', '¨', '©', 'Ö'], '', $collection['subject'])); @@ -593,7 +592,7 @@ class ProcessReleases if ($preID === false && $cleanedName !== '') { // try to match the cleaned searchname to predb title or filename here - $preMatch = $preDB->matchPre($cleanedName); + $preMatch = Predb::matchPre($cleanedName); if ($preMatch !== false) { $cleanedName = $preMatch['title']; $preID = $preMatch['predb_id']; diff --git a/public/admin/predb.php b/public/admin/predb.php index a9ddac2dd..5816dbb6e 100644 --- a/public/admin/predb.php +++ b/public/admin/predb.php @@ -1,20 +1,19 @@ getAll($offset, ITEMS_PER_PAGE, $_REQUEST['presearch']); + $parr = Predb::getAll($offset, ITEMS_PER_PAGE, $_REQUEST['presearch']); } else { $lastSearch = ''; - $parr = $predb->getAll($offset, ITEMS_PER_PAGE); + $parr = Predb::getAll($offset, ITEMS_PER_PAGE); } $page->smarty->assign('pagertotalitems', $parr['count']); diff --git a/public/pages/ajax_preinfo.php b/public/pages/ajax_preinfo.php index db3a73f14..f99a6efc5 100644 --- a/public/pages/ajax_preinfo.php +++ b/public/pages/ajax_preinfo.php @@ -1,6 +1,6 @@ show404(); } -$pre = new PreDb(['Settings' => $page->settings]); -$predata = $pre->getOne($_REQUEST['id']); +$predata = Predb::getOne($_REQUEST['id']); if (! $predata) { echo 'No pre info'; @@ -21,19 +20,19 @@ if (! $predata) { if (isset($predata['nuked'])) { $nuked = ''; switch ($predata['nuked']) { - case PreDb::PRE_NUKED: + case Predb::PRE_NUKED: $nuked = 'NUKED'; break; - case PreDb::PRE_MODNUKE: + case Predb::PRE_MODNUKE: $nuked = 'MODNUKED'; break; - case PreDb::PRE_OLDNUKE: + case Predb::PRE_OLDNUKE: $nuked = 'OLDNUKE'; break; - case PreDb::PRE_RENUKED: + case Predb::PRE_RENUKED: $nuked = 'RENUKE'; break; - case PreDb::PRE_UNNUKED: + case Predb::PRE_UNNUKED: $nuked = 'UNNUKED'; break; } diff --git a/public/pages/details.php b/public/pages/details.php index b3af58e14..692278f88 100644 --- a/public/pages/details.php +++ b/public/pages/details.php @@ -1,12 +1,12 @@ getAnimeInfo($data['anidbid']); } - $preDb = new PreDb(); - $pre = $preDb->getForRelease($data['predb_id']); + $pre = Predb::getForRelease($data['predb_id']); $releasefiles = ReleaseFile::getReleaseFiles($data['id']);