| Total Complexity | 76 |
| Total Lines | 558 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReleaseBrowseService 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 ReleaseBrowseService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ReleaseBrowseService |
||
| 18 | { |
||
| 19 | private const CACHE_VERSION_KEY = 'releases:cache_version'; |
||
| 20 | |||
| 21 | // RAR/ZIP Password indicator. |
||
| 22 | public const PASSWD_NONE = 0; // No password. |
||
| 23 | |||
| 24 | public const PASSWD_RAR = 1; // Definitely passworded. |
||
| 25 | |||
| 26 | public function __construct() {} |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Used for Browse results with optional search term filtering via search index. |
||
| 30 | * |
||
| 31 | * @param string|null $searchTerm Optional search term to filter by (uses search index) |
||
| 32 | * @return Collection|mixed |
||
| 33 | */ |
||
| 34 | public function getBrowseRange($page, $cat, $start, $num, $orderBy, int $maxAge = -1, array $excludedCats = [], int|string $groupName = -1, int $minSize = 0, ?string $searchTerm = null): mixed |
||
| 35 | { |
||
| 36 | $cacheVersion = $this->getCacheVersion(); |
||
| 37 | $page = max(1, $page); |
||
| 38 | $start = max(0, $start); |
||
| 39 | |||
| 40 | $orderBy = $this->getBrowseOrder($orderBy); |
||
| 41 | |||
| 42 | // Use search index filtering when a search term is provided |
||
| 43 | $searchIndexFilter = ''; |
||
| 44 | $searchIndexIds = []; |
||
| 45 | if (! empty($searchTerm) && Search::isAvailable()) { |
||
| 46 | $searchResult = Search::searchReleasesWithFuzzy(['searchname' => $searchTerm], $num * 10); |
||
| 47 | $searchIndexIds = $searchResult['ids'] ?? []; |
||
| 48 | |||
| 49 | if (config('app.debug') && ($searchResult['fuzzy'] ?? false)) { |
||
| 50 | Log::debug('getBrowseRange: Using fuzzy search results for browse filtering'); |
||
| 51 | } |
||
| 52 | |||
| 53 | if (empty($searchIndexIds)) { |
||
| 54 | // No results from search index, return empty result |
||
| 55 | return []; |
||
| 56 | } |
||
| 57 | |||
| 58 | $searchIndexFilter = sprintf(' AND r.id IN (%s)', implode(',', array_map('intval', $searchIndexIds))); |
||
| 59 | } |
||
| 60 | |||
| 61 | $qry = sprintf( |
||
| 62 | "SELECT r.id, r.searchname, r.groups_id, r.guid, r.postdate, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id, r.haspreview, r.jpgstatus, r.nfostatus, cp.title AS parent_category, c.title AS sub_category, r.group_name, |
||
| 63 | CONCAT(cp.title, ' > ', c.title) AS category_name, |
||
| 64 | CONCAT(cp.id, ',', c.id) AS category_ids, |
||
| 65 | df.failed AS failed, |
||
| 66 | rn.releases_id AS nfoid, |
||
| 67 | re.releases_id AS reid, |
||
| 68 | v.tvdb, v.trakt, v.tvrage, v.tvmaze, v.imdb, v.tmdb, |
||
| 69 | m.imdbid, m.tmdbid, m.traktid, |
||
| 70 | tve.title, tve.firstaired |
||
| 71 | FROM |
||
| 72 | ( |
||
| 73 | SELECT r.id, r.searchname, r.guid, r.postdate, r.groups_id, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id, r.haspreview, r.jpgstatus, r.nfostatus, g.name AS group_name, r.movieinfo_id |
||
| 74 | FROM releases r |
||
| 75 | LEFT JOIN usenet_groups g ON g.id = r.groups_id |
||
| 76 | WHERE r.passwordstatus %1\$s |
||
| 77 | %2\$s %3\$s %4\$s %5\$s %6\$s %7\$s |
||
| 78 | ORDER BY %8\$s %9\$s %10\$s |
||
| 79 | ) r |
||
| 80 | LEFT JOIN categories c ON c.id = r.categories_id |
||
| 81 | LEFT JOIN root_categories cp ON cp.id = c.root_categories_id |
||
| 82 | LEFT OUTER JOIN videos v ON r.videos_id = v.id |
||
| 83 | LEFT OUTER JOIN tv_episodes tve ON r.tv_episodes_id = tve.id |
||
| 84 | LEFT OUTER JOIN movieinfo m ON m.id = r.movieinfo_id |
||
| 85 | LEFT OUTER JOIN video_data re ON re.releases_id = r.id |
||
| 86 | LEFT OUTER JOIN release_nfos rn ON rn.releases_id = r.id |
||
| 87 | LEFT OUTER JOIN dnzb_failures df ON df.release_id = r.id |
||
| 88 | GROUP BY r.id |
||
| 89 | ORDER BY %8\$s %9\$s", |
||
| 90 | $this->showPasswords(), |
||
| 91 | Category::getCategorySearch($cat), |
||
| 92 | ($maxAge > 0 ? (' AND postdate > NOW() - INTERVAL '.$maxAge.' DAY ') : ''), |
||
| 93 | (\count($excludedCats) ? (' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')') : ''), |
||
| 94 | ((int) $groupName !== -1 ? sprintf(' AND g.name = %s ', escapeString($groupName)) : ''), |
||
| 95 | ($minSize > 0 ? sprintf('AND r.size >= %d', $minSize) : ''), |
||
| 96 | $searchIndexFilter, |
||
| 97 | $orderBy[0], |
||
| 98 | $orderBy[1], |
||
| 99 | ($start === 0 ? ' LIMIT '.$num : ' LIMIT '.$num.' OFFSET '.$start) |
||
| 100 | ); |
||
| 101 | |||
| 102 | $cacheKey = md5($cacheVersion.$qry.$page); |
||
| 103 | $releases = Cache::get($cacheKey); |
||
| 104 | if ($releases !== null) { |
||
| 105 | return $releases; |
||
| 106 | } |
||
| 107 | $sql = DB::select($qry); |
||
| 108 | if (\count($sql) > 0) { |
||
| 109 | // When using search index, use the ID count for total rows |
||
| 110 | if (! empty($searchIndexIds)) { |
||
| 111 | $sql[0]->_totalcount = $sql[0]->_totalrows = count($searchIndexIds); |
||
| 112 | } else { |
||
| 113 | $possibleRows = $this->getBrowseCount($cat, $maxAge, $excludedCats, $groupName); |
||
| 114 | $sql[0]->_totalcount = $sql[0]->_totalrows = $possibleRows; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
| 118 | Cache::put($cacheKey, $sql, $expiresAt); |
||
| 119 | |||
| 120 | return $sql; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Used for pager on browse page. |
||
| 125 | * Optimized to avoid expensive COUNT queries on large tables. |
||
| 126 | * Uses sample-based counting and avoids JOINs whenever possible. |
||
| 127 | */ |
||
| 128 | public function getBrowseCount(array $cat, int $maxAge = -1, array $excludedCats = [], int|string $groupName = ''): int |
||
| 129 | { |
||
| 130 | $maxResults = (int) config('nntmux.max_pager_results', 500000); |
||
| 131 | $cacheExpiry = (int) config('nntmux.cache_expiry_short', 5); |
||
| 132 | |||
| 133 | // Build a unique cache key for this specific query |
||
| 134 | $cacheKey = 'browse_count_'.md5(serialize($cat).$maxAge.serialize($excludedCats).$groupName); |
||
| 135 | |||
| 136 | // Check cache first - use longer cache time for count queries since they're expensive |
||
| 137 | $count = Cache::get($cacheKey); |
||
| 138 | if ($count !== null) { |
||
| 139 | return (int) $count; |
||
| 140 | } |
||
| 141 | |||
| 142 | // Build optimized count query - avoid JOINs when possible |
||
| 143 | $conditions = ['r.passwordstatus '.$this->showPasswords()]; |
||
| 144 | |||
| 145 | // Add category conditions |
||
| 146 | $catQuery = Category::getCategorySearch($cat); |
||
| 147 | $catQuery = preg_replace('/^(WHERE|AND)\s+/i', '', trim($catQuery)); |
||
| 148 | if (! empty($catQuery) && $catQuery !== '1=1') { |
||
| 149 | $conditions[] = $catQuery; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($maxAge > 0) { |
||
| 153 | $conditions[] = 'r.postdate > NOW() - INTERVAL '.$maxAge.' DAY'; |
||
| 154 | } |
||
| 155 | |||
| 156 | if (! empty($excludedCats)) { |
||
| 157 | $conditions[] = 'r.categories_id NOT IN ('.implode(',', array_map('intval', $excludedCats)).')'; |
||
| 158 | } |
||
| 159 | |||
| 160 | // Only add group filter if specified - this requires a JOIN |
||
| 161 | $needsGroupJoin = (int) $groupName !== -1; |
||
| 162 | if ($needsGroupJoin) { |
||
| 163 | $conditions[] = sprintf('g.name = %s', escapeString($groupName)); |
||
| 164 | } |
||
| 165 | |||
| 166 | $whereSql = 'WHERE '.implode(' AND ', $conditions); |
||
| 167 | |||
| 168 | try { |
||
| 169 | // For queries with maxResults limit, use sample-based counting |
||
| 170 | if ($maxResults > 0) { |
||
| 171 | // Quick check using a small sample (1000 rows) to determine if we should |
||
| 172 | // just return maxResults or do a full count |
||
| 173 | $sampleLimit = min(1000, $maxResults); |
||
| 174 | |||
| 175 | if ($needsGroupJoin) { |
||
| 176 | // Need JOIN for group filtering |
||
| 177 | $sampleQuery = sprintf( |
||
| 178 | 'SELECT r.id FROM releases r INNER JOIN usenet_groups g ON g.id = r.groups_id %s ORDER BY r.id DESC LIMIT %d', |
||
| 179 | $whereSql, |
||
| 180 | $sampleLimit |
||
| 181 | ); |
||
| 182 | } else { |
||
| 183 | // No JOIN needed - much faster query on releases table only |
||
| 184 | $sampleQuery = sprintf( |
||
| 185 | 'SELECT r.id FROM releases r %s ORDER BY r.id DESC LIMIT %d', |
||
| 186 | $whereSql, |
||
| 187 | $sampleLimit |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | $sampleResult = DB::select($sampleQuery); |
||
| 192 | $sampleCount = count($sampleResult); |
||
| 193 | |||
| 194 | // If we got the full sample, assume there are more rows than maxResults |
||
| 195 | if ($sampleCount >= $sampleLimit) { |
||
| 196 | Cache::put($cacheKey, $maxResults, now()->addMinutes($cacheExpiry * 2)); |
||
| 197 | |||
| 198 | return $maxResults; |
||
| 199 | } |
||
| 200 | |||
| 201 | // Fewer than sample limit - this is the actual count |
||
| 202 | $count = $sampleCount; |
||
| 203 | } else { |
||
| 204 | // No max limit set, need full count |
||
| 205 | if ($needsGroupJoin) { |
||
| 206 | $query = sprintf( |
||
| 207 | 'SELECT COUNT(r.id) AS count FROM releases r INNER JOIN usenet_groups g ON g.id = r.groups_id %s', |
||
| 208 | $whereSql |
||
| 209 | ); |
||
| 210 | } else { |
||
| 211 | // No JOIN needed - simple count on releases table |
||
| 212 | $query = sprintf('SELECT COUNT(r.id) AS count FROM releases r %s', $whereSql); |
||
| 213 | } |
||
| 214 | $result = DB::select($query); |
||
| 215 | $count = isset($result[0]) ? (int) $result[0]->count : 0; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Cache with longer expiry for count queries |
||
| 219 | Cache::put($cacheKey, $count, now()->addMinutes($cacheExpiry * 2)); |
||
| 220 | |||
| 221 | return $count; |
||
| 222 | } catch (\Exception $e) { |
||
| 223 | Log::error('getBrowseCount failed', ['error' => $e->getMessage()]); |
||
| 224 | |||
| 225 | return 0; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the passworded releases clause. |
||
| 231 | */ |
||
| 232 | public function showPasswords(): string |
||
| 233 | { |
||
| 234 | $show = (int) Settings::settingValue('showpasswordedrelease'); |
||
| 235 | $setting = $show ?? 0; |
||
| 236 | |||
| 237 | return match ($setting) { |
||
| 238 | 1 => '<= '.self::PASSWD_RAR, |
||
| 239 | default => '= '.self::PASSWD_NONE, |
||
| 240 | }; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Use to order releases on site. |
||
| 245 | */ |
||
| 246 | public function getBrowseOrder(array|string $orderBy): array |
||
| 247 | { |
||
| 248 | $orderArr = explode('_', ($orderBy === '' ? 'posted_desc' : $orderBy)); |
||
| 249 | $orderField = match ($orderArr[0]) { |
||
| 250 | 'cat' => 'categories_id', |
||
| 251 | 'name' => 'searchname', |
||
| 252 | 'size' => 'size', |
||
| 253 | 'files' => 'totalpart', |
||
| 254 | 'stats' => 'grabs', |
||
| 255 | default => 'postdate', |
||
| 256 | }; |
||
| 257 | |||
| 258 | return [$orderField, isset($orderArr[1]) && preg_match('/^(asc|desc)$/i', $orderArr[1]) ? $orderArr[1] : 'desc']; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return ordering types usable on site. |
||
| 263 | * |
||
| 264 | * @return string[] |
||
| 265 | */ |
||
| 266 | public function getBrowseOrdering(): array |
||
| 267 | { |
||
| 268 | return [ |
||
| 269 | 'name_asc', |
||
| 270 | 'name_desc', |
||
| 271 | 'cat_asc', |
||
| 272 | 'cat_desc', |
||
| 273 | 'posted_asc', |
||
| 274 | 'posted_desc', |
||
| 275 | 'size_asc', |
||
| 276 | 'size_desc', |
||
| 277 | 'files_asc', |
||
| 278 | 'files_desc', |
||
| 279 | 'stats_asc', |
||
| 280 | 'stats_desc', |
||
| 281 | ]; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return \Illuminate\Cache\|\Illuminate\Database\Eloquent\Collection|mixed |
||
| 286 | */ |
||
| 287 | public function getShowsRange($userShows, $offset, $limit, $orderBy, int $maxAge = -1, array $excludedCats = []) |
||
| 288 | { |
||
| 289 | $orderBy = $this->getBrowseOrder($orderBy); |
||
| 290 | $sql = sprintf( |
||
| 291 | "SELECT r.id, r.searchname, r.guid, r.postdate, r.groups_id, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id, r.haspreview, r.jpgstatus, cp.title AS parent_category, c.title AS sub_category, |
||
| 292 | CONCAT(cp.title, '->', c.title) AS category_name |
||
| 293 | FROM releases r |
||
| 294 | LEFT JOIN categories c ON c.id = r.categories_id |
||
| 295 | LEFT JOIN root_categories cp ON cp.id = c.root_categories_id |
||
| 296 | WHERE %s %s |
||
| 297 | AND r.categories_id BETWEEN %d AND %d |
||
| 298 | AND r.passwordstatus %s |
||
| 299 | %s |
||
| 300 | GROUP BY r.id |
||
| 301 | ORDER BY %s %s %s", |
||
| 302 | $this->uSQL($userShows, 'videos_id'), |
||
| 303 | (! empty($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''), |
||
| 304 | Category::TV_ROOT, |
||
| 305 | Category::TV_OTHER, |
||
| 306 | $this->showPasswords(), |
||
| 307 | ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : ''), |
||
| 308 | $orderBy[0], |
||
| 309 | $orderBy[1], |
||
| 310 | ($offset === false ? '' : (' LIMIT '.$limit.' OFFSET '.$offset)) |
||
| 311 | ); |
||
| 312 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
| 313 | $result = Cache::get(md5($sql)); |
||
| 314 | if ($result !== null) { |
||
| 315 | return $result; |
||
| 316 | } |
||
| 317 | $result = Release::fromQuery($sql); |
||
| 318 | Cache::put(md5($sql), $result, $expiresAt); |
||
| 319 | |||
| 320 | return $result; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function getShowsCount($userShows, int $maxAge = -1, array $excludedCats = []): int |
||
| 339 | ) |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Creates part of a query for some functions. |
||
| 345 | */ |
||
| 346 | public function uSQL(Collection|array $userQuery, string $type): string |
||
| 347 | { |
||
| 348 | $sql = '(1=2 '; |
||
| 349 | foreach ($userQuery as $query) { |
||
| 350 | $sql .= sprintf('OR (r.%s = %d', $type, $query->$type); |
||
| 351 | if (! empty($query->categories)) { |
||
| 352 | $catsArr = explode('|', $query->categories); |
||
| 353 | if (\count($catsArr) > 1) { |
||
| 354 | $sql .= sprintf(' AND r.categories_id IN (%s)', implode(',', $catsArr)); |
||
| 355 | } else { |
||
| 356 | $sql .= sprintf(' AND r.categories_id = %d', $catsArr[0]); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | $sql .= ') '; |
||
| 360 | } |
||
| 361 | $sql .= ') '; |
||
| 362 | |||
| 363 | return $sql; |
||
| 364 | } |
||
| 365 | |||
| 366 | public static function bumpCacheVersion(): void |
||
| 367 | { |
||
| 368 | $current = Cache::get(self::CACHE_VERSION_KEY, 1); |
||
| 369 | Cache::forever(self::CACHE_VERSION_KEY, $current + 1); |
||
| 370 | } |
||
| 371 | |||
| 372 | private function getCacheVersion(): int |
||
| 373 | { |
||
| 374 | return Cache::get(self::CACHE_VERSION_KEY, 1); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Search releases using the search index with category filtering. |
||
| 379 | * This method pre-filters results via the search index before hitting the database, |
||
| 380 | * significantly improving performance for searches with text terms. |
||
| 381 | * |
||
| 382 | * @param string $searchTerm Search term to match |
||
| 383 | * @param array $categories Category IDs to filter by (optional) |
||
| 384 | * @param int $limit Maximum number of results |
||
| 385 | * @return array Array of release IDs matching the search criteria |
||
| 386 | */ |
||
| 387 | public function searchByIndexWithCategories(string $searchTerm, array $categories = [], int $limit = 1000): array |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get releases by external media ID using search index. |
||
| 417 | * Useful for movie/TV browse pages that need to find all releases for a specific movie/show. |
||
| 418 | * |
||
| 419 | * @param array $externalIds Associative array of external IDs (e.g., ['imdbid' => 123456]) |
||
| 420 | * @param int $limit Maximum number of results |
||
| 421 | * @return array Array of release IDs |
||
| 422 | */ |
||
| 423 | public function getReleasesByExternalId(array $externalIds, int $limit = 100): array |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the count of releases for pager. |
||
| 434 | * |
||
| 435 | * @param string $query The query to get the count from. |
||
| 436 | */ |
||
| 437 | private function getPagerCount(string $query): int |
||
| 575 | } |
||
| 576 | } |
||
| 577 | } |
||
| 578 |