MediaWiki REL1_40
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 MediaWikiServices::getInstance()->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 = MediaWikiServices::getInstance()->getContentLanguage()->ucfirst( $row->page_title );
84 if ( $upper == $lower ) {
85 $this->output( "\"$display\" already uppercase.\n" );
86
87 return $this->progress( 0 );
88 }
89
90 $target = Title::makeTitle( $row->page_namespace, $upper );
91 if ( $target->exists() ) {
92 // Prefix "CapsCleanup" to bypass the conflict
93 $target = Title::newFromText( 'CapsCleanup/' . $display );
94 }
95 $ok = $this->movePage(
96 $current,
97 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
98 $target,
99 'Converting page title to first-letter uppercase',
100 false
101 );
102 if ( $ok ) {
103 $this->progress( 1 );
104 if ( $row->page_namespace == $this->namespace ) {
105 $talk = $target->getTalkPage();
106 $row->page_namespace = $talk->getNamespace();
107 if ( $talk->exists() ) {
108 return $this->processRowToUppercase( $row );
109 }
110 }
111 }
112
113 return $this->progress( 0 );
114 }
115
116 protected function processRowToLowercase( $row ) {
117 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
118 $display = $current->getPrefixedText();
119 $upper = $row->page_title;
120 $lower = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $row->page_title );
121 if ( $upper == $lower ) {
122 $this->output( "\"$display\" already lowercase.\n" );
123
124 return $this->progress( 0 );
125 }
126
127 $target = Title::makeTitle( $row->page_namespace, $lower );
128 if ( $target->exists() ) {
129 $targetDisplay = $target->getPrefixedText();
130 $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
131
132 return $this->progress( 0 );
133 }
134
135 $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
136 if ( $ok === true ) {
137 $this->progress( 1 );
138 if ( $row->page_namespace == $this->namespace ) {
139 $talk = $target->getTalkPage();
140 $row->page_namespace = $talk->getNamespace();
141 if ( $talk->exists() ) {
142 return $this->processRowToLowercase( $row );
143 }
144 }
145 }
146
147 return $this->progress( 0 );
148 }
149
157 private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
158 $display = $current->getPrefixedText();
159 $targetDisplay = $target->getPrefixedText();
160
161 if ( $this->dryrun ) {
162 $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
163 $ok = 'OK';
164 } else {
165 $mp = MediaWikiServices::getInstance()->getMovePageFactory()
166 ->newMovePage( $current, $target );
167 $status = $mp->move( $this->user, $reason, $createRedirect );
168 $ok = $status->isOK() ? 'OK' : $status->getMessage( false, false, 'en' )->text();
169 $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
170 }
171
172 return $ok === 'OK';
173 }
174}
175
176$maintClass = CleanupCaps::class;
177require_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.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
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.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
getTalkPage()
Get a Title object associated with the talk page of this article.
Definition Title.php:1671
exists( $flags=0)
Check if page exists.
Definition Title.php:3523
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1923
Generic class to cleanup a database table.
progress( $updated)
runTable( $params)
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:793
$maintClass