56 parent::__construct();
58This script cleans up the title fields in various tables to
remove entries that
59will be rejected by the constructor of
TitleValue. This constructor
throws an
60exception when invalid data is encountered, which will not normally occur on
61regular page views, but can happen on query special pages.
63The script targets titles matching the regular expression /^_|[ \r\
n\t]|_$/.
64Because any foreign key relationships involving these titles will already be
65broken, the titles are corrected to a valid version or the rows are deleted
66entirely, depending on the table.
68The script runs with the expectation that STDOUT is redirected to a file.
71 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
72 'not specified, the script will report invalid titles but not clean them up.',
74 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
75 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
76 'tables will be processed. Available tables are: ' .
77 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
127 list( $table, $prefix ) = $tableParams;
128 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
129 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
130 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
132 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
140 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
143 if ( isset( $linksMigration::$mapping[$table] ) ) {
144 list( $nsField,$titleField ) = $linksMigration->getTitleFields( $table );
145 $joinConds = $linksMigration->getQueryInfo( $table )[
'joins'];
146 $tables = $linksMigration->getQueryInfo( $table )[
'tables'];
150 $percent =
$dbr->anyString();
151 $res =
$dbr->newSelectQueryBuilder()
155 'title' => $titleField,
159 ->where(
$dbr->makeList( [
160 $titleField .
$dbr->buildLike( $percent,
' ', $percent ),
161 $titleField .
$dbr->buildLike( $percent,
"\r", $percent ),
162 $titleField .
$dbr->buildLike( $percent,
"\n", $percent ),
163 $titleField .
$dbr->buildLike( $percent,
"\t", $percent ),
164 $titleField .
$dbr->buildLike(
'_', $percent ),
165 $titleField .
$dbr->buildLike( $percent,
'_' ),
167 ->joinConds( $joinConds )
169 ->caller( __METHOD__ )
173 if ( !
$res->numRows() ) {
180 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
182 foreach (
$res as $row ) {
183 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
190 if ( $table ===
'logging' || $table ===
'archive' ) {
191 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
192 foreach (
$res as $row ) {
195 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
199 if ( $table !==
'page' && $table !==
'redirect' ) {
200 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
206 $services = MediaWikiServices::getInstance();
207 $lbFactory = $services->getDBLoadBalancerFactory();
218IMPORTANT: This script does not fix invalid entries in the $table table.
219Consider repairing these rows, and rows in related tables, by hand.
220You may like to run, or borrow logic from, the cleanupTitles.php script.
233 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
234 $affectedRowCount = 0;
235 foreach (
$res as $row ) {
238 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
240 $dbw->update( $table,
241 [ $titleField => $newTitle ],
242 [ $idField => $row->id ],
244 $affectedRowCount += $dbw->affectedRows();
246 $lbFactory->waitForReplication();
247 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
251 case 'recentchanges':
257 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
258 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
259 $lbFactory->waitForReplication();
260 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
263 case 'protected_titles':
267 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
268 $affectedRowCount = 0;
269 foreach (
$res as $row ) {
270 $dbw->delete( $table,
271 [ $nsField => $row->ns, $titleField => $row->title ],
273 $affectedRowCount += $dbw->affectedRows();
275 $lbFactory->waitForReplication();
276 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
280 case 'templatelinks':
281 case 'categorylinks':
285 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
286 $linksMigration = MediaWikiServices::getInstance()->getLinksMigration();
287 $wikiPageFactory = $services->getWikiPageFactory();
288 foreach (
$res as $row ) {
289 $wp = $wikiPageFactory->newFromID( $row->id );
291 RefreshLinks::fixLinksFromArticle( $row->id );
293 if ( isset( $linksMigration::$mapping[$table] ) ) {
294 $conds = $linksMigration->getLinksConditions(
296 Title::makeTitle( $row->ns, $row->title )
299 $conds = [ $nsField => $row->ns, $titleField => $row->title ];
302 $dbw->delete( $table,
303 array_merge( [ $idField => $row->id ], $conds ),
307 $lbFactory->waitForReplication();
308 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );