Issues (433)

app/Console/Commands/DeleteReleases.php (6 issues)

1
<?php
2
3
namespace App\Console\Commands;
4
5
use Blacklight\ElasticSearchSiteSearch;
6
use Blacklight\ManticoreSearch;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\DB;
9
10
class DeleteReleases extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'nntmux:delete-releases
18
                            {criteria?* : Advanced criteria in original format}
19
                            {--group= : Group name to filter by}
20
                            {--group-like= : Group name pattern to filter by (partial match)}
21
                            {--poster= : Poster name (fromname) to filter by}
22
                            {--poster-like= : Poster name pattern to filter by (partial match)}
23
                            {--name= : Release name to filter by}
24
                            {--name-like= : Release name pattern to filter by (partial match)}
25
                            {--search= : Search name to filter by}
26
                            {--search-like= : Search name pattern to filter by (partial match)}
27
                            {--category= : Category ID to filter by}
28
                            {--guid= : Specific GUID to filter by}
29
                            {--size-min= : Minimum size in bytes}
30
                            {--size-max= : Maximum size in bytes}
31
                            {--size= : Exact size in bytes}
32
                            {--hours-old= : Delete releases older than X hours}
33
                            {--hours-new= : Delete releases newer than X hours}
34
                            {--parts-min= : Minimum number of parts}
35
                            {--parts-max= : Maximum number of parts}
36
                            {--parts= : Exact number of parts}
37
                            {--completion-max= : Maximum completion percentage}
38
                            {--nzb-status= : NZB status to filter by}
39
                            {--imdb= : IMDB ID to filter by (use NULL for no IMDB)}
40
                            {--rage= : Rage ID to filter by}
41
                            {--dry-run : Show what would be deleted without actually deleting}
42
                            {--force : Skip confirmation prompt}';
43
44
    /**
45
     * The console command description.
46
     *
47
     * @var string
48
     */
49
    protected $description = 'Delete releases based on specified criteria';
50
51
    /**
52
     * Create a new command instance.
53
     */
54
    public function __construct()
55
    {
56
        parent::__construct();
57
    }
58
59
    /**
60
     * Execute the console command.
61
     */
62
    public function handle(): int
