54 parent::__construct();
56This script cleans up the title fields in various tables to
remove entries that
57will be rejected by the constructor of TitleValue. This constructor
throws an
58exception when invalid data is encountered, which will not normally occur on
59regular page views, but can happen on query special pages.
61The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
62Because any foreign key relationships involving these titles will already be
63broken, the titles are corrected to a valid version or the rows are deleted
64entirely, depending on the table.
66The script runs with the expectation that STDOUT is redirected to a file.
69 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
70 'not specified, the script will report invalid titles but not clean them up.',
72 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
73 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
74 'tables will be processed. Available tables are: ' .
75 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
125 [ $table, $prefix ] = $tableParams;
126 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
127 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
128 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
130 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
137 if ( isset( $tableParams[
'virtualDomain'] ) ) {
139 $tableParams[
'virtualDomain'],
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" );
215 if ( isset( $tableParams[
'virtualDomain'] ) ) {
217 $tableParams[
'virtualDomain']
230IMPORTANT: This script does not fix invalid entries in the $table table.
231Consider repairing these rows, and rows in related tables, by hand.
232You may like to run, or borrow logic from, the cleanupTitles.php script.
245 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
246 $affectedRowCount = 0;
247 foreach ( $res as $row ) {
250 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
252 $dbw->newUpdateQueryBuilder()
254 ->set( [ $titleField => $newTitle ] )
255 ->where( [ $idField => $row->id ] )
256 ->caller( __METHOD__ )
258 $affectedRowCount += $dbw->affectedRows();
261 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
265 case 'recentchanges':
271 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
272 $dbw->newDeleteQueryBuilder()
273 ->deleteFrom( $table )
274 ->where( [ $idField => $ids ] )
275 ->caller( __METHOD__ )->execute();
277 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
280 case 'protected_titles':
284 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
285 $affectedRowCount = 0;
286 foreach ( $res as $row ) {
287 $dbw->newDeleteQueryBuilder()
288 ->deleteFrom( $table )
289 ->where( [ $nsField => $row->ns, $titleField => $row->title ] )
290 ->caller( __METHOD__ )->execute();
291 $affectedRowCount += $dbw->affectedRows();
294 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
298 case 'templatelinks':
299 case 'categorylinks':
304 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
306 $wikiPageFactory = $services->getWikiPageFactory();
307 foreach ( $res as $row ) {
308 $wp = $wikiPageFactory->newFromID( $row->id );
310 RefreshLinks::fixLinksFromArticle( $row->id );
312 if ( isset( $linksMigration::$mapping[$table] ) ) {
313 $conds = $linksMigration->getLinksConditions(
315 Title::makeTitle( $row->ns, $row->title )
318 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
321 $dbw->newDeleteQueryBuilder()
322 ->deleteFrom( $table )
323 ->where( array_merge( [ $idField => $row->id ], $conds ) )
324 ->caller( __METHOD__ )->execute();
328 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );