2 # Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 $options = array( 'fix', 'suffix', 'help' );
23 require_once( 'commandLine.inc' );
25 if(isset( $options['help'] ) ) {
27 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
28 --help : this help message
29 --fix : attempt to automatically fix errors
30 --suffix=<text> : dupes will be renamed with correct namespace with <text>
31 appended after the article name.
32 --prefix=<text> : Do an explicit check for the given title prefix
33 in place of the standard namespace list.
34 --verbose : Display output for checked namespaces without conflicts
40 class NamespaceConflictChecker
{
41 function NamespaceConflictChecker( $db, $verbose=false ) {
43 $this->verbose
= $verbose;
46 function checkAll( $fix, $suffix = '' ) {
47 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
48 global $wgCapitalLinks;
52 // List interwikis first, so they'll be overridden
53 // by any conflicting local namespaces.
54 foreach( $this->getInterwikiList() as $prefix ) {
55 $name = $wgContLang->ucfirst( $prefix );
59 // Now pull in all canonical and alias namespaces...
60 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
61 // This includes $wgExtraNamespaces
66 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
71 foreach( $wgNamespaceAliases as $name => $ns ) {
74 foreach( $wgContLang->namespaceAliases
as $name => $ns ) {
78 if( !$wgCapitalLinks ) {
79 // We'll need to check for lowercase keys as well,
80 // since we're doing case-sensitive searches in the db.
81 foreach( $spaces as $name => $ns ) {
82 $lcname = $wgContLang->lcfirst( $name );
83 $spaces[$lcname] = $ns;
90 foreach( $spaces as $name => $ns ) {
91 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
96 private function getInterwikiList() {
97 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
98 while( $row = $this->db
->fetchObject( $result ) ) {
99 $prefixes[] = $row->iw_prefix
;
101 $this->db
->freeResult( $result );
105 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
107 $header = "Checking interwiki prefix: \"$name\"\n";
109 $header = "Checking namespace $ns: \"$name\"\n";
112 $conflicts = $this->getConflicts( $ns, $name );
113 $count = count( $conflicts );
115 if( $this->verbose
) {
117 echo "... no conflicts detected!\n";
123 echo "... $count conflicts detected:\n";
125 foreach( $conflicts as $row ) {
126 $resolvable = $this->reportConflict( $row, $suffix );
127 $ok = $ok && $resolvable;
128 if( $fix && ( $resolvable ||
$suffix != '' ) ) {
129 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
136 * @todo: do this for reals
138 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
139 echo "Checking prefix \"$prefix\" vs namespace $key\n";
140 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
143 function getConflicts( $ns, $name ) {
145 $table = $this->db
->tableName( $page );
147 $prefix = $this->db
->strencode( $name );
148 $likeprefix = str_replace( '_', '\\_', $prefix);
149 $encNamespace = $this->db
->addQuotes( $ns );
151 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
153 // An interwiki; try an alternate encoding with '-' for ':'
154 $titleSql = "CONCAT('$prefix-',$titleSql)";
157 $sql = "SELECT {$page}_id AS id,
158 {$page}_title AS oldtitle,
159 $encNamespace AS namespace,
162 WHERE {$page}_namespace=0
163 AND {$page}_title LIKE '$likeprefix:%'";
165 $result = $this->db
->query( $sql, 'NamespaceConflictChecker::getConflicts' );
168 while( $row = $this->db
->fetchObject( $result ) ) {
171 $this->db
->freeResult( $result );
176 function reportConflict( $row, $suffix ) {
177 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
179 // Title is also an illegal title...
180 // For the moment we'll let these slide to cleanupTitles or whoever.
181 printf( "... %d (0,\"%s\")\n",
184 echo "... *** cannot resolve automatically; illegal title ***\n";
188 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
191 $newTitle->getNamespace(),
192 $newTitle->getDbKey(),
193 $newTitle->getPrefixedText() );
195 $id = $newTitle->getArticleId();
197 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
204 function resolveConflict( $row, $resolvable, $suffix ) {
206 $row->title
.= $suffix;
207 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
208 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
210 $tables = array( 'page' );
211 foreach( $tables as $table ) {
212 $this->resolveConflictOn( $row, $table );
217 function resolveConflictOn( $row, $table ) {
218 echo "... resolving on $table... ";
219 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
220 $this->db
->update( $table,
222 "{$table}_namespace" => $newTitle->getNamespace(),
223 "{$table}_title" => $newTitle->getDbKey(),
226 "{$table}_namespace" => 0,
227 "{$table}_title" => $row->oldtitle
,
238 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
240 $verbose = isset( $options['verbose'] );
241 $fix = isset( $options['fix'] );
242 $suffix = isset( $options['suffix'] ) ?
$options['suffix'] : '';
243 $prefix = isset( $options['prefix'] ) ?
$options['prefix'] : '';
244 $key = isset( $options['key'] ) ?
intval( $options['key'] ) : 0;
246 $dbw = wfGetDB( DB_MASTER
);
247 $duper = new NamespaceConflictChecker( $dbw, $verbose );
250 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
252 $retval = $duper->checkAll( $fix, $suffix );
256 echo "\nLooks good!\n";
259 echo "\nOh noeees\n";