63
    {
64
        $dryRun = $this->option('dry-run');
65
        $force = $this->option('force');
66
67
        // Build criteria array from simple options
68
        $criteria = $this->buildCriteriaFromOptions();
69
70
        // Add any advanced criteria
71
        $advancedCriteria = $this->argument('criteria');
72
        if (! empty($advancedCriteria)) {
73
            $criteria = array_merge($criteria, $advancedCriteria);
0 ignored issues
show
It seems like $advancedCriteria can also be of type boolean and string; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

73
            $criteria = array_merge($criteria, /** @scrutinizer ignore-type */ $advancedCriteria);
Loading history...
74
        }
75
76
        if (empty($criteria)) {
77
            $this->showUsage();
78
79
            return 1;
80
        }
81
82
        $this->info('Delete Releases Command');
83
        $this->newLine();
84
85
        if ($dryRun) {
86
            $this->warn('DRY RUN MODE - No releases will actually be deleted');
87
            $this->newLine();
88
89
            return $this->performDryRun($criteria);
90
        }
91
92
        // Show confirmation unless force flag is set
93
        if (! $force) {
94
            $this->warn('You are about to delete releases matching the following criteria:');
95
            foreach ($criteria as $criterion) {
96
                $this->line("  - {$criterion}");
97
            }
98
            $this->newLine();
99
100
            if (! $this->confirm('Are you sure you want to proceed with the deletion?')) {
101
                $this->info('Operation cancelled.');
102
103
                return 0;
104
            }
105
        }
106
107
        try {
108
            $this->info('Starting release deletion process...');
109
110
            // Build the query to find releases to delete
111
            $query = $this->buildQueryFromCriteria($criteria);
112
            if (! $query) {
113
                $this->error('Could not build query from criteria.');
114
115
                return 1;
116
            }
117
118
            // Get releases to delete
119
            $releases = DB::select($query);
120
            if (empty($releases)) {
121
                $this->info('No releases found matching the specified criteria.');
122
123
                return 0;
124
            }
125
126
            $count = count($releases);
127
            $this->info("Found {$count} release(s) to delete...");
128
129
            $deleted = 0;
130
            foreach ($releases as $release) {
131
                try {
132
                    // Delete the release using direct database operations
133
                    $releaseId = $release->id;
134
135
                    $releaseName = $release->searchname ?: ($release->name ?: 'Unknown');
136
                    $this->line("Deleting: {$releaseName}");
137
138
                    // Delete related data first (to maintain referential integrity)
139
                    DB::delete('DELETE FROM user_downloads WHERE releases_id = ?', [$releaseId]);
140
                    DB::delete('DELETE FROM users_releases WHERE releases_id = ?', [$releaseId]);
141
                    DB::delete('DELETE FROM release_files WHERE releases_id = ?', [$releaseId]);
142
                    DB::delete('DELETE FROM release_comments WHERE releases_id = ?', [$releaseId]);
143
                    DB::delete('DELETE FROM release_nfos WHERE releases_id = ?', [$releaseId]);
144
                    DB::delete('DELETE FROM release_subtitles WHERE releases_id = ?', [$releaseId]);
145
146
                    // Delete the main release record
147
                    DB::delete('DELETE FROM releases WHERE id = ?', [$releaseId]);
148
149
                    // Delete from search indexes
150
                    $identifiers = ['id' => $releaseId, 'g' => $release->guid];
151
                    ManticoreSearch::deleteRelease($identifiers);
0 ignored issues
show
Bug Best Practice introduced by
The method Blacklight\ManticoreSearch::deleteRelease() is not static, but was called statically. ( Ignorable by Annotation )

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

151
                    ManticoreSearch::/** @scrutinizer ignore-call */ 
152
                                     deleteRelease($identifiers);
Loading history...
152
                    ElasticSearchSiteSearch::deleteRelease($releaseId);
0 ignored issues
show
Bug Best Practice introduced by
The method Blacklight\ElasticSearch...Search::deleteRelease() is not static, but was called statically. ( Ignorable by Annotation )

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

152
                    ElasticSearchSiteSearch::/** @scrutinizer ignore-call */ 
153
                                             deleteRelease($releaseId);
Loading history...
153
154
                    $deleted++;
155
156
                    // Show progress for large deletions
157
                    if ($deleted % 100 == 0) {
158
                        $this->info("Deleted {$deleted}/{$count} releases...");
159
                    }
160
161
                } catch (\Exception $e) {
162
                    $releaseName = $release->searchname ?? 'Unknown';
163
                    $this->warn("Failed to delete release {$releaseName}: ".$e->getMessage());
164
                }
165
            }
166
167
            $this->info("Successfully deleted {$deleted} release(s).");
168
169
            return 0;
170
171
        } catch (\Exception $e) {
172
            $this->error('An error occurred: '.$e->getMessage());
173
174
            return 1;
175
        }
176
    }
177
178
    /**
179
     * Perform a dry run to show what releases would be deleted.
180
     */
181
    protected function performDryRun(array $criteria): int
182
    {
183
        try {
184
            $this->info('Analyzing releases matching the following criteria:');
185
            foreach ($criteria as $criterion) {
186
                $this->line("  - {$criterion}");
187
            }
188
            $this->newLine();
189
190
            // Get the query that would be executed
191
            $query = $this->buildQueryFromCriteria($criteria);
192
193
            if (! $query) {
194
                $this->error('Could not build query from criteria.');
195
196
                return 1;
197
            }
198
199
            // Execute the query to get preview results
200
            $releases = DB::select($query);
201
202
            if (empty($releases)) {
203
                $this->info('No releases found matching the specified criteria.');
204
205
                return 0;
206
            }
207
208
            $count = count($releases);
209
            $this->info("Found {$count} release(s) that would be deleted:");
210
            $this->newLine();
211
212
            // Show sample of releases (first 10)
213
            $displayCount = min(10, $count);
214
            for ($i = 0; $i < $displayCount; $i++) {
215
                $release = $releases[$i];
216
                $releaseName = isset($release->searchname) ? $release->searchname : (isset($release->name) ? $release->name : 'Unknown');
217
                $releaseGuid = isset($release->guid) ? $release->guid : $release->id;
218
                $this->line(sprintf('  [%s] %s', $releaseGuid, $releaseName));
219
            }
220
221
            if ($count > 10) {
222
                $this->line('  ... and '.($count - 10).' more releases');
223
            }
224
225
            $this->newLine();
226
            $this->warn("Total: {$count} release(s) would be deleted");
227
            $this->info('Use without --dry-run to actually delete these releases');
228
229
            return 0;
230
231
        } catch (\Exception $e) {
232
            $this->error('Error during dry run: '.$e->getMessage());
233
234
            return 1;
235
        }
236
    }
