summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/cleanupInvalidDbKeys.php
blob: 54ed3aad1ff63bc91d6d0b3d86f84836818633c2 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
 * Cleans up invalid titles in various tables.
 *
 * 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';

/**
 * Maintenance script that cleans up invalid titles in various tables.
 *
 * @since 1.29
 * @ingroup Maintenance
 */
class CleanupInvalidDbKeys extends Maintenance {
	/** @var array List of tables to clean up, and the field prefix for that table */
	protected static $tables = [
		// Data tables
		[ 'page', 'page' ],
		[ 'redirect', 'rd', 'idField' => 'rd_from' ],
		[ 'archive', 'ar' ],
		[ 'logging', 'log' ],
		[ 'protected_titles', 'pt', 'idField' => 0 ],
		[ 'category', 'cat', 'nsField' => 14 ],
		[ 'recentchanges', 'rc' ],
		[ 'watchlist', 'wl' ],
		// The querycache tables' qc(c)_title and qcc_titletwo may contain titles,
		// but also usernames or other things like that, so we leave them alone

		// Links tables
		[ 'pagelinks', 'pl', 'idField' => 'pl_from' ],
		[ 'templatelinks', 'tl', 'idField' => 'tl_from' ],
		[ 'categorylinks', 'cl', 'idField' => 'cl_from', 'nsField' => 14, 'titleField' => 'cl_to' ],
	];

	public function __construct() {
		parent::__construct();
		$this->addDescription( <<<'TEXT'
This script cleans up the title fields in various tables to remove entries that
will be rejected by the constructor of TitleValue.  This constructor throws an
exception when invalid data is encountered, which will not normally occur on
regular page views, but can happen on query special pages.

The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
Because any foreign key relationships involving these titles will already be
broken, the titles are corrected to a valid version or the rows are deleted
entirely, depending on the table.

The script runs with the expectation that STDOUT is redirected to a file.
TEXT
		);
		$this->addOption( 'fix', 'Actually clean up invalid titles. If this parameter is ' .
			'not specified, the script will report invalid titles but not clean them up.',
			false, false );
		$this->addOption( 'table', 'The table(s) to process. This option can be specified ' .
			'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
			'tables will be processed. Available tables are: ' .
			implode( ', ', array_column( static::$tables, 0 ) ), false, true, 't', true );

		$this->setBatchSize( 500 );
	}

	public function execute() {
		$tablesToProcess = $this->getOption( 'table' );
		foreach ( static::$tables as $tableParams ) {
			if ( !$tablesToProcess || in_array( $tableParams[0], $tablesToProcess ) ) {
				$this->cleanupTable( $tableParams );
			}
		}

		$this->outputStatus( 'Done!' );
		if ( $this->hasOption( 'fix' ) ) {
			$this->outputStatus( ' Cleaned up invalid DB keys on ' . wfWikiID() . "!\n" );
		}
	}

	/**
	 * Prints text to STDOUT, and STDERR if STDOUT was redirected to a file.
	 * Used for progress reporting.
	 *
	 * @param string $str Text to write to both places
	 * @param string|null $channel Ignored
	 */
	protected function outputStatus( $str, $channel = null ) {
		// Make it easier to find progress lines in the STDOUT log
		if ( trim( $str ) ) {
			fwrite( STDOUT, '*** ' . trim( $str ) . "\n" );
		}
		fwrite( STDERR, $str );
	}

	/**
	 * Prints text to STDOUT. Used for logging output.
	 *
	 * @param string $str Text to write
	 */
	protected function writeToReport( $str ) {
		fwrite( STDOUT, $str );
	}

