MediaWiki
master
cleanupEmptyCategories.php
Go to the documentation of this file.
1
<?php
24
use
MediaWiki\Category\Category
;
25
26
require_once __DIR__ .
'/Maintenance.php'
;
27
34
class
CleanupEmptyCategories
extends
LoggedUpdateMaintenance
{
35
36
public
function
__construct
() {
37
parent::__construct();
38
$this->
addDescription
(
39
<<<TEXT
40
This script will clean up the category table by removing entries
for
empty
41
categories without a description page and adding entries
for
empty categories
42
with a description page. It will print out progress indicators every batch. The
43
script is perfectly safe to
run
on large, live wikis, and running it multiple
44
times is harmless. You may want to use the throttling options
if
it
's causing
45
too much load; they will not affect correctness.
46
47
If the script is stopped and later resumed, you can use the --mode and --begin
48
options with the last printed progress indicator to pick up where you left off.
49
50
When the script has finished, it will make a note of this in the database, and
51
will not run again without the --force option.
52
TEXT
53
);
54
55
$this->addOption(
56
'
mode
',
57
'
"add"
empty categories with description pages,
"remove"
empty categories
'
58
. '
without description pages, or
"both"
',
59
false,
60
true
61
);
62
$this->addOption(
63
'
begin
',
64
'
Only
do
categories whose names are alphabetically after the provided name
',
65
false,
66
true
67
);
68
$this->addOption(
69
'
throttle
',
70
'
Wait
this
many milliseconds after each batch. Default: 0
',
71
false,
72
true
73
);
74
}
75
76
protected function getUpdateKey() {
77
return '
cleanup empty categories
';
78
}
79
80
protected function doDBUpdates() {
81
$mode = $this->getOption( '
mode
', '
both
' );
82
$begin = $this->getOption( '
begin
', '
' );
83
$throttle = $this->getOption( '
throttle
', 0 );
84
85
if ( !in_array( $mode, [ '
add
', '
remove
', '
both
' ] ) ) {
86
$this->output( "--mode must be '
add
', '
remove
', or '
both
'.\n" );
87
return false;
88
}
89
90
$dbw = $this->getPrimaryDB();
91
92
$throttle = intval( $throttle );
93
94
if ( $mode === '
add
' || $mode === '
both
' ) {
95
if ( $begin !== '
' ) {
96
$where = [ $dbw->expr( '
page_title
', '
>
', $begin ) ];
97
} else {
98
$where = [];
99
}
100
101
$this->output( "Adding empty categories with description pages...\n" );
102
while ( true ) {
103
# Find which category to update
104
$rows = $dbw->newSelectQueryBuilder()
105
->select( '
page_title
' )
106
->from( '
page
' )
107
->leftJoin( '
category
', null, '
page_title = cat_title
' )
108
->where( $where )
109
->andWhere( [ '
page_namespace
' => NS_CATEGORY, '
cat_title
' => null ] )
110
->orderBy( '
page_title
' )
111
->limit( $this->getBatchSize() )
112
->caller( __METHOD__ )->fetchResultSet();
113
if ( !$rows || $rows->numRows() <= 0 ) {
114
break;
115
}
116
117
foreach ( $rows as $row ) {
118
$name = $row->page_title;
119
$where = [ $dbw->expr( '
page_title
', '
>
', $name ) ];
120
121
# Use the row to update the category count
122
$cat = Category::newFromName( $name );
123
if ( !is_object( $cat ) ) {
124
$this->output( "The category named $name is not valid?!\n" );
125
} else {
126
$cat->refreshCounts();
127
}
128
}
129
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable $rows has at at least one item
130
$this->output( "--mode=$mode --begin=$name\n" );
131
132
$this->waitForReplication();
133
usleep( $throttle * 1000 );
134
}
135
136
$begin = '
';
137
}
138
139
if ( $mode === '
remove
' || $mode === '
both
' ) {
140
if ( $begin !== '
' ) {
141
$where = [ $dbw->expr( '
cat_title
', '
>
', $begin ) ];
142
} else {
143
$where = [];
144
}
145
146
$this->output( "Removing empty categories without description pages...\n" );
147
while ( true ) {
148
# Find which category to update
149
$rows = $dbw->newSelectQueryBuilder()
150
->select( '
cat_title
' )
151
->from( '
category
' )
152
->leftJoin( '
page
', null, [ '
page_namespace
' => NS_CATEGORY, '
page_title = cat_title
' ] )
153
->where( $where )
154
->andWhere( [ '
page_title
' => null, '
cat_pages
' => 0 ] )
155
->orderBy( '
cat_title
' )
156
->limit( $this->getBatchSize() )
157
->caller( __METHOD__ )->fetchResultSet();
158
if ( !$rows || $rows->numRows() <= 0 ) {
159
break;
160
}
161
foreach ( $rows as $row ) {
162
$name = $row->cat_title;
163
$where = [ $dbw->expr( '
cat_title
', '
>
', $name ) ];
164
165
# Use the row to update the category count
166
$cat = Category::newFromName( $name );
167
if ( !is_object( $cat ) ) {
168
$this->output( "The category named $name is not valid?!\n" );
169
} else {
170
$cat->refreshCounts();
171
}
172
}
173
174
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
175
$this->output( "--mode=remove --begin=$name\n" );
176
177
$this->waitForReplication();
178
usleep( $throttle * 1000 );
179
}
180
}
181
182
$this->output( "Category cleanup complete.\n" );
183
184
return true;
185
}
186
}
187
188
$maintClass = CleanupEmptyCategories::class;
189
require_once RUN_MAINTENANCE_IF_MAIN;
run
run()
Run the job.
Definition
UploadJobTrait.php:72
CleanupEmptyCategories
Maintenance script to clean up empty categories in the category table.
Definition
cleanupEmptyCategories.php:34
CleanupEmptyCategories\__construct
__construct()
Default constructor.
Definition
cleanupEmptyCategories.php:36
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition
LoggedUpdateMaintenance.php:26
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition
Maintenance.php:329
MediaWiki\Category\Category
Category objects are immutable, strictly speaking.
Definition
Category.php:42
maintenance
cleanupEmptyCategories.php
Generated on Thu Apr 25 2024 20:25:32 for MediaWiki by
1.10.0