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" );
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
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
Maintenance script to clean up broken page links when somebody turns on or off $wgCapitalLinks.
processRowToUppercase( $row)
__construct()
Default constructor.
processRowToLowercase( $row)
execute()
Do the actual work.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
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:1616
exists( $flags=0)
Check if page exists.
Definition Title.php:3138
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1858
internal since 1.36
Definition User.php:93
Generic class to cleanup a database table.
progress( $updated)
runTable( $params)
$maintClass