MediaWiki  1.33.0
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->batchSize = 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 ( !is_null( $title )
53  && $title->canExist()
54  && $title->getNamespace() == $row->page_namespace
55  && $title->getDBkey() === $row->page_title
56  ) {
57  $this->progress( 0 ); // all is fine
58 
59  return;
60  }
61 
62  if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
63  $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
64  $this->progress( 0 );
65  } elseif ( is_null( $title ) ) {
66  $this->output( "page $row->page_id ($display) is illegal.\n" );
67  $this->moveIllegalPage( $row );
68  $this->progress( 1 );
69  } else {
70  $this->output( "page $row->page_id ($display) doesn't match self.\n" );
71  $this->moveInconsistentPage( $row, $title );
72  $this->progress( 1 );
73  }
74  }
75 
80  protected function fileExists( $name ) {
81  // XXX: Doesn't actually check for file existence, just presence of image record.
82  // This is reasonable, since cleanupImages.php only iterates over the image table.
83  $dbr = $this->getDB( DB_REPLICA );
84  $row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__ );
85 
86  return $row !== false;
87  }
88 
92  protected function moveIllegalPage( $row ) {
93  $legal = 'A-Za-z0-9_/\\\\-';
94  $legalized = preg_replace_callback( "!([^$legal])!",
95  [ $this, 'hexChar' ],
96  $row->page_title );
97  if ( $legalized == '.' ) {
98  $legalized = '(dot)';
99  }
100  if ( $legalized == '_' ) {
101  $legalized = '(space)';
102  }
103  $legalized = 'Broken/' . $legalized;
104 
105  $title = Title::newFromText( $legalized );
106  if ( is_null( $title ) ) {
107  $clean = 'Broken/id:' . $row->page_id;
108  $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
109  $title = Title::newFromText( $clean );
110  } elseif ( $title->exists() ) {
111  $clean = 'Broken/id:' . $row->page_id;
112  $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
113  $title = Title::newFromText( $clean );
114  }
115 
116  $dest = $title->getDBkey();
117  if ( $this->dryrun ) {
118  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
119  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
120  } else {
121  $this->output( "renaming $row->page_id ($row->page_namespace," .
122  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
123  $dbw = $this->getDB( DB_MASTER );
124  $dbw->update( 'page',
125  [ 'page_title' => $dest ],
126  [ 'page_id' => $row->page_id ],
127  __METHOD__ );
128  }
129  }
130 
135  protected function moveInconsistentPage( $row, Title $title ) {
136  if ( $title->exists( Title::GAID_FOR_UPDATE )
137  || $title->getInterwiki()
138  || !$title->canExist()
139  ) {
140  $titleImpossible = $title->getInterwiki() || !$title->canExist();
141  if ( $titleImpossible ) {
142  $prior = $title->getPrefixedDBkey();
143  } else {
144  $prior = $title->getDBkey();
145  }
146 
147  # Old cleanupTitles could move articles there. See T25147.
148  $ns = $row->page_namespace;
149  if ( $ns < 0 ) {
150  $ns = 0;
151  }
152 
153  # Namespace which no longer exists. Put the page in the main namespace
154  # since we don't have any idea of the old namespace name. See T70501.
155  if ( !MWNamespace::exists( $ns ) ) {
156  $ns = 0;
157  }
158 
159  if ( !$titleImpossible && !$title->exists() ) {
160  // Looks like the current title, after cleaning it up, is valid and available
161  $clean = $prior;
162  } else {
163  $clean = 'Broken/' . $prior;
164  }
165  $verified = Title::makeTitleSafe( $ns, $clean );
166  if ( !$verified || $verified->exists() ) {
167  $blah = "Broken/id:" . $row->page_id;
168  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
169  $verified = Title::makeTitleSafe( $ns, $blah );
170  }
171  $title = $verified;
172  }
173  if ( is_null( $title ) ) {
174  $this->fatalError( "Something awry; empty title." );
175  }
176  $ns = $title->getNamespace();
177  $dest = $title->getDBkey();
178 
179  if ( $this->dryrun ) {
180  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
181  "'$row->page_title') to ($ns,'$dest')\n" );
182  } else {
183  $this->output( "renaming $row->page_id ($row->page_namespace," .
184  "'$row->page_title') to ($ns,'$dest')\n" );
185  $dbw = $this->getDB( DB_MASTER );
186  $dbw->update( 'page',
187  [
188  'page_namespace' => $ns,
189  'page_title' => $dest
190  ],
191  [ 'page_id' => $row->page_id ],
192  __METHOD__ );
193  MediaWikiServices::getInstance()->getLinkCache()->clear();
194  }
195  }
196 }
197 
199 require_once RUN_MAINTENANCE_IF_MAIN;
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:306
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:788
TitleCleanup\processRow
processRow( $row)
Definition: cleanupTitles.php:47
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
NS_FILE
const NS_FILE
Definition: Defines.php:70
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
TitleCleanup\fileExists
fileExists( $name)
Definition: cleanupTitles.php:80
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
TitleCleanup\moveInconsistentPage
moveInconsistentPage( $row, Title $title)
Definition: cleanupTitles.php:135
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
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:74
TitleCleanup\moveIllegalPage
moveIllegalPage( $row)
Definition: cleanupTitles.php:92
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
$maintClass
$maintClass
Definition: cleanupTitles.php:198
Title\GAID_FOR_UPDATE
const GAID_FOR_UPDATE
Used to be GAID_FOR_UPDATE define.
Definition: Title.php:55
MWNamespace\exists
static exists( $index)
Returns whether the specified namespace exists.
Definition: MWNamespace.php:183
Title
Represents a title within MediaWiki.
Definition: Title.php:40
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23