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" );
142 $percent =
$dbr->anyString();
148 'title' => $titleField,
152 $titleField .
$dbr->buildLike( $percent,
' ', $percent ),
153 $titleField .
$dbr->buildLike( $percent,
"\r", $percent ),
154 $titleField .
$dbr->buildLike( $percent,
"\n", $percent ),
155 $titleField .
$dbr->buildLike( $percent,
"\t", $percent ),
156 $titleField .
$dbr->buildLike(
'_', $percent ),
157 $titleField .
$dbr->buildLike( $percent,
'_' ),
160 [
'LIMIT' => $this->getBatchSize() ]
164 if ( !
$res->numRows() ) {
171 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
173 foreach (
$res as $row ) {
174 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
181 if ( $table ===
'logging' || $table ===
'archive' ) {
182 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
183 foreach (
$res as $row ) {
186 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
190 if ( $table !==
'page' && $table !==
'redirect' ) {
191 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
197 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
208IMPORTANT: This script does not fix invalid entries in the $table table.
209Consider repairing these rows, and rows in related tables, by hand.
210You may like to run, or borrow logic from, the cleanupTitles.php script.
223 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
224 $affectedRowCount = 0;
225 foreach (
$res as $row ) {
228 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
230 $dbw->update( $table,
231 [ $titleField => $newTitle ],
232 [ $idField => $row->id ],
234 $affectedRowCount += $dbw->affectedRows();
236 $lbFactory->waitForReplication();
237 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
241 case 'recentchanges':
247 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
248 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
249 $lbFactory->waitForReplication();
250 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
253 case 'protected_titles':
257 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
258 $affectedRowCount = 0;
259 foreach (
$res as $row ) {
260 $dbw->delete( $table,
261 [ $nsField => $row->ns, $titleField => $row->title ],
263 $affectedRowCount += $dbw->affectedRows();
265 $lbFactory->waitForReplication();
266 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
270 case 'templatelinks':
271 case 'categorylinks':
275 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
276 foreach (
$res as $row ) {
277 $wp = WikiPage::newFromID( $row->id );
279 RefreshLinks::fixLinksFromArticle( $row->id );
282 $dbw->delete( $table,
283 [ $idField => $row->id, $nsField => $row->ns, $titleField => $row->title ],
287 $lbFactory->waitForReplication();
288 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );