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