MediaWiki  1.34.0
cleanupCaps.php
Go to the documentation of this file.
1 <?php
33 
34 require_once __DIR__ . '/cleanupTable.inc';
35 
42 class CleanupCaps extends TableCleanup {
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  $target,
97  'Converting page title to first-letter uppercase',
98  false
99  );
100  if ( $ok ) {
101  $this->progress( 1 );
102  if ( $row->page_namespace == $this->namespace ) {
103  $talk = $target->getTalkPage();
104  $row->page_namespace = $talk->getNamespace();
105  if ( $talk->exists() ) {
106  return $this->processRowToUppercase( $row );
107  }
108  }
109  }
110 
111  return $this->progress( 0 );
112  }
113 
114  protected function processRowToLowercase( $row ) {
115  $current = Title::makeTitle( $row->page_namespace, $row->page_title );
116  $display = $current->getPrefixedText();
117  $upper = $row->page_title;
118  $lower = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $row->page_title );
119  if ( $upper == $lower ) {
120  $this->output( "\"$display\" already lowercase.\n" );
121 
122  return $this->progress( 0 );
123  }
124 
125  $target = Title::makeTitle( $row->page_namespace, $lower );
126  if ( $target->exists() ) {
127  $targetDisplay = $target->getPrefixedText();
128  $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
129 
130  return $this->progress( 0 );
131  }
132 
133  $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
134  if ( $ok === true ) {
135  $this->progress( 1 );
136  if ( $row->page_namespace == $this->namespace ) {
137  $talk = $target->getTalkPage();
138  $row->page_namespace = $talk->getNamespace();
139  if ( $talk->exists() ) {
140  return $this->processRowToLowercase( $row );
141  }
142  }
143  }
144 
145  return $this->progress( 0 );
146  }
147 
155  private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
156  $display = $current->getPrefixedText();
157  $targetDisplay = $target->getPrefixedText();
158 
159  if ( $this->dryrun ) {
160  $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
161  $ok = 'OK';
162  } else {
163  $mp = MediaWikiServices::getInstance()->getMovePageFactory()
164  ->newMovePage( $current, $target );
165  $status = $mp->move( $this->user, $reason, $createRedirect );
166  $ok = $status->isOK() ? 'OK' : $status->getMessage( false, false, 'en' )->text();
167  $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
168  }
169 
170  return $ok === 'OK';
171  }
172 }
173 
174 $maintClass = CleanupCaps::class;
175 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
CleanupCaps\__construct
__construct()
Default constructor.
Definition: cleanupCaps.php:47
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1818
CleanupCaps\execute
execute()
Do the actual work.
Definition: cleanupCaps.php:53
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:737
CleanupCaps
Maintenance script to clean up broken page links when somebody turns on or off $wgCapitalLinks.
Definition: cleanupCaps.php:42
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:74
$maintClass
$maintClass
Definition: cleanupCaps.php:174
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
CleanupCaps\processRowToUppercase
processRowToUppercase( $row)
Definition: cleanupCaps.php:78
CleanupCaps\$user
$user
Definition: cleanupCaps.php:44
TableCleanup\runTable
runTable( $params)
Definition: cleanupTable.inc:108
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
CleanupCaps\movePage
movePage(Title $current, Title $target, $reason, $createRedirect)
Definition: cleanupCaps.php:155
CleanupCaps\$namespace
$namespace
Definition: cleanupCaps.php:45
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
CleanupCaps\processRowToLowercase
processRowToLowercase( $row)
Definition: cleanupCaps.php:114
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288