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
{
29 public function __construct() {
30 parent
::__construct();
31 $this->mDescription
= "";
32 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
33 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with\n" .
34 "\t\t<text> Appended after the article name", false, true );
35 $this->addOption( 'prefix', "Do an explicit check for the given title prefix\n" .
36 "\t\tappended after the article name", false, true );
39 public function execute() {
42 $this->db
= wfGetDB( DB_MASTER
);
43 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
45 $fix = $this->hasOption( 'fix' );
46 $suffix = $this->getOption( 'suffix', '' );
47 $prefix = $this->getOption( 'prefix', '' );
48 $key = intval( $this->getOption( 'key', 0 ) );
51 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
53 $retval = $this->checkAll( $fix, $suffix );
57 $this->output( "\nLooks good!\n" );
59 $this->output( "\nOh noeees\n" );
65 * @param $fix Boolean: whether or not to fix broken entries
66 * @param $suffix String: suffix to append to renamed articles
68 private function checkAll( $fix, $suffix = '' ) {
69 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
70 global $wgCapitalLinks;
74 // List interwikis first, so they'll be overridden
75 // by any conflicting local namespaces.
76 foreach ( $this->getInterwikiList() as $prefix ) {
77 $name = $wgContLang->ucfirst( $prefix );
81 // Now pull in all canonical and alias namespaces...
82 foreach ( $wgCanonicalNamespaceNames as $ns => $name ) {
83 // This includes $wgExtraNamespaces
88 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
93 foreach ( $wgNamespaceAliases as $name => $ns ) {
96 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
100 // We'll need to check for lowercase keys as well,
101 // since we're doing case-sensitive searches in the db.
102 foreach ( $spaces as $name => $ns ) {
103 $moreNames = array();
104 $moreNames[] = $wgContLang->uc( $name );
105 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
106 $moreNames[] = $wgContLang->ucwords( $name );
107 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
108 $moreNames[] = $wgContLang->ucwordbreaks( $name );
109 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
110 if ( !$wgCapitalLinks ) {
111 foreach ( $moreNames as $altName ) {
112 $moreNames[] = $wgContLang->lcfirst( $altName );
114 $moreNames[] = $wgContLang->lcfirst( $name );
116 foreach ( array_unique( $moreNames ) as $altName ) {
117 if ( $altName !== $name ) {
118 $spaces[$altName] = $ns;
127 foreach ( $spaces as $name => $ns ) {
128 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
134 * Get the interwiki list
136 * @todo Needs to respect interwiki cache!
139 private function getInterwikiList() {
140 $result = $this->db
->select( 'interwiki', array( 'iw_prefix' ) );
142 foreach ( $result as $row ) {
143 $prefixes[] = $row->iw_prefix
;
145 $this->db
->freeResult( $result );
151 * @param $ns Integer: a namespace id
152 * @param $name String
153 * @param $fix Boolean: whether to fix broken entries
154 * @param $suffix String: suffix to append to renamed articles
156 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
157 $conflicts = $this->getConflicts( $ns, $name );
158 $count = count( $conflicts );
164 foreach ( $conflicts as $row ) {
165 $resolvable = $this->reportConflict( $row, $suffix );
166 $ok = $ok && $resolvable;
167 if ( $fix && ( $resolvable ||
$suffix != '' ) ) {
168 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
175 * @todo: do this for reals
177 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
178 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
179 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
183 * Find pages in mainspace that have a prefix of the new namespace
184 * so we know titles that will need migrating
186 * @param $ns Integer: namespace id (id for new namespace?)
187 * @param $name String: prefix that is being made a namespace
189 private function getConflicts( $ns, $name ) {
191 $table = $this->db
->tableName( $page );
193 $prefix = $this->db
->strencode( $name );
194 $encNamespace = $this->db
->addQuotes( $ns );
196 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
198 // An interwiki; try an alternate encoding with '-' for ':'
199 $titleSql = $this->db
->buildConcat( array( "'$prefix-'", $titleSql ) );
202 $sql = "SELECT {$page}_id AS id,
203 {$page}_title AS oldtitle,
204 $encNamespace + {$page}_namespace AS namespace,
206 {$page}_namespace AS oldnamespace
208 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
209 AND {$page}_title " . $this->db
->buildLike( $name . ':', $this->db
->anyString() );
211 $result = $this->db
->query( $sql, __METHOD__
);
214 foreach ( $result as $row ) {
217 $this->db
->freeResult( $result );
223 * Report any conflicts we find
225 private function reportConflict( $row, $suffix ) {
226 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
227 if ( is_null( $newTitle ) ||
!$newTitle->canExist() ) {
228 // Title is also an illegal title...
229 // For the moment we'll let these slide to cleanupTitles or whoever.
230 $this->output( sprintf( "... %d (%d,\"%s\")\n",
234 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
238 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
242 $newTitle->getNamespace(),
243 $newTitle->getDBkey(),
244 $newTitle->getPrefixedText() ) );
246 $id = $newTitle->getArticleId();
248 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
256 * Resolve any conflicts
258 * @param $row Object: row from the page table to fix
259 * @param $resolvable Boolean
260 * @param $suffix String: suffix to append to the fixed page
262 private function resolveConflict( $row, $resolvable, $suffix ) {
263 if ( !$resolvable ) {
264 $this->output( "... *** old title {$row->title}\n" );
266 $row->title
.= $suffix;
267 $this->output( "... *** new title {$row->title}\n" );
268 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
270 $this->output( "... !!! invalid title\n" );
273 if ( $id = $title->getArticleId() ) {
274 $this->output( "... *** page exists with ID $id ***\n" );
279 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
281 $this->resolveConflictOn( $row, 'page', 'page' );
286 * Resolve a given conflict
288 * @param $row Object: row from the old broken entry
289 * @param $table String: table to update
290 * @param $prefix String: prefix for column name, like page or ar
292 private function resolveConflictOn( $row, $table, $prefix ) {
293 $this->output( "... resolving on $table... " );
294 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
295 $this->db
->update( $table,
297 "{$prefix}_namespace" => $newTitle->getNamespace(),
298 "{$prefix}_title" => $newTitle->getDBkey(),
301 // "{$prefix}_namespace" => 0,
302 // "{$prefix}_title" => $row->oldtitle,
303 "{$prefix}_id" => $row->id
,
306 $this->output( "ok.\n" );
311 $maintClass = "NamespaceConflictChecker";
312 require_once( DO_MAINTENANCE
);