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
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;
193require_once RUN_MAINTENANCE_IF_MAIN;
194// @codeCoverageIgnoreEnd
run()
Run the job.
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.