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 $services = MediaWikiServices::getInstance();
198 $lbFactory = $services->getDBLoadBalancerFactory();
209IMPORTANT: This script does not fix invalid entries in the $table table.
210Consider repairing these rows, and rows in related tables, by hand.
211You may like to run, or borrow logic from, the cleanupTitles.php script.
224 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
225 $affectedRowCount = 0;
226 foreach (
$res as $row ) {
229 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
231 $dbw->update( $table,
232 [ $titleField => $newTitle ],
233 [ $idField => $row->id ],
235 $affectedRowCount += $dbw->affectedRows();
237 $lbFactory->waitForReplication();
238 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
242 case 'recentchanges':
248 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
249 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
250 $lbFactory->waitForReplication();
251 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
254 case 'protected_titles':
258 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
259 $affectedRowCount = 0;
260 foreach (
$res as $row ) {
261 $dbw->delete( $table,
262 [ $nsField => $row->ns, $titleField => $row->title ],
264 $affectedRowCount += $dbw->affectedRows();
266 $lbFactory->waitForReplication();
267 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
271 case 'templatelinks':
272 case 'categorylinks':
276 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
277 $wikiPageFactory = $services->getWikiPageFactory();
278 foreach (
$res as $row ) {
279 $wp = $wikiPageFactory->newFromID( $row->id );
281 RefreshLinks::fixLinksFromArticle( $row->id );
284 $dbw->delete( $table,
285 [ $idField => $row->id, $nsField => $row->ns, $titleField => $row->title ],
289 $lbFactory->waitForReplication();
290 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );