Passed
Push — master ( e1e244...63e104 )
by Darko
16:20 queued 06:13
created

DeleteReleases::showUsage()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 68
rs 8.9599
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
Bug introduced by
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
                    ManticoreSearch::deleteRelease($releaseId);
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

150
                    ManticoreSearch::/** @scrutinizer ignore-call */ 
151
                                     deleteRelease($releaseId);
Loading history...
151
                    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

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