phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
4 *
5 * Copyright © 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that checks for articles to fix after
31 * adding/deleting namespaces.
32 *
33 * @ingroup Maintenance
34 */
35 class NamespaceConflictChecker extends Maintenance {
36
37 /**
38 * @var DatabaseBase
39 */
40 protected $db;
41
42 public function __construct() {
43 parent::__construct();
44 $this->mDescription = "";
45 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
46 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
47 "<text> appended after the article name", false, true );
48 $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
49 "appended after the article name", false, true );
50 }
51
52 public function execute() {
53 global $wgTitle;
54
55 $this->db = wfGetDB( DB_MASTER );
56 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
57
58 $fix = $this->hasOption( 'fix' );
59 $suffix = $this->getOption( 'suffix', '' );
60 $prefix = $this->getOption( 'prefix', '' );
61 $key = intval( $this->getOption( 'key', 0 ) );
62
63 if ( $prefix ) {
64 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
65 } else {
66 $retval = $this->checkAll( $fix, $suffix );
67 }
68
69 if ( $retval ) {
70 $this->output( "\nLooks good!\n" );
71 } else {
72 $this->output( "\nOh noeees\n" );
73 }
74 }
75
76 /**
77 * @todo Document
78 * @param $fix Boolean: whether or not to fix broken entries
79 * @param $suffix String: suffix to append to renamed articles
80 *
81 * @return bool
82 */
83 private function checkAll( $fix, $suffix = '' ) {
84 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
85
86 $spaces = array();
87
88 // List interwikis first, so they'll be overridden
89 // by any conflicting local namespaces.
90 foreach ( $this->getInterwikiList() as $prefix ) {
91 $name = $wgContLang->ucfirst( $prefix );
92 $spaces[$name] = 0;
93 }
94
95 // Now pull in all canonical and alias namespaces...
96 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
97 // This includes $wgExtraNamespaces
98 if ( $name !== '' ) {
99 $spaces[$name] = $ns;
100 }
101 }
102 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
103 if ( $name !== '' ) {
104 $spaces[$name] = $ns;
105 }
106 }
107 foreach ( $wgNamespaceAliases as $name => $ns ) {
108 $spaces[$name] = $ns;
109 }
110 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
111 $spaces[$name] = $ns;
112 }
113
114 // We'll need to check for lowercase keys as well,
115 // since we're doing case-sensitive searches in the db.
116 foreach ( $spaces as $name => $ns ) {
117 $moreNames = array();
118 $moreNames[] = $wgContLang->uc( $name );
119 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
120 $moreNames[] = $wgContLang->ucwords( $name );
121 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
122 $moreNames[] = $wgContLang->ucwordbreaks( $name );
123 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
124 if ( !$wgCapitalLinks ) {
125 foreach ( $moreNames as $altName ) {
126 $moreNames[] = $wgContLang->lcfirst( $altName );
127 }
128 $moreNames[] = $wgContLang->lcfirst( $name );
129 }
130 foreach ( array_unique( $moreNames ) as $altName ) {
131 if ( $altName !== $name ) {
132 $spaces[$altName] = $ns;
133 }
134 }
135 }
136
137 ksort( $spaces );
138 asort( $spaces );
139
140 $ok = true;
141 foreach ( $spaces as $name => $ns ) {
142 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
143 }
144 return $ok;
145 }
146
147 /**
148 * Get the interwiki list
149 *
150 * @return Array
151 */
152 private function getInterwikiList() {
153 $result = Interwiki::getAllPrefixes();
154 $prefixes = array();
155 foreach ( $result as $row ) {
156 $prefixes[] = $row['iw_prefix'];
157 }
158 return $prefixes;
159 }
160
161 /**
162 * @todo Document
163 * @param $ns Integer: a namespace id
164 * @param $name String
165 * @param $fix Boolean: whether to fix broken entries
166 * @param $suffix String: suffix to append to renamed articles
167 * @return bool
168 */
169 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
170 $conflicts = $this->getConflicts( $ns, $name );
171 $count = count( $conflicts );
172 if ( $count == 0 ) {
173 return true;
174 }
175
176 $ok = true;
177 foreach ( $conflicts as $row ) {
178 $resolvable = $this->reportConflict( $row, $suffix );
179 $ok = $ok && $resolvable;
180 if ( $fix && ( $resolvable || $suffix != '' ) ) {
181 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
182 }
183 }
184 return $ok;
185 }
186
187 /**
188 * @todo Do this for real
189 * @param $key
190 * @param $prefix
191 * @param $fix
192 * @param $suffix string
193 * @return bool
194 */
195 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
196 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
197 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
198 }
199
200 /**
201 * Find pages in mainspace that have a prefix of the new namespace
202 * so we know titles that will need migrating
203 *
204 * @param $ns Integer: namespace id (id for new namespace?)
205 * @param $name String: prefix that is being made a namespace
206 *
207 * @return array
208 */
209 private function getConflicts( $ns, $name ) {
210 $page = 'page';
211 $table = $this->db->tableName( $page );
212
213 $prefix = $this->db->strencode( $name );
214 $encNamespace = $this->db->addQuotes( $ns );
215
216 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
217 if ( $ns == 0 ) {
218 // An interwiki; try an alternate encoding with '-' for ':'
219 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
220 }
221
222 $sql = "SELECT {$page}_id AS id,
223 {$page}_title AS oldtitle,
224 $encNamespace + {$page}_namespace AS namespace,
225 $titleSql AS title,
226 {$page}_namespace AS oldnamespace
227 FROM {$table}
228 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
229 AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
230
231 $result = $this->db->query( $sql, __METHOD__ );
232
233 $set = array();
234 foreach ( $result as $row ) {
235 $set[] = $row;
236 }
237 return $set;
238 }
239
240 /**
241 * Report any conflicts we find
242 *
243 * @return bool
244 */
245 private function reportConflict( $row, $suffix ) {
246 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
247 if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
248 // Title is also an illegal title...
249 // For the moment we'll let these slide to cleanupTitles or whoever.
250 $this->output( sprintf( "... %d (%d,\"%s\")\n",
251 $row->id,
252 $row->oldnamespace,
253 $row->oldtitle ) );
254 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
255 return false;
256 }
257
258 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
259 $row->id,
260 $row->oldnamespace,
261 $row->oldtitle,
262 $newTitle->getNamespace(),
263 $newTitle->getDBkey(),
264 $newTitle->getPrefixedText() ) );
265
266 $id = $newTitle->getArticleID();
267 if ( $id ) {
268 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
269 return false;
270 } else {
271 return true;
272 }
273 }
274
275 /**
276 * Resolve any conflicts
277 *
278 * @param $row Object: row from the page table to fix
279 * @param $resolvable Boolean
280 * @param $suffix String: suffix to append to the fixed page
281 * @return bool
282 */
283 private function resolveConflict( $row, $resolvable, $suffix ) {
284 if ( !$resolvable ) {
285 $this->output( "... *** old title {$row->title}\n" );
286 while ( true ) {
287 $row->title .= $suffix;
288 $this->output( "... *** new title {$row->title}\n" );
289 $title = Title::makeTitleSafe( $row->namespace, $row->title );
290 if ( !$title ) {
291 $this->output( "... !!! invalid title\n" );
292 return false;
293 }
294 $id = $title->getArticleID();
295 if ( $id ) {
296 $this->output( "... *** page exists with ID $id ***\n" );
297 } else {
298 break;
299 }
300 }
301 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
302 }
303 $this->resolveConflictOn( $row, 'page', 'page' );
304 return true;
305 }
306
307 /**
308 * Resolve a given conflict
309 *
310 * @param $row Object: row from the old broken entry
311 * @param $table String: table to update
312 * @param $prefix String: prefix for column name, like page or ar
313 * @return bool
314 */
315 private function resolveConflictOn( $row, $table, $prefix ) {
316 $this->output( "... resolving on $table... " );
317 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
318 $this->db->update( $table,
319 array(
320 "{$prefix}_namespace" => $newTitle->getNamespace(),
321 "{$prefix}_title" => $newTitle->getDBkey(),
322 ),
323 array(
324 // "{$prefix}_namespace" => 0,
325 // "{$prefix}_title" => $row->oldtitle,
326 "{$prefix}_id" => $row->id,
327 ),
328 __METHOD__ );
329 $this->output( "ok.\n" );
330 return true;
331 }
332 }
333
334 $maintClass = "NamespaceConflictChecker";
335 require_once RUN_MAINTENANCE_IF_MAIN;