58 parent::__construct();
60This script cleans up the title fields in various tables to
remove entries that
61will be rejected by the constructor of
TitleValue. This constructor
throws an
62exception when invalid data is encountered, which will not normally occur on
63regular page views, but can happen on query special pages.
65The script targets titles matching the regular expression /^_|[ \r\n\t]|_$/.
66Because any foreign key relationships involving these titles will already be
67broken, the titles are corrected to a valid version or the rows are deleted
68entirely, depending on the table.
70The script runs with the expectation that STDOUT is redirected to a file.
73 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
74 'not specified, the script will report invalid titles but not clean them up.',
76 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
77 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
78 'tables will be processed. Available tables are: ' .
79 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
129 [ $table, $prefix ] = $tableParams;
130 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
131 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
132 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
134 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
142 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
145 if ( isset( $linksMigration::$mapping[$table] ) ) {
146 [ $nsField,$titleField ] = $linksMigration->getTitleFields( $table );
147 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
148 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
152 $percent =
$dbr->anyString();
153 $res =
$dbr->newSelectQueryBuilder()
157 'title' => $titleField,
161 ->where(
$dbr->makeList( [
162 $titleField .
$dbr->buildLike( $percent,
' ', $percent ),
163 $titleField .
$dbr->buildLike( $percent,
"\r", $percent ),
164 $titleField .
$dbr->buildLike( $percent,
"\n", $percent ),
165 $titleField .
$dbr->buildLike( $percent,
"\t", $percent ),
166 $titleField .
$dbr->buildLike(
'_', $percent ),
167 $titleField .
$dbr->buildLike( $percent,
'_' ),
169 ->joinConds( $joinConds )
171 ->caller( __METHOD__ )
175 if ( !
$res->numRows() ) {
182 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
184 foreach (
$res as $row ) {
185 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
192 if ( $table ===
'logging' || $table ===
'archive' ) {
193 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
194 foreach (
$res as $row ) {
197 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
201 if ( $table !==
'page' && $table !==
'redirect' ) {
202 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
208 $services = MediaWikiServices::getInstance();
219IMPORTANT: This script does not fix invalid entries in the $table table.
220Consider repairing these rows, and rows in related tables, by hand.
221You may like to run, or borrow logic from, the cleanupTitles.php script.
234 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
235 $affectedRowCount = 0;
236 foreach (
$res as $row ) {
239 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
241 $dbw->update( $table,
242 [ $titleField => $newTitle ],
243 [ $idField => $row->id ],
245 $affectedRowCount += $dbw->affectedRows();
248 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
252 case 'recentchanges':
258 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
259 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
261 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
264 case 'protected_titles':
268 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
269 $affectedRowCount = 0;
270 foreach (
$res as $row ) {
271 $dbw->delete( $table,
272 [ $nsField => $row->ns, $titleField => $row->title ],
274 $affectedRowCount += $dbw->affectedRows();
277 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
281 case 'templatelinks':
282 case 'categorylinks':
286 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
287 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
288 $wikiPageFactory = $services->getWikiPageFactory();
289 foreach (
$res as $row ) {
290 $wp = $wikiPageFactory->newFromID( $row->id );
292 RefreshLinks::fixLinksFromArticle( $row->id );
294 if ( isset( $linksMigration::$mapping[$table] ) ) {
295 $conds = $linksMigration->getLinksConditions(
297 Title::makeTitle( $row->ns, $row->title )
300 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
303 $dbw->delete( $table,
304 array_merge( [ $idField => $row->id ], $conds ),
309 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );