24 require_once __DIR__ .
'/Maintenance.php';
41 [
'redirect',
'rd',
'idField' =>
'rd_from' ],
44 [
'protected_titles',
'pt',
'idField' => 0 ],
45 [
'category',
'cat',
'nsField' => 14 ],
46 [
'recentchanges',
'rc' ],
47 [
'watchlist',
'wl' ],
52 [
'pagelinks',
'pl',
'idField' =>
'pl_from' ],
53 [
'templatelinks',
'tl',
'idField' =>
'tl_from' ],
54 [
'categorylinks',
'cl',
'idField' =>
'cl_from',
'nsField' => 14,
'titleField' =>
'cl_to' ],
58 parent::__construct();
60 This script cleans up the title fields in various tables to
remove entries that
61 will be rejected by the constructor of
TitleValue. This constructor
throws an
62 exception when invalid data is encountered, which will not normally occur on
63 regular page views, but can happen on query special pages.
65 The script targets titles matching the regular expression /^_|[ \r\
n\t]|_$/.
66 Because any foreign key relationships involving these titles will already be
67 broken, the titles are corrected to a valid version or the rows are deleted
68 entirely, depending on the table.
70 The script runs with the expectation that STDOUT is redirected to a file.
73 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
74 'not specified, the script will report invalid titles but not clean them up.',
76 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
77 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
78 'tables will be processed. Available tables are: ' .
79 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
85 $tablesToProcess = $this->
getOption(
'table' );
86 foreach ( static::$tables as $tableParams ) {
87 if ( !$tablesToProcess || in_array( $tableParams[0], $tablesToProcess ) ) {
94 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
95 $this->
outputStatus(
" Cleaned up invalid DB keys on $dbDomain!\n" );
108 if ( trim( $str ) ) {
109 fwrite( STDOUT,
'*** ' . trim( $str ) .
"\n" );
111 fwrite( STDERR, $str );
120 fwrite( STDOUT, $str );
129 [ $table, $prefix ] = $tableParams;
130 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
131 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
132 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
134 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
142 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
145 if ( isset( $linksMigration::$mapping[$table] ) ) {
146 [ $nsField,$titleField ] = $linksMigration->getTitleFields( $table );
147 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
148 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
152 $percent =
$dbr->anyString();
153 $res =
$dbr->newSelectQueryBuilder()
157 'title' => $titleField,
161 ->where(
$dbr->makeList( [
162 $titleField .
$dbr->buildLike( $percent,
' ', $percent ),
163 $titleField .
$dbr->buildLike( $percent,
"\r", $percent ),
164 $titleField .
$dbr->buildLike( $percent,
"\n", $percent ),
165 $titleField .
$dbr->buildLike( $percent,
"\t", $percent ),
166 $titleField .
$dbr->buildLike(
'_', $percent ),
167 $titleField .
$dbr->buildLike( $percent,
'_' ),
169 ->joinConds( $joinConds )
171 ->caller( __METHOD__ )
175 if ( !
$res->numRows() ) {
182 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
184 foreach (
$res as $row ) {
185 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
192 if ( $table ===
'logging' || $table ===
'archive' ) {
193 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
194 foreach (
$res as $row ) {
197 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
201 if ( $table !==
'page' && $table !==
'redirect' ) {
202 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
208 $services = MediaWikiServices::getInstance();
219 IMPORTANT: This script does not fix invalid entries in the $table table.
220 Consider repairing these rows, and rows in related tables, by hand.
221 You may like to run, or borrow logic from, the cleanupTitles.php script.
234 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
235 $affectedRowCount = 0;
236 foreach (
$res as $row ) {
239 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
241 $dbw->update( $table,
242 [ $titleField => $newTitle ],
243 [ $idField => $row->id ],
245 $affectedRowCount += $dbw->affectedRows();
248 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
252 case 'recentchanges':
258 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
259 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
261 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
264 case 'protected_titles':
268 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
269 $affectedRowCount = 0;
270 foreach (
$res as $row ) {
271 $dbw->delete( $table,
272 [ $nsField => $row->ns, $titleField => $row->title ],
274 $affectedRowCount += $dbw->affectedRows();
277 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
281 case 'templatelinks':
282 case 'categorylinks':
286 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
287 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
288 $wikiPageFactory = $services->getWikiPageFactory();
289 foreach (
$res as $row ) {
290 $wp = $wikiPageFactory->newFromID( $row->id );
294 if ( isset( $linksMigration::$mapping[$table] ) ) {
295 $conds = $linksMigration->getLinksConditions(
297 Title::makeTitle( $row->ns, $row->title )
300 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
303 $dbw->delete( $table,
304 array_merge( [ $idField => $row->id ], $conds ),
309 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );
323 return strtr( trim( $invalidTitle,
'_' ),
324 [
' ' =>
'_',
"\r" =>
'',
"\n" =>
'',
"\t" =>
'_' ] );
329 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that cleans up invalid titles in various tables.
static makeValidTitle( $invalidTitle)
Fix possible validation issues in the given title (DB key).
static array[] $tables
List of tables to clean up, and the field prefix for that table.
cleanupTable( $tableParams)
Identifies, and optionally cleans up, invalid titles.
outputStatus( $str, $channel=null)
Prints text to STDOUT, and STDERR if STDOUT was redirected to a file.
writeToReport( $str)
Prints text to STDOUT.
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
static fixLinksFromArticle( $id, $ns=false, $beforeTimestamp=false)
Run LinksUpdate for all links on a given page_id.
Represents a page (or page fragment) title within MediaWiki.