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