| Total Complexity | 252 |
| Total Lines | 1305 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReleaseSearchService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReleaseSearchService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class ReleaseSearchService |
||
| 20 | { |
||
| 21 | private const CACHE_VERSION_KEY = 'releases:cache_version'; |
||
| 22 | |||
| 23 | // RAR/ZIP Password indicator. |
||
| 24 | public const PASSWD_NONE = 0; |
||
| 25 | |||
| 26 | public const PASSWD_RAR = 1; |
||
| 27 | |||
| 28 | public function __construct() {} |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Function for searching on the site (by subject, searchname or advanced). |
||
| 32 | * |
||
| 33 | * @return array|Collection|mixed |
||
| 34 | */ |
||
| 35 | public function search( |
||
| 36 | array $searchArr, |
||
| 37 | $groupName, |
||
| 38 | $sizeFrom, |
||
| 39 | $sizeTo, |
||
| 40 | $daysNew, |
||
| 41 | $daysOld, |
||
| 42 | int $offset = 0, |
||
| 43 | int $limit = 1000, |
||
| 44 | array|string $orderBy = '', |
||
| 45 | int $maxAge = -1, |
||
| 46 | array $excludedCats = [], |
||
| 47 | string $type = 'basic', |
||
| 48 | array $cat = [-1], |
||
| 49 | int $minSize = 0 |
||
| 50 | ): mixed { |
||
| 51 | if (config('app.debug')) { |
||
| 52 | Log::debug('ReleaseSearchService::search called', [ |
||
| 53 | 'searchArr' => $searchArr, |
||
| 54 | 'limit' => $limit, |
||
| 55 | ]); |
||
| 56 | } |
||
| 57 | |||
| 58 | // Get search results from index |
||
| 59 | $searchResult = $this->performIndexSearch($searchArr, $limit); |
||
| 60 | |||
| 61 | if (config('app.debug')) { |
||
| 62 | Log::debug('ReleaseSearchService::search after performIndexSearch', [ |
||
| 63 | 'result_count' => count($searchResult), |
||
| 64 | ]); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (count($searchResult) === 0) { |
||
| 68 | return collect(); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Build WHERE clause |
||
| 72 | $whereSql = $this->buildSearchWhereClause( |
||
| 73 | $searchResult, |
||
| 74 | $groupName, |
||
| 75 | $sizeFrom, |
||
| 76 | $sizeTo, |
||
| 77 | $daysNew, |
||
| 78 | $daysOld, |
||
| 79 | $maxAge, |
||
| 80 | $excludedCats, |
||
| 81 | $type, |
||
| 82 | $cat, |
||
| 83 | $minSize |
||
| 84 | ); |
||
| 85 | |||
| 86 | // Build base SQL |
||
| 87 | $baseSql = $this->buildSearchBaseSql($whereSql); |
||
| 88 | |||
| 89 | // Get order by clause |
||
| 90 | $orderBy = $this->getBrowseOrder($orderBy === '' ? 'posted_desc' : $orderBy); |
||
| 91 | |||
| 92 | // Build final SQL with pagination |
||
| 93 | $sql = sprintf( |
||
| 94 | 'SELECT * FROM (%s) r ORDER BY r.%s %s LIMIT %d OFFSET %d', |
||
| 95 | $baseSql, |
||
| 96 | $orderBy[0], |
||
| 97 | $orderBy[1], |
||
| 98 | $limit, |
||
| 99 | $offset |
||
| 100 | ); |
||
| 101 | |||
| 102 | // Check cache |
||
| 103 | $cacheKey = md5($this->getCacheVersion().$sql); |
||
| 104 | $releases = Cache::get($cacheKey); |
||
| 105 | if ($releases !== null) { |
||
| 106 | return $releases; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Execute query |
||
| 110 | $releases = Release::fromQuery($sql); |
||
| 111 | |||
| 112 | // Add total count for pagination |
||
| 113 | if ($releases->isNotEmpty()) { |
||
| 114 | $releases[0]->_totalrows = $this->getPagerCount($baseSql); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Cache results |
||
| 118 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
| 119 | Cache::put($cacheKey, $releases, $expiresAt); |
||
| 120 | |||
| 121 | return $releases; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Search function for API. |
||
| 126 | * |
||
| 127 | * @return Collection|mixed |
||
| 128 | */ |
||
| 129 | public function apiSearch($searchName, $groupName, int $offset = 0, int $limit = 1000, int $maxAge = -1, array $excludedCats = [], array $cat = [-1], int $minSize = 0): mixed |
||
| 130 | { |
||
| 131 | if (config('app.debug')) { |
||
| 132 | Log::debug('ReleaseSearchService::apiSearch called', [ |
||
| 133 | 'searchName' => $searchName, |
||
| 134 | 'groupName' => $groupName, |
||
| 135 | 'offset' => $offset, |
||
| 136 | 'limit' => $limit, |
||
| 137 | ]); |
||
| 138 | } |
||
| 139 | |||
| 140 | // Early return if searching with no results |
||
| 141 | $searchResult = []; |
||
| 142 | if ($searchName !== -1 && $searchName !== '' && $searchName !== null) { |
||
| 143 | // Use the unified Search facade with fuzzy fallback |
||
| 144 | $fuzzyResult = Search::searchReleasesWithFuzzy($searchName, $limit); |
||
| 145 | $searchResult = $fuzzyResult['ids'] ?? []; |
||
| 146 | |||
| 147 | if (config('app.debug') && ($fuzzyResult['fuzzy'] ?? false)) { |
||
| 148 | Log::debug('apiSearch: Using fuzzy search results'); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Fall back to MySQL if search engine returned no results (only if enabled) |
||
| 152 | if (empty($searchResult) && config('nntmux.mysql_search_fallback', false) === true) { |
||
| 153 | if (config('app.debug')) { |
||
| 154 | Log::debug('apiSearch: Falling back to MySQL search'); |
||
| 155 | } |
||
| 156 | $searchResult = $this->performMySQLSearch(['searchname' => $searchName], $limit); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (empty($searchResult)) { |
||
| 160 | if (config('app.debug')) { |
||
| 161 | Log::debug('apiSearch: No results from any search engine'); |
||
| 162 | } |
||
| 163 | |||
| 164 | return collect(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | $conditions = [ |
||
| 169 | sprintf('r.passwordstatus %s', $this->showPasswords()), |
||
| 170 | ]; |
||
| 171 | |||
| 172 | if ($maxAge > 0) { |
||
| 173 | $conditions[] = sprintf('r.postdate > (NOW() - INTERVAL %d DAY)', $maxAge); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ((int) $groupName !== -1) { |
||
| 177 | $groupId = UsenetGroup::getIDByName($groupName); |
||
| 178 | if ($groupId) { |
||
|
|
|||
| 179 | $conditions[] = sprintf('r.groups_id = %d', $groupId); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | $catQuery = Category::getCategorySearch($cat); |
||
| 184 | $catQuery = preg_replace('/^(WHERE|AND)\s+/i', '', trim($catQuery)); |
||
| 185 | if (! empty($catQuery) && $catQuery !== '1=1') { |
||
| 186 | $conditions[] = $catQuery; |
||
| 187 | } |
||
| 188 | |||
| 189 | if (! empty($excludedCats)) { |
||
| 190 | $conditions[] = sprintf('r.categories_id NOT IN (%s)', implode(',', array_map('intval', $excludedCats))); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (! empty($searchResult)) { |
||
| 194 | $conditions[] = sprintf('r.id IN (%s)', implode(',', array_map('intval', $searchResult))); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($minSize > 0) { |
||
| 198 | $conditions[] = sprintf('r.size >= %d', $minSize); |
||
| 199 | } |
||
| 200 | |||
| 201 | $whereSql = 'WHERE '.implode(' AND ', $conditions); |
||
| 202 | |||
| 203 | // Optimized query: remove unused columns/joins (haspreview, jpgstatus, *_id columns, nfo/video_data/failures) |
||
| 204 | $sql = sprintf( |
||
| 205 | "SELECT r.id, r.searchname, r.guid, r.postdate, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, |
||
| 206 | cp.title AS parent_category, c.title AS sub_category, |
||
| 207 | CONCAT(cp.title, ' > ', c.title) AS category_name, |
||
| 208 | g.name AS group_name, |
||
| 209 | m.imdbid, m.tmdbid, m.traktid, |
||
| 210 | v.tvdb, v.trakt, v.tvrage, v.tvmaze, v.imdb, v.tmdb, |
||
| 211 | tve.firstaired, tve.title, tve.series, tve.episode |
||
| 212 | FROM releases r |
||
| 213 | INNER JOIN categories c ON c.id = r.categories_id |
||
| 214 | INNER JOIN root_categories cp ON cp.id = c.root_categories_id |
||
| 215 | LEFT JOIN usenet_groups g ON g.id = r.groups_id |
||
| 216 | LEFT JOIN videos v ON r.videos_id = v.id AND r.videos_id > 0 |
||
| 217 | LEFT JOIN tv_episodes tve ON r.tv_episodes_id = tve.id AND r.tv_episodes_id > 0 |
||
| 218 | LEFT JOIN movieinfo m ON m.id = r.movieinfo_id AND r.movieinfo_id > 0 |
||
| 219 | %s |
||
| 220 | ORDER BY r.postdate DESC |
||
| 221 | LIMIT %d OFFSET %d", |
||
| 222 | $whereSql, |
||
| 223 | $limit, |
||
| 224 | $offset |
||
| 225 | ); |
||
| 226 | |||
| 227 | $cacheKey = md5($sql); |
||
| 228 | $cachedReleases = Cache::get($cacheKey); |
||
| 229 | if ($cachedReleases !== null) { |
||
| 230 | return $cachedReleases; |
||
| 231 | } |
||
| 232 | |||
| 233 | $releases = Release::fromQuery($sql); |
||
| 234 | |||
| 235 | if ($releases->isNotEmpty()) { |
||
| 236 | $countSql = sprintf('SELECT COUNT(*) as count FROM releases r %s', $whereSql); |
||
| 237 | $countResult = Release::fromQuery($countSql); |
||
| 238 | $releases[0]->_totalrows = $countResult[0]->count ?? 0; |
||
| 239 | } |
||
| 240 | |||
| 241 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
| 242 | Cache::put($cacheKey, $releases, $expiresAt); |
||
| 243 | |||
| 244 | return $releases; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Search for TV shows via API. |
||
| 249 | * |
||
| 250 | * @return array|\Illuminate\Cache\|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|mixed |
||
| 251 | */ |
||
| 252 | public function tvSearch(array $siteIdArr = [], string $series = '', string $episode = '', string $airDate = '', int $offset = 0, int $limit = 100, string $name = '', array $cat = [-1], int $maxAge = -1, int $minSize = 0, array $excludedCategories = []): mixed |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Search TV Shows via APIv2. |
||
| 516 | * |
||
| 517 | * @return Collection|mixed |
||
| 518 | */ |
||
| 519 | public function apiTvSearch(array $siteIdArr = [], string $series = '', string $episode = '', string $airDate = '', int $offset = 0, int $limit = 100, string $name = '', array $cat = [-1], int $maxAge = -1, int $minSize = 0, array $excludedCategories = []): mixed |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Search anime releases. |
||
| 659 | * |
||
| 660 | * @return Collection|mixed |
||
| 661 | */ |
||
| 662 | public function animeSearch($aniDbID, int $offset = 0, int $limit = 100, string $name = '', array $cat = [-1], int $maxAge = -1, array $excludedCategories = []): mixed |
||
| 723 | } |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Movies search through API and site. |
||
| 727 | * |
||
| 728 | * @return Collection|mixed |
||
| 729 | */ |
||
| 730 | public function moviesSearch(int $imDbId = -1, int $tmDbId = -1, int $traktId = -1, int $offset = 0, int $limit = 100, string $name = '', array $cat = [-1], int $maxAge = -1, int $minSize = 0, array $excludedCategories = []): mixed |
||
| 731 | { |
||
| 732 | $searchResult = []; |
||
| 733 | |||
| 734 | // OPTIMIZATION: If we have external IDs, use the search index to find releases directly |
||
| 735 | // This avoids expensive database JOINs by using indexed external ID fields in releases_rt |
||
| 736 | $externalIds = []; |
||
| 737 | if ($imDbId !== -1 && $imDbId > 0) { |
||
| 738 | $externalIds['imdbid'] = $imDbId; |
||
| 739 | } |
||
| 740 | if ($tmDbId !== -1 && $tmDbId > 0) { |
||
| 741 | $externalIds['tmdbid'] = $tmDbId; |
||
| 742 | } |
||
| 743 | if ($traktId !== -1 && $traktId > 0) { |
||
| 744 | $externalIds['traktid'] = $traktId; |
||
| 745 | } |
||
| 746 | |||
| 747 | // Use search index for external ID lookups (much faster than database JOINs) |
||
| 748 | if (! empty($externalIds)) { |
||
| 749 | $searchResult = Search::searchReleasesByExternalId($externalIds, $limit * 2); |
||
| 750 | |||
| 751 | if (config('app.debug') && ! empty($searchResult)) { |
||
| 752 | Log::debug('moviesSearch: Found releases via search index by external IDs', [ |
||
| 753 | 'externalIds' => $externalIds, |
||
| 754 | 'count' => count($searchResult), |
||
| 755 | ]); |
||
| 756 | } |
||
| 757 | } |
||
| 758 | |||
| 759 | // If no external IDs provided or index search failed, search by name |
||
| 760 | if (empty($searchResult) && ! empty($name)) { |
||
| 761 | // Use the unified Search facade with fuzzy fallback |
||
| 762 | $fuzzyResult = Search::searchReleasesWithFuzzy($name, $limit); |
||
| 763 | $searchResult = $fuzzyResult['ids'] ?? []; |
||
| 764 | |||
| 765 | // Fall back to MySQL if search engine returned no results (only if enabled) |
||
| 766 | if (empty($searchResult) && config('nntmux.mysql_search_fallback', false) === true) { |
||
| 767 | $searchResult = $this->performMySQLSearch(['searchname' => $name], $limit); |
||
| 768 | } |
||
| 769 | |||
| 770 | // Only return empty if we were specifically searching by name but found nothing |
||
| 771 | if (empty($searchResult)) { |
||
| 772 | return collect(); |
||
| 773 | } |
||
| 774 | } |
||
| 775 | |||
| 776 | // Build the base conditions for movie search |
||
| 777 | // Note: we don't have MOVIE_ROOT constant that marks a parent category, |
||
| 778 | // so we'll rely on the category search logic instead |
||
| 779 | $conditions = [ |
||
| 780 | sprintf('r.passwordstatus %s', $this->showPasswords()), |
||
| 781 | ]; |
||
| 782 | |||
| 783 | if (! empty($searchResult)) { |
||
| 784 | $conditions[] = sprintf('r.id IN (%s)', implode(',', array_map('intval', $searchResult))); |
||
| 785 | } |
||
| 786 | |||
| 787 | // When we have external IDs but no index results, fall back to database query |
||
| 788 | // This handles the case where the index might be empty/out of sync |
||
| 789 | $needsMovieJoin = false; |
||
| 790 | if (empty($searchResult) && ! empty($externalIds)) { |
||
| 791 | $needsMovieJoin = true; |
||
| 792 | if ($imDbId !== -1 && $imDbId > 0) { |
||
| 793 | $conditions[] = sprintf('r.imdbid = %d', $imDbId); |
||
| 794 | } |
||
| 795 | if ($tmDbId !== -1 && $tmDbId > 0) { |
||
| 796 | $conditions[] = sprintf('m.tmdbid = %d', $tmDbId); |
||
| 797 | } |
||
| 798 | if ($traktId !== -1 && $traktId > 0) { |
||
| 799 | $conditions[] = sprintf('m.traktid = %d', $traktId); |
||
| 800 | } |
||
| 801 | } |
||
| 802 | |||
| 803 | if (! empty($excludedCategories)) { |
||
| 804 | $conditions[] = sprintf('r.categories_id NOT IN (%s)', implode(',', array_map('intval', $excludedCategories))); |
||
| 805 | } |
||
| 806 | |||
| 807 | $catQuery = Category::getCategorySearch($cat, 'movies'); |
||
| 808 | $catQuery = preg_replace('/^(WHERE|AND)\s+/i', '', trim($catQuery)); |
||
| 809 | if (! empty($catQuery) && $catQuery !== '1=1') { |
||
| 810 | $conditions[] = $catQuery; |
||
| 811 | } |
||
| 812 | if ($maxAge > 0) { |
||
| 813 | $conditions[] = sprintf('r.postdate > (NOW() - INTERVAL %d DAY)', $maxAge); |
||
| 814 | } |
||
| 815 | if ($minSize > 0) { |
||
| 816 | $conditions[] = sprintf('r.size >= %d', $minSize); |
||
| 817 | } |
||
| 818 | |||
| 819 | $whereSql = 'WHERE '.implode(' AND ', $conditions); |
||
| 820 | |||
| 821 | // Only join movieinfo if we need to filter by tmdbid/traktid (database fallback) |
||
| 822 | // When using search index, we already have the release IDs and don't need the join |
||
| 823 | $joinSql = $needsMovieJoin ? 'INNER JOIN movieinfo m ON m.imdbid = r.imdbid' : 'LEFT JOIN movieinfo m ON m.id = r.movieinfo_id'; |
||
| 824 | |||
| 825 | // Select only fields required by XML/API transformers |
||
| 826 | $baseSql = sprintf( |
||
| 827 | "SELECT r.id, r.searchname, r.guid, r.postdate, r.categories_id, |
||
| 828 | r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, |
||
| 829 | r.adddate, |
||
| 830 | %s |
||
| 831 | cp.title AS parent_category, c.title AS sub_category, |
||
| 832 | CONCAT(cp.title, ' > ', c.title) AS category_name, |
||
| 833 | g.name AS group_name |
||
| 834 | FROM releases r |
||
| 835 | INNER JOIN categories c ON c.id = r.categories_id |
||
| 836 | INNER JOIN root_categories cp ON cp.id = c.root_categories_id |
||
| 837 | %s |
||
| 838 | LEFT JOIN usenet_groups g ON g.id = r.groups_id |
||
| 839 | %s", |
||
| 840 | 'm.imdbid, m.tmdbid, m.traktid,', |
||
| 841 | $joinSql, |
||
| 842 | $whereSql |
||
| 843 | ); |
||
| 844 | |||
| 845 | $sql = sprintf('%s ORDER BY r.postdate DESC LIMIT %d OFFSET %d', $baseSql, $limit, $offset); |
||
| 846 | $cacheKey = md5($sql.serialize(func_get_args())); |
||
| 847 | if (($releases = Cache::get($cacheKey)) !== null) { |
||
| 848 | return $releases; |
||
| 849 | } |
||
| 850 | |||
| 851 | $releases = Release::fromQuery($sql); |
||
| 852 | |||
| 853 | if ($releases->isNotEmpty()) { |
||
| 854 | // Optimize: Execute count query using same WHERE clause |
||
| 855 | $countSql = sprintf( |
||
| 856 | 'SELECT COUNT(*) as count FROM releases r %s %s', |
||
| 857 | $needsMovieJoin ? $joinSql : '', |
||
| 858 | $whereSql |
||
| 859 | ); |
||
| 860 | $countResult = DB::selectOne($countSql); |
||
| 861 | $releases[0]->_totalrows = $countResult->count ?? 0; |
||
| 862 | } |
||
| 863 | |||
| 864 | Cache::put($cacheKey, $releases, now()->addMinutes(config('nntmux.cache_expiry_medium'))); |
||
| 865 | |||
| 866 | return $releases; |
||
| 867 | } |
||
| 868 | |||
| 869 | public function searchSimilar($currentID, $name, array $excludedCats = []): bool|array |
||
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Perform index search using Elasticsearch or Manticore, with MySQL fallback |
||
| 900 | */ |
||
| 901 | private function performIndexSearch(array $searchArr, int $limit): array |
||
| 902 | { |
||
| 903 | // Filter out -1 values and empty strings |
||
| 904 | $searchFields = Arr::where($searchArr, static function ($value) { |
||
| 905 | return $value !== -1 && $value !== '' && $value !== null; |
||
| 906 | }); |
||
| 907 | |||
| 908 | if (empty($searchFields)) { |
||
| 909 | if (config('app.debug')) { |
||
| 910 | Log::debug('performIndexSearch: searchFields is empty after filtering', [ |
||
| 911 | 'original' => $searchArr, |
||
| 912 | ]); |
||
| 913 | } |
||
| 914 | |||
| 915 | return []; |
||
| 916 | } |
||
| 917 | |||
| 918 | if (config('app.debug')) { |
||
| 919 | Log::debug('performIndexSearch: starting search', [ |
||
| 920 | 'search_driver' => config('search.default'), |
||
| 921 | 'searchFields' => $searchFields, |
||
| 922 | 'limit' => $limit, |
||
| 923 | ]); |
||
| 924 | } |
||
| 925 | |||
| 926 | // Use the unified Search facade with fuzzy fallback |
||
| 927 | // This will try exact search first, then fuzzy if no results |
||
| 928 | $searchResult = Search::searchReleasesWithFuzzy($searchFields, $limit); |
||
| 929 | $result = $searchResult['ids'] ?? []; |
||
| 930 | |||
| 931 | if (config('app.debug')) { |
||
| 932 | Log::debug('performIndexSearch: Search result', [ |
||
| 933 | 'count' => count($result), |
||
| 934 | 'fuzzy_used' => $searchResult['fuzzy'] ?? false, |
||
| 935 | ]); |
||
| 936 | } |
||
| 937 | |||
| 938 | // If search returned results, use them |
||
| 939 | if (! empty($result)) { |
||
| 940 | return $result; |
||
| 941 | } |
||
| 942 | |||
| 943 | // Fallback to MySQL LIKE search when search engine is unavailable (only if enabled) |
||
| 944 | if (config('nntmux.mysql_search_fallback', false) === true) { |
||
| 945 | if (config('app.debug')) { |
||
| 946 | Log::debug('performIndexSearch: Falling back to MySQL search'); |
||
| 947 | } |
||
| 948 | |||
| 949 | return $this->performMySQLSearch($searchFields, $limit); |
||
| 950 | } |
||
| 951 | |||
| 952 | return []; |
||
| 953 | } |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Fallback MySQL search when full-text search engines are unavailable |
||
| 957 | */ |
||
| 958 | private function performMySQLSearch(array $searchFields, int $limit): array |
||
| 959 | { |
||
| 960 | try { |
||
| 961 | $query = Release::query()->select('id'); |
||
| 962 | |||
| 963 | foreach ($searchFields as $field => $value) { |
||
| 964 | if (! empty($value)) { |
||
| 965 | // Split search terms and search for each |
||
| 966 | $terms = preg_split('/\s+/', trim($value)); |
||
| 967 | foreach ($terms as $term) { |
||
| 968 | $term = trim($term); |
||
| 969 | if (strlen($term) >= 2) { |
||
| 970 | $query->where($field, 'LIKE', '%'.$term.'%'); |
||
| 971 | } |
||
| 972 | } |
||
| 973 | } |
||
| 974 | } |
||
| 975 | |||
| 976 | $results = $query->limit($limit)->pluck('id')->toArray(); |
||
| 977 | |||
| 978 | if (config('app.debug')) { |
||
| 979 | Log::debug('performMySQLSearch: MySQL fallback result count', ['count' => count($results)]); |
||
| 980 | } |
||
| 981 | |||
| 982 | return $results; |
||
| 983 | } catch (\Throwable $e) { |
||
| 984 | Log::error('performMySQLSearch: MySQL fallback failed', [ |
||
| 985 | 'error' => $e->getMessage(), |
||
| 986 | ]); |
||
| 987 | |||
| 988 | return []; |
||
| 989 | } |
||
| 990 | } |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Build WHERE clause for search query |
||
| 994 | */ |
||
| 995 | private function buildSearchWhereClause( |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Build size conditions for WHERE clause |
||
| 1061 | */ |
||
| 1062 | private function buildSizeConditions($sizeFrom, $sizeTo): array |
||
| 1063 | { |
||
| 1064 | $sizeRange = [ |
||
| 1065 | 1 => 1, |
||
| 1066 | 2 => 2.5, |
||
| 1067 | 3 => 5, |
||
| 1068 | 4 => 10, |
||
| 1069 | 5 => 20, |
||
| 1070 | 6 => 30, |
||
| 1071 | 7 => 40, |
||
| 1072 | 8 => 80, |
||
| 1073 | 9 => 160, |
||
| 1074 | 10 => 320, |
||
| 1075 | 11 => 640, |
||
| 1076 | ]; |
||
| 1077 | |||
| 1078 | $conditions = []; |
||
| 1079 | |||
| 1080 | if (array_key_exists($sizeFrom, $sizeRange)) { |
||
| 1081 | $conditions[] = sprintf('r.size > %d', 104857600 * (int) $sizeRange[$sizeFrom]); |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | if (array_key_exists($sizeTo, $sizeRange)) { |
||
| 1085 | $conditions[] = sprintf('r.size < %d', 104857600 * (int) $sizeRange[$sizeTo]); |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | return $conditions; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Build category condition based on search type |
||
| 1093 | */ |
||
| 1094 | private function buildCategoryCondition(string $type, array $cat): string |
||
| 1095 | { |
||
| 1096 | if ($type === 'basic') { |
||
| 1097 | $catSearch = Category::getCategorySearch($cat); |
||
| 1098 | // Remove WHERE and AND from the beginning as we're building it into a larger WHERE clause |
||
| 1099 | $catSearch = preg_replace('/^(WHERE|AND)\s+/i', '', trim($catSearch)); |
||
| 1100 | |||
| 1101 | // Don't return '1=1' as it's not needed |
||
| 1102 | return ($catSearch === '1=1') ? '' : $catSearch; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | if ($type === 'advanced' && (int) $cat[0] !== -1) { |
||
| 1106 | return sprintf('r.categories_id = %d', (int) $cat[0]); |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | return ''; |
||
| 1110 | } |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Build base SQL for search query |
||
| 1114 | */ |
||
| 1115 | private function buildSearchBaseSql(string $whereSql): string |
||
| 1116 | { |
||
| 1117 | return sprintf( |
||
| 1118 | "SELECT r.id, r.searchname, r.guid, r.postdate, r.groups_id, r.categories_id, r.size, |
||
| 1119 | r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, |
||
| 1120 | r.videos_id, r.tv_episodes_id, r.haspreview, r.jpgstatus, |
||
| 1121 | cp.title AS parent_category, c.title AS sub_category, |
||
| 1122 | CONCAT(cp.title, ' > ', c.title) AS category_name, |
||
| 1123 | df.failed AS failed, |
||
| 1124 | g.name AS group_name, |
||
| 1125 | rn.releases_id AS nfoid, |
||
| 1126 | re.releases_id AS reid, |
||
| 1127 | cp.id AS categoryparentid, |
||
| 1128 | v.tvdb, v.trakt, v.tvrage, v.tvmaze, v.imdb, v.tmdb, |
||
| 1129 | tve.firstaired |
||
| 1130 | FROM releases r |
||
| 1131 | LEFT OUTER JOIN video_data re ON re.releases_id = r.id |
||
| 1132 | LEFT OUTER JOIN videos v ON r.videos_id = v.id |
||
| 1133 | LEFT OUTER JOIN tv_episodes tve ON r.tv_episodes_id = tve.id |
||
| 1134 | LEFT OUTER JOIN release_nfos rn ON rn.releases_id = r.id |
||
| 1135 | LEFT JOIN usenet_groups g ON g.id = r.groups_id |
||
| 1136 | LEFT JOIN categories c ON c.id = r.categories_id |
||
| 1137 | LEFT JOIN root_categories cp ON cp.id = c.root_categories_id |
||
| 1138 | LEFT OUTER JOIN dnzb_failures df ON df.release_id = r.id |
||
| 1139 | %s", |
||
| 1140 | $whereSql |
||
| 1141 | ); |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Get the passworded releases clause. |
||
| 1146 | */ |
||
| 1147 | public function showPasswords(): string |
||
| 1148 | { |
||
| 1149 | $show = (int) Settings::settingValue('showpasswordedrelease'); |
||
| 1150 | $setting = $show ?? 0; |
||
| 1151 | |||
| 1152 | return match ($setting) { |
||
| 1153 | 1 => '<= '.self::PASSWD_RAR, |
||
| 1154 | default => '= '.self::PASSWD_NONE, |
||
| 1155 | }; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Use to order releases on site. |
||
| 1160 | */ |
||
| 1161 | public function getBrowseOrder(array|string $orderBy): array |
||
| 1162 | { |
||
| 1163 | $orderArr = explode('_', ($orderBy === '' ? 'posted_desc' : $orderBy)); |
||
| 1164 | $orderField = match ($orderArr[0]) { |
||
| 1165 | 'cat' => 'categories_id', |
||
| 1166 | 'name' => 'searchname', |
||
| 1167 | 'size' => 'size', |
||
| 1168 | 'files' => 'totalpart', |
||
| 1169 | 'stats' => 'grabs', |
||
| 1170 | default => 'postdate', |
||
| 1171 | }; |
||
| 1172 | |||
| 1173 | return [$orderField, isset($orderArr[1]) && preg_match('/^(asc|desc)$/i', $orderArr[1]) ? $orderArr[1] : 'desc']; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | private function getCacheVersion(): int |
||
| 1177 | { |
||
| 1178 | return Cache::get(self::CACHE_VERSION_KEY, 1); |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Get the count of releases for pager. |
||
| 1183 | * |
||
| 1184 | * @param string $query The query to get the count from. |
||
| 1185 | */ |
||
| 1186 | private function getPagerCount(string $query): int |
||
| 1324 | } |
||
| 1325 | } |
||
| 1326 | } |
||
| 1327 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: