3 * Check for articles to fix after adding/deleting namespaces
5 * Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @ingroup Maintenance
26 require_once( dirname( __FILE__
) . '/Maintenance.php' );
28 class NamespaceConflictChecker
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->mDescription
= "";
38 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
39 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
40 "<text> appended after the article name", false, true );
41 $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
42 "appended after the article name", false, true );
45 public function execute() {
48 $this->db
= wfGetDB( DB_MASTER
);
49 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
51 $fix = $this->hasOption( 'fix' );
52 $suffix = $this->getOption( 'suffix', '' );
53 $prefix = $this->getOption( 'prefix', '' );
54 $key = intval( $this->getOption( 'key', 0 ) );
57 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
59 $retval = $this->checkAll( $fix, $suffix );
63 $this->output( "\nLooks good!\n" );
65 $this->output( "\nOh noeees\n" );
71 * @param $fix Boolean: whether or not to fix broken entries
72 * @param $suffix String: suffix to append to renamed articles
76 private function checkAll( $fix, $suffix = '' ) {
77 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
81 // List interwikis first, so they'll be overridden
82 // by any conflicting local namespaces.
83 foreach ( $this->getInterwikiList() as $prefix ) {
84 $name = $wgContLang->ucfirst( $prefix );
88 // Now pull in all canonical and alias namespaces...
89 foreach ( MWNamespace
::getCanonicalNamespaces() as $ns => $name ) {
90 // This includes $wgExtraNamespaces
95 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
100 foreach ( $wgNamespaceAliases as $name => $ns ) {
101 $spaces[$name] = $ns;
103 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
104 $spaces[$name] = $ns;
107 // We'll need to check for lowercase keys as well,
108 // since we're doing case-sensitive searches in the db.
109 foreach ( $spaces as $name => $ns ) {
110 $moreNames = array();
111 $moreNames[] = $wgContLang->uc( $name );
112 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
113 $moreNames[] = $wgContLang->ucwords( $name );
114 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
115 $moreNames[] = $wgContLang->ucwordbreaks( $name );
116 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
117 if ( !$wgCapitalLinks ) {
118 foreach ( $moreNames as $altName ) {
119 $moreNames[] = $wgContLang->lcfirst( $altName );
121 $moreNames[] = $wgContLang->lcfirst( $name );
123 foreach ( array_unique( $moreNames ) as $altName ) {
124 if ( $altName !== $name ) {
125 $spaces[$altName] = $ns;
134 foreach ( $spaces as $name => $ns ) {
135 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
141 * Get the interwiki list
143 * @todo Needs to respect interwiki cache!
146 private function getInterwikiList() {
147 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
149 foreach ( $result as $row ) {
150 $prefixes[] = $row->iw_prefix
;
157 * @param $ns Integer: a namespace id
158 * @param $name String
159 * @param $fix Boolean: whether to fix broken entries
160 * @param $suffix String: suffix to append to renamed articles
163 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
164 $conflicts = $this->getConflicts( $ns, $name );
165 $count = count( $conflicts );
171 foreach ( $conflicts as $row ) {
172 $resolvable = $this->reportConflict( $row, $suffix );
173 $ok = $ok && $resolvable;
174 if ( $fix && ( $resolvable ||
$suffix != '' ) ) {
175 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
182 * @todo: do this for reals
186 * @param $suffix string
189 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
190 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
191 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
195 * Find pages in mainspace that have a prefix of the new namespace
196 * so we know titles that will need migrating
198 * @param $ns Integer: namespace id (id for new namespace?)
199 * @param $name String: prefix that is being made a namespace
203 private function getConflicts( $ns, $name ) {
205 $table = $this->db
->tableName( $page );
207 $prefix = $this->db
->strencode( $name );
208 $encNamespace = $this->db
->addQuotes( $ns );
210 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
212 // An interwiki; try an alternate encoding with '-' for ':'
213 $titleSql = $this->db
->buildConcat( array( "'$prefix-'", $titleSql ) );
216 $sql = "SELECT {$page}_id AS id,
217 {$page}_title AS oldtitle,
218 $encNamespace + {$page}_namespace AS namespace,
220 {$page}_namespace AS oldnamespace
222 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
223 AND {$page}_title " . $this->db
->buildLike( $name . ':', $this->db
->anyString() );
225 $result = $this->db
->query( $sql, __METHOD__
);
228 foreach ( $result as $row ) {
235 * Report any conflicts we find
239 private function reportConflict( $row, $suffix ) {
240 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
241 if ( is_null( $newTitle ) ||
!$newTitle->canExist() ) {
242 // Title is also an illegal title...
243 // For the moment we'll let these slide to cleanupTitles or whoever.
244 $this->output( sprintf( "... %d (%d,\"%s\")\n",
248 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
252 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
256 $newTitle->getNamespace(),
257 $newTitle->getDBkey(),
258 $newTitle->getPrefixedText() ) );
260 $id = $newTitle->getArticleID();
262 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
270 * Resolve any conflicts
272 * @param $row Object: row from the page table to fix
273 * @param $resolvable Boolean
274 * @param $suffix String: suffix to append to the fixed page
277 private function resolveConflict( $row, $resolvable, $suffix ) {
278 if ( !$resolvable ) {
279 $this->output( "... *** old title {$row->title}\n" );
281 $row->title
.= $suffix;
282 $this->output( "... *** new title {$row->title}\n" );
283 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
285 $this->output( "... !!! invalid title\n" );
288 $id = $title->getArticleID();
290 $this->output( "... *** page exists with ID $id ***\n" );
295 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
297 $this->resolveConflictOn( $row, 'page', 'page' );
302 * Resolve a given conflict
304 * @param $row Object: row from the old broken entry
305 * @param $table String: table to update
306 * @param $prefix String: prefix for column name, like page or ar
309 private function resolveConflictOn( $row, $table, $prefix ) {
310 $this->output( "... resolving on $table... " );
311 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
312 $this->db
->update( $table,
314 "{$prefix}_namespace" => $newTitle->getNamespace(),
315 "{$prefix}_title" => $newTitle->getDBkey(),
318 // "{$prefix}_namespace" => 0,
319 // "{$prefix}_title" => $row->oldtitle,
320 "{$prefix}_id" => $row->id
,
323 $this->output( "ok.\n" );
328 $maintClass = "NamespaceConflictChecker";
329 require_once( RUN_MAINTENANCE_IF_MAIN
);