Total Complexity | 179 |
Total Lines | 967 |
Duplicated Lines | 0 % |
Changes | 36 | ||
Bugs | 12 | Features | 0 |
Complex classes like Releases 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 Releases, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Releases extends Release |
||
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 int $passwordStatus; |
||
27 | |||
28 | private ManticoreSearch $manticoreSearch; |
||
29 | |||
30 | private ElasticSearchSiteSearch $elasticSearch; |
||
31 | |||
32 | public function __construct() |
||
33 | { |
||
34 | parent::__construct(); |
||
35 | $this->manticoreSearch = new ManticoreSearch; |
||
36 | $this->elasticSearch = new ElasticSearchSiteSearch; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Used for Browse results. |
||
41 | * |
||
42 | * @return Collection|mixed |
||
43 | */ |
||
44 | public function getBrowseRange($page, $cat, $start, $num, $orderBy, int $maxAge = -1, array $excludedCats = [], int|string $groupName = -1, int $minSize = 0): mixed |
||
45 | { |
||
46 | $page = max(1, $page); |
||
47 | $start = max(0, $start); |
||
48 | |||
49 | $orderBy = $this->getBrowseOrder($orderBy); |
||
50 | |||
51 | $query = self::query() |
||
52 | ->with(['group', 'category', 'category.parent', 'video', 'video.episode', 'videoData', 'nfo', 'failed']) |
||
53 | ->where('nzbstatus', NZB::NZB_ADDED) |
||
54 | ->where('passwordstatus', $this->showPasswords()); |
||
55 | |||
56 | if ($cat) { |
||
57 | $categories = Category::getCategorySearch($cat, null, true); |
||
58 | // If categories is empty, we don't want to return anything. |
||
59 | if ($categories !== null) { |
||
60 | // if we have more than one category, we need to use whereIn |
||
61 | if (count(Arr::wrap($categories)) > 1) { |
||
62 | $query->whereIn('categories_id', $categories); |
||
63 | } else { |
||
64 | $query->where('categories_id', $categories); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | if ($maxAge > 0) { |
||
70 | $query->where('postdate', '>', now()->subDays($maxAge)); |
||
71 | } |
||
72 | |||
73 | if (! empty($excludedCats)) { |
||
74 | $query->whereNotIn('categories_id', $excludedCats); |
||
75 | } |
||
76 | |||
77 | if ($groupName !== -1) { |
||
78 | $query->whereHas('group', function ($q) use ($groupName) { |
||
79 | $q->where('name', $groupName); |
||
80 | }); |
||
81 | } |
||
82 | |||
83 | if ($minSize > 0) { |
||
84 | $query->where('size', '>=', $minSize); |
||
85 | } |
||
86 | |||
87 | $query->orderBy($orderBy[0], $orderBy[1]) |
||
88 | ->skip($start) |
||
89 | ->take($num); |
||
90 | $releases = Cache::get(md5($query->toRawSql().$page)); |
||
|
|||
91 | if ($releases !== null) { |
||
92 | return $releases; |
||
93 | } |
||
94 | |||
95 | $sql = $query->get(); |
||
96 | if ($sql->isNotEmpty()) { |
||
97 | $possibleRows = $sql->count(); |
||
98 | $sql[0]->_totalcount = $sql[0]->_totalrows = $possibleRows; |
||
99 | } |
||
100 | |||
101 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
102 | Cache::put(md5($query->toRawSql().$page), $sql, $expiresAt); |
||
103 | |||
104 | return $sql; |
||
105 | } |
||
106 | |||
107 | public function showPasswords(): string |
||
108 | { |
||
109 | $show = (int) Settings::settingValue('showpasswordedrelease'); |
||
110 | $setting = $show ?? 0; |
||
111 | |||
112 | return match ($setting) { |
||
113 | 1 => '<= '.self::PASSWD_RAR, |
||
114 | default => '= '.self::PASSWD_NONE, |
||
115 | }; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Use to order releases on site. |
||
120 | */ |
||
121 | public function getBrowseOrder(array|string $orderBy): array |
||
122 | { |
||
123 | $orderArr = explode('_', ($orderBy === '' ? 'posted_desc' : $orderBy)); |
||
124 | $orderField = match ($orderArr[0]) { |
||
125 | 'cat' => 'categories_id', |
||
126 | 'name' => 'searchname', |
||
127 | 'size' => 'size', |
||
128 | 'files' => 'totalpart', |
||
129 | 'stats' => 'grabs', |
||
130 | default => 'postdate', |
||
131 | }; |
||
132 | |||
133 | return [$orderField, isset($orderArr[1]) && preg_match('/^(asc|desc)$/i', $orderArr[1]) ? $orderArr[1] : 'desc']; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Return ordering types usable on site. |
||
138 | * |
||
139 | * @return string[] |
||
140 | */ |
||
141 | public function getBrowseOrdering(): array |
||
156 | ]; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return Collection|\Illuminate\Support\Collection|Release[] |
||
161 | */ |
||
162 | public function getForExport(string $postFrom = '', string $postTo = '', string $groupID = ''): Collection|array|\Illuminate\Support\Collection |
||
163 | { |
||
164 | $query = self::query() |
||
165 | ->where('r.nzbstatus', NZB::NZB_ADDED) |
||
166 | ->select(['r.searchname', 'r.guid', 'g.name as gname', DB::raw("CONCAT(cp.title,'_',c.title) AS catName")]) |
||
167 | ->from('releases as r') |
||
168 | ->leftJoin('categories as c', 'c.id', '=', 'r.categories_id') |
||
169 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
170 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'r.groups_id'); |
||
171 | |||
172 | if ($groupID !== '') { |
||
173 | $query->where('r.groups_id', $groupID); |
||
174 | } |
||
175 | |||
176 | if ($postFrom !== '') { |
||
177 | $dateParts = explode('/', $postFrom); |
||
178 | if (\count($dateParts) === 3) { |
||
179 | $query->where('r.postdate', '>', $dateParts[2].'-'.$dateParts[1].'-'.$dateParts[0].'00:00:00'); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if ($postTo !== '') { |
||
184 | $dateParts = explode('/', $postTo); |
||
185 | if (\count($dateParts) === 3) { |
||
186 | $query->where('r.postdate', '<', $dateParts[2].'-'.$dateParts[1].'-'.$dateParts[0].'23:59:59'); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return $query->get(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return mixed|string |
||
195 | */ |
||
196 | public function getEarliestUsenetPostDate(): mixed |
||
197 | { |
||
198 | $row = self::query()->selectRaw("DATE_FORMAT(min(postdate), '%d/%m/%Y') AS postdate")->first(); |
||
199 | |||
200 | return $row === null ? '01/01/2014' : $row['postdate']; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return mixed|string |
||
205 | */ |
||
206 | public function getLatestUsenetPostDate(): mixed |
||
207 | { |
||
208 | $row = self::query()->selectRaw("DATE_FORMAT(max(postdate), '%d/%m/%Y') AS postdate")->first(); |
||
209 | |||
210 | return $row === null ? '01/01/2014' : $row['postdate']; |
||
211 | } |
||
212 | |||
213 | public function getReleasedGroupsForSelect(bool $blnIncludeAll = true): array |
||
214 | { |
||
215 | $groups = self::query() |
||
216 | ->selectRaw('DISTINCT g.id, g.name') |
||
217 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id') |
||
218 | ->get(); |
||
219 | $temp_array = []; |
||
220 | |||
221 | if ($blnIncludeAll) { |
||
222 | $temp_array[-1] = '--All Groups--'; |
||
223 | } |
||
224 | |||
225 | foreach ($groups as $group) { |
||
226 | $temp_array[$group['id']] = $group['name']; |
||
227 | } |
||
228 | |||
229 | return $temp_array; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @return Collection|mixed |
||
234 | */ |
||
235 | public function getShowsRange($userShows, $offset, $limit, $orderBy, int $maxAge = -1, array $excludedCats = []): mixed |
||
236 | { |
||
237 | $orderBy = $this->getBrowseOrder($orderBy); |
||
238 | |||
239 | $query = self::query() |
||
240 | ->with(['group', 'category', 'category.parent', 'video', 'video.episode']) |
||
241 | ->where('nzbstatus', NZB::NZB_ADDED) |
||
242 | ->where('passwordstatus', $this->showPasswords()) |
||
243 | ->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER]) |
||
244 | ->when($maxAge > 0, function ($q) use ($maxAge) { |
||
245 | $q->where('postdate', '>', now()->subDays($maxAge)); |
||
246 | }) |
||
247 | ->when(! empty($excludedCats), function ($q) use ($excludedCats) { |
||
248 | $q->whereNotIn('categories_id', $excludedCats); |
||
249 | }) |
||
250 | ->whereRaw($this->uSQL($userShows, 'videos_id')) |
||
251 | ->orderBy($orderBy[0], $orderBy[1]) |
||
252 | ->offset($offset) |
||
253 | ->limit($limit); |
||
254 | |||
255 | $cacheKey = md5($query->toRawSql()); |
||
256 | $cacheTTL = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
257 | |||
258 | $releases = Cache::get($cacheKey); |
||
259 | if ($releases !== null) { |
||
260 | return $releases; |
||
261 | } |
||
262 | |||
263 | $releases = $query->get(); |
||
264 | |||
265 | if ($releases->isNotEmpty()) { |
||
266 | $releases[0]->_totalrows = $query->count(); |
||
267 | } |
||
268 | |||
269 | Cache::put($cacheKey, $releases, $cacheTTL); |
||
270 | |||
271 | return $releases; |
||
272 | } |
||
273 | |||
274 | public function getShowsCount($userShows, int $maxAge = -1, array $excludedCats = []): int |
||
275 | { |
||
276 | return $this->getPagerCount( |
||
277 | sprintf( |
||
278 | 'SELECT r.id |
||
279 | FROM releases r |
||
280 | WHERE %s %s |
||
281 | AND r.nzbstatus = %d |
||
282 | AND r.categories_id BETWEEN %d AND %d |
||
283 | AND r.passwordstatus %s |
||
284 | %s', |
||
285 | $this->uSQL($userShows, 'videos_id'), |
||
286 | (\count($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''), |
||
287 | NZB::NZB_ADDED, |
||
288 | Category::TV_ROOT, |
||
289 | Category::TV_OTHER, |
||
290 | $this->showPasswords(), |
||
291 | ($maxAge > 0 ? sprintf(' AND r.postdate > NOW() - INTERVAL %d DAY ', $maxAge) : '') |
||
292 | ) |
||
293 | ); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @throws \Exception |
||
298 | */ |
||
299 | public function deleteMultiple(int|array|string $list): void |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Deletes a single release by GUID, and all the corresponding files. |
||
313 | * |
||
314 | * @param array $identifiers ['g' => Release GUID(mandatory), 'id => ReleaseID(optional, pass |
||
315 | * false)] |
||
316 | * |
||
317 | * @throws \Exception |
||
318 | */ |
||
319 | public function deleteSingle(array $identifiers, NZB $nzb, ReleaseImage $releaseImage): void |
||
320 | { |
||
321 | // Delete NZB from disk. |
||
322 | $nzbPath = $nzb->NZBPath($identifiers['g']); |
||
323 | if (! empty($nzbPath)) { |
||
324 | File::delete($nzbPath); |
||
325 | } |
||
326 | |||
327 | // Delete images. |
||
328 | $releaseImage->delete($identifiers['g']); |
||
329 | |||
330 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
331 | if ($identifiers['i'] === false) { |
||
332 | $identifiers['i'] = Release::query()->where('guid', $identifiers['g'])->first(['id']); |
||
333 | if ($identifiers['i'] !== null) { |
||
334 | $identifiers['i'] = $identifiers['i']['id']; |
||
335 | } |
||
336 | } |
||
337 | if ($identifiers['i'] !== null) { |
||
338 | $params = [ |
||
339 | 'index' => 'releases', |
||
340 | 'id' => $identifiers['i'], |
||
341 | ]; |
||
342 | |||
343 | try { |
||
344 | Elasticsearch::delete($params); |
||
345 | } catch (Missing404Exception $e) { |
||
346 | // we do nothing here just catch the error, we don't care if release is missing from ES, we are deleting it anyway |
||
347 | } |
||
348 | } |
||
349 | } else { |
||
350 | // Delete from sphinx. |
||
351 | $this->manticoreSearch->deleteRelease($identifiers); |
||
352 | } |
||
353 | |||
354 | // Delete from DB. |
||
355 | self::whereGuid($identifiers['g'])->delete(); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @return bool|int |
||
360 | */ |
||
361 | public function updateMulti($guids, $category, $grabs, $videoId, $episodeId, $anidbId, $imdbId) |
||
362 | { |
||
363 | if (! \is_array($guids) || \count($guids) < 1) { |
||
364 | return false; |
||
365 | } |
||
366 | |||
367 | $update = [ |
||
368 | 'categories_id' => $category === -1 ? 'categories_id' : $category, |
||
369 | 'grabs' => $grabs, |
||
370 | 'videos_id' => $videoId, |
||
371 | 'tv_episodes_id' => $episodeId, |
||
372 | 'anidbid' => $anidbId, |
||
373 | 'imdbid' => $imdbId, |
||
374 | ]; |
||
375 | |||
376 | return self::query()->whereIn('guid', $guids)->update($update); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Creates part of a query for some functions. |
||
381 | */ |
||
382 | public function uSQL(Collection|array $userQuery, string $type): string |
||
383 | { |
||
384 | $sql = '(1=2 '; |
||
385 | foreach ($userQuery as $query) { |
||
386 | $sql .= sprintf('OR (r.%s = %d', $type, $query->$type); |
||
387 | if (! empty($query->categories)) { |
||
388 | $catsArr = explode('|', $query->categories); |
||
389 | if (\count($catsArr) > 1) { |
||
390 | $sql .= sprintf(' AND r.categories_id IN (%s)', implode(',', $catsArr)); |
||
391 | } else { |
||
392 | $sql .= sprintf(' AND r.categories_id = %d', $catsArr[0]); |
||
393 | } |
||
394 | } |
||
395 | $sql .= ') '; |
||
396 | } |
||
397 | $sql .= ') '; |
||
398 | |||
399 | return $sql; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Function for searching on the site (by subject, searchname or advanced). |
||
404 | * |
||
405 | * @return array|Collection|mixed |
||
406 | */ |
||
407 | public function search(array $searchArr, $groupName, $sizeFrom, $sizeTo, $daysNew, $daysOld, int $offset = 0, int $limit = 1000, array|string $orderBy = '', int $maxAge = -1, array $excludedCats = [], string $type = 'basic', array $cat = [-1], int $minSize = 0): mixed |
||
408 | { |
||
409 | $sizeRange = [ |
||
410 | 1 => 1, |
||
411 | 2 => 2.5, |
||
412 | 3 => 5, |
||
413 | 4 => 10, |
||
414 | 5 => 20, |
||
415 | 6 => 30, |
||
416 | 7 => 40, |
||
417 | 8 => 80, |
||
418 | 9 => 160, |
||
419 | 10 => 320, |
||
420 | 11 => 640, |
||
421 | ]; |
||
422 | |||
423 | if ($orderBy === '') { |
||
424 | $orderBy = ['postdate', 'desc']; |
||
425 | } else { |
||
426 | $orderBy = $this->getBrowseOrder($orderBy); |
||
427 | } |
||
428 | |||
429 | $searchFields = Arr::where($searchArr, static function ($value) { |
||
430 | return $value !== -1; |
||
431 | }); |
||
432 | |||
433 | $phrases = array_values($searchFields); |
||
434 | |||
435 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
436 | $searchResult = $this->elasticSearch->indexSearch($phrases, $limit); |
||
437 | } else { |
||
438 | $searchResult = $this->manticoreSearch->searchIndexes('releases_rt', '', [], $searchFields); |
||
439 | if (! empty($searchResult)) { |
||
440 | $searchResult = Arr::wrap(Arr::get($searchResult, 'id')); |
||
441 | } |
||
442 | } |
||
443 | |||
444 | if (count($searchResult) === 0) { |
||
445 | return collect(); |
||
446 | } |
||
447 | |||
448 | $query = self::query() |
||
449 | ->with(['group', 'category', 'category.parent', 'video', 'video.episode', 'nfo', 'failed']) |
||
450 | ->where('nzbstatus', NZB::NZB_ADDED) |
||
451 | ->where('passwordstatus', $this->showPasswords()) |
||
452 | ->whereIn('id', $searchResult); |
||
453 | |||
454 | if ($type === 'basic') { |
||
455 | $categories = Category::getCategorySearch($cat, null, true); |
||
456 | if ($categories !== null) { |
||
457 | $query->whereIn('categories_id', Arr::wrap($categories)); |
||
458 | } |
||
459 | } elseif ($type === 'advanced' && (int) $cat[0] !== -1) { |
||
460 | $query->where('categories_id', $cat[0]); |
||
461 | } |
||
462 | |||
463 | if ($maxAge > 0) { |
||
464 | $query->where('postdate', '>', now()->subDays($maxAge)); |
||
465 | } |
||
466 | |||
467 | if (! empty($excludedCats)) { |
||
468 | $query->whereNotIn('categories_id', $excludedCats); |
||
469 | } |
||
470 | |||
471 | if ((int) $groupName !== -1) { |
||
472 | $query->whereHas('group', function ($q) use ($groupName) { |
||
473 | $q->where('name', $groupName); |
||
474 | }); |
||
475 | } |
||
476 | |||
477 | if ($sizeFrom > 0 && array_key_exists($sizeFrom, $sizeRange)) { |
||
478 | $query->where('size', '>', 104857600 * (int) $sizeRange[$sizeFrom]); |
||
479 | } |
||
480 | |||
481 | if ($sizeTo > 0 && array_key_exists($sizeTo, $sizeRange)) { |
||
482 | $query->where('size', '<', 104857600 * (int) $sizeRange[$sizeTo]); |
||
483 | } |
||
484 | |||
485 | if ($daysNew !== -1) { |
||
486 | $query->where('postdate', '<', now()->subDays($daysNew)); |
||
487 | } |
||
488 | |||
489 | if ($daysOld !== -1) { |
||
490 | $query->where('postdate', '>', now()->subDays($daysOld)); |
||
491 | } |
||
492 | |||
493 | if ($minSize > 0) { |
||
494 | $query->where('size', '>=', $minSize); |
||
495 | } |
||
496 | |||
497 | $query->orderBy($orderBy[0], $orderBy[1]) |
||
498 | ->offset($offset) |
||
499 | ->limit($limit); |
||
500 | |||
501 | $cacheKey = md5($query->toRawSql()); |
||
502 | $cacheTTL = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
503 | |||
504 | $releases = Cache::get($cacheKey); |
||
505 | if ($releases !== null) { |
||
506 | return $releases; |
||
507 | } |
||
508 | |||
509 | $releases = $query->get(); |
||
510 | |||
511 | if ($releases->isNotEmpty()) { |
||
512 | $releases[0]->_totalrows = $query->count(); |
||
513 | } |
||
514 | |||
515 | Cache::put($cacheKey, $releases, $cacheTTL); |
||
516 | |||
517 | return $releases; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Search function for API. |
||
522 | * |
||
523 | * @return Collection|mixed |
||
524 | */ |
||
525 | public function apiSearch($searchName, $groupName, int $offset = 0, int $limit = 1000, int $maxAge = -1, array $excludedCats = [], array $cat = [-1], int $minSize = 0): mixed |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Search for TV shows via API. |
||
595 | * |
||
596 | * @return array|\Illuminate\Cache\|Collection|\Illuminate\Support\Collection|mixed |
||
597 | */ |
||
598 | 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 |
||
599 | { |
||
600 | $siteSQL = []; |
||
601 | $showSql = ''; |
||
602 | foreach ($siteIdArr as $column => $Id) { |
||
603 | if ($Id > 0) { |
||
604 | $siteSQL[] = sprintf('v.%s = %d', $column, $Id); |
||
733 | } |
||
734 | |||
735 | /** |
||
736 | * Search TV Shows via APIv2. |
||
737 | * |
||
738 | * @return Collection|mixed |
||
739 | */ |
||
740 | 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 |
||
843 | } |
||
844 | |||
845 | 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 |
||
846 | { |
||
847 | $query = self::query() |
||
848 | ->with(['movieinfo', 'group', 'category', 'category.parent', 'nfo']) |
||
849 | ->whereBetween('categories_id', [Category::MOVIE_ROOT, Category::MOVIE_OTHER]) |
||
850 | ->where('nzbstatus', NZB::NZB_ADDED) |
||
851 | ->where('passwordstatus', $this->showPasswords()); |
||
852 | |||
853 | if (! empty($name)) { |
||
854 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
855 | $searchResult = $this->elasticSearch->indexSearchTMA($name, $limit); |
||
856 | } else { |
||
857 | $searchResult = $this->manticoreSearch->searchIndexes('releases_rt', $name, ['searchname']); |
||
858 | if (! empty($searchResult)) { |
||
859 | $searchResult = Arr::wrap(Arr::get($searchResult, 'id')); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | if (count($searchResult) === 0) { |
||
864 | return collect(); |
||
865 | } |
||
866 | |||
867 | $query->whereIn('id', $searchResult); |
||
868 | } |
||
869 | |||
870 | if ($imDbId !== -1 && is_numeric($imDbId)) { |
||
871 | $query->whereHas('movieinfo', function ($q) use ($imDbId) { |
||
872 | $q->where('imdbid', $imDbId); |
||
873 | }); |
||
874 | } |
||
875 | |||
876 | if ($tmDbId !== -1 && is_numeric($tmDbId)) { |
||
877 | $query->whereHas('movieinfo', function ($q) use ($tmDbId) { |
||
878 | $q->where('tmdbid', $tmDbId); |
||
879 | }); |
||
880 | } |
||
881 | |||
882 | if ($traktId !== -1 && is_numeric($traktId)) { |
||
883 | $query->whereHas('movieinfo', function ($q) use ($traktId) { |
||
884 | $q->where('traktid', $traktId); |
||
885 | }); |
||
886 | } |
||
887 | |||
888 | if (! empty($excludedCategories)) { |
||
889 | $query->whereNotIn('categories_id', $excludedCategories); |
||
890 | } |
||
891 | |||
892 | if ($cat !== [-1]) { |
||
893 | $query->whereIn('categories_id', $cat); |
||
894 | } |
||
895 | |||
896 | if ($maxAge > 0) { |
||
897 | $query->where('postdate', '>', now()->subDays($maxAge)); |
||
898 | } |
||
899 | |||
900 | if ($minSize > 0) { |
||
901 | $query->where('size', '>=', $minSize); |
||
902 | } |
||
903 | |||
904 | $totalRows = $query->count(); |
||
905 | |||
906 | $query->orderBy('postdate', 'desc') |
||
907 | ->offset($offset) |
||
908 | ->limit($limit); |
||
909 | |||
910 | $cacheKey = md5($query->toRawSql()); |
||
911 | $cacheTTL = now()->addMinutes(config('nntmux.cache_expiry_medium')); |
||
912 | |||
913 | $releases = Cache::get($cacheKey); |
||
914 | if ($releases !== null) { |
||
915 | return $releases; |
||
916 | } |
||
917 | |||
918 | $releases = $query->get(); |
||
919 | |||
920 | if ($releases->isNotEmpty()) { |
||
921 | $releases[0]->_totalrows = $totalRows; |
||
922 | } |
||
923 | |||
924 | Cache::put($cacheKey, $releases, $cacheTTL); |
||
925 | |||
926 | return $releases; |
||
927 | } |
||
928 | |||
929 | public function searchSimilar($currentID, $name, array $excludedCats = []): bool|array |
||
956 | } |
||
957 | |||
958 | /** |
||
959 | * Get count of releases for pager. |
||
960 | * |
||
961 | * @param string $query The query to get the count from. |
||
962 | */ |
||
963 | private function getPagerCount(string $query): int |
||
988 |