MediaWiki master
cleanupEmptyCategories.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
38
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
42 <<<TEXT
43This script will clean up the category table by removing entries for empty
44categories without a description page and adding entries for empty categories
45with a description page. It will print out progress indicators every batch. The
46script is perfectly safe to run on large, live wikis, and running it multiple
47times is harmless. You may want to use the throttling options if it's causing
48too much load; they will not affect correctness.
49
50If the script is stopped and later resumed, you can use the --mode and --begin
51options with the last printed progress indicator to pick up where you left off.
52
53When the script has finished, it will make a note of this in the database, and
54will not run again without the --force option.
55TEXT
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
80 protected function getUpdateKey() {
81 return 'cleanup empty categories';
82 }
83
85 protected function doDBUpdates() {
86 $mode = $this->getOption( 'mode', 'both' );
87 $begin = $this->getOption( 'begin', '' );
88 $throttle = $this->getOption( 'throttle', 0 );
89
90 if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) {
91 $this->output( "--mode must be 'add', 'remove', or 'both'.\n" );
92 return false;
93 }
94
95 $dbw = $this->getPrimaryDB();
96
97 $throttle = intval( $throttle );
98
99 if ( $mode === 'add' || $mode === 'both' ) {
100 if ( $begin !== '' ) {
101 $where = [ $dbw->expr( 'page_title', '>', $begin ) ];
102 } else {
103 $where = [];
104 }
105
106 $this->output( "Adding empty categories with description pages...\n" );
107 while ( true ) {
108 # Find which category to update
109 $rows = $dbw->newSelectQueryBuilder()
110 ->select( 'page_title' )
111 ->from( 'page' )
112 ->leftJoin( 'category', null, 'page_title = cat_title' )
113 ->where( $where )
114 ->andWhere( [ 'page_namespace' => NS_CATEGORY, 'cat_title' => null ] )
115 ->orderBy( 'page_title' )
116 ->limit( $this->getBatchSize() )
117 ->caller( __METHOD__ )->fetchResultSet();
118 if ( !$rows || $rows->numRows() <= 0 ) {
119 break;
120 }
121
122 foreach ( $rows as $row ) {
123 $name = $row->page_title;
124 $where = [ $dbw->expr( 'page_title', '>', $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 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $rows has at at least one item
135 $this->output( "--mode=$mode --begin=$name\n" );
136
137 $this->waitForReplication();
138 usleep( $throttle * 1000 );
139 }
140
141 $begin = '';
142 }
143
144 if ( $mode === 'remove' || $mode === 'both' ) {
145 if ( $begin !== '' ) {
146 $where = [ $dbw->expr( 'cat_title', '>', $begin ) ];
147 } else {
148 $where = [];
149 }
150
151 $this->output( "Removing empty categories without description pages...\n" );
152 while ( true ) {
153 # Find which category to update
154 $rows = $dbw->newSelectQueryBuilder()
155 ->select( 'cat_title' )
156 ->from( 'category' )
157 ->leftJoin( 'page', null, [ 'page_namespace' => NS_CATEGORY, 'page_title = cat_title' ] )
158 ->where( $where )
159 ->andWhere( [ 'page_title' => null, 'cat_pages' => 0 ] )
160 ->orderBy( 'cat_title' )
161 ->limit( $this->getBatchSize() )
162 ->caller( __METHOD__ )->fetchResultSet();
163 if ( !$rows || $rows->numRows() <= 0 ) {
164 break;
165 }
166 foreach ( $rows as $row ) {
167 $name = $row->cat_title;
168 $where = [ $dbw->expr( 'cat_title', '>', $name ) ];
169
170 # Use the row to update the category count
171 $cat = Category::newFromName( $name );
172 if ( !is_object( $cat ) ) {
173 $this->output( "The category named $name is not valid?!\n" );
174 } else {
175 $cat->refreshCounts();
176 }
177 }
178
179 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
180 $this->output( "--mode=remove --begin=$name\n" );
181
182 $this->waitForReplication();
183 usleep( $throttle * 1000 );
184 }
185 }
186
187 $this->output( "Category cleanup complete.\n" );
188
189 return true;
190 }
191}
192
193// @codeCoverageIgnoreStart
194$maintClass = CleanupEmptyCategories::class;
195require_once RUN_MAINTENANCE_IF_MAIN;
196// @codeCoverageIgnoreEnd
Maintenance script to clean up empty categories in the category table.
__construct()
Default constructor.
Category objects are immutable, strictly speaking.
Definition Category.php:44
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
addDescription( $text)
Set the description text.