c0d1773aab70d71301366053e6d05383c959d431
[lhc/web/wiklou.git] / maintenance / cleanupCaps.php
1 <?php
2 /*
3 * Script to clean up broken page links when somebody turns on $wgCapitalLinks.
4 *
5 * Usage: php cleanupCaps.php [--dry-run]
6 * Options:
7 * --dry-run don't actually try moving them
8 *
9 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @author Brion Vibber <brion at pobox.com>
28 * @ingroup maintenance
29 */
30
31 require_once( dirname(__FILE__) . '/cleanupTable.inc' );
32
33 class CapsCleanup extends TableCleanup {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Script to cleanup capitalization";
37 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
38 }
39
40 public function execute() {
41 global $wgCapitalLinks, $wgUser;
42 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
43 $this->dryrun = $this->hasOption( 'dry-run' );
44 $wgUser->setName( 'Conversion script' );
45 if( $wgCapitalLinks )
46 $this->error( "\$wgCapitalLinks is on -- no need for caps links cleanup.", true );
47
48 $this->runTable( $this->targetTable,
49 'WHERE page_namespace=' . $this->namespace,
50 array( &$this, 'processPage' ) );
51 }
52
53 protected function processPage( $row ) {
54 global $wgContLang;
55
56 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
57 $display = $current->getPrefixedText();
58 $upper = $row->page_title;
59 $lower = $wgContLang->lcfirst( $row->page_title );
60 if( $upper == $lower ) {
61 $this->output( "\"$display\" already lowercase." );
62 return $this->progress( 0 );
63 }
64
65 $target = Title::makeTitle( $row->page_namespace, $lower );
66 $targetDisplay = $target->getPrefixedText();
67 if( $target->exists() ) {
68 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists" );
69 return $this->progress( 0 );
70 }
71
72 if( $this->dryrun ) {
73 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED" );
74 $ok = true;
75 } else {
76 $ok = $current->moveTo( $target, false, 'Converting page titles to lowercase' );
77 $this->output( "\"$display\" -> \"$targetDisplay\": $ok" );
78 }
79 if( $ok === true ) {
80 $this->progress( 1 );
81 if( $row->page_namespace == $this->namespace ) {
82 $talk = $target->getTalkPage();
83 $row->page_namespace = $talk->getNamespace();
84 if( $talk->exists() ) {
85 return $this->processPage( $row );
86 }
87 }
88 } else {
89 $this->progress( 0 );
90 }
91 }
92 }
93
94 $maintClass = "CapsCleanup";
95 require_once( DO_MAINTENANCE );