summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/recountCategories.php
blob: 7e8f0636d5ebc00a58d423c71e1b92ea2ac9f8f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
 * Refreshes category counts.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

use MediaWiki\MediaWikiServices;

/**
 * Maintenance script that refreshes category membership counts in the category
 * table.
 *
 * (The populateCategory.php script will also recalculate counts, but
 * recountCategories only updates rows that need to be updated, making it more
 * efficient.)
 *
 * @ingroup Maintenance
 */
class RecountCategories extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription( <<<'TEXT'
This script refreshes the category membership counts stored in the category
table. As time passes, these counts often drift from the actual number of
category members. The script identifies rows where the value in the category
table does not match the number of categorylinks rows for that category, and
updates the category table accordingly.

To fully refresh the data in the category table, you need to run this script
three times: once in each mode. Alternatively, just one mode can be run if
required.
TEXT
		);
		$this->addOption(
			'mode',
			'(REQUIRED) Which category count column to recompute: "pages", "subcats" or "files".',
			true,
			true
		);
		$this->addOption(
			'begin',
			'Only recount categories with cat_id greater than the given value',
			false,
			true
		);
		$this->addOption(
			'throttle',
			'Wait this many milliseconds after each batch. Default: 0',
			false,
			true
		);

		$this->setBatchSize( 500 );
	}

	public function execute() {
		$this->mode = $this->getOption( 'mode' );
		if ( !in_array( $this->mode, [ 'pages', 'subcats', 'files' ] ) ) {
			$this->fatalError( 'Please specify a valid mode: one of "pages", "subcats" or "files".' );
		}

		$this->minimumId = intval( $this->getOption( 'begin', 0 ) );

		// do the work, batch by batch
		$affectedRows = 0;
		while ( ( $result = $this->doWork() ) !== false ) {
			$affectedRows += $result;
			usleep( $this->getOption( 'throttle', 0 ) * 1000 );
		}

		$this->output( "Done! Updated the {$this->mode} counts of $affectedRows categories.\n" .
			"Now run the script using the other --mode options if you haven't already.\n" );
		if ( $this->mode === 'pages' ) {
			$this->output(
				"Also run 'php cleanupEmptyCategories.php --mode remove' to remove empty,\n" .
				"nonexistent categories from the category table.\n\n" );
		}
	}

	protected function doWork() {
		$this->output( "Finding up to {$this->getBatchSize()} drifted rows " .
			"starting at cat_id {$this->getBatchSize()}...\n" );

		$countingConds = [ 'cl_to = cat_title' ];
		if ( $this->mode === 'subcats' ) {
			$countingConds['cl_type'] = 'subcat';
		} elseif ( $this->mode === 'files' ) {
			$countingConds['cl_type'] = 'file';
		}

		$dbr = $this->getDB( DB_REPLICA, 'vslow' );
		$countingSubquery = $dbr->selectSQLText( 'categorylinks',
			'COUNT(*)',
			$countingConds,
			__METHOD__ );

		// First, let's find out which categories have drifted and need to be updated.
		// The query counts the categorylinks for each category on the replica DB,
		// but this data can't be used for updating the master, so we don't include it
		// in the results.
		$idsToUpdate = $dbr->selectFieldValues( 'category',
			'cat_id',
			[
				'cat_id > ' . $this->minimumId,
				"cat_{$this->mode} != ($countingSubquery)"
			],
			__METHOD__,
			[ 'LIMIT' => $this->getBatchSize() ]
		);
		if ( !$idsToUpdate ) {
			return false;
		}
		$this->output( "Updating cat_{$this->mode} field on " .
			count( $idsToUpdate ) . " rows...\n" );

		// In the next batch, start where this query left off. The rows selected
		// in this iteration shouldn't be selected again after being updated, but
		// we still keep track of where we are up to, as extra protection against
		// infinite loops.
		$this->minimumId = end( $idsToUpdate );

		// Now, on master, find the correct counts for these categories.
		$dbw = $this->getDB( DB_MASTER );
		$res = $dbw->select( 'category',
			[ 'cat_id', 'count' => "($countingSubquery)" ],
			[ 'cat_id' => $idsToUpdate ],
			__METHOD__ );

		// Update the category counts on the rows we just identified.
		// This logic is equivalent to Category::refreshCounts, except here, we
		// don't remove rows when cat_pages is zero and the category description page
		// doesn't exist - instead we print a suggestion to run
		// cleanupEmptyCategories.php.
		$affectedRows = 0;
		foreach ( $res as $row ) {
			$dbw->update( 'category',
				[ "cat_{$this->mode}" => $row->count ],
				[
					'cat_id' => $row->cat_id,
					"cat_{$this->mode} != " . (int)( $row->count ),
				],
				__METHOD__ );
			$affectedRows += $dbw->affectedRows();
		}

		MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();

		return $affectedRows;
	}
}

$maintClass = RecountCategories::class;
require_once RUN_MAINTENANCE_IF_MAIN;