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 Brooke Vibber <bvibber@wikimedia.org>
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 Brooke Vibber <bvibber@wikimedia.org>
29 * @ingroup Maintenance
32 use MediaWiki\Title\Title
;
33 use MediaWiki\User\User
;
35 // @codeCoverageIgnoreStart
36 require_once __DIR__
. '/TableCleanup.php';
37 // @codeCoverageIgnoreEnd
40 * Maintenance script to clean up broken page links when somebody turns
41 * on or off $wgCapitalLinks.
43 * @ingroup Maintenance
45 class CleanupCaps
extends TableCleanup
{
52 public function __construct() {
53 parent
::__construct();
54 $this->addDescription( 'Script to cleanup capitalization' );
55 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
58 public function execute() {
59 $this->user
= User
::newSystemUser( 'Conversion script', [ 'steal' => true ] );
61 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
64 $this->getServiceContainer()->getNamespaceInfo()->
65 isCapitalized( $this->namespace )
67 $this->output( "Will be moving pages to first letter capitalized titles" );
68 $callback = 'processRowToUppercase';
70 $this->output( "Will be moving pages to first letter lowercase titles" );
71 $callback = 'processRowToLowercase';
74 $this->dryrun
= $this->hasOption( 'dry-run' );
78 'conds' => [ 'page_namespace' => $this->namespace ],
80 'callback' => $callback ] );
83 protected function processRowToUppercase( $row ) {
84 $current = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
85 $display = $current->getPrefixedText();
86 $lower = $row->page_title
;
87 $upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title
);
88 if ( $upper == $lower ) {
89 $this->output( "\"$display\" already uppercase.\n" );
95 $target = Title
::makeTitle( $row->page_namespace
, $upper );
96 if ( $target->exists() ) {
97 // Prefix "CapsCleanup" to bypass the conflict
98 $target = Title
::newFromText( 'CapsCleanup/' . $display );
100 $ok = $this->movePage(
102 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
104 'Converting page title to first-letter uppercase',
108 $this->progress( 1 );
109 if ( $row->page_namespace
== $this->namespace ) {
110 $talk = $target->getTalkPage();
111 $row->page_namespace
= $talk->getNamespace();
112 if ( $talk->exists() ) {
113 $this->processRowToUppercase( $row );
119 $this->progress( 0 );
122 protected function processRowToLowercase( $row ) {
123 $current = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
124 $display = $current->getPrefixedText();
125 $upper = $row->page_title
;
126 $lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title
);
127 if ( $upper == $lower ) {
128 $this->output( "\"$display\" already lowercase.\n" );
130 $this->progress( 0 );
134 $target = Title
::makeTitle( $row->page_namespace
, $lower );
135 if ( $target->exists() ) {
136 $targetDisplay = $target->getPrefixedText();
137 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
139 $this->progress( 0 );
143 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
144 if ( $ok === true ) {
145 $this->progress( 1 );
146 if ( $row->page_namespace
== $this->namespace ) {
147 $talk = $target->getTalkPage();
148 $row->page_namespace
= $talk->getNamespace();
149 if ( $talk->exists() ) {
150 $this->processRowToLowercase( $row );
156 $this->progress( 0 );
160 * @param Title $current
161 * @param Title $target
162 * @param string $reason
163 * @param bool $createRedirect
164 * @return bool Success
166 private function movePage( Title
$current, Title
$target, $reason, $createRedirect ) {
167 $display = $current->getPrefixedText();
168 $targetDisplay = $target->getPrefixedText();
170 if ( $this->dryrun
) {
171 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
174 $mp = $this->getServiceContainer()->getMovePageFactory()
175 ->newMovePage( $current, $target );
176 $status = $mp->move( $this->user
, $reason, $createRedirect );
177 $ok = $status->isOK() ?
'OK' : 'FAILED';
178 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
179 if ( !$status->isOK() ) {
180 $this->error( $status );
188 // @codeCoverageIgnoreStart
189 $maintClass = CleanupCaps
::class;
190 require_once RUN_MAINTENANCE_IF_MAIN
;
191 // @codeCoverageIgnoreEnd