1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Models\Category; |
6
|
|
|
use App\Models\Release; |
7
|
|
|
use Blacklight\Categorize; |
8
|
|
|
use Blacklight\NameFixer; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
|
11
|
|
|
class RecategorizeReleases extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'nntmux:recategorize-releases |
19
|
|
|
{--misc : Re-categorize all releases in misc categories} |
20
|
|
|
{--all : Re-categorize all releases} |
21
|
|
|
{--test : Test only, no updates} |
22
|
|
|
{--group= : Re-categorize all releases in a group} |
23
|
|
|
{--groups= : Re-categorize all releases in a list of groups} |
24
|
|
|
{--category= : Re-categorize all releases in a category} |
25
|
|
|
{--categories= : Re-categorize all releases in a list of categories}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Re-categorize releases based on their name and group.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
*/ |
37
|
|
|
public function handle() |
38
|
|
|
{ |
39
|
|
|
$countQuery = Release::query(); |
40
|
|
|
if ($this->option('misc')) { |
41
|
|
|
$countQuery->whereIn('categories_id', Category::OTHERS_GROUP); |
42
|
|
|
} elseif ($this->option('all')) { |
43
|
|
|
$countQuery->where('iscategorized', 0); |
44
|
|
|
} elseif ($this->option('group')) { |
45
|
|
|
$countQuery->where('groups_id', $this->option('group')); |
46
|
|
|
} elseif ($this->option('groups')) { |
47
|
|
|
$countQuery->whereIn('groups_id', explode(',', $this->option('groups'))); |
48
|
|
|
} elseif ($this->option('category')) { |
49
|
|
|
$countQuery->where('categories_id', $this->option('category')); |
50
|
|
|
} elseif ($this->option('categories')) { |
51
|
|
|
$countQuery->whereIn('categories_id', explode(',', $this->option('categories'))); |
52
|
|
|
} elseif ($this->option('test')) { |
53
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
$this->error('You must specify at least one option. See: --help'); |
56
|
|
|
exit(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$count = $countQuery->count(); |
60
|
|
|
|
61
|
|
|
$cat = new Categorize(); |
62
|
|
|
$results = $countQuery->select(['id', 'searchname', 'fromname', 'groups_id', 'categories_id'])->get(); |
63
|
|
|
$bar = $this->output->createProgressBar($count); |
|
|
|
|
64
|
|
|
$bar->start(); |
65
|
|
|
foreach ($results as $result) { |
66
|
|
|
$bar->advance(); |
67
|
|
|
$catId = $cat->determineCategory($result->groups_id, $result->searchname, $result->fromname); |
68
|
|
|
if ((int) $result->categories_id !== (int) $catId['categories_id']) { |
69
|
|
|
if ($this->option('test')) { |
70
|
|
|
$this->info('Would have changed '.$result->searchname.' from '.$result->categories_id.' to '.$catId['categories_id']); |
71
|
|
|
} else { |
72
|
|
|
Release::query()->where('id', $result->id)->update([ |
73
|
|
|
'iscategorized' => 1, |
74
|
|
|
'videos_id' => 0, |
75
|
|
|
'tv_episodes_id' => 0, |
76
|
|
|
'imdbid' => null, |
77
|
|
|
'musicinfo_id' => null, |
78
|
|
|
'consoleinfo_id' => null, |
79
|
|
|
'gamesinfo_id' => 0, |
80
|
|
|
'bookinfo_id' => 0, |
81
|
|
|
'anidbid' => null, |
82
|
|
|
'xxxinfo_id' => 0, |
83
|
|
|
'categories_id' => $catId['categories_id'], |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
NameFixer::echoChangedReleaseName([ |
87
|
|
|
'new_name' => $result->searchname, |
88
|
|
|
'old_name' => $result->searchname, |
89
|
|
|
'new_category' => $catId['categories_id'], |
90
|
|
|
'old_category' => $result->categories_id, |
91
|
|
|
'group' => $result->group->name, |
92
|
|
|
'releases_id' => $result->id, |
93
|
|
|
'method' => 'Recategorize', |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$bar->finish(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.