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
22 * @ingroup Maintenance
25 $options = array( 'fix', 'suffix', 'help' );
28 require_once( 'commandLine.inc' );
30 if(isset( $options['help'] ) ) {
32 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
33 --help : this help message
34 --fix : attempt to automatically fix errors
35 --suffix=<text> : dupes will be renamed with correct namespace with <text>
36 appended after the article name.
37 --prefix=<text> : Do an explicit check for the given title prefix
38 in place of the standard namespace list.
39 --verbose : Display output for checked namespaces without conflicts
40 --wiki=<wiki> : enter the wiki database to edit
45 class NamespaceConflictChecker
{
46 function NamespaceConflictChecker( $db, $verbose=false ) {
48 $this->verbose
= $verbose;
51 function checkAll( $fix, $suffix = '' ) {
52 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
53 global $wgCapitalLinks;
57 // List interwikis first, so they'll be overridden
58 // by any conflicting local namespaces.
59 foreach( $this->getInterwikiList() as $prefix ) {
60 $name = $wgContLang->ucfirst( $prefix );
64 // Now pull in all canonical and alias namespaces...
65 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
66 // This includes $wgExtraNamespaces
71 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
76 foreach( $wgNamespaceAliases as $name => $ns ) {
79 foreach( $wgContLang->namespaceAliases
as $name => $ns ) {
83 // We'll need to check for lowercase keys as well,
84 // since we're doing case-sensitive searches in the db.
85 foreach( $spaces as $name => $ns ) {
87 $moreNames[] = $wgContLang->uc( $name );
88 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
89 $moreNames[] = $wgContLang->ucwords( $name );
90 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
91 $moreNames[] = $wgContLang->ucwordbreaks( $name );
92 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
93 if( !$wgCapitalLinks ) {
94 foreach( $moreNames as $altName ) {
95 $moreNames[] = $wgContLang->lcfirst( $altName );
97 $moreNames[] = $wgContLang->lcfirst( $name );
99 foreach( array_unique( $moreNames ) as $altName ) {
100 if( $altName !== $name ) {
101 $spaces[$altName] = $ns;
110 foreach( $spaces as $name => $ns ) {
111 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
116 private function getInterwikiList() {
117 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
119 while( $row = $this->db
->fetchObject( $result ) ) {
120 $prefixes[] = $row->iw_prefix
;
122 $this->db
->freeResult( $result );
126 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
128 $header = "Checking interwiki prefix: \"$name\"\n";
130 $header = "Checking namespace $ns: \"$name\"\n";
133 $conflicts = $this->getConflicts( $ns, $name );
134 $count = count( $conflicts );
136 if( $this->verbose
) {
138 echo "... no conflicts detected!\n";
144 echo "... $count conflicts detected:\n";
146 foreach( $conflicts as $row ) {
147 $resolvable = $this->reportConflict( $row, $suffix );
148 $ok = $ok && $resolvable;
149 if( $fix && ( $resolvable ||
$suffix != '' ) ) {
150 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
157 * @todo: do this for reals
159 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
160 echo "Checking prefix \"$prefix\" vs namespace $key\n";
161 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
164 function getConflicts( $ns, $name ) {
166 $table = $this->db
->tableName( $page );
168 $prefix = $this->db
->strencode( $name );
169 $likeprefix = str_replace( '_', '\\_', $prefix);
170 $encNamespace = $this->db
->addQuotes( $ns );
172 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
174 // An interwiki; try an alternate encoding with '-' for ':'
175 $titleSql = "CONCAT('$prefix-',$titleSql)";
178 $sql = "SELECT {$page}_id AS id,
179 {$page}_title AS oldtitle,
180 $encNamespace AS namespace,
183 WHERE {$page}_namespace=0
184 AND {$page}_title LIKE '$likeprefix:%'";
186 $result = $this->db
->query( $sql, 'NamespaceConflictChecker::getConflicts' );
189 while( $row = $this->db
->fetchObject( $result ) ) {
192 $this->db
->freeResult( $result );
197 function reportConflict( $row, $suffix ) {
198 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
199 if( is_null($newTitle) ||
!$newTitle->canExist() ) {
200 // Title is also an illegal title...
201 // For the moment we'll let these slide to cleanupTitles or whoever.
202 printf( "... %d (0,\"%s\")\n",
205 echo "... *** cannot resolve automatically; illegal title ***\n";
209 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
212 $newTitle->getNamespace(),
213 $newTitle->getDBkey(),
214 $newTitle->getPrefixedText() );
216 $id = $newTitle->getArticleId();
218 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
225 function resolveConflict( $row, $resolvable, $suffix ) {
227 echo "... *** old title {$row->title}\n";
229 $row->title
.= $suffix;
230 echo "... *** new title {$row->title}\n";
231 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
233 echo "... !!! invalid title\n";
236 if ( $id = $title->getArticleId() ) {
237 echo "... *** page exists with ID $id ***\n";
242 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
244 $tables = array( 'page' );
245 foreach( $tables as $table ) {
246 $this->resolveConflictOn( $row, $table );
251 function resolveConflictOn( $row, $table ) {
252 echo "... resolving on $table... ";
253 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
254 $this->db
->update( $table,
256 "{$table}_namespace" => $newTitle->getNamespace(),
257 "{$table}_title" => $newTitle->getDBkey(),
260 "{$table}_namespace" => 0,
261 "{$table}_title" => $row->oldtitle
,
272 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
274 $verbose = isset( $options['verbose'] );
275 $fix = isset( $options['fix'] );
276 $suffix = isset( $options['suffix'] ) ?
$options['suffix'] : '';
277 $prefix = isset( $options['prefix'] ) ?
$options['prefix'] : '';
278 $key = isset( $options['key'] ) ?
intval( $options['key'] ) : 0;
280 $dbw = wfGetDB( DB_MASTER
);
281 $duper = new NamespaceConflictChecker( $dbw, $verbose );
284 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
286 $retval = $duper->checkAll( $fix, $suffix );
290 echo "\nLooks good!\n";
293 echo "\nOh noeees\n";