Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[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
36 * on or off $wgCapitalLinks.
37 *
38 * @ingroup Maintenance
39 */
40 class CapsCleanup extends TableCleanup {
41
42 private $user;
43 private $namespace;
44
45 public function __construct() {
46 parent::__construct();
47 $this->addDescription( 'Script to cleanup capitalization' );
48 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
49 }
50
51 public function execute() {
52 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
53
54 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
55
56 if ( MWNamespace::isCapitalized( $this->namespace ) ) {
57 $this->output( "Will be moving pages to first letter capitalized titles" );
58 $callback = 'processRowToUppercase';
59 } else {
60 $this->output( "Will be moving pages to first letter lowercase titles" );
61 $callback = 'processRowToLowercase';
62 }
63
64 $this->dryrun = $this->hasOption( 'dry-run' );
65
66 $this->runTable( [
67 'table' => 'page',
68 'conds' => [ 'page_namespace' => $this->namespace ],
69 'index' => 'page_id',
70 'callback' => $callback ] );
71 }
72
73 protected function processRowToUppercase( $row ) {
74 global $wgContLang;
75
76 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
77 $display = $current->getPrefixedText();
78 $lower = $row->page_title;
79 $upper = $wgContLang->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( __CLASS__ . '/' . $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 global $wgContLang;
113
114 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
115 $display = $current->getPrefixedText();
116 $upper = $row->page_title;
117 $lower = $wgContLang->lcfirst( $row->page_title );
118 if ( $upper == $lower ) {
119 $this->output( "\"$display\" already lowercase.\n" );
120
121 return $this->progress( 0 );
122 }
123
124 $target = Title::makeTitle( $row->page_namespace, $lower );
125 if ( $target->exists() ) {
126 $targetDisplay = $target->getPrefixedText();
127 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
128
129 return $this->progress( 0 );
130 }
131
132 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
133 if ( $ok === true ) {
134 $this->progress( 1 );
135 if ( $row->page_namespace == $this->namespace ) {
136 $talk = $target->getTalkPage();
137 $row->page_namespace = $talk->getNamespace();
138 if ( $talk->exists() ) {
139 return $this->processRowToLowercase( $row );
140 }
141 }
142 }
143
144 return $this->progress( 0 );
145 }
146
147 /**
148 * @param Title $current
149 * @param Title $target
150 * @param string $reason
151 * @param bool $createRedirect
152 * @return bool Success
153 */
154 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
155 $display = $current->getPrefixedText();
156 $targetDisplay = $target->getPrefixedText();
157
158 if ( $this->dryrun ) {
159 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
160 $ok = 'OK';
161 } else {
162 $mp = new MovePage( $current, $target );
163 $status = $mp->move( $this->user, $reason, $createRedirect );
164 $ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
165 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
166 }
167
168 return $ok === 'OK';
169 }
170 }
171
172 $maintClass = CapsCleanup::class;
173 require_once RUN_MAINTENANCE_IF_MAIN;