53 parent::__construct();
55This script cleans up the title fields in various tables to
remove entries that
56will be rejected by the constructor of
TitleValue. This constructor
throws an
57exception when invalid data is encountered, which will not normally occur on
58regular page views, but can happen on query special pages.
60The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
61Because any foreign key relationships involving these titles will already be
62broken, the titles are corrected to a valid version or the rows are deleted
63entirely, depending on the table.
65The script runs with the expectation that STDOUT is redirected to a file.
68 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
69 'not specified, the script will report invalid titles but not clean them up.',
71 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
72 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
73 'tables will be processed. Available tables are: ' .
74 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
124 [ $table, $prefix ] = $tableParams;
125 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
126 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
127 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
129 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
136 if ( isset( $tableParams[
'virtualDomain'] ) ) {
138 $tableParams[
'virtualDomain'],
148 if ( isset( $linksMigration::$mapping[$table] ) ) {
149 [ $nsField, $titleField ] = $linksMigration->getTitleFields( $table );
150 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
151 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
155 $percent = $dbr->anyString();
158 ->expr( $titleField, IExpression::LIKE,
new LikeValue( $percent,
' ', $percent ) )
159 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\r", $percent ) )
160 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\n", $percent ) )
161 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
"\t", $percent ) )
162 ->or( $titleField, IExpression::LIKE,
new LikeValue(
'_', $percent ) )
163 ->or( $titleField, IExpression::LIKE,
new LikeValue( $percent,
'_' ) );
164 $res = $dbr->newSelectQueryBuilder()
168 'title' => $titleField,
172 ->joinConds( $joinConds )
174 ->caller( __METHOD__ )
177 $this->
outputStatus(
"Number of invalid rows: " . $res->numRows() .
"\n" );
178 if ( !$res->numRows() ) {
185 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
187 foreach ( $res as $row ) {
188 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
195 if ( $table ===
'logging' || $table ===
'archive' ) {
196 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
197 foreach ( $res as $row ) {
200 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
204 if ( $table !==
'page' && $table !==
'redirect' ) {
205 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
214 if ( isset( $tableParams[
'virtualDomain'] ) ) {
216 $tableParams[
'virtualDomain']
229IMPORTANT: This script does not fix invalid entries in the $table table.
230Consider repairing these rows, and rows in related tables, by hand.
231You may like to run, or borrow logic from, the cleanupTitles.php script.
244 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
245 $affectedRowCount = 0;
246 foreach ( $res as $row ) {
249 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
251 $dbw->newUpdateQueryBuilder()
253 ->set( [ $titleField => $newTitle ] )
254 ->where( [ $idField => $row->id ] )
255 ->caller( __METHOD__ )
257 $affectedRowCount += $dbw->affectedRows();
260 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
264 case 'recentchanges':
270 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
271 $dbw->newDeleteQueryBuilder()
272 ->deleteFrom( $table )
273 ->where( [ $idField => $ids ] )
274 ->caller( __METHOD__ )->execute();
276 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
279 case 'protected_titles':
283 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
284 $affectedRowCount = 0;
285 foreach ( $res as $row ) {
286 $dbw->newDeleteQueryBuilder()
287 ->deleteFrom( $table )
288 ->where( [ $nsField => $row->ns, $titleField => $row->title ] )
289 ->caller( __METHOD__ )->execute();
290 $affectedRowCount += $dbw->affectedRows();
293 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
297 case 'templatelinks':
298 case 'categorylinks':
303 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
305 $wikiPageFactory = $services->getWikiPageFactory();
306 foreach ( $res as $row ) {
307 if ( $wikiPageFactory->newFromID( $row->id ) ) {
308 RefreshLinks::fixLinksFromArticle( $row->id );
311 $dbw->newDeleteQueryBuilder()
312 ->deleteFrom( $table )
313 ->where( [ $idField => $row->id ] )
314 ->andWhere( $linksMigration->getLinksConditions(
318 ->caller( __METHOD__ )->execute();
322 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );