Merge "Adding param documentation for Parser::internalParse()"
[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 * https://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
41 private $user;
42
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Script to cleanup capitalization' );
46 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
47 }
48
49 public function execute() {
50 global $wgCapitalLinks;
51
52 if ( $wgCapitalLinks ) {
53 $this->error( "\$wgCapitalLinks is on -- no need for caps links cleanup.", true );
54 }
55
56 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
57
58 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
59 $this->dryrun = $this->hasOption( 'dry-run' );
60
61 $this->runTable( [
62 'table' => 'page',
63 'conds' => [ 'page_namespace' => $this->namespace ],
64 'index' => 'page_id',
65 'callback' => 'processRow' ] );
66 }
67
68 protected function processRow( $row ) {
69 global $wgContLang;
70
71 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
72 $display = $current->getPrefixedText();
73 $upper = $row->page_title;
74 $lower = $wgContLang->lcfirst( $row->page_title );
75 if ( $upper == $lower ) {
76 $this->output( "\"$display\" already lowercase.\n" );
77
78 return $this->progress( 0 );
79 }
80
81 $target = Title::makeTitle( $row->page_namespace, $lower );
82 $targetDisplay = $target->getPrefixedText();
83 if ( $target->exists() ) {
84 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
85
86 return $this->progress( 0 );
87 }
88
89 if ( $this->dryrun ) {
90 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
91 $ok = true;
92 } else {
93 $mp = new MovePage( $current, $target );
94 $status = $mp->move( $this->user, 'Converting page titles to lowercase', true );
95 $ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
96 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
97 }
98 if ( $ok === true ) {
99 $this->progress( 1 );
100 if ( $row->page_namespace == $this->namespace ) {
101 $talk = $target->getTalkPage();
102 $row->page_namespace = $talk->getNamespace();
103 if ( $talk->exists() ) {
104 return $this->processRow( $row );
105 }
106 }
107 }
108
109 return $this->progress( 0 );
110 }
111 }
112
113 $maintClass = "CapsCleanup";
114 require_once RUN_MAINTENANCE_IF_MAIN;