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