24 require_once __DIR__ .
'/Maintenance.php';
40 [
'redirect',
'rd',
'idField' =>
'rd_from' ],
43 [
'protected_titles',
'pt',
'idField' => 0 ],
44 [
'category',
'cat',
'nsField' => 14 ],
45 [
'recentchanges',
'rc' ],
46 [
'watchlist',
'wl' ],
51 [
'pagelinks',
'pl',
'idField' =>
'pl_from' ],
52 [
'templatelinks',
'tl',
'idField' =>
'tl_from' ],
53 [
'categorylinks',
'cl',
'idField' =>
'cl_from',
'nsField' => 14,
'titleField' =>
'cl_to' ],
57 parent::__construct();
59 This script cleans up the title fields in various tables to
remove entries that
60 will be rejected by the constructor of TitleValue. This constructor
throws an
61 exception when invalid data is encountered, which will not normally occur on
62 regular page views, but can happen on query special pages.
64 The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
65 Because any foreign key relationships involving these titles will already be
66 broken, the titles are corrected to a valid version or the rows are deleted
67 entirely, depending on the table.
69 The script runs with the expectation that STDOUT is redirected to a file.
72 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
73 'not specified, the script will report invalid titles but not clean them up.',
75 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
76 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
77 'tables will be processed. Available tables are: ' .
78 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
84 $tablesToProcess = $this->
getOption(
'table' );
85 foreach ( static::$tables as $tableParams ) {
86 if ( !$tablesToProcess || in_array( $tableParams[0], $tablesToProcess ) ) {
93 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
94 $this->
outputStatus(
" Cleaned up invalid DB keys on $dbDomain!\n" );
107 if ( trim( $str ) ) {
108 fwrite( STDOUT,
'*** ' . trim( $str ) .
"\n" );
110 fwrite( STDERR, $str );
119 fwrite( STDOUT, $str );
128 [ $table, $prefix ] = $tableParams;
129 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
130 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
131 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
133 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
144 if ( isset( $linksMigration::$mapping[$table] ) ) {
145 [ $nsField, $titleField ] = $linksMigration->getTitleFields( $table );
146 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
147 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
151 $percent = $dbr->anyString();
152 $res = $dbr->newSelectQueryBuilder()
156 'title' => $titleField,
160 ->where( $dbr->makeList( [
161 $titleField . $dbr->buildLike( $percent,
' ', $percent ),
162 $titleField . $dbr->buildLike( $percent,
"\r", $percent ),
163 $titleField . $dbr->buildLike( $percent,
"\n", $percent ),
164 $titleField . $dbr->buildLike( $percent,
"\t", $percent ),
165 $titleField . $dbr->buildLike(
'_', $percent ),
166 $titleField . $dbr->buildLike( $percent,
'_' ),
168 ->joinConds( $joinConds )
170 ->caller( __METHOD__ )
173 $this->
outputStatus(
"Number of invalid rows: " . $res->numRows() .
"\n" );
174 if ( !$res->numRows() ) {
181 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
183 foreach ( $res as $row ) {
184 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
191 if ( $table ===
'logging' || $table ===
'archive' ) {
192 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
193 foreach ( $res as $row ) {
196 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
200 if ( $table !==
'page' && $table !==
'redirect' ) {
201 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
218 IMPORTANT: This script does not fix invalid entries in the $table table.
219 Consider repairing these rows, and rows in related tables, by hand.
220 You may like to run, or borrow logic from, the cleanupTitles.php script.
233 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
234 $affectedRowCount = 0;
235 foreach ( $res as $row ) {
238 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
240 $dbw->update( $table,
241 [ $titleField => $newTitle ],
242 [ $idField => $row->id ],
244 $affectedRowCount += $dbw->affectedRows();
247 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
251 case 'recentchanges':
257 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
258 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
260 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
263 case 'protected_titles':
267 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
268 $affectedRowCount = 0;
269 foreach ( $res as $row ) {
270 $dbw->delete( $table,
271 [ $nsField => $row->ns, $titleField => $row->title ],
273 $affectedRowCount += $dbw->affectedRows();
276 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
280 case 'templatelinks':
281 case 'categorylinks':
285 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
287 $wikiPageFactory = $services->getWikiPageFactory();
288 foreach ( $res as $row ) {
289 $wp = $wikiPageFactory->newFromID( $row->id );
293 if ( isset( $linksMigration::$mapping[$table] ) ) {
294 $conds = $linksMigration->getLinksConditions(
296 Title::makeTitle( $row->ns, $row->title )
299 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
302 $dbw->delete( $table,
303 array_merge( [ $idField => $row->id ], $conds ),
308 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );
322 return strtr( trim( $invalidTitle,
'_' ),
323 [
' ' =>
'_',
"\r" =>
'',
"\n" =>
'',
"\t" =>
'_' ] );
328 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.
getServiceContainer()
Returns the main service container.
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)
Run LinksUpdate for all links on a given page_id.