60 parent::__construct();
62This script cleans up the title fields in various tables to
remove entries that
63will be rejected by the constructor of TitleValue. This constructor
throws an
64exception when invalid data is encountered, which will not normally occur on
65regular page views, but can happen on query special pages.
67The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
68Because any foreign key relationships involving these titles will already be
69broken, the titles are corrected to a valid version or the rows are deleted
70entirely, depending on the table.
72The script runs with the expectation that STDOUT is redirected to a file.
75 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
76 'not specified, the script will report invalid titles but not clean them up.',
78 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
79 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
80 'tables will be processed. Available tables are: ' .
81 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
131 [ $table, $prefix ] = $tableParams;
132 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
133 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
134 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
136 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
147 if ( isset( $linksMigration::$mapping[$table] ) ) {
148 [ $nsField, $titleField ] = $linksMigration->getTitleFields( $table );
149 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
150 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
154 $percent = $dbr->anyString();
157 ->expr( $titleField, IExpression::LIKE,
new LikeValue( $percent,
' ', $percent ) )
158 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\r", $percent ) )
159 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\n", $percent ) )
160 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\t", $percent ) )
161 ->or( $titleField, IExpression::LIKE,
new LikeValue(
'_', $percent ) )
162 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
'_' ) );
163 $res = $dbr->newSelectQueryBuilder()
167 'title' => $titleField,
171 ->joinConds( $joinConds )
173 ->caller( __METHOD__ )
176 $this->
outputStatus(
"Number of invalid rows: " . $res->numRows() .
"\n" );
177 if ( !$res->numRows() ) {
184 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
186 foreach ( $res as $row ) {
187 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
194 if ( $table ===
'logging' || $table ===
'archive' ) {
195 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
196 foreach ( $res as $row ) {
199 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
203 if ( $table !==
'page' && $table !==
'redirect' ) {
204 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
221IMPORTANT: This script does not fix invalid entries in the $table table.
222Consider repairing these rows, and rows in related tables, by hand.
223You may like to
run, or borrow logic from, the cleanupTitles.php script.
236 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
237 $affectedRowCount = 0;
238 foreach ( $res as $row ) {
241 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
243 $dbw->newUpdateQueryBuilder()
245 ->set( [ $titleField => $newTitle ] )
246 ->where( [ $idField => $row->id ] )
247 ->caller( __METHOD__ )
249 $affectedRowCount += $dbw->affectedRows();
252 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
256 case 'recentchanges':
262 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
263 $dbw->newDeleteQueryBuilder()
264 ->deleteFrom( $table )
265 ->where( [ $idField => $ids ] )
266 ->caller( __METHOD__ )->execute();
268 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
271 case 'protected_titles':
275 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
276 $affectedRowCount = 0;
277 foreach ( $res as $row ) {
278 $dbw->newDeleteQueryBuilder()
279 ->deleteFrom( $table )
280 ->where( [ $nsField => $row->ns, $titleField => $row->title ] )
281 ->caller( __METHOD__ )->execute();
282 $affectedRowCount += $dbw->affectedRows();
285 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
289 case 'templatelinks':
290 case 'categorylinks':
295 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
297 $wikiPageFactory = $services->getWikiPageFactory();
298 foreach ( $res as $row ) {
299 $wp = $wikiPageFactory->newFromID( $row->id );
301 RefreshLinks::fixLinksFromArticle( $row->id );
303 if ( isset( $linksMigration::$mapping[$table] ) ) {
304 $conds = $linksMigration->getLinksConditions(
306 Title::makeTitle( $row->ns, $row->title )
309 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
312 $dbw->newDeleteQueryBuilder()
313 ->deleteFrom( $table )
314 ->where( array_merge( [ $idField => $row->id ], $conds ) )
315 ->caller( __METHOD__ )->execute();
319 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );