MediaWiki  master
cleanupTitles.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/TableCleanup.php';
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->newSelectQueryBuilder()
86  ->select( '*' )
87  ->from( 'image' )
88  ->where( [ 'img_name' => $name ] )
89  ->caller( __METHOD__ )
90  ->fetchRow();
91 
92  return $row !== false;
93  }
94 
98  protected function moveIllegalPage( $row ) {
99  $legal = 'A-Za-z0-9_/\\\\-';
100  $legalized = preg_replace_callback( "!([^$legal])!",
101  [ $this, 'hexChar' ],
102  $row->page_title );
103  if ( $legalized == '.' ) {
104  $legalized = '(dot)';
105  }
106  if ( $legalized == '_' ) {
107  $legalized = '(space)';
108  }
109  $legalized = 'Broken/' . $legalized;
110 
111  $title = Title::newFromText( $legalized );
112  if ( $title === null ) {
113  $clean = 'Broken/id:' . $row->page_id;
114  $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
115  $title = Title::newFromText( $clean );
116  } elseif ( $title->exists() ) {
117  $clean = 'Broken/id:' . $row->page_id;
118  $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
119  $title = Title::newFromText( $clean );
120  }
121 
122  $dest = $title->getDBkey();
123  if ( $this->dryrun ) {
124  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
125  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
126  } else {
127  $this->output( "renaming $row->page_id ($row->page_namespace," .
128  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
129  $dbw = $this->getDB( DB_PRIMARY );
130  $dbw->update( 'page',
131  [ 'page_title' => $dest ],
132  [ 'page_id' => $row->page_id ],
133  __METHOD__ );
134  }
135  }
136 
141  protected function moveInconsistentPage( $row, Title $title ) {
142  if ( $title->exists( Title::READ_LATEST )
143  || $title->getInterwiki()
144  || !$title->canExist()
145  ) {
146  $titleImpossible = $title->getInterwiki() || !$title->canExist();
147  if ( $titleImpossible ) {
148  $prior = $title->getPrefixedDBkey();
149  } else {
150  $prior = $title->getDBkey();
151  }
152 
153  # Old cleanupTitles could move articles there. See T25147.
154  $ns = $row->page_namespace;
155  if ( $ns < 0 ) {
156  $ns = 0;
157  }
158 
159  # Namespace which no longer exists. Put the page in the main namespace
160  # since we don't have any idea of the old namespace name. See T70501.
161  if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
162  $ns = 0;
163  }
164 
165  if ( !$titleImpossible && !$title->exists() ) {
166  // Looks like the current title, after cleaning it up, is valid and available
167  $clean = $prior;
168  } else {
169  $clean = 'Broken/' . $prior;
170  }
171  $verified = Title::makeTitleSafe( $ns, $clean );
172  if ( !$verified || $verified->exists() ) {
173  $blah = "Broken/id:" . $row->page_id;
174  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
175  $verified = Title::makeTitleSafe( $ns, $blah );
176  }
177  $title = $verified;
178  }
179  if ( $title === null ) {
180  $this->fatalError( "Something awry; empty title." );
181  }
182  $ns = $title->getNamespace();
183  $dest = $title->getDBkey();
184 
185  if ( $this->dryrun ) {
186  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
187  "'$row->page_title') to ($ns,'$dest')\n" );
188  } else {
189  $this->output( "renaming $row->page_id ($row->page_namespace," .
190  "'$row->page_title') to ($ns,'$dest')\n" );
191  $dbw = $this->getDB( DB_PRIMARY );
192  $dbw->update( 'page',
193  [
194  'page_namespace' => $ns,
195  'page_title' => $dest
196  ],
197  [ 'page_id' => $row->page_id ],
198  __METHOD__ );
199  MediaWikiServices::getInstance()->getLinkCache()->clear();
200  }
201  }
202 }
203 
204 $maintClass = TitleCleanup::class;
205 require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition: Defines.php:70
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Generic class to cleanup a database table.
progress( $updated)
Maintenance script to clean up broken, unparseable titles.
moveIllegalPage( $row)
processRow( $row)
moveInconsistentPage( $row, Title $title)
__construct()
Default constructor.
fileExists( $name)
Represents a title within MediaWiki.
Definition: Title.php:49
static makeName( $ns, $title, $fragment='', $interwiki='', $canonicalNamespace=false)
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:854
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:664
$maintClass
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28