hasMany(PredbHash::class, 'predb_id'); } public function release(): HasMany { return $this->hasMany(Release::class, 'predb_id'); } /** * Attempts to match PreDB titles to releases. * * * @throws \RuntimeException */ public static function checkPre(bool|int|string $dateLimit = false): void { $consoleTools = new ColorCLI; $updated = 0; if (config('nntmux.echocli')) { (new ColorCLI)->header('Querying DB for release search names not matched with PreDB titles.'); } $query = self::query() ->where('releases.predb_id', '<', 1) ->join('releases', 'predb.title', '=', 'releases.searchname') ->select(['predb.id as predb_id', 'releases.id as releases_id']); if ($dateLimit !== false && (int) $dateLimit > 0) { $query->where('adddate', '>', now()->subDays((int) $dateLimit)); } $res = $query->get(); if ($res !== null) { $total = \count($res); (new ColorCLI)->primary(number_format($total).' releases to match.'); foreach ($res as $row) { Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]); if (config('nntmux.echocli')) { $consoleTools->overWritePrimary( 'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total) ); } } if (config('nntmux.echocli')) { echo PHP_EOL; } if (config('nntmux.echocli')) { (new ColorCLI)->header( 'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.' ); } } } /** * Try to match a single release to a PreDB title when the release is created. * * @return array|false Array with title/id from PreDB if found, false if not found. */ public static function matchPre(string $cleanerName) { if (empty($cleanerName)) { return false; } $titleCheck = self::query()->where('title', $cleanerName)->first(['id']); if ($titleCheck !== null) { return [ 'title' => $cleanerName, 'predb_id' => $titleCheck['id'], ]; } // Check if clean name matches a PreDB filename. $fileCheck = self::query()->where('filename', $cleanerName)->first(['id', 'title']); if ($fileCheck !== null) { return [ 'title' => $fileCheck['title'], 'predb_id' => $fileCheck['id'], ]; } return false; } /** * @return mixed * * @throws \Exception */ public static function getAll(string $search = '') { $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium')); $predb = Cache::get(md5($search)); if ($predb !== null) { return $predb; } $sql = self::query() ->leftJoin('releases', 'releases.predb_id', '=', 'predb.id') ->select('predb.*', 'releases.guid') ->orderByDesc('predb.predate'); if (! empty($search)) { $ids = Search::searchPredb($search); $sql->whereIn('predb.id', $ids); } $predb = $sql->paginate(config('nntmux.items_per_page')); $predb->withPath(url('admin/predb')); Cache::put(md5($search), $predb, $expiresAt); return $predb; } /** * Get all PRE's for a release. * * * @return \Illuminate\Database\Eloquent\Collection|static[] */ public static function getForRelease($preID) { return self::query()->where('id', $preID)->get(); } /** * Return a single PRE for a release. * * * @return Model|null|static */ public static function getOne($preID) { return self::query()->where('id', $preID)->first(); } public function searchableAs(): string { return 'ft_predb_filename'; } public function toSearchableArray(): array { return [ 'filename' => $this->filename, ]; } }