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