Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CleanupCaps
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 5
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 processRowToUppercase
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
42
 processRowToLowercase
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
 movePage
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
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 Brooke Vibber <bvibber@wikimedia.org>
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 Brooke Vibber <bvibber@wikimedia.org>
29 * @ingroup Maintenance
30 */
31
32use MediaWiki\Title\Title;
33use MediaWiki\User\User;
34
35// @codeCoverageIgnoreStart
36require_once __DIR__ . '/TableCleanup.php';
37// @codeCoverageIgnoreEnd
38
39/**
40 * Maintenance script to clean up broken page links when somebody turns
41 * on or off $wgCapitalLinks.
42 *
43 * @ingroup Maintenance
44 */
45class CleanupCaps extends TableCleanup {
46
47    /** @var User */
48    private $user;
49    /** @var int */
50    private $namespace;
51
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 );
56    }
57
58    public function execute() {
59        $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
60
61        $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
62
63        if (
64            $this->getServiceContainer()->getNamespaceInfo()->
65                isCapitalized( $this->namespace )
66        ) {
67            $this->output( "Will be moving pages to first letter capitalized titles" );
68            $callback = 'processRowToUppercase';
69        } else {
70            $this->output( "Will be moving pages to first letter lowercase titles" );
71            $callback = 'processRowToLowercase';
72        }
73
74        $this->dryrun = $this->hasOption( 'dry-run' );
75
76        $this->runTable( [
77            'table' => 'page',
78            'conds' => [ 'page_namespace' => $this->namespace ],
79            'index' => 'page_id',
80            'callback' => $callback ] );
81    }
82
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" );
90
91            $this->progress( 0 );
92            return;
93        }
94
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 );
99        }
100        $ok = $this->movePage(
101            $current,
102            // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
103            $target,
104            'Converting page title to first-letter uppercase',
105            false
106        );
107        if ( $ok ) {
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 );
114                    return;
115                }
116            }
117        }
118
119        $this->progress( 0 );
120    }
121
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" );
129
130            $this->progress( 0 );
131            return;
132        }
133
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" );
138
139            $this->progress( 0 );
140            return;
141        }
142
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 );
151                    return;
152                }
153            }
154        }
155
156        $this->progress( 0 );
157    }
158
159    /**
160     * @param Title $current
161     * @param Title $target
162     * @param string $reason
163     * @param bool $createRedirect
164     * @return bool Success
165     */
166    private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
167        $display = $current->getPrefixedText();
168        $targetDisplay = $target->getPrefixedText();
169
170        if ( $this->dryrun ) {
171            $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
172            $ok = 'OK';
173        } else {
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 );
181            }
182        }
183
184        return $ok === 'OK';
185    }
186}
187
188// @codeCoverageIgnoreStart
189$maintClass = CleanupCaps::class;
190require_once RUN_MAINTENANCE_IF_MAIN;
191// @codeCoverageIgnoreEnd