MediaWiki  1.28.1
cleanupTitles.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/cleanupTable.inc';
29 
35 class TitleCleanup extends TableCleanup {
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Script to clean up broken, unparseable titles' );
39  }
40 
44  protected function processRow( $row ) {
46  $display = Title::makeName( $row->page_namespace, $row->page_title );
47  $verified = $wgContLang->normalize( $display );
48  $title = Title::newFromText( $verified );
49 
50  if ( !is_null( $title )
51  && $title->canExist()
52  && $title->getNamespace() == $row->page_namespace
53  && $title->getDBkey() === $row->page_title
54  ) {
55  $this->progress( 0 ); // all is fine
56 
57  return;
58  }
59 
60  if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
61  $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
62  $this->progress( 0 );
63  } elseif ( is_null( $title ) ) {
64  $this->output( "page $row->page_id ($display) is illegal.\n" );
65  $this->moveIllegalPage( $row );
66  $this->progress( 1 );
67  } else {
68  $this->output( "page $row->page_id ($display) doesn't match self.\n" );
69  $this->moveInconsistentPage( $row, $title );
70  $this->progress( 1 );
71  }
72  }
73 
78  protected function fileExists( $name ) {
79  // XXX: Doesn't actually check for file existence, just presence of image record.
80  // This is reasonable, since cleanupImages.php only iterates over the image table.
81  $dbr = $this->getDB( DB_REPLICA );
82  $row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__ );
83 
84  return $row !== false;
85  }
86 
90  protected function moveIllegalPage( $row ) {
91  $legal = 'A-Za-z0-9_/\\\\-';
92  $legalized = preg_replace_callback( "!([^$legal])!",
93  [ $this, 'hexChar' ],
94  $row->page_title );
95  if ( $legalized == '.' ) {
96  $legalized = '(dot)';
97  }
98  if ( $legalized == '_' ) {
99  $legalized = '(space)';
100  }
101  $legalized = 'Broken/' . $legalized;
102 
103  $title = Title::newFromText( $legalized );
104  if ( is_null( $title ) ) {
105  $clean = 'Broken/id:' . $row->page_id;
106  $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
107  $title = Title::newFromText( $clean );
108  } elseif ( $title->exists() ) {
109  $clean = 'Broken/id:' . $row->page_id;
110  $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
111  $title = Title::newFromText( $clean );
112  }
113 
114  $dest = $title->getDBkey();
115  if ( $this->dryrun ) {
116  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
117  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
118  } else {
119  $this->output( "renaming $row->page_id ($row->page_namespace," .
120  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
121  $dbw = $this->getDB( DB_MASTER );
122  $dbw->update( 'page',
123  [ 'page_title' => $dest ],
124  [ 'page_id' => $row->page_id ],
125  __METHOD__ );
126  }
127  }
128 
133  protected function moveInconsistentPage( $row, $title ) {
134  if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
135  if ( $title->getInterwiki() || !$title->canExist() ) {
136  $prior = $title->getPrefixedDBkey();
137  } else {
138  $prior = $title->getDBkey();
139  }
140 
141  # Old cleanupTitles could move articles there. See bug 23147.
142  $ns = $row->page_namespace;
143  if ( $ns < 0 ) {
144  $ns = 0;
145  }
146 
147  # Namespace which no longer exists. Put the page in the main namespace
148  # since we don't have any idea of the old namespace name. See bug 68501.
149  if ( !MWNamespace::exists( $ns ) ) {
150  $ns = 0;
151  }
152 
153  $clean = 'Broken/' . $prior;
154  $verified = Title::makeTitleSafe( $ns, $clean );
155  if ( !$verified || $verified->exists() ) {
156  $blah = "Broken/id:" . $row->page_id;
157  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
158  $verified = Title::makeTitleSafe( $ns, $blah );
159  }
160  $title = $verified;
161  }
162  if ( is_null( $title ) ) {
163  $this->error( "Something awry; empty title.", true );
164  }
165  $ns = $title->getNamespace();
166  $dest = $title->getDBkey();
167 
168  if ( $this->dryrun ) {
169  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
170  "'$row->page_title') to ($ns,'$dest')\n" );
171  } else {
172  $this->output( "renaming $row->page_id ($row->page_namespace," .
173  "'$row->page_title') to ($ns,'$dest')\n" );
174  $dbw = $this->getDB( DB_MASTER );
175  $dbw->update( 'page',
176  [
177  'page_namespace' => $ns,
178  'page_title' => $dest
179  ],
180  [ 'page_id' => $row->page_id ],
181  __METHOD__ );
182  LinkCache::singleton()->clear();
183  }
184  }
185 }
186 
187 $maintClass = "TitleCleanup";
188 require_once RUN_MAINTENANCE_IF_MAIN;
progress($updated)
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
static exists($index)
Returns whether the specified namespace exists.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const DB_MASTER
Definition: defines.php:23
fileExists($name)
static singleton()
Get an instance of this class.
Definition: LinkCache.php:64
Generic class to cleanup a database table.
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
const NS_FILE
Definition: Defines.php:62
addDescription($text)
Set the description text.
moveIllegalPage($row)
output($out, $channel=null)
Throw some output to the user.
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
$maintClass
static makeName($ns, $title, $fragment= '', $interwiki= '', $canonicalNamespace=false)
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:725
error($err, $die=0)
Throw an error to the user.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
const DB_REPLICA
Definition: defines.php:22
Maintenance script to clean up broken, unparseable titles.
moveInconsistentPage($row, $title)
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300