3 * Clean up empty categories in the category table.
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.
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.
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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to clean up empty categories in the category table.
29 * @ingroup Maintenance
32 class CleanupEmptyCategories
extends LoggedUpdateMaintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->addDescription(
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.
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.
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.
55 '"add" empty categories with description pages, "remove" empty categories '
56 . 'without description pages, or "both"',
62 'Only do categories whose names are alphabetically after the provided name',
68 'Wait this many milliseconds after each batch. Default: 0',
74 protected function getUpdateKey() {
75 return 'cleanup empty categories';
78 protected function doDBUpdates() {
79 $mode = $this->getOption( 'mode', 'both' );
80 $begin = $this->getOption( 'begin', '' );
81 $throttle = $this->getOption( 'throttle', 0 );
83 if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) {
84 $this->output( "--mode must be 'add', 'remove', or 'both'.\n" );
88 $dbw = $this->getDB( DB_MASTER
);
90 $throttle = intval( $throttle );
92 if ( $mode === 'add' ||
$mode === 'both' ) {
93 if ( $begin !== '' ) {
94 $where = [ 'page_title > ' . $dbw->addQuotes( $begin ) ];
99 $this->output( "Adding empty categories with description pages...\n" );
101 # Find which category to update
102 $rows = $dbw->select(
103 [ 'page', 'category' ],
105 array_merge( $where, [
106 'page_namespace' => NS_CATEGORY
,
111 'ORDER BY' => 'page_title',
112 'LIMIT' => $this->mBatchSize
,
115 'category' => [ 'LEFT JOIN', 'page_title = cat_title' ],
118 if ( !$rows ||
$rows->numRows() <= 0 ) {
123 foreach ( $rows as $row ) {
124 $name = $row->page_title
;
125 $where = [ 'page_title > ' . $dbw->addQuotes( $name ) ];
127 # Use the row to update the category count
128 $cat = Category
::newFromName( $name );
129 if ( !is_object( $cat ) ) {
130 $this->output( "The category named $name is not valid?!\n" );
132 $cat->refreshCounts();
135 $this->output( "--mode=$mode --begin=$name\n" );
138 usleep( $throttle * 1000 );
144 if ( $mode === 'remove' ||
$mode === 'both' ) {
145 if ( $begin !== '' ) {
146 $where = [ 'cat_title > ' . $dbw->addQuotes( $begin ) ];
151 $this->output( "Removing empty categories without description pages...\n" );
153 # Find which category to update
154 $rows = $dbw->select(
155 [ 'category', 'page' ],
157 array_merge( $where, [
158 'page_title' => null,
163 'ORDER BY' => 'cat_title',
164 'LIMIT' => $this->mBatchSize
,
167 'page' => [ 'LEFT JOIN', [
168 'page_namespace' => NS_CATEGORY
, 'page_title = cat_title'
172 if ( !$rows ||
$rows->numRows() <= 0 ) {
176 foreach ( $rows as $row ) {
177 $name = $row->cat_title
;
178 $where = [ 'cat_title > ' . $dbw->addQuotes( $name ) ];
180 # Use the row to update the category count
181 $cat = Category
::newFromName( $name );
182 if ( !is_object( $cat ) ) {
183 $this->output( "The category named $name is not valid?!\n" );
185 $cat->refreshCounts();
189 $this->output( "--mode=remove --begin=$name\n" );
192 usleep( $throttle * 1000 );
196 $this->output( "Category cleanup complete.\n" );
202 $maintClass = 'CleanupEmptyCategories';
203 require_once RUN_MAINTENANCE_IF_MAIN
;