3 * Check for articles to fix after adding/deleting namespaces
5 * Copyright © 2005-2007 Brion Vibber <brion@pobox.com>
6 * https://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
24 * @ingroup Maintenance
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that checks for articles to fix after
31 * adding/deleting namespaces.
33 * @ingroup Maintenance
35 class NamespaceConflictChecker
extends Maintenance
{
42 public function __construct() {
43 parent
::__construct();
44 $this->mDescription
= "";
45 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
46 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
47 "<text> appended after the article name", false, true );
48 $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
49 "appended after the article name", false, true );
52 public function execute() {
53 $this->db
= wfGetDB( DB_MASTER
);
55 $fix = $this->hasOption( 'fix' );
56 $suffix = $this->getOption( 'suffix', '' );
57 $prefix = $this->getOption( 'prefix', '' );
58 $key = intval( $this->getOption( 'key', 0 ) );
61 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
63 $retval = $this->checkAll( $fix, $suffix );
67 $this->output( "\nLooks good!\n" );
69 $this->output( "\nOh noeees\n" );
75 * @param bool $fix Whether or not to fix broken entries
76 * @param string $suffix Suffix to append to renamed articles
80 private function checkAll( $fix, $suffix = '' ) {
81 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
85 // List interwikis first, so they'll be overridden
86 // by any conflicting local namespaces.
87 foreach ( $this->getInterwikiList() as $prefix ) {
88 $name = $wgContLang->ucfirst( $prefix );
92 // Now pull in all canonical and alias namespaces...
93 foreach ( MWNamespace
::getCanonicalNamespaces() as $ns => $name ) {
94 // This includes $wgExtraNamespaces
99 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
100 if ( $name !== '' ) {
101 $spaces[$name] = $ns;
104 foreach ( $wgNamespaceAliases as $name => $ns ) {
105 $spaces[$name] = $ns;
107 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
108 $spaces[$name] = $ns;
111 // We'll need to check for lowercase keys as well,
112 // since we're doing case-sensitive searches in the db.
113 foreach ( $spaces as $name => $ns ) {
114 $moreNames = array();
115 $moreNames[] = $wgContLang->uc( $name );
116 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
117 $moreNames[] = $wgContLang->ucwords( $name );
118 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
119 $moreNames[] = $wgContLang->ucwordbreaks( $name );
120 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
121 if ( !$wgCapitalLinks ) {
122 foreach ( $moreNames as $altName ) {
123 $moreNames[] = $wgContLang->lcfirst( $altName );
125 $moreNames[] = $wgContLang->lcfirst( $name );
127 foreach ( array_unique( $moreNames ) as $altName ) {
128 if ( $altName !== $name ) {
129 $spaces[$altName] = $ns;
138 foreach ( $spaces as $name => $ns ) {
139 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
145 * Get the interwiki list
149 private function getInterwikiList() {
150 $result = Interwiki
::getAllPrefixes();
152 foreach ( $result as $row ) {
153 $prefixes[] = $row['iw_prefix'];
160 * @param int $ns A namespace id
161 * @param string $name
162 * @param bool $fix Whether to fix broken entries
163 * @param string $suffix Suffix to append to renamed articles
166 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
167 $conflicts = $this->getConflicts( $ns, $name );
168 $count = count( $conflicts );
174 foreach ( $conflicts as $row ) {
175 $resolvable = $this->reportConflict( $row, $suffix );
176 $ok = $ok && $resolvable;
177 if ( $fix && ( $resolvable ||
$suffix != '' ) ) {
178 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
185 * @todo Do this for real
187 * @param string $name
189 * @param string $suffix
192 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
193 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
194 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
198 * Find pages in mainspace that have a prefix of the new namespace
199 * so we know titles that will need migrating
201 * @param int $ns Namespace id (id for new namespace?)
202 * @param string $name Prefix that is being made a namespace
206 private function getConflicts( $ns, $name ) {
208 $table = $this->db
->tableName( $page );
210 $prefix = $this->db
->strencode( $name );
211 $encNamespace = $this->db
->addQuotes( $ns );
213 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
215 // An interwiki; try an alternate encoding with '-' for ':'
216 $titleSql = $this->db
->buildConcat( array( "'$prefix-'", $titleSql ) );
219 $sql = "SELECT {$page}_id AS id,
220 {$page}_title AS oldtitle,
221 $encNamespace + {$page}_namespace AS namespace,
223 {$page}_namespace AS oldnamespace
225 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
226 AND {$page}_title " . $this->db
->buildLike( $name . ':', $this->db
->anyString() );
228 $result = $this->db
->query( $sql, __METHOD__
);
231 foreach ( $result as $row ) {
238 * Report any conflicts we find
240 * @param stdClass $row
241 * @param string $suffix
244 private function reportConflict( $row, $suffix ) {
245 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
246 if ( is_null( $newTitle ) ||
!$newTitle->canExist() ) {
247 // Title is also an illegal title...
248 // For the moment we'll let these slide to cleanupTitles or whoever.
249 $this->output( sprintf( "... %d (%d,\"%s\")\n",
253 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
257 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
261 $newTitle->getNamespace(),
262 $newTitle->getDBkey(),
263 $newTitle->getPrefixedText() ) );
265 $id = $newTitle->getArticleID();
267 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
275 * Resolve any conflicts
277 * @param stClass $row Row from the page table to fix
278 * @param bool $resolvable
279 * @param string $suffix Suffix to append to the fixed page
282 private function resolveConflict( $row, $resolvable, $suffix ) {
283 if ( !$resolvable ) {
284 $this->output( "... *** old title {$row->title}\n" );
286 $row->title
.= $suffix;
287 $this->output( "... *** new title {$row->title}\n" );
288 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
290 $this->output( "... !!! invalid title\n" );
293 $id = $title->getArticleID();
295 $this->output( "... *** page exists with ID $id ***\n" );
300 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
302 $this->resolveConflictOn( $row, 'page', 'page' );
307 * Resolve a given conflict
309 * @param stdClass $row Row from the old broken entry
310 * @param string $table Table to update
311 * @param string $prefix Prefix for column name, like page or ar
314 private function resolveConflictOn( $row, $table, $prefix ) {
315 $this->output( "... resolving on $table... " );
316 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
317 $this->db
->update( $table,
319 "{$prefix}_namespace" => $newTitle->getNamespace(),
320 "{$prefix}_title" => $newTitle->getDBkey(),
323 // "{$prefix}_namespace" => 0,
324 // "{$prefix}_title" => $row->oldtitle,
325 "{$prefix}_id" => $row->id
,
328 $this->output( "ok.\n" );
333 $maintClass = "NamespaceConflictChecker";
334 require_once RUN_MAINTENANCE_IF_MAIN
;