Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / cleanupEmptyCategories.php
blobca97cc1a33acc7695580c6cc492ede1f702380c5
1 <?php
2 /**
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Category\Category;
25 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
31 /**
32 * Maintenance script to clean up empty categories in the category table.
34 * @ingroup Maintenance
35 * @since 1.28
37 class CleanupEmptyCategories extends LoggedUpdateMaintenance {
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.
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.
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
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
65 $this->addOption(
66 'begin',
67 'Only do categories whose names are alphabetically after the provided name',
68 false,
69 true
71 $this->addOption(
72 'throttle',
73 'Wait this many milliseconds after each batch. Default: 0',
74 false,
75 true
79 protected function getUpdateKey() {
80 return 'cleanup empty categories';
83 protected function doDBUpdates() {
84 $mode = $this->getOption( 'mode', 'both' );
85 $begin = $this->getOption( 'begin', '' );
86 $throttle = $this->getOption( 'throttle', 0 );
88 if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) {
89 $this->output( "--mode must be 'add', 'remove', or 'both'.\n" );
90 return false;
93 $dbw = $this->getPrimaryDB();
95 $throttle = intval( $throttle );
97 if ( $mode === 'add' || $mode === 'both' ) {
98 if ( $begin !== '' ) {
99 $where = [ $dbw->expr( 'page_title', '>', $begin ) ];
100 } else {
101 $where = [];
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;
120 foreach ( $rows as $row ) {
121 $name = $row->page_title;
122 $where = [ $dbw->expr( 'page_title', '>', $name ) ];
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();
132 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $rows has at at least one item
133 $this->output( "--mode=$mode --begin=$name\n" );
135 $this->waitForReplication();
136 usleep( $throttle * 1000 );
139 $begin = '';
142 if ( $mode === 'remove' || $mode === 'both' ) {
143 if ( $begin !== '' ) {
144 $where = [ $dbw->expr( 'cat_title', '>', $begin ) ];
145 } else {
146 $where = [];
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;
164 foreach ( $rows as $row ) {
165 $name = $row->cat_title;
166 $where = [ $dbw->expr( 'cat_title', '>', $name ) ];
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();
177 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
178 $this->output( "--mode=remove --begin=$name\n" );
180 $this->waitForReplication();
181 usleep( $throttle * 1000 );
185 $this->output( "Category cleanup complete.\n" );
187 return true;
191 // @codeCoverageIgnoreStart
192 $maintClass = CleanupEmptyCategories::class;
193 require_once RUN_MAINTENANCE_IF_MAIN;
194 // @codeCoverageIgnoreEnd