From 48bdb159a89c145a8e4c09315f52172cb8a24fd7 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 10 Oct 2017 14:16:28 +0200 Subject: [PATCH] Update Genre model, MusicInfo model and related migration, use these models in some of the queries --- Changelog | 1 + app/Models/Genre.php | 5 + app/Models/MusicInfo.php | 24 ++- ...17_09_02_205058_create_musicinfo_table.php | 16 +- nntmux/Music.php | 175 ++++++++---------- nntmux/Nfo.php | 158 ++++++++-------- nntmux/Releases.php | 78 +++----- nntmux/XXX.php | 85 +++------ 8 files changed, 249 insertions(+), 293 deletions(-) diff --git a/Changelog b/Changelog index da3d611d0..47fa91af0 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2017-10-10 DariusIIi + * Chg: Update Genre model, MusicInfo model and related migration, use these models in some of the queries * Chg: Move makeFieldLinks function from classes to helper 2017-10-09 DariusIII * Chg: Update model relations, update ReleaseNfo model and fix one bug diff --git a/app/Models/Genre.php b/app/Models/Genre.php index b0a4e939a..0b1c3c385 100644 --- a/app/Models/Genre.php +++ b/app/Models/Genre.php @@ -19,4 +19,9 @@ class Genre extends Model * @var array */ protected $guarded = []; + + public function music() + { + return $this->hasMany('App\Models\MusicInfo', 'genres_id'); + } } diff --git a/app/Models/MusicInfo.php b/app/Models/MusicInfo.php index ef95c2502..88943c495 100644 --- a/app/Models/MusicInfo.php +++ b/app/Models/MusicInfo.php @@ -6,5 +6,27 @@ use Illuminate\Database\Eloquent\Model; class MusicInfo extends Model { - // + /** + * @var string + */ + protected $table = 'musicinfo'; + /** + * @var bool + */ + protected $dateFormat = false; + + /** + * @var bool + */ + public $timestamps = true; + + /** + * @var array + */ + protected $guarded = []; + + public function genre() + { + return $this->belongsTo('App\Models\Genre', 'genres_id'); + } } diff --git a/database/migrations/2017_09_02_205058_create_musicinfo_table.php b/database/migrations/2017_09_02_205058_create_musicinfo_table.php index b78a0019d..fdf11ddf8 100644 --- a/database/migrations/2017_09_02_205058_create_musicinfo_table.php +++ b/database/migrations/2017_09_02_205058_create_musicinfo_table.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateMusicnfoTable extends Migration +class CreateMusicinfoTable extends Migration { /** * Run the migrations. @@ -15,7 +15,21 @@ class CreateMusicnfoTable extends Migration { Schema::create('musicinfo', function (Blueprint $table) { $table->increments('id'); + $table->string('title', 255); + $table->string('asin', 128)->nullable(); + $table->string('url', 1000)->nullable(); + $table->unsignedInteger('salesrank')->nullable(); + $table->string('artist', 255)->nullable(); + $table->string('publisher', 255)->nullable(); + $table->dateTime('releasedate')->nullable(); + $table->string('review', 3000)->nullable(); + $table->string('year', 4); + $table->integer('genres_id')->nullable(); + $table->string('tracks', 3000)->default(0); + $table->tinyInteger('cover')->default(0); $table->timestamps(); + $table->unique(['asin'], 'ux_musicinfo_asin'); + DB::statement('ALTER TABLE musicinfo ADD FULLTEXT INDEX ix_musicinfo_artist_title_ft (artist, title)'); }); } diff --git a/nntmux/Music.php b/nntmux/Music.php index eadc74e36..aef5059c1 100755 --- a/nntmux/Music.php +++ b/nntmux/Music.php @@ -3,7 +3,9 @@ namespace nntmux; use App\Models\Genre; +use App\Models\MusicInfo; use App\Models\Release; +use Carbon\Carbon; use nntmux\db\DB; use ApaiIO\ApaiIO; use GuzzleHttp\Client; @@ -59,7 +61,7 @@ class Music public $imgSavePath; /** - * @var string + * @var bool */ public $renamed; @@ -97,53 +99,39 @@ class Music /** * @param $id - * - * @return array|bool + * @return \Illuminate\Database\Eloquent\Model|null|static */ public function getMusicInfo($id) { - return $this->pdo->queryOneRow(sprintf('SELECT musicinfo.*, genres.title AS genres FROM musicinfo LEFT OUTER JOIN genres ON genres.id = musicinfo.genres_id WHERE musicinfo.id = %d ', $id)); + return MusicInfo::query()->with('genre')->where('id', $id)->first(); } /** * @param $artist * @param $album - * - * @return array|bool + * @return \Illuminate\Database\Eloquent\Model|null|static */ public function getMusicInfoByName($artist, $album) { - $pdo = $this->pdo; - $like = 'ILIKE'; - if ($pdo->DbSystem() === 'mysql') { - $like = 'LIKE'; - } - //only used to get a count of words - $searchwords = $searchsql = ''; - $ft = $pdo->queryDirect("SHOW INDEX FROM musicinfo WHERE key_name = 'ix_musicinfo_artist_title_ft'"); - if ($ft->rowCount() !== 2) { - $searchsql .= sprintf(" artist LIKE %s AND title %s %s'", $pdo->escapeString('%'.$artist.'%'), $like, $pdo->escapeString('%'.$album.'%')); - } else { - $album = preg_replace('/( - | -|\(.+\)|\(|\))/', ' ', $album); - $album = preg_replace('/[^\w ]+/', '', $album); - $album = preg_replace('/(WEB|FLAC|CD)/', '', $album); - $album = trim(preg_replace('/\s\s+/i', ' ', $album)); - $album = trim($album); - $words = explode(' ', $album); + $searchwords = ''; + $album = preg_replace('/( - | -|\(.+\)|\(|\))/', ' ', $album); + $album = preg_replace('/[^\w ]+/', '', $album); + $album = preg_replace('/(WEB|FLAC|CD)/', '', $album); + $album = trim(preg_replace('/\s\s+/i', ' ', $album)); + $album = trim($album); + $words = explode(' ', $album); - foreach ($words as $word) { - $word = trim(rtrim(trim($word), '-')); - if ($word !== '' && $word !== '-') { - $word = '+'.$word; - $searchwords .= sprintf('%s ', $word); - } + foreach ($words as $word) { + $word = trim(rtrim(trim($word), '-')); + if ($word !== '' && $word !== '-') { + $word = '+'.$word; + $searchwords .= sprintf('%s ', $word); } - $searchwords = trim($searchwords); - $searchsql .= sprintf(' MATCH(artist, title) AGAINST(%s IN BOOLEAN MODE)', $pdo->escapeString($searchwords)); } + $searchwords = trim($searchwords); - return $pdo->queryOneRow(sprintf('SELECT * FROM musicinfo WHERE %s', $searchsql)); + return MusicInfo::query()->whereRaw('MATCH(artist, title) AGAINST(? IN BOOLEAN MODE)', $searchwords)->first(); } /** @@ -154,6 +142,7 @@ class Music * @param array $excludedcats * * @return array + * @throws \Exception */ public function getMusicRange($cat, $start, $num, $orderby, array $excludedcats = []) { @@ -257,7 +246,7 @@ class Music * * @return array */ - public function getMusicOrder($orderby) + public function getMusicOrder($orderby): array { $order = ($orderby == '') ? 'r.postdate' : $orderby; $orderArr = explode('_', $order); @@ -293,7 +282,7 @@ class Music /** * @return array */ - public function getMusicOrdering() + public function getMusicOrdering(): array { return ['artist_asc', 'artist_desc', 'posted_asc', 'posted_desc', 'size_asc', 'size_desc', 'files_asc', 'files_desc', 'stats_asc', 'stats_desc', 'year_asc', 'year_desc', 'genre_asc', 'genre_desc']; } @@ -301,7 +290,7 @@ class Music /** * @return array */ - public function getBrowseByOptions() + public function getBrowseByOptions(): array { return ['artist' => 'artist', 'title' => 'title', 'genre' => 'genres_id', 'year' => 'year']; } @@ -309,7 +298,7 @@ class Music /** * @return string */ - public function getBrowseBy() + public function getBrowseBy(): string { $browseby = ' '; $browsebyArr = $this->getBrowseByOptions(); @@ -341,28 +330,22 @@ class Music * @param $cover * @param $genres_id */ - public function update($id, $title, $asin, $url, $salesrank, $artist, $publisher, $releasedate, $year, $tracks, $cover, $genres_id) + public function update($id, $title, $asin, $url, $salesrank, $artist, $publisher, $releasedate, $year, $tracks, $cover, $genres_id): void { - $this->pdo->queryExec( - sprintf( - ' - UPDATE musicinfo - SET title = %s, asin = %s, url = %s, salesrank = %s, artist = %s, publisher = %s, releasedate = %s, - year = %s, tracks = %s, cover = %d, genres_id = %d, updated_at = NOW() - WHERE id = %d', - $this->pdo->escapeString($title), - $this->pdo->escapeString($asin), - $this->pdo->escapeString($url), - $salesrank, - $this->pdo->escapeString($artist), - $this->pdo->escapeString($publisher), - $this->pdo->escapeString($releasedate), - $this->pdo->escapeString($year), - $this->pdo->escapeString($tracks), - $cover, - $genres_id, - $id - ) + MusicInfo::query()->where('id', $id)->update( + [ + 'title' => $title, + 'asin' => $asin, + 'url' => $url, + 'salesrank' => $salesrank, + 'artist' => $artist, + 'publisher' => $publisher, + 'releasedate' => $releasedate, + 'year' => $year, + 'tracks' => $tracks, + 'cover' => $cover, + 'genres_id' => $genres_id, + ] ); } @@ -374,7 +357,7 @@ class Music * @return bool * @throws \Exception */ - public function updateMusicInfo($title, $year, $amazdata = null) + public function updateMusicInfo($title, $year, $amazdata = null): bool { $gen = new Genres(['Settings' => $this->pdo]); $ri = new ReleaseImage($this->pdo); @@ -383,7 +366,7 @@ class Music $mus = []; if ($title !== '') { $amaz = $this->fetchAmazonProperties($title); - } elseif ($amazdata != null) { + } elseif ($amazdata !== null) { $amaz = $amazdata; } else { $amaz = false; @@ -493,16 +476,44 @@ class Music $mus['musicgenre'] = $genreName; $mus['musicgenres_id'] = $genreKey; - $check = $this->pdo->queryOneRow(sprintf('SELECT id FROM musicinfo WHERE asin = %s', $this->pdo->escapeString($mus['asin']))); - if ($check === false) { - $musicId = $this->pdo->queryInsert(sprintf('INSERT INTO musicinfo (title, asin, url, salesrank, artist, publisher, ' - .'releasedate, review, year, genres_id, tracks, cover, created_at, updated_at) VALUES ' - .'(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, now(), now())', $this->pdo->escapeString($mus['title']), $this->pdo->escapeString($mus['asin']), $this->pdo->escapeString($mus['url']), $mus['salesrank'], $this->pdo->escapeString($mus['artist']), $this->pdo->escapeString($mus['publisher']), $mus['releasedate'], $this->pdo->escapeString($mus['review']), $this->pdo->escapeString($mus['year']), ($mus['musicgenres_id'] == -1 ? 'null' : $mus['musicgenres_id']), $this->pdo->escapeString($mus['tracks']), $mus['cover'])); + $check = MusicInfo::query()->where('asin', $mus['asin'])->first(['id']); + if ($check === null) { + $musicId = MusicInfo::query()->insertGetId( + [ + 'title' => $mus['title'], + 'asin' =>$mus['asin'], + 'url' => $mus['url'], + 'salesrank' => $mus['salesrank'], + 'artist' => $mus['artist'], + 'publisher' => $mus['publisher'], + 'releasedate' => $mus['releasedate'], + 'review' => $mus['review'], + 'year' => $mus['year'], + 'genres_id' => (int) $mus['musicgenres_id'] === -1 ? 'null' : $mus['musicgenres_id'], + 'tracks' => $mus['tracks'], + 'cover' => $mus['cover'], + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ] + ); } else { $musicId = $check['id']; - $this->pdo->queryExec(sprintf('UPDATE musicinfo SET title = %s, asin = %s, url = %s, salesrank = %s, artist = %s, ' - .'publisher = %s, releasedate = %s, review = %s, year = %s, genres_id = %s, tracks = %s, cover = %s, ' - .'updated_at = NOW() WHERE id = %d', $this->pdo->escapeString($mus['title']), $this->pdo->escapeString($mus['asin']), $this->pdo->escapeString($mus['url']), $mus['salesrank'], $this->pdo->escapeString($mus['artist']), $this->pdo->escapeString($mus['publisher']), $mus['releasedate'], $this->pdo->escapeString($mus['review']), $this->pdo->escapeString($mus['year']), ($mus['musicgenres_id'] == -1 ? 'null' : $mus['musicgenres_id']), $this->pdo->escapeString($mus['tracks']), $mus['cover'], $musicId)); + MusicInfo::query()->where('id', $musicId)->update( + [ + 'title' => $mus['title'], + 'asin' => $mus['asin'], + 'url' => $mus['url'], + 'salesrank' => $mus['salesrank'], + 'artist' => $mus['artist'], + 'publisher' => $mus['publisher'], + 'releasedate' => $mus['releasedate'], + 'review' => $mus['review'], + 'year' => $mus['year'], + 'genres_id' => (int) $mus['musicgenres_id'] === -1 ? 'null' : $mus['musicgenres_id'], + 'tracks' => $mus['tracks'], + 'cover' => $mus['cover'], + ] + ); } if ($musicId) { @@ -735,36 +746,6 @@ class Music } } - /** - * @param bool $activeOnly - * - * @return array - */ - public function getGenres($activeOnly = false): ?array - { - if ($activeOnly) { - return $this->pdo->query( - ' - SELECT ge.* - FROM genres ge - INNER JOIN - ( - SELECT DISTINCT musicgenres_id - FROM musicinfo - ) x ON x.genres_id = ge.id - WHERE ge.type = " . Category::MUSIC_ROOT . " - ORDER BY title' - ); - } else { - return $this->pdo->query( - ' - SELECT * FROM genres - WHERE type = " . Category::MUSIC_ROOT . " - ORDER BY title' - ); - } - } - /** * @param $nodeId * diff --git a/nntmux/Nfo.php b/nntmux/Nfo.php index fed68b5a8..82b0acfec 100755 --- a/nntmux/Nfo.php +++ b/nntmux/Nfo.php @@ -60,9 +60,9 @@ class Nfo protected $echo; const NFO_FAILED = -9; // We failed to get a NFO after admin set max retries. - const NFO_UNPROC = -1; // Release has not been processed yet. - const NFO_NONFO = 0; // Release has no NFO. - const NFO_FOUND = 1; // Release has an NFO. + const NFO_UNPROC = -1; // Release has not been processed yet. + const NFO_NONFO = 0; // Release has no NFO. + const NFO_FOUND = 1; // Release has an NFO. /** * Default constructor. @@ -74,9 +74,9 @@ class Nfo public function __construct(array $options = []) { $defaults = [ - 'Echo' => false, - 'Settings' => null, - ]; + 'Echo' => false, + 'Settings' => null, + ]; $options += $defaults; $this->echo = ($options['Echo'] && NN_ECHOCLI); $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); @@ -106,26 +106,26 @@ class Nfo if (preg_match('/tvmaze\.com\/shows\/(\d{1,6})/i', $str, $matches)) { $return = - [ - 'showid' => trim($matches[1]), - 'site' => 'tvmaze', - ]; + [ + 'showid' => trim($matches[1]), + 'site' => 'tvmaze', + ]; } if (preg_match('/imdb\.com\/title\/(tt\d{1,8})/i', $str, $matches)) { $return = - [ - 'showid' => trim($matches[1]), - 'site' => 'imdb', - ]; + [ + 'showid' => trim($matches[1]), + 'site' => 'imdb', + ]; } if (preg_match('/thetvdb\.com\/\?tab=series&id=(\d{1,8})/i', $str, $matches)) { $return = - [ - 'showid' => trim($matches[1]), - 'site' => 'thetvdb', - ]; + [ + 'showid' => trim($matches[1]), + 'site' => 'thetvdb', + ]; } return $return; @@ -149,9 +149,11 @@ class Nfo // Make sure it's not too big or small, size needs to be at least 12 bytes for header checking. Ignore common file types. $size = strlen($possibleNFO); if ($size < 65535 && - $size > 11 && - ! preg_match( - '/\A(\s*<\?xml|=newz\[NZB\]=|RIFF|\s*[RP]AR|.{0,10}(JFIF|matroska|ftyp|ID3))|;\s*Generated\s*by.*SF\w/i', $possibleNFO)) { + $size > 11 && + ! preg_match( + '/\A(\s*<\?xml|=newz\[NZB\]=|RIFF|\s*[RP]AR|.{0,10}(JFIF|matroska|ftyp|ID3))|;\s*Generated\s*by.*SF\w/i', + $possibleNFO + )) { // File/GetId3 work with files, so save to disk. $tmpPath = $this->tmpPath.$guid.'.nfo'; file_put_contents($tmpPath, $possibleNFO); @@ -160,7 +162,7 @@ class Nfo $result = Utility::fileInfo($tmpPath); if (! empty($result)) { - // Check if it's text. + // Check if it's text. if (preg_match('/(ASCII|ISO-8859|UTF-(8|16|32).*?)\s*text/', $result)) { @unlink($tmpPath); @@ -181,7 +183,7 @@ class Nfo @unlink($tmpPath); if (isset($check['error'])) { - // Check if it's a par2. + // Check if it's a par2. $par2info = new Par2Info(); $par2info->setData($possibleNFO); if ($par2info->error) { @@ -225,14 +227,14 @@ class Nfo if ((int) $release['completion'] === 0) { $nzbContents = new NZBContents( - [ - 'Echo' => $this->echo, - 'NNTP' => $nntp, - 'Nfo' => $this, - 'Settings' => $this->pdo, - 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Settings' => $this->pdo, 'Nfo' => $this]), - ] - ); + [ + 'Echo' => $this->echo, + 'NNTP' => $nntp, + 'Nfo' => $this, + 'Settings' => $this->pdo, + 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Settings' => $this->pdo, 'Nfo' => $this]), + ] + ); $nzbContents->parseNZB($release['guid'], $release['id'], $release['groups_id']); } @@ -259,13 +261,13 @@ class Nfo $maxRetries = ($dummy >= 0 ? -($dummy + 1) : self::NFO_UNPROC); return sprintf( - 'AND r.nzbstatus = %d AND r.nfostatus BETWEEN %d AND %d %s %s', - NZB::NZB_ADDED, - ($maxRetries < -8 ? -8 : $maxRetries), - self::NFO_UNPROC, - ($maxSize > 0 ? ('AND r.size < '.($maxSize * 1073741824)) : ''), - ($minSize > 0 ? ('AND r.size > '.($minSize * 1048576)) : '') - ); + 'AND r.nzbstatus = %d AND r.nfostatus BETWEEN %d AND %d %s %s', + NZB::NZB_ADDED, + ($maxRetries < -8 ? -8 : $maxRetries), + self::NFO_UNPROC, + ($maxSize > 0 ? ('AND r.size < '.($maxSize * 1073741824)) : ''), + ($minSize > 0 ? ('AND r.size > '.($minSize * 1048576)) : '') + ); } /** @@ -288,46 +290,48 @@ class Nfo $optionsQuery = self::NfoQueryString(); $res = $this->pdo->query( - sprintf(' + sprintf( + ' SELECT r.id, r.guid, r.groups_id, r.name FROM releases r WHERE 1=1 %s %s %s ORDER BY r.nfostatus ASC, r.postdate DESC LIMIT %d', - $optionsQuery, - $guidCharQuery, - $groupIDQuery, - $this->nzbs - ) - ); + $optionsQuery, + $guidCharQuery, + $groupIDQuery, + $this->nzbs + ) + ); $nfoCount = count($res); if ($nfoCount > 0) { ColorCLI::doEcho( - ColorCLI::primary( - PHP_EOL. - ($guidChar === '' ? '' : '['.$guidChar.'] '). - ($groupID === '' ? '' : '['.$groupID.'] '). - 'Processing '.$nfoCount. - ' NFO(s), starting at '.$this->nzbs. - ' * = hidden NFO, + = NFO, - = no NFO, f = download failed.' - ) - ); + ColorCLI::primary( + PHP_EOL. + ($guidChar === '' ? '' : '['.$guidChar.'] '). + ($groupID === '' ? '' : '['.$groupID.'] '). + 'Processing '.$nfoCount. + ' NFO(s), starting at '.$this->nzbs. + ' * = hidden NFO, + = NFO, - = no NFO, f = download failed.' + ) + ); if ($this->echo) { // Get count of releases per nfo status $nfoStats = $this->pdo->queryDirect( - sprintf(' + sprintf( + ' SELECT r.nfostatus AS status, COUNT(r.id) AS count FROM releases r WHERE 1=1 %s %s %s GROUP BY r.nfostatus ORDER BY r.nfostatus ASC', - $optionsQuery, - $guidCharQuery, - $groupIDQuery - ) - ); + $optionsQuery, + $guidCharQuery, + $groupIDQuery + ) + ); if ($nfoStats instanceof \Traversable) { $outString = PHP_EOL.'Available to process'; foreach ($nfoStats as $row) { @@ -339,14 +343,14 @@ class Nfo $groups = new Groups(['Settings' => $this->pdo]); $nzbContents = new NZBContents( - [ - 'Echo' => $this->echo, - 'NNTP' => $nntp, - 'Nfo' => $this, - 'Settings' => $this->pdo, - 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Nfo' => $this, 'Settings' => $this->pdo]), - ] - ); + [ + 'Echo' => $this->echo, + 'NNTP' => $nntp, + 'Nfo' => $this, + 'Settings' => $this->pdo, + 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Nfo' => $this, 'Settings' => $this->pdo]), + ] + ); $movie = new Movie(['Echo' => $this->echo, 'Settings' => $this->pdo]); foreach ($res as $arr) { @@ -372,18 +376,18 @@ class Nfo // Remove nfo that we cant fetch after 5 attempts. $releases = $this->pdo->queryDirect( - sprintf( - 'SELECT r.id + sprintf( + 'SELECT r.id FROM releases r WHERE r.nzbstatus = %d AND r.nfostatus < %d AND r.nfostatus > %d %s %s', - NZB::NZB_ADDED, - $this->maxRetries, - self::NFO_FAILED, - $groupIDQuery, - $guidCharQuery - ) - ); + NZB::NZB_ADDED, + $this->maxRetries, + self::NFO_FAILED, + $groupIDQuery, + $guidCharQuery + ) + ); if ($releases instanceof \Traversable) { foreach ($releases as $release) { diff --git a/nntmux/Releases.php b/nntmux/Releases.php index b13b77a60..e0764a0f3 100755 --- a/nntmux/Releases.php +++ b/nntmux/Releases.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Release; use nntmux\db\DB; use App\Models\Settings; use nntmux\utility\Utility; @@ -18,12 +19,12 @@ class Releases const PASSWD_RAR = 10; // Definitely passworded. /** - * @var DB + * @var \nntmux\db\DB */ public $pdo; /** - * @var Groups + * @var \nntmux\Groups */ public $groups; @@ -33,12 +34,12 @@ class Releases public $updateGrabs; /** - * @var ReleaseSearch + * @var \nntmux\ReleaseSearch */ public $releaseSearch; /** - * @var SphinxSearch + * @var \nntmux\SphinxSearch */ public $sphinxSearch; @@ -53,7 +54,7 @@ class Releases public $passwordStatus; /** - * @var Category + * @var \nntmux\Category */ public $category; @@ -717,35 +718,29 @@ class Releases $parts, $grabs, $size, - $postedDate, + $postedDate, $addedDate, $videoId, $episodeId, $imDbID, $aniDbID ): void { - $this->pdo->queryExec( - sprintf( - 'UPDATE releases - SET name = %s, searchname = %s, fromname = %s, categories_id = %d, - totalpart = %d, grabs = %d, size = %s, postdate = %s, adddate = %s, videos_id = %d, - tv_episodes_id = %s, imdbid = %d, anidbid = %d - WHERE id = %d', - $this->pdo->escapeString($name), - $this->pdo->escapeString($searchName), - $this->pdo->escapeString($fromName), - $categoryID, - $parts, - $grabs, - $this->pdo->escapeString($size), - $this->pdo->escapeString($postedDate), - $this->pdo->escapeString($addedDate), - $videoId, - $episodeId, - $imDbID, - $aniDbID, - $ID - ) + Release::query()->where('id', $ID)->update( + [ + 'name' => $name, + 'searchname' => $searchName, + 'fromname' => $fromName, + 'categories_id' => $categoryID, + 'totalpart' => $parts, + 'grabs' => $grabs, + 'size' => $size, + 'postdate' => $postedDate, + 'addate' => $addedDate, + 'videos_id' => $videoId, + 'tv_episodes_id' => $episodeId, + 'imdbid' => $imDbID, + 'anidbid' => $aniDbID, + ] ); $this->sphinxSearch->updateRelease($ID, $this->pdo); } @@ -758,8 +753,7 @@ class Releases * @param $episodeId * @param $anidbId * @param $imdbId - * - * @return array|bool|int + * @return bool|int */ public function updateMulti($guids, $category, $grabs, $videoId, $episodeId, $anidbId, $imdbId) { @@ -776,29 +770,7 @@ class Releases 'imdbid' => $imdbId, ]; - $updateSql = []; - foreach ($update as $key => $value) { - if ($value !== '') { - $updateSql[] = sprintf($key.'=%s', $this->pdo->escapeString($value)); - } - } - - if (count($updateSql) < 1) { - return -1; - } - - $updateGuids = []; - foreach ($guids as $guid) { - $updateGuids[] = $this->pdo->escapeString($guid); - } - - return $this->pdo->queryExec( - sprintf( - 'UPDATE releases SET %s WHERE guid IN (%s)', - implode(', ', $updateSql), - implode(', ', $updateGuids) - ) - ); + return Release::query()->whereIn('guid', $guids)->update($update); } /** diff --git a/nntmux/XXX.php b/nntmux/XXX.php index 18ba1d37e..7e467229e 100755 --- a/nntmux/XXX.php +++ b/nntmux/XXX.php @@ -2,6 +2,7 @@ namespace nntmux; +use App\Models\Genre; use nntmux\db\DB; use App\Models\XxxInfo; use App\Models\Settings; @@ -304,41 +305,6 @@ class XXX return $browseBy; } - /** - * Create click-able links to actors/genres/directors/etc.. - * - * @param $data - * @param $field - * - * @return string - */ - public function makeFieldLinks($data, $field): string - { - if (empty($data[$field])) { - return ''; - } - - $tmpArr = explode(',', $data[$field]); - $newArr = []; - $i = 0; - foreach ($tmpArr as $ta) { - if (trim($ta) === '') { - continue; - } - if ($field === 'genre') { - $ta = $this->getGenres(true, $ta); - $ta = $ta['title']; - } - if ($i > 7) { - break; - } //only use first 8 - $newArr[] = ''.$ta.''; - $i++; - } - - return implode(', ', $newArr); - } - /** * Update XXX Information from getXXXCovers.php in misc/testing/PostProc. * @@ -397,24 +363,17 @@ class XXX /** * Get all genres for search-filter.tpl. * - * @param bool $activeOnly * - * @return array|null + * @param bool $activeOnly + * @return array */ - public function getAllGenres($activeOnly = false): ?array + public function getAllGenres($activeOnly = false): array { - $ret = null; - + $ret = []; if ($activeOnly) { - $res = $this->pdo->query( - 'SELECT title FROM genres WHERE disabled = 0 AND type = '. - Category::XXX_ROOT.' ORDER BY title' - ); + $res = Genre::query()->where(['disabled' => 0, 'type' => Category::XXX_ROOT])->orderBy('title')->get(['title']); } else { - $res = $this->pdo->query( - 'SELECT title FROM genres WHERE disabled = 1 AND type = '. - Category::XXX_ROOT.' ORDER BY title' - ); + $res = Genre::query()->where(['type' => Category::XXX_ROOT])->orderBy('title')->get(['title']); } foreach ($res as $arr => $value) { @@ -434,17 +393,15 @@ class XXX */ public function getGenres($activeOnly = false, $gid = null) { - if ($gid !== null) { - $gid = ' AND id = '.$this->pdo->escapeString($gid).' ORDER BY title'; - } else { - $gid = ' ORDER BY title'; - } - if ($activeOnly) { - return $this->pdo->queryOneRow('SELECT title FROM genres WHERE disabled = 0 AND type = '.Category::XXX_ROOT.$gid); + return Genre::query()->where(['disabled' => 0, 'type' => Category::XXX_ROOT])->when($gid !== null, function ($query) use ($gid) { + return $query->where('id', $gid); + })->orderBy('title')->get(['title']); } - return $this->pdo->queryOneRow('SELECT title FROM genres WHERE disabled = 1 AND type = '.Category::XXX_ROOT.$gid); + return Genre::query()->where(['disabled' => 1, 'type' => Category::XXX_ROOT])->when($gid !== null, function ($query) use ($gid) { + return $query->where('id', $gid); + })->orderBy('title')->get(['title']); } /** @@ -459,15 +416,15 @@ class XXX $ret = null; if (! is_array($arr)) { - $res = $this->pdo->queryOneRow('SELECT id FROM genres WHERE title = '.$this->pdo->escapeString($arr)); - if ($res !== false) { + $res = Genre::query()->where('title', $arr)->first(['id']); + if ($res !== null) { return $res['id']; } } foreach ($arr as $key => $value) { - $res = $this->pdo->queryOneRow('SELECT id FROM genres WHERE title = '.$this->pdo->escapeString($value)); - if ($res !== false) { + $res = Genre::query()->where('title', $value)->first(['id']); + if ($res !== null) { $ret .= ','.$res['id']; } else { $ret .= ','.$this->insertGenre($value); @@ -482,15 +439,15 @@ class XXX /** * Inserts Genre and returns last affected row (Genre ID). * - * @param $genre * - * @return bool + * @param $genre + * @return int|string */ - private function insertGenre($genre): bool + private function insertGenre($genre) { $res = ''; if ($genre !== null) { - $res = $this->pdo->queryInsert(sprintf('INSERT INTO genres (title, type, disabled) VALUES (%s ,%d ,%d)', $this->pdo->escapeString($genre), Category::XXX_ROOT, 0)); + $res = Genre::query()->insertGetId(['title' => $genre, 'type' => Category::XXX_ROOT, 'disabled' => 0]); } return $res;