28require_once __DIR__ .
'/Maintenance.php';
42 parent::__construct();
44This script refreshes the category membership counts stored in the category
45table. As time passes, these counts often drift from the actual number of
46category members. The script identifies rows where the value in the category
47table does not match the number of categorylinks rows
for that category, and
48updates the category table accordingly.
50To fully refresh the data in the category table, you need to run
this script
51for all three modes. Alternatively, just one mode can be run
if required.
56 '(REQUIRED) Which category count column to recompute: "pages", "subcats", "files" or "all".',
62 'Only recount categories with cat_id greater than the given value',
68 'Wait this many milliseconds after each batch. Default: 0',
75 'Skip running cleanupEmptyCategories if the "page" mode is selected',
84 $originalMode = $this->
getOption(
'mode' );
85 if ( !in_array( $originalMode, [
'pages',
'subcats',
'files',
'all' ] ) ) {
86 $this->
fatalError(
'Please specify a valid mode: one of "pages", "subcats", "files" or "all".' );
89 if ( $originalMode ===
'all' ) {
90 $modes = [
'pages',
'subcats',
'files' ];
92 $modes = [ $originalMode ];
95 foreach ( $modes as $mode ) {
96 $this->
output(
"Starting to recount {$mode} counts.\n" );
97 $this->minimumId = intval( $this->
getOption(
'begin', 0 ) );
102 while ( ( $result = $this->
doWork( $mode ) ) !==
false ) {
103 $affectedRows += $result;
104 usleep( $this->
getOption(
'throttle', 0 ) * 1000 );
107 $this->
output(
"Updated the {$mode} counts of $affectedRows categories.\n" );
111 $this->
output(
"Done!\n" );
112 if ( $originalMode !==
'all' ) {
113 $this->
output(
"Now run the script using the other --mode options if you haven't already.\n" );
116 if ( in_array(
'pages', $modes ) ) {
117 if ( $this->
hasOption(
'skip-cleanup' ) ) {
119 "Also run 'php cleanupEmptyCategories.php --mode remove' to remove empty,\n" .
120 "nonexistent categories from the category table.\n\n" );
122 $this->
output(
"Running cleanupEmptyCategories.php\n" );
123 $cleanup = $this->
runChild( CleanupEmptyCategories::class );
124 '@phan-var CleanupEmptyCategories $cleanup';
127 $cleanup->loadParamsAndArgs( $this->mSelf, [], [] );
129 $cleanup->setForce(
true );
135 protected function doWork(
string $mode ): int|false {
136 $this->
output(
"Finding up to {$this->getBatchSize()} drifted rows " .
137 "greater than cat_id {$this->minimumId}...\n" );
142 MainConfigNames::CategoryLinksSchemaMigrationStage
146 $queryBuilder = $dbr->newSelectQueryBuilder()
147 ->select(
'COUNT(*)' )
148 ->from(
'categorylinks' )
149 ->where(
'cl_to = cat_title' );
151 $queryBuilder = $dbr->newSelectQueryBuilder()
152 ->select(
'COUNT(*)' )
153 ->from(
'categorylinks' )
154 ->join(
'linktarget',
null,
'cl_target_id = lt_id' )
156 'lt_title' =>
'cat_title',
161 if ( $mode ===
'subcats' ) {
162 $queryBuilder->andWhere( [
'cl_type' =>
'subcat' ] );
163 } elseif ( $mode ===
'files' ) {
164 $queryBuilder->andWhere( [
'cl_type' =>
'file' ] );
167 $countingSubquery = $queryBuilder->caller( __METHOD__ )->getSQL();
173 $idsToUpdate = $dbr->newSelectQueryBuilder()
176 ->where( [ $dbr->expr(
'cat_id',
'>', (
int)$this->minimumId ),
"cat_{$mode} != ($countingSubquery)" ] )
178 ->caller( __METHOD__ )->fetchFieldValues();
179 if ( !$idsToUpdate ) {
182 $this->
output(
"Updating cat_{$mode} field on " .
183 count( $idsToUpdate ) .
" rows...\n" );
189 $this->minimumId = end( $idsToUpdate );
193 $res = $dbw->newSelectQueryBuilder()
194 ->select( [
'cat_id',
'count' =>
"($countingSubquery)" ] )
196 ->where( [
'cat_id' => $idsToUpdate ] )
197 ->caller( __METHOD__ )->fetchResultSet();
205 foreach ( $res as $row ) {
206 $dbw->newUpdateQueryBuilder()
207 ->update(
'category' )
208 ->set( [
"cat_{$mode}" => $row->count ] )
210 'cat_id' => $row->cat_id,
211 $dbw->expr(
"cat_{$mode}",
'!=', (
int)$row->count ),
213 ->caller( __METHOD__ )
215 $affectedRows += $dbw->affectedRows();
220 return $affectedRows;
226require_once RUN_MAINTENANCE_IF_MAIN;
const SCHEMA_COMPAT_READ_OLD
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
runChild( $maintClass, $classFile=null)
Returns an instance of the given maintenance script, with all of the current arguments passed to it.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script that refreshes category membership counts in the category table.
__construct()
Default constructor.
execute()
Do the actual work.