MediaWiki REL1_30
cleanupTitles.php
Go to the documentation of this file.
1<?php
29
30require_once __DIR__ . '/cleanupTable.inc';
31
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Script to clean up broken, unparseable titles' );
41 }
42
46 protected function processRow( $row ) {
47 global $wgContLang;
48 $display = Title::makeName( $row->page_namespace, $row->page_title );
49 $verified = $wgContLang->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 if ( $title->getInterwiki() || !$title->canExist() ) {
141 $prior = $title->getPrefixedDBkey();
142 } else {
143 $prior = $title->getDBkey();
144 }
145
146 # Old cleanupTitles could move articles there. See T25147.
147 $ns = $row->page_namespace;
148 if ( $ns < 0 ) {
149 $ns = 0;
150 }
151
152 # Namespace which no longer exists. Put the page in the main namespace
153 # since we don't have any idea of the old namespace name. See T70501.
154 if ( !MWNamespace::exists( $ns ) ) {
155 $ns = 0;
156 }
157
158 $clean = 'Broken/' . $prior;
159 $verified = Title::makeTitleSafe( $ns, $clean );
160 if ( !$verified || $verified->exists() ) {
161 $blah = "Broken/id:" . $row->page_id;
162 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
163 $verified = Title::makeTitleSafe( $ns, $blah );
164 }
165 $title = $verified;
166 }
167 if ( is_null( $title ) ) {
168 $this->error( "Something awry; empty title.", true );
169 }
170 $ns = $title->getNamespace();
171 $dest = $title->getDBkey();
172
173 if ( $this->dryrun ) {
174 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
175 "'$row->page_title') to ($ns,'$dest')\n" );
176 } else {
177 $this->output( "renaming $row->page_id ($row->page_namespace," .
178 "'$row->page_title') to ($ns,'$dest')\n" );
179 $dbw = $this->getDB( DB_MASTER );
180 $dbw->update( 'page',
181 [
182 'page_namespace' => $ns,
183 'page_title' => $dest
184 ],
185 [ 'page_id' => $row->page_id ],
186 __METHOD__ );
187 MediaWikiServices::getInstance()->getLinkCache()->clear();
188 }
189 }
190}
191
192$maintClass = "TitleCleanup";
193require_once RUN_MAINTENANCE_IF_MAIN;
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
addDescription( $text)
Set the description text.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Generic class to cleanup a database table.
progress( $updated)
Maintenance script to clean up broken, unparseable titles.
moveIllegalPage( $row)
moveInconsistentPage( $row, Title $title)
__construct()
Default constructor.
fileExists( $name)
Represents a title within MediaWiki.
Definition Title.php:39
$maintClass
if(! $regexes) $dbr
Definition cleanup.php:94
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:57
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
const NS_FILE
Definition Defines.php:71
require_once RUN_MAINTENANCE_IF_MAIN
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26