MediaWiki master
cleanupCaps.php
Go to the documentation of this file.
1<?php
34
35// @codeCoverageIgnoreStart
36require_once __DIR__ . '/TableCleanup.php';
37// @codeCoverageIgnoreEnd
38
46
48 private $user;
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\n" );
68 $callback = 'processRowToUppercase';
69 } else {
70 $this->output( "Will be moving pages to first letter lowercase titles\n" );
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( \stdClass $row ) {
84 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
85 // Set the ID of the page because Title::exists will return false
86 // unless the Article ID is already known, because Title::canExist will be false
87 // when $wgCapitalLinks is true and the old title is in lower case.
88 $current->mArticleID = $row->page_id;
89 $display = $current->getPrefixedText();
90 $lower = $row->page_title;
91 $upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title );
92 if ( $upper == $lower ) {
93 $this->output( "\"$display\" already uppercase.\n" );
94
95 $this->progress( 0 );
96 return;
97 }
98
99 $target = Title::makeTitle( $row->page_namespace, $upper );
100 if ( $target->exists() ) {
101 // Prefix "CapsCleanup" to bypass the conflict
102 $target = Title::newFromText( 'CapsCleanup/' . $display );
103 }
104 $ok = $this->movePage(
105 $current,
106 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
107 $target,
108 'Converting page title to first-letter uppercase',
109 false
110 );
111 if ( !$ok ) {
112 $this->progress( 0 );
113 return;
114 }
115
116 $this->progress( 1 );
117 if ( $row->page_namespace == $this->namespace ) {
118 // We need to fetch the existence of the talk page from the DB directly because
119 // Title::exists will return false if Title::canExist returns false. Title::canExist will
120 // return false if $wgCapitalLinks is true and the old title is in lowercase form.
121 $namespaceInfo = $this->getServiceContainer()->getNamespaceInfo();
122 if ( $namespaceInfo->canHaveTalkPage( $current ) ) {
123 $talkNamespace = $namespaceInfo->getTalk( $row->page_namespace );
124 $talkPageExists = $this->getReplicaDB()->newSelectQueryBuilder()
125 ->select( '1' )
126 ->from( 'page' )
127 ->where( [ 'page_title' => $row->page_title, 'page_namespace' => $talkNamespace ] )
128 ->caller( __METHOD__ )
129 ->fetchField();
130 if ( $talkPageExists ) {
131 $row->page_namespace = $talkNamespace;
132 $this->processRowToUppercase( $row );
133 }
134 }
135 }
136 }
137
138 protected function processRowToLowercase( \stdClass $row ) {
139 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
140 $display = $current->getPrefixedText();
141 $upper = $row->page_title;
142 $lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title );
143 if ( $upper == $lower ) {
144 $this->output( "\"$display\" already lowercase.\n" );
145
146 $this->progress( 0 );
147 return;
148 }
149
150 $target = Title::makeTitle( $row->page_namespace, $lower );
151 if ( $target->exists() ) {
152 $targetDisplay = $target->getPrefixedText();
153 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
154
155 $this->progress( 0 );
156 return;
157 }
158
159 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
160 if ( $ok !== true ) {
161 $this->progress( 0 );
162 }
163
164 $this->progress( 1 );
165 if ( $row->page_namespace == $this->namespace ) {
166 $talk = $current->getTalkPage();
167 if ( $talk->exists() ) {
168 $row->page_namespace = $talk->getNamespace();
169 $this->processRowToLowercase( $row );
170 }
171 }
172 }
173
181 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
182 $display = $current->getPrefixedText();
183 $targetDisplay = $target->getPrefixedText();
184
185 if ( $this->dryrun ) {
186 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
187 $ok = 'OK';
188 } else {
189 $mp = $this->getServiceContainer()->getMovePageFactory()
190 ->newMovePage( $current, $target );
191 $status = $mp->move( $this->user, $reason, $createRedirect );
192 $ok = $status->isOK() ? 'OK' : 'FAILED';
193 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
194 if ( !$status->isOK() ) {
195 $this->error( $status );
196 }
197 }
198
199 return $ok === 'OK';
200 }
201}
202
203// @codeCoverageIgnoreStart
204$maintClass = CleanupCaps::class;
205require_once RUN_MAINTENANCE_IF_MAIN;
206// @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.
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:78
getTalkPage()
Get a Title object associated with the talk page of this article.
Definition Title.php:1617
exists( $flags=0)
Check if page exists.
Definition Title.php:3152
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1859
User class for the MediaWiki software.
Definition User.php:123
Generic class to cleanup a database table.
progress( $updated)
runTable( $params)
$maintClass