250a688b9aadedfac894b7f15f93f0383606f437
[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( array(
49 'table' => 'page',
50 'conds' => array( 'page_namespace' => $this->namespace ),
51 'index' => 'page_id',
52 'callback' => 'processRow' ) );
53 }
54
55 protected function processRow( $row ) {
56 global $wgContLang;
57
58 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
59 $display = $current->getPrefixedText();
60 $upper = $row->page_title;
61 $lower = $wgContLang->lcfirst( $row->page_title );
62 if ( $upper == $lower ) {
63 $this->output( "\"$display\" already lowercase.\n" );
64 return $this->progress( 0 );
65 }
66
67 $target = Title::makeTitle( $row->page_namespace, $lower );
68 $targetDisplay = $target->getPrefixedText();
69 if ( $target->exists() ) {
70 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
71 return $this->progress( 0 );
72 }
73
74 if ( $this->dryrun ) {
75 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
76 $ok = true;
77 } else {
78 $ok = $current->moveTo( $target, false, 'Converting page titles to lowercase' );
79 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
80 }
81 if ( $ok === true ) {
82 $this->progress( 1 );
83 if ( $row->page_namespace == $this->namespace ) {
84 $talk = $target->getTalkPage();
85 $row->page_namespace = $talk->getNamespace();
86 if ( $talk->exists() ) {
87 return $this->processRow( $row );
88 }
89 }
90 } else {
91 $this->progress( 0 );
92 }
93 }
94 }
95
96 $maintClass = "CapsCleanup";
97 require_once( DO_MAINTENANCE );