MediaWiki  1.29.0
cleanupCaps.php
Go to the documentation of this file.
1 <?php
32 require_once __DIR__ . '/cleanupTable.inc';
33 
40 class CapsCleanup extends TableCleanup {
41 
42  private $user;
43  private $namespace;
44 
45  public function __construct() {
46  parent::__construct();
47  $this->addDescription( 'Script to cleanup capitalization' );
48  $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
49  }
50 
51  public function execute() {
52  $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
53 
54  $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
55 
56  if ( MWNamespace::isCapitalized( $this->namespace ) ) {
57  $this->output( "Will be moving pages to first letter capitalized titles" );
58  $callback = 'processRowToUppercase';
59  } else {
60  $this->output( "Will be moving pages to first letter lowercase titles" );
61  $callback = 'processRowToLowercase';
62  }
63 
64  $this->dryrun = $this->hasOption( 'dry-run' );
65 
66  $this->runTable( [
67  'table' => 'page',
68  'conds' => [ 'page_namespace' => $this->namespace ],
69  'index' => 'page_id',
70  'callback' => $callback ] );
71  }
72 
73  protected function processRowToUppercase( $row ) {
75 
76  $current = Title::makeTitle( $row->page_namespace, $row->page_title );
77  $display = $current->getPrefixedText();
78  $lower = $row->page_title;
79  $upper = $wgContLang->ucfirst( $row->page_title );
80  if ( $upper == $lower ) {
81  $this->output( "\"$display\" already uppercase.\n" );
82 
83  return $this->progress( 0 );
84  }
85 
86  $target = Title::makeTitle( $row->page_namespace, $upper );
87  if ( $target->exists() ) {
88  // Prefix "CapsCleanup" to bypass the conflict
89  $target = Title::newFromText( __CLASS__ . '/' . $display );
90  }
91  $ok = $this->movePage(
92  $current,
93  $target,
94  'Converting page title to first-letter uppercase',
95  false
96  );
97  if ( $ok ) {
98  $this->progress( 1 );
99  if ( $row->page_namespace == $this->namespace ) {
100  $talk = $target->getTalkPage();
101  $row->page_namespace = $talk->getNamespace();
102  if ( $talk->exists() ) {
103  return $this->processRowToUppercase( $row );
104  }
105  }
106  }
107 
108  return $this->progress( 0 );
109  }
110 
111  protected function processRowToLowercase( $row ) {
113 
114  $current = Title::makeTitle( $row->page_namespace, $row->page_title );
115  $display = $current->getPrefixedText();
116  $upper = $row->page_title;
117  $lower = $wgContLang->lcfirst( $row->page_title );
118  if ( $upper == $lower ) {
119  $this->output( "\"$display\" already lowercase.\n" );
120 
121  return $this->progress( 0 );
122  }
123 
124  $target = Title::makeTitle( $row->page_namespace, $lower );
125  if ( $target->exists() ) {
126  $targetDisplay = $target->getPrefixedText();
127  $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
128 
129  return $this->progress( 0 );
130  }
131 
132  $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
133  if ( $ok === true ) {
134  $this->progress( 1 );
135  if ( $row->page_namespace == $this->namespace ) {
136  $talk = $target->getTalkPage();
137  $row->page_namespace = $talk->getNamespace();
138  if ( $talk->exists() ) {
139  return $this->processRowToLowercase( $row );
140  }
141  }
142  }
143 
144  return $this->progress( 0 );
145  }
146 
154  private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
155  $display = $current->getPrefixedText();
156  $targetDisplay = $target->getPrefixedText();
157 
158  if ( $this->dryrun ) {
159  $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
160  $ok = 'OK';
161  } else {
162  $mp = new MovePage( $current, $target );
163  $status = $mp->move( $this->user, $reason, $createRedirect );
164  $ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
165  $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
166  }
167 
168  return $ok === 'OK';
169  }
170 }
171 
172 $maintClass = "CapsCleanup";
173 require_once RUN_MAINTENANCE_IF_MAIN;
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:265
CapsCleanup\processRowToUppercase
processRowToUppercase( $row)
Definition: cleanupCaps.php:73
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1451
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
CapsCleanup\movePage
movePage(Title $current, Title $target, $reason, $createRedirect)
Definition: cleanupCaps.php:154
CapsCleanup\processRowToLowercase
processRowToLowercase( $row)
Definition: cleanupCaps.php:111
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:684
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
MovePage
Handles the backend logic of moving a page from one title to another.
Definition: MovePage.php:30
CapsCleanup\$user
$user
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:215
CapsCleanup\execute
execute()
Do the actual work.
Definition: cleanupCaps.php:51
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
TableCleanup\progress
progress( $updated)
Definition: cleanupTable.inc:74
$maintClass
$maintClass
Definition: cleanupCaps.php:172
TableCleanup
Generic class to cleanup a database table.
Definition: cleanupTable.inc:31
TableCleanup\runTable
runTable( $params)
Definition: cleanupTable.inc:108
Title
Represents a title within MediaWiki.
Definition: Title.php:39
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
MWNamespace\isCapitalized
static isCapitalized( $index)
Is the namespace first-letter capitalized?
Definition: MWNamespace.php:383
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
CapsCleanup
Maintenance script to clean up broken page links when somebody turns on or off $wgCapitalLinks.
Definition: cleanupCaps.php:40
CapsCleanup\__construct
__construct()
Default constructor.
Definition: cleanupCaps.php:45
CapsCleanup\$namespace
$namespace
Definition: cleanupCaps.php:43
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56