Issues (833)

app/Models/Predb.php (2 issues)

Labels
1
<?php
2
3
namespace App\Models;
4
5
use App\Services\Search\ElasticSearchService;
6
use App\Services\Search\ManticoreSearchService;
7
use Blacklight\ColorCLI;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Facades\Cache;
12
use Laravel\Scout\Searchable;
13
14
/**
15
 * App\Models\Predb.
16
 *
17
 * @property mixed $release
18
 * @property mixed $hash
19
 * @property int $id Primary key
20
 * @property string $title
21
 * @property string|null $nfo
22
 * @property string|null $size
23
 * @property string|null $category
24
 * @property string|null $predate
25
 * @property string $source
26
 * @property int $requestid
27
 * @property int $groups_id FK to groups
28
 * @property bool $nuked Is this pre nuked? 0 no 2 yes 1 un nuked 3 mod nuked
29
 * @property string|null $nukereason If this pre is nuked, what is the reason?
30
 * @property string|null $files How many files does this pre have ?
31
 * @property string $filename
32
 * @property bool $searched
33
 *
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereCategory($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFilename($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFiles($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereGroupsId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereId($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNfo($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNuked($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNukereason($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb wherePredate($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereRequestid($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSearched($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSize($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSource($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereTitle($value)
48
 *
49
 * @mixin \Eloquent
50
 *
51
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newModelQuery()
52
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newQuery()
53
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb query()
54
 */
55
class Predb extends Model
56
{
57
    use Searchable;
0 ignored issues
show
The trait Laravel\Scout\Searchable requires the property $queryCallback which is not provided by App\Models\Predb.
Loading history...
58
59
    // Nuke status.
60
    public const PRE_NONUKE = 0; // Pre is not nuked.
61
62
    public const PRE_UNNUKED = 1; // Pre was un nuked.
63
64
    public const PRE_NUKED = 2; // Pre is nuked.
65
66
    public const PRE_MODNUKE = 3; // Nuke reason was modified.
67
68
    public const PRE_RENUKED = 4; // Pre was re nuked.
69
70
    public const PRE_OLDNUKE = 5; // Pre is nuked for being old.
71
72
    /**
73
     * @var string
74
     */
75
    protected $table = 'predb';
76
77
    /**
78
     * @var bool
79
     */
80
    public $timestamps = false;
81
82
    /**
83
     * @var bool
84
     */
85
    protected $dateFormat = false;
86
87
    /**
88
     * @var array
89
     */
90
    protected $guarded = [];
91
92
    public function hash(): HasMany
93
    {
94
        return $this->hasMany(PredbHash::class, 'predb_id');
95
    }
96
97
    public function release(): HasMany
98
    {
99
        return $this->hasMany(Release::class, 'predb_id');
100
    }
101
102
    /**
103
     * Attempts to match PreDB titles to releases.
104
     *
105
     *
106
     * @throws \RuntimeException
107
     */
108
    public static function checkPre(bool|int|string $dateLimit = false): void
109
    {
110
        $consoleTools = new ColorCLI;
111
        $updated = 0;
112
113
        if (config('nntmux.echocli')) {
114
            (new ColorCLI)->header('Querying DB for release search names not matched with PreDB titles.');
115
        }
116
117
        $query = self::query()
118
            ->where('releases.predb_id', '<', 1)
119
            ->join('releases', 'predb.title', '=', 'releases.searchname')
120
            ->select(['predb.id as predb_id', 'releases.id as releases_id']);
121
        if ($dateLimit !== false && (int) $dateLimit > 0) {
122
            $query->where('adddate', '>', now()->subDays((int) $dateLimit));
123
        }
124
125
        $res = $query->get();
126
127
        if ($res !== null) {
128
            $total = \count($res);
129
            (new ColorCLI)->primary(number_format($total).' releases to match.');
130
131
            foreach ($res as $row) {
132
                Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]);
133
134
                if (config('nntmux.echocli')) {
135
                    $consoleTools->overWritePrimary(
136
                        'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total)
137
                    );
138
                }
139
            }
140
            if (config('nntmux.echocli')) {
141
                echo PHP_EOL;
142
            }
143
144
            if (config('nntmux.echocli')) {
145
                (new ColorCLI)->header(
146
                    'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.'
147
                );
148
            }
149
        }
150
    }
151
152
    /**
153
     * Try to match a single release to a PreDB title when the release is created.
154
     *
155
     * @return array|false Array with title/id from PreDB if found, false if not found.
156
     */
157
    public static function matchPre(string $cleanerName)
158
    {
159
        if (empty($cleanerName)) {
160
            return false;
161
        }
162
163
        $titleCheck = self::query()->where('title', $cleanerName)->first(['id']);
164
165
        if ($titleCheck !== null) {
166
            return [
167
                'title' => $cleanerName,
168
                'predb_id' => $titleCheck['id'],
169
            ];
170
        }
171
172
        // Check if clean name matches a PreDB filename.
173
        $fileCheck = self::query()->where('filename', $cleanerName)->first(['id', 'title']);
174
175
        if ($fileCheck !== null) {
176
            return [
177
                'title' => $fileCheck['title'],
178
                'predb_id' => $fileCheck['id'],
179
            ];
180
        }
181
182
        return false;
183
    }
184
185
    /**
186
     * @return mixed
187
     *
188
     * @throws \Exception
189
     */
190
    public static function getAll(string $search = '')
191
    {
192
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
193
        $predb = Cache::get(md5($search));
194
        if ($predb !== null) {
195
            return $predb;
196
        }
197
        $sql = self::query()
198
            ->leftJoin('releases', 'releases.predb_id', '=', 'predb.id')
199
            ->select('predb.*', 'releases.guid')
200
            ->orderByDesc('predb.predate');
0 ignored issues
show
'predb.predate' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
            ->orderByDesc(/** @scrutinizer ignore-type */ 'predb.predate');
Loading history...
201
        if (! empty($search)) {
202
            if (config('nntmux.elasticsearch_enabled') === true) {
203
                $ids = app(ElasticSearchService::class)->predbIndexSearch($search);
204
            } else {
205
                $manticore = app(ManticoreSearchService::class);
206
                $ids = Arr::get($manticore->searchIndexes('predb_rt', $search, ['title']), 'id');
207
            }
208
            $sql->whereIn('predb.id', $ids);
209
        }
210
211
        $predb = $sql->paginate(config('nntmux.items_per_page'));
212
        $predb->withPath(url('admin/predb'));
213
        Cache::put(md5($search), $predb, $expiresAt);
214
215
        return $predb;
216
    }
217
218
    /**
219
     * Get all PRE's for a release.
220
     *
221
     *
222
     * @return \Illuminate\Database\Eloquent\Collection|static[]
223
     */
224
    public static function getForRelease($preID)
225
    {
226
        return self::query()->where('id', $preID)->get();
227
    }
228
229
    /**
230
     * Return a single PRE for a release.
231
     *
232
     *
233
     * @return Model|null|static
234
     */
235
    public static function getOne($preID)
236
    {
237
        return self::query()->where('id', $preID)->first();
238
    }
239
240
    public function searchableAs(): string
241
    {
242
        return 'ft_predb_filename';
243
    }
244
245
    public function toSearchableArray(): array
246
    {
247
        return [
248
            'filename' => $this->filename,
249
        ];
250
    }
251
}
252