237
238
    /**
239
     * Build the SQL query from criteria array.
240
     */
241
    protected function buildQueryFromCriteria(array $criteria): ?string
242
    {
243
        // Start with base query
244
        $query = 'SELECT id, guid, searchname, name FROM releases WHERE 1=1';
245
246
        foreach ($criteria as $criterion) {
247
            if ($criterion === 'ignore') {
248
                continue;
249
            }
250
251
            $queryPart = $this->formatCriterionToSql($criterion);
252
            if ($queryPart) {
253
                $query .= $queryPart;
254
            }
255
        }
256
257
        return $this->cleanSpaces($query);
258
    }
259
260
    /**
261
     * Convert a single criterion to SQL WHERE clause.
262
     */
263
    protected function formatCriterionToSql(string $criterion): ?string
264
    {
265
        $args = explode('=', $criterion);
266
        if (count($args) !== 3) {
267
            return null;
268
        }
269
270
        $column = trim($args[0]);
271
        $modifier = trim($args[1]);
272
        $value = trim($args[2], '"\'');
273
274
        switch ($column) {
275
            case 'categories_id':
276
                if ($modifier === 'equals') {
277
                    return " AND categories_id = {$value}";
278
                }
279
                break;
280
            case 'imdbid':
281
                if ($modifier === 'equals') {
282
                    if ($value === 'NULL') {
283
                        return ' AND imdbid IS NULL';
284
                    }
285
286
                    return " AND imdbid = {$value}";
287
                }
288
                break;
289
            case 'nzbstatus':
290
                if ($modifier === 'equals') {
291
                    return " AND nzbstatus = {$value}";
292
                }
293
                break;
294
            case 'rageid':
295
                if ($modifier === 'equals') {
296
                    return " AND rageid = {$value}";
297
                }
298
                break;
299
            case 'totalpart':
300
                switch ($modifier) {
301
                    case 'equals':
302
                        return " AND totalpart = {$value}";
303
                    case 'bigger':
304
                        return " AND totalpart > {$value}";
305
                    case 'smaller':
306
                        return " AND totalpart < {$value}";
307
                }
308
                break;
309
            case 'completion':
310
                if ($modifier === 'smaller') {
311
                    return " AND completion < {$value}";
312
                }
313
                break;
314
            case 'size':
315
                switch ($modifier) {
316
                    case 'equals':
317
                        return " AND size = {$value}";
318
                    case 'bigger':
319
                        return " AND size > {$value}";
320
                    case 'smaller':
321
                        return " AND size < {$value}";
322
                }
323
                break;
324
            case 'adddate':
325
                switch ($modifier) {
326
                    case 'bigger':
327
                        return " AND adddate < (NOW() - INTERVAL {$value} HOUR)";
328
                    case 'smaller':
329
                        return " AND adddate > (NOW() - INTERVAL {$value} HOUR)";
330
                }
331
                break;
332
            case 'postdate':
333
                switch ($modifier) {
334
                    case 'bigger':
335
                        return " AND postdate < (NOW() - INTERVAL {$value} HOUR)";
336
                    case 'smaller':
337
                        return " AND postdate > (NOW() - INTERVAL {$value} HOUR)";
338
                }
339
                break;
340
            case 'fromname':
341
                switch ($modifier) {
342
                    case 'equals':
343
                        return ' AND fromname = '.DB::connection()->getPdo()->quote($value);
344
                    case 'like':
345
                        return ' AND fromname LIKE '.DB::connection()->getPdo()->quote('%'.str_replace(' ', '%', $value).'%');
346
                }
347
                break;
348
            case 'groupname':
349
                switch ($modifier) {
350
                    case 'equals':
351
                        $group = DB::select('SELECT id FROM usenet_groups WHERE name = ?', [$value]);
352
                        if (! empty($group)) {
353
                            return " AND groups_id = {$group[0]->id}";
354
                        }
355
                        break;
356
                    case 'like':
357
                        $groups = DB::select('SELECT id FROM usenet_groups WHERE name LIKE ?', ['%'.str_replace(' ', '%', $value).'%']);
358
                        if (! empty($groups)) {
359
                            $ids = array_column($groups, 'id');
360
361
                            return ' AND groups_id IN ('.implode(',', $ids).')';
362
                        }
363
                        break;
364
                }
365
                break;
366
            case 'guid':
367
                if ($modifier === 'equals') {
368
                    return ' AND guid = '.DB::connection()->getPdo()->quote($value);
369
                }
370
                break;
371
            case 'name':
372
                switch ($modifier) {
373
                    case 'equals':
374
                        return ' AND name = '.DB::connection()->getPdo()->quote($value);
375
                    case 'like':
376
                        return ' AND name LIKE '.DB::connection()->getPdo()->quote('%'.str_replace(' ', '%', $value).'%');
377
                }
378
                break;
379
            case 'searchname':
380
                switch ($modifier) {
381
                    case 'equals':
382
                        return ' AND searchname = '.DB::connection()->getPdo()->quote($value);
383
                    case 'like':
384
                        return ' AND searchname LIKE '.DB::connection()->getPdo()->quote('%'.str_replace(' ', '%', $value).'%');
385
                }
386
                break;
387
        }
388
389
        return null;
390
    }
