Move ChronologyProtector/TransactionProfiler to Rdbms namespace
[mediawiki.git] / maintenance / cleanupCaps.php
blob2da45ca19251cd0f98e8c9a87f738b6cfd580897
1 <?php
2 /**
3 * Clean up broken page links when somebody turns on $wgCapitalLinks.
5 * Usage: php cleanupCaps.php [--dry-run]
6 * Options:
7 * --dry-run don't actually try moving them
9 * Copyright © 2005 Brion Vibber <brion@pobox.com>
10 * https://www.mediawiki.org/
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.
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.
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
27 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once __DIR__ . '/cleanupTable.inc';
34 /**
35 * Maintenance script to clean up broken page links when somebody turns
36 * on or off $wgCapitalLinks.
38 * @ingroup Maintenance
40 class CapsCleanup extends TableCleanup {
42 private $user;
43 private $namespace;
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 );
51 public function execute() {
52 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
54 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
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';
64 $this->dryrun = $this->hasOption( 'dry-run' );
66 $this->runTable( [
67 'table' => 'page',
68 'conds' => [ 'page_namespace' => $this->namespace ],
69 'index' => 'page_id',
70 'callback' => $callback ] );
73 protected function processRowToUppercase( $row ) {
74 global $wgContLang;
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" );
83 return $this->progress( 0 );
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 );
91 $ok = $this->movePage(
92 $current,
93 $target,
94 'Converting page title to first-letter uppercase',
95 false
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 );
108 return $this->progress( 0 );
111 protected function processRowToLowercase( $row ) {
112 global $wgContLang;
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" );
121 return $this->progress( 0 );
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" );
129 return $this->progress( 0 );
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 );
144 return $this->progress( 0 );
148 * @param Title $current
149 * @param Title $target
150 * @param string $reason
151 * @param bool $createRedirect
152 * @return bool Success
154 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
155 $display = $current->getPrefixedText();
156 $targetDisplay = $target->getPrefixedText();
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" );
168 return $ok === 'OK';
172 $maintClass = "CapsCleanup";
173 require_once RUN_MAINTENANCE_IF_MAIN;