MediaWiki  1.23.6
cleanupTitles.php
Go to the documentation of this file.
1 <?php
32 require_once __DIR__ . '/cleanupTable.inc';
33 
39 class TitleCleanup extends TableCleanup {
40  public function __construct() {
41  parent::__construct();
42  $this->mDescription = "Script to clean up broken, unparseable titles";
43  }
44 
48  protected function processRow( $row ) {
50  $display = Title::makeName( $row->page_namespace, $row->page_title );
51  $verified = $wgContLang->normalize( $display );
52  $title = Title::newFromText( $verified );
53 
54  if ( !is_null( $title )
55  && $title->canExist()
56  && $title->getNamespace() == $row->page_namespace
57  && $title->getDBkey() === $row->page_title
58  ) {
59  $this->progress( 0 ); // all is fine
60 
61  return;
62  }
63 
64  if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
65  $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
66  $this->progress( 0 );
67  } elseif ( is_null( $title ) ) {
68  $this->output( "page $row->page_id ($display) is illegal.\n" );
69  $this->moveIllegalPage( $row );
70  $this->progress( 1 );
71  } else {
72  $this->output( "page $row->page_id ($display) doesn't match self.\n" );
73  $this->moveInconsistentPage( $row, $title );
74  $this->progress( 1 );
75  }
76  }
77 
82  protected function fileExists( $name ) {
83  // XXX: Doesn't actually check for file existence, just presence of image record.
84  // This is reasonable, since cleanupImages.php only iterates over the image table.
85  $dbr = wfGetDB( DB_SLAVE );
86  $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
87 
88  return $row !== false;
89  }
90 
94  protected function moveIllegalPage( $row ) {
95  $legal = 'A-Za-z0-9_/\\\\-';
96  $legalized = preg_replace_callback( "!([^$legal])!",
97  array( &$this, 'hexChar' ),
98  $row->page_title );
99  if ( $legalized == '.' ) {
100  $legalized = '(dot)';
101  }
102  if ( $legalized == '_' ) {
103  $legalized = '(space)';
104  }
105  $legalized = 'Broken/' . $legalized;
106 
107  $title = Title::newFromText( $legalized );
108  if ( is_null( $title ) ) {
109  $clean = 'Broken/id:' . $row->page_id;
110  $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
111  $title = Title::newFromText( $clean );
112  } elseif ( $title->exists() ) {
113  $clean = 'Broken/id:' . $row->page_id;
114  $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
115  $title = Title::newFromText( $clean );
116  }
117 
118  $dest = $title->getDBkey();
119  if ( $this->dryrun ) {
120  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
121  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
122  } else {
123  $this->output( "renaming $row->page_id ($row->page_namespace," .
124  "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
125  $dbw = wfGetDB( DB_MASTER );
126  $dbw->update( 'page',
127  array( 'page_title' => $dest ),
128  array( 'page_id' => $row->page_id ),
129  __METHOD__ );
130  }
131  }
132 
137  protected function moveInconsistentPage( $row, $title ) {
138  if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
139  if ( $title->getInterwiki() || !$title->canExist() ) {
140  $prior = $title->getPrefixedDBkey();
141  } else {
142  $prior = $title->getDBkey();
143  }
144 
145  # Old cleanupTitles could move articles there. See bug 23147.
146  $ns = $row->page_namespace;
147  if ( $ns < 0 ) {
148  $ns = 0;
149  }
150 
151  # Namespace which no longer exists. Put the page in the main namespace
152  # since we don't have any idea of the old namespace name. See bug 68501.
153  if ( !MWNamespace::exists( $ns ) ) {
154  $ns = 0;
155  }
156 
157  $clean = 'Broken/' . $prior;
158  $verified = Title::makeTitleSafe( $ns, $clean );
159  if ( $verified->exists() ) {
160  $blah = "Broken/id:" . $row->page_id;
161  $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
162  $verified = Title::makeTitleSafe( $ns, $blah );
163  }
164  $title = $verified;
165  }
166  if ( is_null( $title ) ) {
167  $this->error( "Something awry; empty title.", true );
168  }
169  $ns = $title->getNamespace();
170  $dest = $title->getDBkey();
171 
172  if ( $this->dryrun ) {
173  $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
174  "'$row->page_title') to ($ns,'$dest')\n" );
175  } else {
176  $this->output( "renaming $row->page_id ($row->page_namespace," .
177  "'$row->page_title') to ($ns,'$dest')\n" );
178  $dbw = wfGetDB( DB_MASTER );
179  $dbw->update( 'page',
180  array(
181  'page_namespace' => $ns,
182  'page_title' => $dest
183  ),
184  array( 'page_id' => $row->page_id ),
185  __METHOD__ );
186  LinkCache::singleton()->clear();
187  }
188  }
189 }
190 
191 $maintClass = "TitleCleanup";
192 require_once RUN_MAINTENANCE_IF_MAIN;
TitleCleanup\__construct
__construct()
Default constructor.
Definition: cleanupTitles.php:40
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:189
TitleCleanup
Maintenance script to clean up broken, unparseable titles.
Definition: cleanupTitles.php:39
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
TitleCleanup\processRow
processRow( $row)
Definition: cleanupTitles.php:48
TitleCleanup\moveInconsistentPage
moveInconsistentPage( $row, $title)
Definition: cleanupTitles.php:137
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
NS_FILE
const NS_FILE
Definition: Defines.php:85
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$wgContLang
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 content language as $wgContLang
Definition: design.txt:56
TitleCleanup\fileExists
fileExists( $name)
Definition: cleanupTitles.php:82
$dbr
$dbr
Definition: testCompression.php:48
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:74
TitleCleanup\moveIllegalPage
moveIllegalPage( $row)
Definition: cleanupTitles.php:94
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$maintClass
$maintClass
Definition: cleanupTitles.php:191
MWNamespace\exists
static exists( $index)
Returns whether the specified namespace exists.
Definition: Namespace.php:171
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title\makeName
static makeName( $ns, $title, $fragment='', $interwiki='')
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:702
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
LinkCache\singleton
static & singleton()
Get an instance of this class.
Definition: LinkCache.php:49