MediaWiki  master
cleanupTitles.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/cleanupTable.inc';
31 
37 class TitleCleanup extends TableCleanup {
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Script to clean up broken, unparseable titles' );
41  $this->setBatchSize( 1000 );
42  }
43 
47  protected function processRow( $row ) {
48  $display = Title::makeName( $row->page_namespace, $row->page_title );
49  $verified = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $display );
50  $title = Title::newFromText( $verified );
51 
52  if ( $title !== null
53  && $title->canExist()
54  && $title->getNamespace() == $row->page_namespace
55  && $title->getDBkey() === $row->page_title
56  ) {
57  // all is fine
58  $this->progress( 0 );
59 
60  return;
61  }
62 
63  if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
64  $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
65  $this->progress( 0 );
66  } elseif ( $title === null ) {
67  $this->output( "page $row->page_id ($display) is illegal.\n" );
68  $this->moveIllegalPage( $row );
69  $this->progress( 1 );
70  } else {
71  $this->output( "page $row->page_id ($display) doesn't match self.\n" );
72  $this->moveInconsistentPage( $row, $title );
73  $this->progress( 1 );
74  }
75  }
76 
81  protected function fileExists( $name ) {
82  // XXX: Doesn't actually check for file existence, just presence of image record.
83  // This is reasonable, since cleanupImages.php only iterates over the image table.
84  $dbr = $this->getDB( DB_REPLICA );
85  $row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__ );
86 
87  return $row !== false;
88  }
89 
93  protected function moveIllegalPage( $row ) {
94  $legal = 'A-Za-z0-9_/\\\\-';
95  $legalized = preg_replace_callback( "!([^$legal])!",
96  [ $this, 'hexChar' ],
97  $row->page_title );
98  if ( $legalized == '.' ) {
99  $legalized = '(dot)';
100  }
101  if ( $legalized == '_' ) {
102  $legalized = '(space)';
103  }
104  $legalized = 'Broken/' . $legalized;
105 
106  $title = Title::newFromText( $legalized );
107  if ( $title === null ) {
108  $clean = 'Broken/id:' . $row->page_id;
109  $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
110  $title = Title::newFromText( $clean );
111  } elseif ( $title->exists() ) {
112  $clean = 'Broken/id:' . $row->page_id;
113  $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
114  $title = Title::newFromText( $clean );
115  }
116 
117  $dest = $title->getDBkey();
118  if ( $this->dryrun ) {
119  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
120  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
121  } else {
122  $this->output( "renaming $row->page_id ($row->page_namespace," .
123  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
124  $dbw = $this->getDB( DB_MASTER );
125  $dbw->update( 'page',
126  [ 'page_title' => $dest ],
127  [ 'page_id' => $row->page_id ],
128  __METHOD__ );
129  }
130  }
131 
136  protected function moveInconsistentPage( $row, Title $title ) {
137  if ( $title->exists( Title::READ_LATEST )
138  || $title->getInterwiki()
139  || !$title->canExist()
140  ) {
141  $titleImpossible = $title->getInterwiki() || !$title->canExist();
142  if ( $titleImpossible ) {
143  $prior = $title->getPrefixedDBkey();
144  } else {
145  $prior = $title->getDBkey();
146  }
147 
148  # Old cleanupTitles could move articles there. See T25147.
149  $ns = $row->page_namespace;
150  if ( $ns < 0 ) {
151  $ns = 0;
152  }
153 
154  # Namespace which no longer exists. Put the page in the main namespace
155  # since we don't have any idea of the old namespace name. See T70501.
156  if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
157  $ns = 0;
158  }
159 
160  if ( !$titleImpossible && !$title->exists() ) {
161  // Looks like the current title, after cleaning it up, is valid and available
162  $clean = $prior;
163  } else {
164  $clean = 'Broken/' . $prior;
165  }
166  $verified = Title::makeTitleSafe( $ns, $clean );
167  if ( !$verified || $verified->exists() ) {
168  $blah = "Broken/id:" . $row->page_id;
169  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
170  $verified = Title::makeTitleSafe( $ns, $blah );
171  }
172  $title = $verified;
173  }
174  if ( $title === null ) {
175  $this->fatalError( "Something awry; empty title." );
176  }
177  $ns = $title->getNamespace();
178  $dest = $title->getDBkey();
179 
180  if ( $this->dryrun ) {
181  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
182  "'$row->page_title') to ($ns,'$dest')\n" );
183  } else {
184  $this->output( "renaming $row->page_id ($row->page_namespace," .
185  "'$row->page_title') to ($ns,'$dest')\n" );
186  $dbw = $this->getDB( DB_MASTER );
187  $dbw->update( 'page',
188  [
189  'page_namespace' => $ns,
190  'page_title' => $dest
191  ],
192  [ 'page_id' => $row->page_id ],
193  __METHOD__ );
194  MediaWikiServices::getInstance()->getLinkCache()->clear();
195  }
196  }
197 }
198 
199 $maintClass = TitleCleanup::class;
200 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
TitleCleanup\__construct
__construct()
Default constructor.
Definition: cleanupTitles.php:38
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
TitleCleanup
Maintenance script to clean up broken, unparseable titles.
Definition: cleanupTitles.php:37
Title\makeName
static makeName( $ns, $title, $fragment='', $interwiki='', $canonicalNamespace=false)
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:820
TitleCleanup\processRow
processRow( $row)
Definition: cleanupTitles.php:47
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:480
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
NS_FILE
const NS_FILE
Definition: Defines.php:75
TitleCleanup\fileExists
fileExists( $name)
Definition: cleanupTitles.php:81
$dbr
$dbr
Definition: testCompression.php:54
TitleCleanup\moveInconsistentPage
moveInconsistentPage( $row, Title $title)
Definition: cleanupTitles.php:136
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:78
TitleCleanup\moveIllegalPage
moveIllegalPage( $row)
Definition: cleanupTitles.php:93
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:618
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
$maintClass
$maintClass
Definition: cleanupTitles.php:199
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:370