3 * Clean up broken page links when somebody turns on $wgCapitalLinks.
5 * Usage: php cleanupCaps.php [--dry-run]
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
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 use MediaWiki\MediaWikiServices
;
34 require_once __DIR__
. '/cleanupTable.inc';
37 * Maintenance script to clean up broken page links when somebody turns
38 * on or off $wgCapitalLinks.
40 * @ingroup Maintenance
42 class CleanupCaps
extends TableCleanup
{
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 );
53 public function execute() {
54 $this->user
= User
::newSystemUser( 'Conversion script', [ 'steal' => true ] );
56 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
59 MediaWikiServices
::getInstance()->getNamespaceInfo()->
60 isCapitalized( $this->namespace )
62 $this->output( "Will be moving pages to first letter capitalized titles" );
63 $callback = 'processRowToUppercase';
65 $this->output( "Will be moving pages to first letter lowercase titles" );
66 $callback = 'processRowToLowercase';
69 $this->dryrun
= $this->hasOption( 'dry-run' );
73 'conds' => [ 'page_namespace' => $this->namespace ],
75 'callback' => $callback ] );
78 protected function processRowToUppercase( $row ) {
79 $current = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
80 $display = $current->getPrefixedText();
81 $lower = $row->page_title
;
82 $upper = MediaWikiServices
::getInstance()->getContentLanguage()->ucfirst( $row->page_title
);
83 if ( $upper == $lower ) {
84 $this->output( "\"$display\" already uppercase.\n" );
86 return $this->progress( 0 );
89 $target = Title
::makeTitle( $row->page_namespace
, $upper );
90 if ( $target->exists() ) {
91 // Prefix "CapsCleanup" to bypass the conflict
92 $target = Title
::newFromText( 'CapsCleanup/' . $display );
94 $ok = $this->movePage(
97 'Converting page title to first-letter uppercase',
101 $this->progress( 1 );
102 if ( $row->page_namespace
== $this->namespace ) {
103 $talk = $target->getTalkPage();
104 $row->page_namespace
= $talk->getNamespace();
105 if ( $talk->exists() ) {
106 return $this->processRowToUppercase( $row );
111 return $this->progress( 0 );
114 protected function processRowToLowercase( $row ) {
115 $current = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
116 $display = $current->getPrefixedText();
117 $upper = $row->page_title
;
118 $lower = MediaWikiServices
::getInstance()->getContentLanguage()->lcfirst( $row->page_title
);
119 if ( $upper == $lower ) {
120 $this->output( "\"$display\" already lowercase.\n" );
122 return $this->progress( 0 );
125 $target = Title
::makeTitle( $row->page_namespace
, $lower );
126 if ( $target->exists() ) {
127 $targetDisplay = $target->getPrefixedText();
128 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
130 return $this->progress( 0 );
133 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
134 if ( $ok === true ) {
135 $this->progress( 1 );
136 if ( $row->page_namespace
== $this->namespace ) {
137 $talk = $target->getTalkPage();
138 $row->page_namespace
= $talk->getNamespace();
139 if ( $talk->exists() ) {
140 return $this->processRowToLowercase( $row );
145 return $this->progress( 0 );
149 * @param Title $current
150 * @param Title $target
151 * @param string $reason
152 * @param bool $createRedirect
153 * @return bool Success
155 private function movePage( Title
$current, Title
$target, $reason, $createRedirect ) {
156 $display = $current->getPrefixedText();
157 $targetDisplay = $target->getPrefixedText();
159 if ( $this->dryrun
) {
160 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
163 $mp = new MovePage( $current, $target );
164 $status = $mp->move( $this->user
, $reason, $createRedirect );
165 $ok = $status->isOK() ?
'OK' : $status->getWikiText( false, false, 'en' );
166 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
173 $maintClass = CleanupCaps
::class;
174 require_once RUN_MAINTENANCE_IF_MAIN
;