24 require_once __DIR__ .
'/Maintenance.php';
37 [
'redirect',
'rd',
'idField' =>
'rd_from' ],
40 [
'protected_titles',
'pt',
'idField' => 0 ],
41 [
'category',
'cat',
'nsField' => 14 ],
42 [
'recentchanges',
'rc' ],
43 [
'watchlist',
'wl' ],
48 [
'pagelinks',
'pl',
'idField' =>
'pl_from' ],
49 [
'templatelinks',
'tl',
'idField' =>
'tl_from' ],
50 [
'categorylinks',
'cl',
'idField' =>
'cl_from',
'nsField' => 14,
'titleField' =>
'cl_to' ],
54 parent::__construct();
56 This script cleans up the title fields in various tables to
remove entries that
57 will be rejected by the constructor of
TitleValue. This constructor
throws an
58 exception when invalid data is encountered, which will not normally occur on
59 regular page views, but can happen on query special pages.
61 The script targets titles matching the regular expression /^_|[ \r\
n\t]|_$/.
62 Because any foreign key relationships involving these titles will already be
63 broken, the titles are corrected to a valid version or the rows are deleted
64 entirely, depending on the table.
66 The script runs with the expectation that STDOUT is redirected to a file.
69 $this->
addOption(
'fix',
'Actually clean up invalid titles. If this parameter is ' .
70 'not specified, the script will report invalid titles but not clean them up.',
72 $this->
addOption(
'table',
'The table(s) to process. This option can be specified ' .
73 'more than once (e.g. -t category -t watchlist). If not specified, all available ' .
74 'tables will be processed. Available tables are: ' .
75 implode(
', ', array_column( static::$tables, 0 ) ),
false,
true,
't',
true );
81 $tablesToProcess = $this->
getOption(
'table' );
82 foreach ( static::$tables as $tableParams ) {
83 if ( !$tablesToProcess || in_array( $tableParams[0], $tablesToProcess ) ) {
91 $this->
outputStatus(
" Cleaned up invalid DB keys on $dbDomain!\n" );
104 if ( trim( $str ) ) {
105 fwrite( STDOUT,
'*** ' . trim( $str ) .
"\n" );
107 fwrite( STDERR, $str );
116 fwrite( STDOUT, $str );
125 list( $table, $prefix ) = $tableParams;
126 $idField = $tableParams[
'idField'] ??
"{$prefix}_id";
127 $nsField = $tableParams[
'nsField'] ??
"{$prefix}_namespace";
128 $titleField = $tableParams[
'titleField'] ??
"{$prefix}_title";
130 $this->
outputStatus(
"Looking for invalid $titleField entries in $table...\n" );
140 $percent =
$dbr->anyString();
146 'title' => $titleField,
150 $titleField .
$dbr->buildLike( $percent,
' ', $percent ),
151 $titleField .
$dbr->buildLike( $percent,
"\r", $percent ),
152 $titleField .
$dbr->buildLike( $percent,
"\n", $percent ),
153 $titleField .
$dbr->buildLike( $percent,
"\t", $percent ),
154 $titleField .
$dbr->buildLike(
'_', $percent ),
155 $titleField .
$dbr->buildLike( $percent,
'_' ),
162 if ( !
$res->numRows() ) {
169 $this->
writeToReport( sprintf(
"%10s | ns | dbkey\n", $idField ) );
171 foreach (
$res as $row ) {
172 $this->
writeToReport( sprintf(
"%10d | %3d | %s\n", $row->id, $row->ns, $row->title ) );
179 if ( $table ===
'logging' || $table ===
'archive' ) {
180 $this->
writeToReport(
"The following updates would be run with the --fix flag:\n" );
181 foreach (
$res as $row ) {
184 "$idField={$row->id}: update '{$row->title}' to '$newTitle'\n" );
188 if ( $table !==
'page' && $table !==
'redirect' ) {
189 $this->
outputStatus(
"Run with --fix to clean up these rows\n" );
204 IMPORTANT: This script does not fix invalid entries in the $table table.
205 Consider repairing these rows, and rows in related tables, by hand.
206 You may like to run, or borrow logic from, the cleanupTitles.php script.
219 "Updating these rows, setting $titleField to the closest valid DB key...\n" );
220 $affectedRowCount = 0;
221 foreach (
$res as $row ) {
224 "$idField={$row->id}: updating '{$row->title}' to '$newTitle'\n" );
226 $dbw->update( $table,
227 [ $titleField => $newTitle ],
228 [ $idField => $row->id ],
230 $affectedRowCount += $dbw->affectedRows();
233 $this->
outputStatus(
"Updated $affectedRowCount rows on $table.\n" );
237 case 'recentchanges':
243 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
244 $dbw->delete( $table, [ $idField => $ids ], __METHOD__ );
246 $this->
outputStatus(
'Deleted ' . $dbw->affectedRows() .
" rows from $table.\n" );
249 case 'protected_titles':
253 $this->
outputStatus(
"Deleting invalid $table rows...\n" );
254 $affectedRowCount = 0;
255 foreach (
$res as $row ) {
256 $dbw->delete( $table,
257 [ $nsField => $row->ns, $titleField => $row->title ],
259 $affectedRowCount += $dbw->affectedRows();
262 $this->
outputStatus(
"Deleted $affectedRowCount rows from $table.\n" );
266 case 'templatelinks':
267 case 'categorylinks':
271 $this->
outputStatus(
"Queueing link update jobs for the pages in $idField...\n" );
272 foreach (
$res as $row ) {
278 $dbw->delete( $table,
279 [ $idField => $row->id, $nsField => $row->ns, $titleField => $row->title ],
284 $this->
outputStatus(
"Link update jobs have been added to the job queue.\n" );
298 return strtr( trim( $invalidTitle,
'_' ),
299 [
' ' =>
'_',
"\r" =>
'',
"\n" =>
'',
"\t" =>
'_' ] );