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 // We'll need to check for lowercase keys as well,
79 // since we're doing case-sensitive searches in the db.
80 foreach( $spaces as $name => $ns ) {
82 $moreNames[] = $wgContLang->uc( $name );
83 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
84 $moreNames[] = $wgContLang->ucwords( $name );
85 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
86 $moreNames[] = $wgContLang->ucwordbreaks( $name );
87 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
88 if( !$wgCapitalLinks ) {
89 foreach( $moreNames as $altName ) {
90 $moreNames[] = $wgContLang->lcfirst( $altName );
92 $moreNames[] = $wgContLang->lcfirst( $name );
94 foreach( array_unique( $moreNames ) as $altName ) {
95 if( $altName !== $name ) {
96 $spaces[$altName] = $ns;
105 foreach( $spaces as $name => $ns ) {
106 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
111 private function getInterwikiList() {
112 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
113 while( $row = $this->db
->fetchObject( $result ) ) {
114 $prefixes[] = $row->iw_prefix
;
116 $this->db
->freeResult( $result );
120 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
122 $header = "Checking interwiki prefix: \"$name\"\n";
124 $header = "Checking namespace $ns: \"$name\"\n";
127 $conflicts = $this->getConflicts( $ns, $name );
128 $count = count( $conflicts );
130 if( $this->verbose
) {
132 echo "... no conflicts detected!\n";
138 echo "... $count conflicts detected:\n";
140 foreach( $conflicts as $row ) {
141 $resolvable = $this->reportConflict( $row, $suffix );
142 $ok = $ok && $resolvable;
143 if( $fix && ( $resolvable ||
$suffix != '' ) ) {
144 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
151 * @todo: do this for reals
153 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
154 echo "Checking prefix \"$prefix\" vs namespace $key\n";
155 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
158 function getConflicts( $ns, $name ) {
160 $table = $this->db
->tableName( $page );
162 $prefix = $this->db
->strencode( $name );
163 $likeprefix = str_replace( '_', '\\_', $prefix);
164 $encNamespace = $this->db
->addQuotes( $ns );
166 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
168 // An interwiki; try an alternate encoding with '-' for ':'
169 $titleSql = "CONCAT('$prefix-',$titleSql)";
172 $sql = "SELECT {$page}_id AS id,
173 {$page}_title AS oldtitle,
174 $encNamespace AS namespace,
177 WHERE {$page}_namespace=0
178 AND {$page}_title LIKE '$likeprefix:%'";
180 $result = $this->db
->query( $sql, 'NamespaceConflictChecker::getConflicts' );
183 while( $row = $this->db
->fetchObject( $result ) ) {
186 $this->db
->freeResult( $result );
191 function reportConflict( $row, $suffix ) {
192 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
194 // Title is also an illegal title...
195 // For the moment we'll let these slide to cleanupTitles or whoever.
196 printf( "... %d (0,\"%s\")\n",
199 echo "... *** cannot resolve automatically; illegal title ***\n";
203 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
206 $newTitle->getNamespace(),
207 $newTitle->getDBkey(),
208 $newTitle->getPrefixedText() );
210 $id = $newTitle->getArticleId();
212 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
219 function resolveConflict( $row, $resolvable, $suffix ) {
221 echo "... *** old title {$row->title}\n";
222 $row->title
.= $suffix;
223 echo "... *** new title {$row->title}\n";
224 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
226 echo "... !!! invalid title\n";
229 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
231 $tables = array( 'page' );
232 foreach( $tables as $table ) {
233 $this->resolveConflictOn( $row, $table );
238 function resolveConflictOn( $row, $table ) {
239 echo "... resolving on $table... ";
240 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
241 $this->db
->update( $table,
243 "{$table}_namespace" => $newTitle->getNamespace(),
244 "{$table}_title" => $newTitle->getDBkey(),
247 "{$table}_namespace" => 0,
248 "{$table}_title" => $row->oldtitle
,
259 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
261 $verbose = isset( $options['verbose'] );
262 $fix = isset( $options['fix'] );
263 $suffix = isset( $options['suffix'] ) ?
$options['suffix'] : '';
264 $prefix = isset( $options['prefix'] ) ?
$options['prefix'] : '';
265 $key = isset( $options['key'] ) ?
intval( $options['key'] ) : 0;
267 $dbw = wfGetDB( DB_MASTER
);
268 $duper = new NamespaceConflictChecker( $dbw, $verbose );
271 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
273 $retval = $duper->checkAll( $fix, $suffix );
277 echo "\nLooks good!\n";
280 echo "\nOh noeees\n";