391
392
    /**
393
     * Clean multiple spaces from a string.
394
     */
395
    protected function cleanSpaces(string $string): string
396
    {
397
        return preg_replace('/\s+/', ' ', trim($string));
398
    }
399
400
    /**
401
     * Build criteria array from simple command options.
402
     */
403
    protected function buildCriteriaFromOptions(): array
404
    {
405
        $criteria = [];
406
407
        // Group filters
408
        if ($this->option('group')) {
409
            $criteria[] = 'groupname=equals="'.$this->option('group').'"';
410
        }
411
        if ($this->option('group-like')) {
412
            $criteria[] = 'groupname=like="'.$this->option('group-like').'"';
413
        }
414
415
        // Poster filters
416
        if ($this->option('poster')) {
417
            $criteria[] = 'fromname=equals="'.$this->option('poster').'"';
418
        }
419
        if ($this->option('poster-like')) {
420
            $criteria[] = 'fromname=like="'.$this->option('poster-like').'"';
421
        }
422
423
        // Name filters
424
        if ($this->option('name')) {
425
            $criteria[] = 'name=equals="'.$this->option('name').'"';
426
        }
427
        if ($this->option('name-like')) {
428
            $criteria[] = 'name=like="'.$this->option('name-like').'"';
429
        }
430
431
        // Search name filters
432
        if ($this->option('search')) {
433
            $criteria[] = 'searchname=equals="'.$this->option('search').'"';
434
        }
435
        if ($this->option('search-like')) {
436
            $criteria[] = 'searchname=like="'.$this->option('search-like').'"';
437
        }
438
439
        // Category filter
440
        if ($this->option('category')) {
441
            $criteria[] = 'categories_id=equals='.$this->option('category');
442
        }
443
444
        // GUID filter
445
        if ($this->option('guid')) {
446
            $criteria[] = 'guid=equals="'.$this->option('guid').'"';
447
        }
448
449
        // Size filters
450
        if ($this->option('size')) {
451
            $criteria[] = 'size=equals='.$this->option('size');
452
        }
453
        if ($this->option('size-min')) {
454
            $criteria[] = 'size=bigger='.$this->option('size-min');
455
        }
456
        if ($this->option('size-max')) {
457
            $criteria[] = 'size=smaller='.$this->option('size-max');
458
        }
459
460
        // Age filters
461
        if ($this->option('hours-old')) {
462
            $criteria[] = 'adddate=bigger='.$this->option('hours-old');
463
        }
464
        if ($this->option('hours-new')) {
465
            $criteria[] = 'adddate=smaller='.$this->option('hours-new');
466
        }
467
468
        // Parts filters
469
        if ($this->option('parts')) {
470
            $criteria[] = 'totalpart=equals='.$this->option('parts');
471
        }
472
        if ($this->option('parts-min')) {
473
            $criteria[] = 'totalpart=bigger='.$this->option('parts-min');
474
        }
475
        if ($this->option('parts-max')) {
476
            $criteria[] = 'totalpart=smaller='.$this->option('parts-max');
477
        }
478
479
        // Completion filter
480
        if ($this->option('completion-max')) {
481
            $criteria[] = 'completion=smaller='.$this->option('completion-max');
482
        }
483
484
        // Status filters
485
        if ($this->option('nzb-status') !== null) {
0 ignored issues
show
The condition $this->option('nzb-status') !== null is always true.
Loading history...
486
            $criteria[] = 'nzbstatus=equals='.$this->option('nzb-status');
487
        }
488
489
        // IMDB filter
490
        if ($this->option('imdb') !== null) {
0 ignored issues
show
The condition $this->option('imdb') !== null is always true.
Loading history...
491
            $criteria[] = 'imdbid=equals='.$this->option('imdb');
492
        }
493
494
        // Rage filter
495
        if ($this->option('rage') !== null) {
0 ignored issues
show
The condition $this->option('rage') !== null is always true.
Loading history...
496
            $criteria[] = 'rageid=equals='.$this->option('rage');
497
        }
498
499
        return $criteria;
500
    }
