MediaWiki master
cleanupCaps.php
Go to the documentation of this file.
1<?php
20
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/TableCleanup.php';
23// @codeCoverageIgnoreEnd
24
32
34 private $user;
36 private $namespace;
37
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Script to cleanup capitalization' );
41 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
42 }
43
44 public function execute() {
45 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
46
47 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
48
49 if (
50 $this->getServiceContainer()->getNamespaceInfo()->
51 isCapitalized( $this->namespace )
52 ) {
53 $this->output( "Will be moving pages to first letter capitalized titles\n" );
54 $callback = 'processRowToUppercase';
55 } else {
56 $this->output( "Will be moving pages to first letter lowercase titles\n" );
57 $callback = 'processRowToLowercase';
58 }
59
60 $this->dryrun = $this->hasOption( 'dry-run' );
61
62 $this->runTable( [
63 'table' => 'page',
64 'conds' => [ 'page_namespace' => $this->namespace ],
65 'index' => 'page_id',
66 'callback' => $callback ] );
67 }
68
69 protected function processRowToUppercase( \stdClass $row ) {
70 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
71 // Set the ID of the page because Title::exists will return false
72 // unless the Article ID is already known, because Title::canExist will be false
73 // when $wgCapitalLinks is true and the old title is in lower case.
74 $current->mArticleID = $row->page_id;
75 $display = $current->getPrefixedText();
76 $lower = $row->page_title;
77 $upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title );
78 if ( $upper == $lower ) {
79 $this->output( "\"$display\" already uppercase.\n" );
80
81 $this->progress( 0 );
82 return;
83 }
84
85 $target = Title::makeTitle( $row->page_namespace, $upper );
86 if ( $target->exists() ) {
87 // Prefix "CapsCleanup" to bypass the conflict
88 $target = Title::newFromText( 'CapsCleanup/' . $display );
89 }
90 $ok = $this->movePage(
91 $current,
92 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
93 $target,
94 'Converting page title to first-letter uppercase',
95 false
96 );
97 if ( !$ok ) {
98 $this->progress( 0 );
99 return;
100 }
101
102 $this->progress( 1 );
103 if ( $row->page_namespace == $this->namespace ) {
104 // We need to fetch the existence of the talk page from the DB directly because
105 // Title::exists will return false if Title::canExist returns false. Title::canExist will
106 // return false if $wgCapitalLinks is true and the old title is in lowercase form.
107 $namespaceInfo = $this->getServiceContainer()->getNamespaceInfo();
108 if ( $namespaceInfo->canHaveTalkPage( $current ) ) {
109 $talkNamespace = $namespaceInfo->getTalk( $row->page_namespace );
110 $talkPageExists = $this->getReplicaDB()->newSelectQueryBuilder()
111 ->select( '1' )
112 ->from( 'page' )
113 ->where( [ 'page_title' => $row->page_title, 'page_namespace' => $talkNamespace ] )
114 ->caller( __METHOD__ )
115 ->fetchField();
116 if ( $talkPageExists ) {
117 $row->page_namespace = $talkNamespace;
118 $this->processRowToUppercase( $row );
119 }
120 }
121 }
122 }
123
124 protected function processRowToLowercase( \stdClass $row ) {
125 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
126 $display = $current->getPrefixedText();
127 $upper = $row->page_title;
128 $lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title );
129 if ( $upper == $lower ) {
130 $this->output( "\"$display\" already lowercase.\n" );
131
132 $this->progress( 0 );
133 return;
134 }
135
136 $target = Title::makeTitle( $row->page_namespace, $lower );
137 if ( $target->exists() ) {
138 $targetDisplay = $target->getPrefixedText();
139 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
140
141 $this->progress( 0 );
142 return;
143 }
144
145 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
146 if ( $ok !== true ) {
147 $this->progress( 0 );
148 }
149
150 $this->progress( 1 );
151 if ( $row->page_namespace == $this->namespace ) {
152 $talk = $current->getTalkPage();
153 if ( $talk->exists() ) {
154 $row->page_namespace = $talk->getNamespace();
155 $this->processRowToLowercase( $row );
156 }
157 }
158 }
159
167 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
168 $display = $current->getPrefixedText();
169 $targetDisplay = $target->getPrefixedText();
170
171 if ( $this->dryrun ) {
172 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
173 $ok = 'OK';
174 } else {
175 $mp = $this->getServiceContainer()->getMovePageFactory()
176 ->newMovePage( $current, $target );
177 $status = $mp->move( $this->user, $reason, $createRedirect );
178 $ok = $status->isOK() ? 'OK' : 'FAILED';
179 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
180 if ( !$status->isOK() ) {
181 $this->error( $status );
182 }
183 }
184
185 return $ok === 'OK';
186 }
187}
188
189// @codeCoverageIgnoreStart
190$maintClass = CleanupCaps::class;
191require_once RUN_MAINTENANCE_IF_MAIN;
192// @codeCoverageIgnoreEnd
Maintenance script to clean up broken page links when somebody turns on or off $wgCapitalLinks.
processRowToUppercase(\stdClass $row)
__construct()
Default constructor.
processRowToLowercase(\stdClass $row)
execute()
Do the actual work.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
getTalkPage()
Get a Title object associated with the talk page of this article.
Definition Title.php:1615
exists( $flags=0)
Check if page exists.
Definition Title.php:3129
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1857
User class for the MediaWiki software.
Definition User.php:110
Generic class to cleanup a database table.
progress( $updated)
runTable( $params)
$maintClass