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
74 private function checkAll( $fix, $suffix = '' ) {
75 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
79 // List interwikis first, so they'll be overridden
80 // by any conflicting local namespaces.
81 foreach ( $this->getInterwikiList() as $prefix ) {
82 $name = $wgContLang->ucfirst( $prefix );
86 // Now pull in all canonical and alias namespaces...
87 foreach ( MWNamespace
::getCanonicalNamespaces() as $ns => $name ) {
88 // This includes $wgExtraNamespaces
93 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
98 foreach ( $wgNamespaceAliases as $name => $ns ) {
101 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
102 $spaces[$name] = $ns;
105 // We'll need to check for lowercase keys as well,
106 // since we're doing case-sensitive searches in the db.
107 foreach ( $spaces as $name => $ns ) {
108 $moreNames = array();
109 $moreNames[] = $wgContLang->uc( $name );
110 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
111 $moreNames[] = $wgContLang->ucwords( $name );
112 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
113 $moreNames[] = $wgContLang->ucwordbreaks( $name );
114 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
115 if ( !$wgCapitalLinks ) {
116 foreach ( $moreNames as $altName ) {
117 $moreNames[] = $wgContLang->lcfirst( $altName );
119 $moreNames[] = $wgContLang->lcfirst( $name );
121 foreach ( array_unique( $moreNames ) as $altName ) {
122 if ( $altName !== $name ) {
123 $spaces[$altName] = $ns;
132 foreach ( $spaces as $name => $ns ) {
133 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
139 * Get the interwiki list
141 * @todo Needs to respect interwiki cache!
144 private function getInterwikiList() {
145 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
147 foreach ( $result as $row ) {
148 $prefixes[] = $row->iw_prefix
;
155 * @param $ns Integer: a namespace id
156 * @param $name String
157 * @param $fix Boolean: whether to fix broken entries
158 * @param $suffix String: suffix to append to renamed articles
160 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
161 $conflicts = $this->getConflicts( $ns, $name );
162 $count = count( $conflicts );
168 foreach ( $conflicts as $row ) {
169 $resolvable = $this->reportConflict( $row, $suffix );
170 $ok = $ok && $resolvable;
171 if ( $fix && ( $resolvable ||
$suffix != '' ) ) {
172 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
179 * @todo: do this for reals
181 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
182 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
183 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
187 * Find pages in mainspace that have a prefix of the new namespace
188 * so we know titles that will need migrating
190 * @param $ns Integer: namespace id (id for new namespace?)
191 * @param $name String: prefix that is being made a namespace
195 private function getConflicts( $ns, $name ) {
197 $table = $this->db
->tableName( $page );
199 $prefix = $this->db
->strencode( $name );
200 $encNamespace = $this->db
->addQuotes( $ns );
202 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
204 // An interwiki; try an alternate encoding with '-' for ':'
205 $titleSql = $this->db
->buildConcat( array( "'$prefix-'", $titleSql ) );
208 $sql = "SELECT {$page}_id AS id,
209 {$page}_title AS oldtitle,
210 $encNamespace + {$page}_namespace AS namespace,
212 {$page}_namespace AS oldnamespace
214 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
215 AND {$page}_title " . $this->db
->buildLike( $name . ':', $this->db
->anyString() );
217 $result = $this->db
->query( $sql, __METHOD__
);
220 foreach ( $result as $row ) {
227 * Report any conflicts we find
231 private function reportConflict( $row, $suffix ) {
232 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
233 if ( is_null( $newTitle ) ||
!$newTitle->canExist() ) {
234 // Title is also an illegal title...
235 // For the moment we'll let these slide to cleanupTitles or whoever.
236 $this->output( sprintf( "... %d (%d,\"%s\")\n",
240 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
244 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
248 $newTitle->getNamespace(),
249 $newTitle->getDBkey(),
250 $newTitle->getPrefixedText() ) );
252 $id = $newTitle->getArticleId();
254 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
262 * Resolve any conflicts
264 * @param $row Object: row from the page table to fix
265 * @param $resolvable Boolean
266 * @param $suffix String: suffix to append to the fixed page
268 private function resolveConflict( $row, $resolvable, $suffix ) {
269 if ( !$resolvable ) {
270 $this->output( "... *** old title {$row->title}\n" );
272 $row->title
.= $suffix;
273 $this->output( "... *** new title {$row->title}\n" );
274 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
276 $this->output( "... !!! invalid title\n" );
279 $id = $title->getArticleId();
281 $this->output( "... *** page exists with ID $id ***\n" );
286 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
288 $this->resolveConflictOn( $row, 'page', 'page' );
293 * Resolve a given conflict
295 * @param $row Object: row from the old broken entry
296 * @param $table String: table to update
297 * @param $prefix String: prefix for column name, like page or ar
299 private function resolveConflictOn( $row, $table, $prefix ) {
300 $this->output( "... resolving on $table... " );
301 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
302 $this->db
->update( $table,
304 "{$prefix}_namespace" => $newTitle->getNamespace(),
305 "{$prefix}_title" => $newTitle->getDBkey(),
308 // "{$prefix}_namespace" => 0,
309 // "{$prefix}_title" => $row->oldtitle,
310 "{$prefix}_id" => $row->id
,
313 $this->output( "ok.\n" );
318 $maintClass = "NamespaceConflictChecker";
319 require_once( RUN_MAINTENANCE_IF_MAIN
);