MediaWiki master
cleanupCaps.php
Go to the documentation of this file.
1<?php
34
35require_once __DIR__ . '/TableCleanup.php';
36
44
45 private $user;
46 private $namespace;
47
48 public function __construct() {
49 parent::__construct();
50 $this->addDescription( 'Script to cleanup capitalization' );
51 $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
52 }
53
54 public function execute() {
55 $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
56
57 $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
58
59 if (
60 $this->getServiceContainer()->getNamespaceInfo()->
61 isCapitalized( $this->namespace )
62 ) {
63 $this->output( "Will be moving pages to first letter capitalized titles" );
64 $callback = 'processRowToUppercase';
65 } else {
66 $this->output( "Will be moving pages to first letter lowercase titles" );
67 $callback = 'processRowToLowercase';
68 }
69
70 $this->dryrun = $this->hasOption( 'dry-run' );
71
72 $this->runTable( [
73 'table' => 'page',
74 'conds' => [ 'page_namespace' => $this->namespace ],
75 'index' => 'page_id',
76 'callback' => $callback ] );
77 }
78
79 protected function processRowToUppercase( $row ) {
80 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
81 $display = $current->getPrefixedText();
82 $lower = $row->page_title;
83 $upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title );
84 if ( $upper == $lower ) {
85 $this->output( "\"$display\" already uppercase.\n" );
86
87 $this->progress( 0 );
88 return;
89 }
90
91 $target = Title::makeTitle( $row->page_namespace, $upper );
92 if ( $target->exists() ) {
93 // Prefix "CapsCleanup" to bypass the conflict
94 $target = Title::newFromText( 'CapsCleanup/' . $display );
95 }
96 $ok = $this->movePage(
97 $current,
98 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
99 $target,
100 'Converting page title to first-letter uppercase',
101 false
102 );
103 if ( $ok ) {
104 $this->progress( 1 );
105 if ( $row->page_namespace == $this->namespace ) {
106 $talk = $target->getTalkPage();
107 $row->page_namespace = $talk->getNamespace();
108 if ( $talk->exists() ) {
109 $this->processRowToUppercase( $row );
110 return;
111 }
112 }
113 }
114
115 $this->progress( 0 );
116 }
117
118 protected function processRowToLowercase( $row ) {
119 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
120 $display = $current->getPrefixedText();
121 $upper = $row->page_title;
122 $lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title );
123 if ( $upper == $lower ) {
124 $this->output( "\"$display\" already lowercase.\n" );
125
126 $this->progress( 0 );
127 return;
128 }
129
130 $target = Title::makeTitle( $row->page_namespace, $lower );
131 if ( $target->exists() ) {
132 $targetDisplay = $target->getPrefixedText();
133 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
134
135 $this->progress( 0 );
136 return;
137 }
138
139 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
140 if ( $ok === true ) {
141 $this->progress( 1 );
142 if ( $row->page_namespace == $this->namespace ) {
143 $talk = $target->getTalkPage();
144 $row->page_namespace = $talk->getNamespace();
145 if ( $talk->exists() ) {
146 $this->processRowToLowercase( $row );
147 return;
148 }
149 }
150 }
151
152 $this->progress( 0 );
153 }
154
162 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
163 $display = $current->getPrefixedText();
164 $targetDisplay = $target->getPrefixedText();
165
166 if ( $this->dryrun ) {
167 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
168 $ok = 'OK';
169 } else {
170 $mp = $this->getServiceContainer()->getMovePageFactory()
171 ->newMovePage( $current, $target );
172 $status = $mp->move( $this->user, $reason, $createRedirect );
173 $ok = $status->isOK() ? 'OK' : 'FAILED';
174 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
175 if ( !$status->isOK() ) {
176 $this->error( $status );
177 }
178 }
179
180 return $ok === 'OK';
181 }
182}
183
184$maintClass = CleanupCaps::class;
185require_once RUN_MAINTENANCE_IF_MAIN;
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:79
getTalkPage()
Get a Title object associated with the talk page of this article.
Definition Title.php:1620
exists( $flags=0)
Check if page exists.
Definition Title.php:3146
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1862
internal since 1.36
Definition User.php:94
Generic class to cleanup a database table.
progress( $updated)
runTable( $params)
$maintClass