Merge "Add index on rc_this_oldid"
[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 use MediaWiki\MediaWikiServices;
33
34 require_once __DIR__ . '/cleanupTable.inc';
35
36 /**
37 * Maintenance script to clean up broken page links when somebody turns
38 * on or off $wgCapitalLinks.
39 *
40 * @ingroup Maintenance
41 */
42 class CleanupCaps extends TableCleanup {
43
44 private $user;
45 private $namespace;
46
47 public function __construct() {
48 parent::__construct();
49 $this->addDescription( 'Script to cleanup capitalization' );
50 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
51 }
52
53 public function execute() {
54 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
55
56 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
57
58 if ( MWNamespace::isCapitalized( $this->namespace ) ) {
59 $this->output( "Will be moving pages to first letter capitalized titles" );
60 $callback = 'processRowToUppercase';
61 } else {
62 $this->output( "Will be moving pages to first letter lowercase titles" );
63 $callback = 'processRowToLowercase';
64 }
65
66 $this->dryrun = $this->hasOption( 'dry-run' );
67
68 $this->runTable( [
69 'table' => 'page',
70 'conds' => [ 'page_namespace' => $this->namespace ],
71 'index' => 'page_id',
72 'callback' => $callback ] );
73 }
74
75 protected function processRowToUppercase( $row ) {
76 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
77 $display = $current->getPrefixedText();
78 $lower = $row->page_title;
79 $upper = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $row->page_title );
80 if ( $upper == $lower ) {
81 $this->output( "\"$display\" already uppercase.\n" );
82
83 return $this->progress( 0 );
84 }
85
86 $target = Title::makeTitle( $row->page_namespace, $upper );
87 if ( $target->exists() ) {
88 // Prefix "CapsCleanup" to bypass the conflict
89 $target = Title::newFromText( 'CapsCleanup/' . $display );
90 }
91 $ok = $this->movePage(
92 $current,
93 $target,
94 'Converting page title to first-letter uppercase',
95 false
96 );
97 if ( $ok ) {
98 $this->progress( 1 );
99 if ( $row->page_namespace == $this->namespace ) {
100 $talk = $target->getTalkPage();
101 $row->page_namespace = $talk->getNamespace();
102 if ( $talk->exists() ) {
103 return $this->processRowToUppercase( $row );
104 }
105 }
106 }
107
108 return $this->progress( 0 );
109 }
110
111 protected function processRowToLowercase( $row ) {
112 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
113 $display = $current->getPrefixedText();
114 $upper = $row->page_title;
115 $lower = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $row->page_title );
116 if ( $upper == $lower ) {
117 $this->output( "\"$display\" already lowercase.\n" );
118
119 return $this->progress( 0 );
120 }
121
122 $target = Title::makeTitle( $row->page_namespace, $lower );
123 if ( $target->exists() ) {
124 $targetDisplay = $target->getPrefixedText();
125 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
126
127 return $this->progress( 0 );
128 }
129
130 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
131 if ( $ok === true ) {
132 $this->progress( 1 );
133 if ( $row->page_namespace == $this->namespace ) {
134 $talk = $target->getTalkPage();
135 $row->page_namespace = $talk->getNamespace();
136 if ( $talk->exists() ) {
137 return $this->processRowToLowercase( $row );
138 }
139 }
140 }
141
142 return $this->progress( 0 );
143 }
144
145 /**
146 * @param Title $current
147 * @param Title $target
148 * @param string $reason
149 * @param bool $createRedirect
150 * @return bool Success
151 */
152 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
153 $display = $current->getPrefixedText();
154 $targetDisplay = $target->getPrefixedText();
155
156 if ( $this->dryrun ) {
157 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
158 $ok = 'OK';
159 } else {
160 $mp = new MovePage( $current, $target );
161 $status = $mp->move( $this->user, $reason, $createRedirect );
162 $ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
163 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
164 }
165
166 return $ok === 'OK';
167 }
168 }
169
170 $maintClass = CleanupCaps::class;
171 require_once RUN_MAINTENANCE_IF_MAIN;