62 parent::__construct();
64This script cleans up the title fields in various tables to
remove entries that
65will be rejected by the constructor of TitleValue. This constructor
throws an
66exception when invalid data is encountered, which will not normally occur on
67regular page views, but can happen on query special pages.
69The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
70Because any foreign key relationships involving these titles will already be
71broken, the titles are corrected to a valid version or the rows are deleted
72entirely, depending on the table.
74The script runs with the expectation that STDOUT is redirected to a file.
77 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
78 'not specified, the script will report invalid titles but not clean them up.',
80 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
81 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
82 'tables will be processed. Available tables are: ' .
83 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
133 [ $table, $prefix ] = $tableParams;
134 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
135 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
136 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
138 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
149 if ( isset( $linksMigration::$mapping[$table] ) ) {
150 [ $nsField, $titleField ] = $linksMigration->getTitleFields( $table );
151 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
152 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
156 $percent = $dbr->anyString();
159 ->expr( $titleField, IExpression::LIKE,
new LikeValue( $percent,
' ', $percent ) )
160 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\r", $percent ) )
161 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\n", $percent ) )
162 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\t", $percent ) )
163 ->or( $titleField, IExpression::LIKE,
new LikeValue(
'_', $percent ) )
164 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
'_' ) );
165 $res = $dbr->newSelectQueryBuilder()
169 'title' => $titleField,
173 ->joinConds( $joinConds )
175 ->caller( __METHOD__ )
178 $this->
outputStatus(
"Number of invalid rows: " . $res->numRows() .
"\n" );
179 if ( !$res->numRows() ) {
186 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
188 foreach ( $res as $row ) {
189 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
196 if ( $table ===
'logging' || $table ===
'archive' ) {
197 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
198 foreach ( $res as $row ) {
201 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
205 if ( $table !==
'page' && $table !==
'redirect' ) {
206 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
223IMPORTANT: This script does not fix invalid entries in the $table table.
224Consider repairing these rows, and rows in related tables, by hand.
225You may like to
run, or borrow logic from, the cleanupTitles.php script.
238 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
239 $affectedRowCount = 0;
240 foreach ( $res as $row ) {
243 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
245 $dbw->newUpdateQueryBuilder()
247 ->set( [ $titleField => $newTitle ] )
248 ->where( [ $idField => $row->id ] )
249 ->caller( __METHOD__ )
251 $affectedRowCount += $dbw->affectedRows();
254 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
258 case 'recentchanges':
264 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
265 $dbw->newDeleteQueryBuilder()
266 ->deleteFrom( $table )
267 ->where( [ $idField => $ids ] )
268 ->caller( __METHOD__ )->execute();
270 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
273 case 'protected_titles':
277 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
278 $affectedRowCount = 0;
279 foreach ( $res as $row ) {
280 $dbw->newDeleteQueryBuilder()
281 ->deleteFrom( $table )
282 ->where( [ $nsField => $row->ns, $titleField => $row->title ] )
283 ->caller( __METHOD__ )->execute();
284 $affectedRowCount += $dbw->affectedRows();
287 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
291 case 'templatelinks':
292 case 'categorylinks':
297 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
299 $wikiPageFactory = $services->getWikiPageFactory();
300 foreach ( $res as $row ) {
301 $wp = $wikiPageFactory->newFromID( $row->id );
303 RefreshLinks::fixLinksFromArticle( $row->id );
305 if ( isset( $linksMigration::$mapping[$table] ) ) {
306 $conds = $linksMigration->getLinksConditions(
308 Title::makeTitle( $row->ns, $row->title )
311 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
314 $dbw->newDeleteQueryBuilder()
315 ->deleteFrom( $table )
316 ->where( array_merge( [ $idField => $row->id ], $conds ) )
317 ->caller( __METHOD__ )->execute();
321 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );