phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / cleanupCaps.php
1 <?php
2 /**
3 * 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 © 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 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
30 */
31
32 require_once __DIR__ . '/cleanupTable.inc';
33
34 /**
35 * Maintenance script to clean up broken page links when somebody turns on $wgCapitalLinks.
36 *
37 * @ingroup Maintenance
38 */
39 class CapsCleanup extends TableCleanup {
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Script to cleanup capitalization";
43 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
44 }
45
46 public function execute() {
47 global $wgCapitalLinks, $wgUser;
48
49 if ( $wgCapitalLinks ) {
50 $this->error( "\$wgCapitalLinks is on -- no need for caps links cleanup.", true );
51 }
52
53 $wgUser = User::newFromName( 'Conversion script' );
54
55 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
56 $this->dryrun = $this->hasOption( 'dry-run' );
57
58 $this->runTable( array(
59 'table' => 'page',
60 'conds' => array( 'page_namespace' => $this->namespace ),
61 'index' => 'page_id',
62 'callback' => 'processRow' ) );
63 }
64
65 protected function processRow( $row ) {
66 global $wgContLang;
67
68 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
69 $display = $current->getPrefixedText();
70 $upper = $row->page_title;
71 $lower = $wgContLang->lcfirst( $row->page_title );
72 if ( $upper == $lower ) {
73 $this->output( "\"$display\" already lowercase.\n" );
74 return $this->progress( 0 );
75 }
76
77 $target = Title::makeTitle( $row->page_namespace, $lower );
78 $targetDisplay = $target->getPrefixedText();
79 if ( $target->exists() ) {
80 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
81 return $this->progress( 0 );
82 }
83
84 if ( $this->dryrun ) {
85 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
86 $ok = true;
87 } else {
88 $ok = $current->moveTo( $target, false, 'Converting page titles to lowercase' );
89 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
90 }
91 if ( $ok === true ) {
92 $this->progress( 1 );
93 if ( $row->page_namespace == $this->namespace ) {
94 $talk = $target->getTalkPage();
95 $row->page_namespace = $talk->getNamespace();
96 if ( $talk->exists() ) {
97 return $this->processRow( $row );
98 }
99 }
100 }
101 return $this->progress( 0 );
102 }
103 }
104
105 $maintClass = "CapsCleanup";
106 require_once RUN_MAINTENANCE_IF_MAIN;