DeleteReleases::cleanSpaces()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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