Passed
Push — dev ( 535ca7...b7c17d )
by Darko
07:22
created

Predb::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\ColorCLI;
6
use Blacklight\ConsoleTools;
7
use Blacklight\ElasticSearchSiteSearch;
8
use Blacklight\SphinxSearch;
9
use Illuminate\Database\Eloquent\Model;
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
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereCategory($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFilename($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereFiles($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereGroupsId($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNfo($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNuked($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereNukereason($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb wherePredate($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereRequestid($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSearched($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSize($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereSource($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb whereTitle($value)
47
 * @mixin \Eloquent
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newModelQuery()
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb newQuery()
50
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Predb query()
51
 */
52
class Predb extends Model
53
{
54
    use Searchable;
0 ignored issues
show
Bug introduced by
The trait Laravel\Scout\Searchable requires the property $queryCallback which is not provided by App\Models\Predb.
Loading history...
55
56
    // Nuke status.
57
    public const PRE_NONUKE = 0; // Pre is not nuked.
58
    public const PRE_UNNUKED = 1; // Pre was un nuked.
59
    public const PRE_NUKED = 2; // Pre is nuked.
60
    public const PRE_MODNUKE = 3; // Nuke reason was modified.
61
    public const PRE_RENUKED = 4; // Pre was re nuked.
62
    public const PRE_OLDNUKE = 5; // Pre is nuked for being old.
63
64
    /**
65
     * @var string
66
     */
67
    protected $table = 'predb';
68
69
    /**
70
     * @var bool
71
     */
72
    public $timestamps = false;
73
74
    /**
75
     * @var bool
76
     */
77
    protected $dateFormat = false;
78
79
    /**
80
     * @var array
81
     */
82
    protected $guarded = [];
83
84
    public function hash()
85
    {
86
        return $this->hasMany(PredbHash::class, 'predb_id');
87
    }
88
89
    public function release()
90
    {
91
        return $this->hasMany(Release::class, 'predb_id');
92
    }
93
94
    /**
95
     * Attempts to match PreDB titles to releases.
96
     *
97
     * @param string|int|bool $dateLimit
98
     * @throws \RuntimeException
99
     */
100
    public static function checkPre($dateLimit = false): void
101
    {
102
        $consoleTools = new ConsoleTools();
103
        $updated = 0;
104
105
        if (config('nntmux.echocli')) {
106
            (new ColorCLI())->header('Querying DB for release search names not matched with PreDB titles.');
107
        }
108
109
        $query = self::query()
110
            ->where('releases.predb_id', '<', 1)
111
            ->join('releases', 'predb.title', '=', 'releases.searchname')
112
            ->select(['predb.id as predb_id', 'releases.id as releases_id']);
113
        if ($dateLimit !== false && (int) $dateLimit > 0) {
114
            $query->where('adddate', '>', now()->subDays((int) $dateLimit));
115
        }
116
117
        $res = $query->get();
118
119
        if ($res !== null) {
120
            $total = \count($res);
121
            (new ColorCLI())->primary(number_format($total).' releases to match.');
122
123
            foreach ($res as $row) {
124
                Release::query()->where('id', $row['releases_id'])->update(['predb_id' => $row['predb_id']]);
125
126
                if (config('nntmux.echocli')) {
127
                    $consoleTools->overWritePrimary(
128
                        'Matching up preDB titles with release searchnames: '.$consoleTools->percentString(++$updated, $total)
129
                        );
130
                }
131
            }
132
            if (config('nntmux.echocli')) {
133
                echo PHP_EOL;
134
            }
135
136
            if (config('nntmux.echocli')) {
137
                (new ColorCLI())->header(
138
                    'Matched '.number_format(($updated > 0) ? $updated : 0).' PreDB titles to release search names.'
139
                );
140
            }
141
        }
142
    }
143
144
    /**
145
     * Try to match a single release to a PreDB title when the release is created.
146
     *
147
     * @param string $cleanerName
148
     *
149
     * @return array|false Array with title/id from PreDB if found, false if not found.
150
     */
151
    public static function matchPre($cleanerName)
152
    {
153
        if (empty($cleanerName)) {
154
            return false;
155
        }
156
157
        $titleCheck = self::query()->where('title', $cleanerName)->first(['id']);
158
159
        if ($titleCheck !== null) {
160
            return [
161
                'title' => $cleanerName,
162
                'predb_id' => $titleCheck['id'],
163
            ];
164
        }
165
166
        // Check if clean name matches a PreDB filename.
167
        $fileCheck = self::query()->where('filename', $cleanerName)->first(['id', 'title']);
168
169
        if ($fileCheck !== null) {
170
            return [
171
                'title' => $fileCheck['title'],
172
                'predb_id' => $fileCheck['id'],
173
            ];
174
        }
175
176
        return false;
177
    }
178
179
    /**
180
     * @param string $search
181
     *
182
     * @return mixed
183
     * @throws \Exception
184
     */
185
    public static function getAll($search = '')
186
    {
187
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
188
        $predb = Cache::get(md5($search));
189
        if ($predb !== null) {
190
            return $predb;
191
        }
192
        $sql = self::query()->leftJoin('releases', 'releases.predb_id', '=', 'predb.id')->orderByDesc('predb.predate');
193
        if (! empty($search)) {
194
            if (config('nntmux.elasticsearch_enabled') === true) {
195
                $ids = (new ElasticSearchSiteSearch())->predbIndexSearch($search);
196
            } else {
197
                $sphinx = new SphinxSearch();
198
                $ids = Arr::pluck($sphinx->searchIndexes('predb_rt', $search, ['title']), 'id');
199
            }
200
            $sql->whereIn('predb.id', $ids);
201
        }
202
203
        $predb = $sql->paginate(config('nntmux.items_per_page'));
204
        Cache::put(md5($search), $predb, $expiresAt);
205
206
        return $predb;
207
    }
208
209
    /**
210
     * Get all PRE's for a release.
211
     *
212
     *
213
     * @param $preID
214
     * @return \Illuminate\Database\Eloquent\Collection|static[]
215
     */
216
    public static function getForRelease($preID)
217
    {
218
        return self::query()->where('id', $preID)->get();
219
    }
220
221
    /**
222
     * Return a single PRE for a release.
223
     *
224
     *
225
     * @param $preID
226
     * @return \Illuminate\Database\Eloquent\Model|null|static
227
     */
228
    public static function getOne($preID)
229
    {
230
        return self::query()->where('id', $preID)->first();
231
    }
232
233
    /**
234
     * @return string
235
     */
236
    public function searchableAs()
237
    {
238
        return 'ft_predb_filename';
239
    }
240
241
    /**
242
     * @return array
243
     */
244
    public function toSearchableArray()
245
    {
246
        return [
247
            'filename' => $this->filename,
248
        ];
249
    }
250
}
251