Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CleanupEmptyCategories | |
0.00% |
0 / 90 |
|
0.00% |
0 / 3 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
getUpdateKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doDBUpdates | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
342 |
1 | <?php |
2 | /** |
3 | * Clean up empty categories in the category table. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\Category\Category; |
25 | |
26 | require_once __DIR__ . '/Maintenance.php'; |
27 | |
28 | /** |
29 | * Maintenance script to clean up empty categories in the category table. |
30 | * |
31 | * @ingroup Maintenance |
32 | * @since 1.28 |
33 | */ |
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; |