501
502
    /**
503
     * Show usage information for the command.
504
     */
505
    protected function showUsage(): void
506
    {
507
        $this->info('Delete releases based on various criteria.');
508
        $this->newLine();
509
510
        $this->info('Simple Usage Examples:');
511
        $this->line('# Delete releases from a specific group');
512
        $this->line('php artisan nntmux:delete-releases --group="alt.binaries.teevee"');
513
        $this->newLine();
514
515
        $this->line('# Delete releases by poster name pattern');
516
        $this->line('php artisan nntmux:delete-releases --poster-like="@spam.com"');
517
        $this->newLine();
518
519
        $this->line('# Delete small releases (under 100MB)');
520
        $this->line('php artisan nntmux:delete-releases --size-max=104857600');
521
        $this->newLine();
522
523
        $this->line('# Delete old releases (older than 30 days)');
524
        $this->line('php artisan nntmux:delete-releases --hours-old=720');
525
        $this->newLine();
526
527
        $this->line('# Delete releases from specific category');
528
        $this->line('php artisan nntmux:delete-releases --category=2999');
529
        $this->newLine();
530
531
        $this->line('# Combine multiple criteria');
532
        $this->line('php artisan nntmux:delete-releases --group-like="movies" --size-max=1000000 --hours-old=168');
533
        $this->newLine();
534
535
        $this->line('# Preview what would be deleted (dry run)');
536
        $this->line('php artisan nntmux:delete-releases --dry-run --group-like="spam"');
537
        $this->newLine();
538
539
        $this->line('# Skip confirmation prompt');
540
        $this->line('php artisan nntmux:delete-releases --force --poster-like="spammer"');
541
        $this->newLine();
542
543
        $this->info('Available Options:');
544
        $this->line('--group           : Exact group name');
545
        $this->line('--group-like      : Group name pattern');
546
        $this->line('--poster          : Exact poster name');
547
        $this->line('--poster-like     : Poster name pattern');
548
        $this->line('--name            : Exact release name');
549
        $this->line('--name-like       : Release name pattern');
550
        $this->line('--search          : Exact search name');
551
        $this->line('--search-like     : Search name pattern');
552
        $this->line('--category        : Category ID');
553
        $this->line('--guid            : Specific GUID');
554
        $this->line('--size            : Exact size in bytes');
555
        $this->line('--size-min        : Minimum size in bytes');
556
        $this->line('--size-max        : Maximum size in bytes');
557
        $this->line('--hours-old       : Delete releases older than X hours');
558
        $this->line('--hours-new       : Delete releases newer than X hours');
559
        $this->line('--parts           : Exact number of parts');
560
        $this->line('--parts-min       : Minimum number of parts');
561
        $this->line('--parts-max       : Maximum number of parts');
562
        $this->line('--completion-max  : Maximum completion percentage');
563
        $this->line('--nzb-status      : NZB status');
564
        $this->line('--imdb            : IMDB ID (use NULL for no IMDB)');
565
        $this->line('--rage            : Rage ID');
566
        $this->line('--dry-run         : Preview without deleting');
567
        $this->line('--force           : Skip confirmation');
568
        $this->newLine();
569
570
        $this->info('Advanced Usage:');
571
        $this->line('You can still use the original complex criteria format:');
572
        $this->line('php artisan nntmux:delete-releases groupname=equals="alt.binaries.teevee" searchname=like="olympics 2014"');
573
    }
574
}
575