	/**
	 * Identifies, and optionally cleans up, invalid titles.
	 *
	 * @param array $tableParams A child array of self::$tables
	 */
	protected function cleanupTable( $tableParams ) {
		$table = $tableParams[0];
		$prefix = $tableParams[1];
		$idField = isset( $tableParams['idField'] ) ?
			$tableParams['idField'] :
			"{$prefix}_id";
		$nsField = isset( $tableParams['nsField'] ) ?
			$tableParams['nsField'] :
			"{$prefix}_namespace";
		$titleField = isset( $tableParams['titleField'] ) ?
			$tableParams['titleField'] :
			"{$prefix}_title";

		$this->outputStatus( "Looking for invalid $titleField entries in $table...\n" );

		// Do all the select queries on the replicas, as they are slow (they use
		// unanchored LIKEs). Naturally this could cause problems if rows are
		// modified after selecting and before deleting/updating, but working on
		// the hypothesis that invalid rows will be old and in all likelihood
		// unreferenced, we should be fine to do it like this.
		$dbr = $this->getDB( DB_REPLICA, 'vslow' );

		// Find all TitleValue-invalid titles.
		$percent = $dbr->anyString(); // DBMS-agnostic equivalent of '%' LIKE wildcard
		$res = $dbr->select(
			$table,
			[
				'id' => $idField,
				'ns' => $nsField,
				'title' => $titleField,
			],
			// The REGEXP operator is not cross-DBMS, so we have to use lots of LIKEs
			[ $dbr->makeList( [
				$titleField . $dbr->buildLike( $percent, ' ', $percent ),
				$titleField . $dbr->buildLike( $percent, "\r", $percent ),
				$titleField . $dbr->buildLike( $percent, "\n", $percent ),
				$titleField . $dbr->buildLike( $percent, "\t", $percent ),
				$titleField . $dbr->buildLike( '_', $percent ),
				$titleField . $dbr->buildLike( $percent, '_' ),
			], LIST_OR ) ],
			__METHOD__,
			[ 'LIMIT' => $this->getBatchSize() ]
		);

		$this->outputStatus( "Number of invalid rows: " . $res->numRows() . "\n" );
		if ( !$res->numRows() ) {
			$this->outputStatus( "\n" );
			return;
		}

		// Write a table of titles to the report file. Also keep a list of the found
		// IDs, as we might need it later for DB updates
		$this->writeToReport( sprintf( "%10s |  ns | dbkey\n", $idField ) );
		$ids = [];
		foreach ( $res as $row ) {
			$this->writeToReport( sprintf( "%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
			$ids[] = $row->id;
		}

		// If we're doing a dry run, output the new titles we would use for the UPDATE
		// queries (if relevant), and finish
		if ( !$this->hasOption( 'fix' ) ) {
			if ( $table === 'logging' || $table === 'archive' ) {
				$this->writeToReport( "The following updates would be run with the --fix flag:\n" );
				foreach ( $res as $row ) {
					$newTitle = self::makeValidTitle( $row->title );
					$this->writeToReport(
						"$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
				}
			}

			if ( $table !== 'page' && $table !== 'redirect' ) {
				$this->outputStatus( "Run with --fix to clean up these rows\n" );
			}
			$this->outputStatus( "\n" );
			return;
		}

		// Fix the bad data, using different logic for the various tables
		$dbw = $this->getDB( DB_MASTER );
		switch ( $table ) {
			case 'page':
			case 'redirect':
				// This shouldn't happen on production wikis, and we already have a script
				// to handle 'page' rows anyway, so just notify the user and let them decide
				// what to do next.
				$this->outputStatus( <<<TEXT
IMPORTANT: This script does not fix invalid entries in the $table table.
Consider repairing these rows, and rows in related tables, by hand.
You may like to run, or borrow logic from, the cleanupTitles.php script.

TEXT
				);
				break;

			case 'archive':
			case 'logging':
				// Rename the title to a corrected equivalent. Any foreign key relationships
				// to the page_title field are already broken, so this will just make sure
				// users can still access the log entries/deleted revisions from the interface
				// using a valid page title.
				$this->outputStatus(
					"Updating these rows, setting $titleField to the closest valid DB key...\n" );
				$affectedRowCount = 0;
				foreach ( $res as $row ) {
					$newTitle = self::makeValidTitle( $row->title );
					$this->writeToReport(
						"$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );

					$dbw->update( $table,
						[ $titleField => $newTitle ],
						[ $idField => $row->id ],
						__METHOD__ );
					$affectedRowCount += $dbw->affectedRows();
				}
				wfWaitForSlaves();
				$this->outputStatus( "Updated $affectedRowCount rows on $table.\n" );

				break;

			case 'recentchanges':
			case 'watchlist':
			case 'category':
				// Since these broken titles can't exist, there's really nothing to watch,
				// nothing can be categorised in them, and they can't have been changed
				// recently, so we can just remove these rows.
				$this->outputStatus( "Deleting invalid $table rows...\n" );
				$dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
				wfWaitForSlaves();
				$this->outputStatus( 'Deleted ' . $dbw->affectedRows() . " rows from $table.\n" );
				break;

			case 'protected_titles':
				// Since these broken titles can't exist, there's really nothing to protect,
				// so we can just remove these rows. Made more complicated by this table
				// not having an ID field
				$this->outputStatus( "Deleting invalid $table rows...\n" );
				$affectedRowCount = 0;
				foreach ( $res as $row ) {
					$dbw->delete( $table,
						[ $nsField => $row->ns, $titleField => $row->title ],
						__METHOD__ );
					$affectedRowCount += $dbw->affectedRows();
				}
				wfWaitForSlaves();
				$this->outputStatus( "Deleted $affectedRowCount rows from $table.\n" );
				break;

			case 'pagelinks':
			case 'templatelinks':
			case 'categorylinks':
				// Update links tables for each page where these bogus links are supposedly
				// located. If the invalid rows don't go away after these jobs go through,
				// they're probably being added by a buggy hook.
				$this->outputStatus( "Queueing link update jobs for the pages in $idField...\n" );
				foreach ( $res as $row ) {
					$wp = WikiPage::newFromID( $row->id );
					if ( $wp ) {
						RefreshLinks::fixLinksFromArticle( $row->id );
					} else {
						// This link entry points to a nonexistent page, so just get rid of it
						$dbw->delete( $table,
							[ $idField => $row->id, $nsField => $row->ns, $titleField => $row->title ],
							__METHOD__ );
					}
				}
				wfWaitForSlaves();
				$this->outputStatus( "Link update jobs have been added to the job queue.\n" );
				break;
		}

		$this->outputStatus( "\n" );
		return;
	}

	/**
	 * Fix possible validation issues in the given title (DB key).
	 *
	 * @param string $invalidTitle
	 * @return string
	 */
	protected static function makeValidTitle( $invalidTitle ) {
		return strtr( trim( $invalidTitle, '_' ),
			[ ' ' => '_', "\r" => '', "\n" => '', "\t" => '_' ] );
	}
}

$maintClass = CleanupInvalidDbKeys::class;
require_once RUN_MAINTENANCE_IF_MAIN;