From e70dbc831e6733e6b0b489a0dd1ccc830c92cd2f Mon Sep 17 00:00:00 2001 From: DariusIII Date: Sun, 13 Oct 2024 12:14:11 +0200 Subject: [PATCH] Further update to getCategorySearch --- Blacklight/Books.php | 2 +- Blacklight/Console.php | 2 +- Blacklight/Games.php | 2 +- Blacklight/Movie.php | 5 +--- Blacklight/Music.php | 2 +- Blacklight/Releases.php | 16 ++++++------- Blacklight/XXX.php | 2 +- app/Http/Controllers/Api/RSS.php | 2 +- app/Models/Category.php | 39 ++++++++++++++++++++------------ 9 files changed, 39 insertions(+), 33 deletions(-) diff --git a/Blacklight/Books.php b/Blacklight/Books.php index 1e853555d..50ebd99f6 100755 --- a/Blacklight/Books.php +++ b/Blacklight/Books.php @@ -107,7 +107,7 @@ class Books $browseby = $this->getBrowseBy(); $catsrch = ''; if (\count($cat) > 0 && $cat[0] !== -1) { - $catsrch = Category::getCategorySearch($cat); + $catsrch = Category::getCategorySearch($cat, true); } $exccatlist = ''; if (\count($excludedCats) > 0) { diff --git a/Blacklight/Console.php b/Blacklight/Console.php index 2354d6e8d..c5558ecf0 100755 --- a/Blacklight/Console.php +++ b/Blacklight/Console.php @@ -140,7 +140,7 @@ class Console $browseBy = $this->getBrowseBy(); $catsrch = ''; if (\count($cat) > 0 && (int) $cat[0] !== -1) { - $catsrch = Category::getCategorySearch($cat); + $catsrch = Category::getCategorySearch($cat, true); } $exccatlist = ''; if (\count($excludedCats) > 0) { diff --git a/Blacklight/Games.php b/Blacklight/Games.php index 821824d96..c9705880a 100755 --- a/Blacklight/Games.php +++ b/Blacklight/Games.php @@ -171,7 +171,7 @@ class Games $browseBy = $this->getBrowseBy(); $catsrch = ''; if (\count($cat) > 0 && $cat[0] !== -1) { - $catsrch = Category::getCategorySearch($cat); + $catsrch = Category::getCategorySearch($cat, true); } if ($maxAge > 0) { $maxAge = sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge); diff --git a/Blacklight/Movie.php b/Blacklight/Movie.php index b88485630..bee0b1ff6 100755 --- a/Blacklight/Movie.php +++ b/Blacklight/Movie.php @@ -187,10 +187,7 @@ class Movie ->where('movieinfo.imdbid', '!=', '0000000') ->when($maxAge > 0, fn ($query) => $query->whereRaw('r.postdate > NOW() - INTERVAL ? DAY', [$maxAge])) ->when(! empty($excludedCats), fn ($query) => $query->whereNotIn('r.categories_id', $excludedCats)) - ->when(! empty($categorySearch), fn ($query) => $query->whereRaw( - // Check if $categorySearch starts with AND or OR and clean it up - preg_match('/^\s*(AND|OR)\s+/i', $categorySearch) ? preg_replace('/^\s*(AND|OR)\s+/i', '', $categorySearch) : $categorySearch - )) + ->when(! empty($categorySearch), fn ($query) => $query->whereRaw($categorySearch)) ->groupBy('movieinfo.imdbid') ->orderBy($order[0], $order[1]) ->offset($start) diff --git a/Blacklight/Music.php b/Blacklight/Music.php index 1d56a405a..7c9f06a46 100755 --- a/Blacklight/Music.php +++ b/Blacklight/Music.php @@ -130,7 +130,7 @@ class Music $browseby = $this->getBrowseBy(); $catsrch = ''; if (\count($cat) > 0 && (int) $cat[0] !== -1) { - $catsrch = Category::getCategorySearch($cat); + $catsrch = Category::getCategorySearch($cat, true); } $exccatlist = ''; if (\count($excludedCats) > 0) { diff --git a/Blacklight/Releases.php b/Blacklight/Releases.php index 78284a08d..5958a0d27 100644 --- a/Blacklight/Releases.php +++ b/Blacklight/Releases.php @@ -82,7 +82,7 @@ class Releases extends Release ORDER BY %8\$s %9\$s", NZB::NZB_ADDED, $this->showPasswords(), - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), ($maxAge > 0 ? (' AND postdate > NOW() - INTERVAL '.$maxAge.' DAY ') : ''), (\count($excludedCats) ? (' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')') : ''), ((int) $groupName !== -1 ? sprintf(' AND g.name = %s ', escapeString($groupName)) : ''), @@ -124,7 +124,7 @@ class Releases extends Release NZB::NZB_ADDED, $this->showPasswords(), ($groupName !== -1 ? sprintf(' AND g.name = %s', escapeString($groupName)) : ''), - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), ($maxAge > 0 ? (' AND r.postdate > NOW() - INTERVAL '.$maxAge.' DAY ') : ''), (\count($excludedCats) ? (' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')') : '') )); @@ -474,7 +474,7 @@ class Releases extends Release $catQuery = ''; if ($type === 'basic') { - $catQuery = Category::getCategorySearch($cat); + $catQuery = Category::getCategorySearch($cat, true); } elseif ($type === 'advanced' && (int) $cat[0] !== -1) { $catQuery = sprintf('AND r.categories_id = %d', $cat[0]); } @@ -560,7 +560,7 @@ class Releases extends Release } } - $catQuery = Category::getCategorySearch($cat); + $catQuery = Category::getCategorySearch($cat, true); $whereSql = sprintf( 'WHERE r.passwordstatus %s AND r.nzbstatus = %d %s %s %s %s %s %s', @@ -712,7 +712,7 @@ class Releases extends Release $this->showPasswords(), $showSql, (! empty($name) && ! empty($searchResult)) ? 'AND r.id IN ('.implode(',', $searchResult).')' : '', - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), $maxAge > 0 ? sprintf('AND r.postdate > NOW() - INTERVAL %d DAY', $maxAge) : '', $minSize > 0 ? sprintf('AND r.size >= %d', $minSize) : '', ! empty($excludedCategories) ? sprintf('AND r.categories_id NOT IN('.implode(',', $excludedCategories).')') : '' @@ -852,7 +852,7 @@ class Releases extends Release $this->showPasswords(), $showSql, (! empty($searchResult) ? 'AND r.id IN ('.implode(',', $searchResult).')' : ''), - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), ($maxAge > 0 ? sprintf('AND r.postdate > NOW() - INTERVAL %d DAY', $maxAge) : ''), ($minSize > 0 ? sprintf('AND r.size >= %d', $minSize) : ''), ! empty($excludedCategories) ? sprintf('AND r.categories_id NOT IN('.implode(',', $excludedCategories).')') : '' @@ -929,7 +929,7 @@ class Releases extends Release ($aniDbID > -1 ? sprintf(' AND r.anidbid = %d ', $aniDbID) : ''), (! empty($searchResult) ? 'AND r.id IN ('.implode(',', $searchResult).')' : ''), ! empty($excludedCategories) ? sprintf('AND r.categories_id NOT IN('.implode(',', $excludedCategories).')') : '', - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : '') ); $baseSql = sprintf( @@ -1002,7 +1002,7 @@ class Releases extends Release ($tmDbId !== -1 && is_numeric($tmDbId)) ? sprintf(' AND m.tmdbid = %d ', $tmDbId) : '', ($traktId !== -1 && is_numeric($traktId)) ? sprintf(' AND m.traktid = %d ', $traktId) : '', ! empty($excludedCategories) ? sprintf('AND r.categories_id NOT IN('.implode(',', $excludedCategories).')') : '', - Category::getCategorySearch($cat), + Category::getCategorySearch($cat, true), $maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : '', $minSize > 0 ? sprintf('AND r.size >= %d', $minSize) : '' ); diff --git a/Blacklight/XXX.php b/Blacklight/XXX.php index a54b3ec00..145a3f40a 100755 --- a/Blacklight/XXX.php +++ b/Blacklight/XXX.php @@ -73,7 +73,7 @@ class XXX { $catSrch = ''; if (\count($cat) > 0 && $cat[0] !== -1) { - $catSrch = Category::getCategorySearch($cat); + $catSrch = Category::getCategorySearch($cat, true); } $order = $this->getXXXOrder($orderBy); $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); diff --git a/app/Http/Controllers/Api/RSS.php b/app/Http/Controllers/Api/RSS.php index 50cefa769..c4e8d3ac9 100644 --- a/app/Http/Controllers/Api/RSS.php +++ b/app/Http/Controllers/Api/RSS.php @@ -44,7 +44,7 @@ class RSS extends ApiController $userID ); } elseif ((int) $cat[0] !== -1) { - $catSearch = Category::getCategorySearch($cat); + $catSearch = Category::getCategorySearch($cat, true); } } $sql = diff --git a/app/Models/Category.php b/app/Models/Category.php index 072a61f48..e07ecc6ae 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -304,18 +304,17 @@ class Category extends Model return $result; } - public static function getCategorySearch(array $cat = []): string + public static function getCategorySearch(array $cat = [], bool $useRaw = false): array|string { $categories = []; - // If multiple categories were sent in a single array position, slice and add them - if (strpos($cat[0], ',') !== false) { - $tmpcats = explode(',', $cat[0]); - // Reset the category to the first comma separated value in the string - $cat[0] = $tmpcats[0]; - // Add the remaining categories in the string to the original array - foreach (array_slice($tmpcats, 1) as $tmpcat) { - $cat[] = $tmpcat; + // If multiple categories were sent in a single array position, split them + if (isset($cat[0]) && str_contains($cat[0], ',')) { + $tempCategories = explode(',', $cat[0]); + // Reset the first category and add the remaining ones + $cat[0] = $tempCategories[0]; + foreach (array_slice($tempCategories, 1) as $tempCategory) { + $cat[] = $tempCategory; } } @@ -328,14 +327,24 @@ class Category extends Model } } + // Return based on whether it's for raw SQL or Laravel query builder $catCount = count($categories); - $catSearch = match ($catCount) { - 0 => 'AND 1=1', - 1 => $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : '', - default => ' AND r.categories_id IN ('.implode(', ', $categories).') ', - }; - return $catSearch; + if ($useRaw) { + // Raw MySQL query string + return match ($catCount) { + 0 => '1=1', // Always true condition + 1 => $categories[0] !== -1 ? 'AND r.categories_id = '.$categories[0] : '1=1', // Single category case + default => 'AND r.categories_id IN ('.implode(', ', $categories).')', // Multiple categories + }; + } + + // Laravel Query Builder array + return match ($catCount) { + 0 => ['1=1'], // Always true condition + 1 => [$categories[0] !== -1 ? 'r.categories_id = ?' : '1=1', $categories[0]], + default => 'r.categories_id IN ('.implode(', ', $categories).')', + }; } /**