no need for position:absolute
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 $options = array( 'fix', 'suffix' );
21
22 /** */
23 require_once( 'commandLine.inc' );
24 #require_once( 'maintenance/userDupes.inc' );
25
26 class NamespaceConflictChecker {
27 function NamespaceConflictChecker( &$db ) {
28 $this->db =& $db;
29 }
30
31 function checkAll( $fix, $suffix = '' ) {
32 global $wgContLang;
33 $spaces = $wgContLang->getNamespaces();
34 $ok = true;
35 foreach( $spaces as $ns => $name ) {
36 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
37 }
38 return $ok;
39 }
40
41 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
42 echo "Checking namespace $ns: \"$name\"\n";
43 if( $name == '' ) {
44 echo "... skipping article namespace\n";
45 return true;
46 }
47
48 $conflicts = $this->getConflicts( $ns, $name );
49 $count = count( $conflicts );
50 if( $count == 0 ) {
51 echo "... no conflicts detected!\n";
52 return true;
53 }
54
55 echo "... $count conflicts detected:\n";
56 $ok = true;
57 foreach( $conflicts as $row ) {
58 $resolvable = $this->reportConflict( $row, $suffix );
59 $ok = $ok && $resolvable;
60 if( $fix && ( $resolvable || $suffix != '' ) ) {
61 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
62 }
63 }
64 return $ok;
65 }
66
67 function getConflicts( $ns, $name ) {
68 $page = $this->newSchema() ? 'page' : 'cur';
69 $table = $this->db->tableName( $page );
70
71 $prefix = $this->db->strencode( $name );
72 $likeprefix = str_replace( '_', '\\_', $prefix);
73
74 $sql = "SELECT {$page}_id AS id,
75 {$page}_title AS oldtitle,
76 $ns AS namespace,
77 TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
78 FROM {$table}
79 WHERE {$page}_namespace=0
80 AND {$page}_title LIKE '$likeprefix:%'";
81
82 $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
83
84 $set = array();
85 while( $row = $this->db->fetchObject( $result ) ) {
86 $set[] = $row;
87 }
88 $this->db->freeResult( $result );
89
90 return $set;
91 }
92
93 function reportConflict( $row, $suffix ) {
94 $newTitle = Title::makeTitle( $row->namespace, $row->title );
95 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
96 $row->id,
97 $row->oldtitle,
98 $row->namespace,
99 $row->title,
100 $newTitle->getPrefixedText() );
101
102 $id = $newTitle->getArticleId();
103 if( $id ) {
104 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
105 return false;
106 } else {
107 return true;
108 }
109 }
110
111 function resolveConflict( $row, $resolvable, $suffix ) {
112 if( !$resolvable ) {
113 $row->title .= $suffix;
114 $title = Title::makeTitle( $row->namespace, $row->title );
115 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
116 }
117 $tables = $this->newSchema()
118 ? array( 'page' )
119 : array( 'cur', 'old' );
120 foreach( $tables as $table ) {
121 $this->resolveConflictOn( $row, $table );
122 }
123 return true;
124 }
125
126 function resolveConflictOn( $row, $table ) {
127 $fname = 'NamespaceConflictChecker::resolveConflictOn';
128 echo "... resolving on $table... ";
129 $this->db->update( $table,
130 array(
131 "{$table}_namespace" => $row->namespace,
132 "{$table}_title" => $row->title,
133 ),
134 array(
135 "{$table}_namespace" => 0,
136 "{$table}_title" => $row->oldtitle,
137 ),
138 $fname );
139 echo "ok.\n";
140 return true;
141 }
142
143 function newSchema() {
144 global $wgVersion;
145 return version_compare( $wgVersion, '1.5alpha', 'ge' );
146 }
147 }
148
149
150
151
152 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
153
154 $fix = isset( $options['fix'] );
155 $suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
156 $dbw =& wfGetDB( DB_MASTER );
157 $duper = new NamespaceConflictChecker( $dbw );
158 $retval = $duper->checkAll( $fix, $suffix );
159
160 if( $retval ) {
161 echo "\nLooks good!\n";
162 exit( 0 );
163 } else {
164 echo "\nOh noeees\n";
165 exit( -1 );
166 }
167
168 ?>