2005-06-30 00:37:56 +00:00
|
|
|
<?php
|
2010-09-05 13:15:48 +00:00
|
|
|
/**
|
2012-06-25 19:54:41 +00:00
|
|
|
* Clean up broken page links when somebody turns on $wgCapitalLinks.
|
2005-06-30 00:37:56 +00:00
|
|
|
*
|
|
|
|
|
* Usage: php cleanupCaps.php [--dry-run]
|
|
|
|
|
* Options:
|
|
|
|
|
* --dry-run don't actually try moving them
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2024-02-09 01:02:16 +00:00
|
|
|
* Copyright © 2005 Brooke Vibber <bvibber@wikimedia.org>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-30 00:37:56 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2006-01-07 13:09:30 +00:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2005-06-30 00:37:56 +00:00
|
|
|
* (at your option) any later version.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-30 00:37:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-30 00:37:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-04-05 07:43:17 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-06-30 00:37:56 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* @file
|
2024-02-09 01:02:16 +00:00
|
|
|
* @author Brooke Vibber <bvibber@wikimedia.org>
|
2010-09-05 13:15:48 +00:00
|
|
|
* @ingroup Maintenance
|
2005-06-30 00:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2018-07-29 12:24:54 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-09-23 10:49:16 +00:00
|
|
|
require_once __DIR__ . '/TableCleanup.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2005-06-30 00:37:56 +00:00
|
|
|
|
2012-06-25 19:54:41 +00:00
|
|
|
/**
|
2015-06-06 04:42:45 +00:00
|
|
|
* Maintenance script to clean up broken page links when somebody turns
|
|
|
|
|
* on or off $wgCapitalLinks.
|
2012-06-25 19:54:41 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2018-05-24 01:34:01 +00:00
|
|
|
class CleanupCaps extends TableCleanup {
|
2014-10-11 05:43:02 +00:00
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var User */
|
2014-10-11 05:43:02 +00:00
|
|
|
private $user;
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var int */
|
2015-06-06 04:42:45 +00:00
|
|
|
private $namespace;
|
2014-10-11 05:43:02 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Script to cleanup capitalization' );
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
public function execute() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
|
2011-12-28 19:49:44 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->namespace = intval( $this->getOption( 'namespace', 0 ) );
|
2015-06-06 04:42:45 +00:00
|
|
|
|
2018-08-05 17:58:51 +00:00
|
|
|
if (
|
2023-08-31 09:21:12 +00:00
|
|
|
$this->getServiceContainer()->getNamespaceInfo()->
|
2018-08-05 17:58:51 +00:00
|
|
|
isCapitalized( $this->namespace )
|
|
|
|
|
) {
|
2015-06-06 04:42:45 +00:00
|
|
|
$this->output( "Will be moving pages to first letter capitalized titles" );
|
|
|
|
|
$callback = 'processRowToUppercase';
|
|
|
|
|
} else {
|
|
|
|
|
$this->output( "Will be moving pages to first letter lowercase titles" );
|
|
|
|
|
$callback = 'processRowToLowercase';
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->dryrun = $this->hasOption( 'dry-run' );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->runTable( [
|
2009-09-24 04:19:25 +00:00
|
|
|
'table' => 'page',
|
2016-02-17 09:09:32 +00:00
|
|
|
'conds' => [ 'page_namespace' => $this->namespace ],
|
2009-09-24 04:19:25 +00:00
|
|
|
'index' => 'page_id',
|
2015-06-06 04:42:45 +00:00
|
|
|
'callback' => $callback ] );
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2015-06-06 04:42:45 +00:00
|
|
|
protected function processRowToUppercase( $row ) {
|
|
|
|
|
$current = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
$display = $current->getPrefixedText();
|
|
|
|
|
$lower = $row->page_title;
|
2023-08-31 09:21:12 +00:00
|
|
|
$upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title );
|
2015-06-06 04:42:45 +00:00
|
|
|
if ( $upper == $lower ) {
|
|
|
|
|
$this->output( "\"$display\" already uppercase.\n" );
|
|
|
|
|
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->progress( 0 );
|
|
|
|
|
return;
|
2015-06-06 04:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$target = Title::makeTitle( $row->page_namespace, $upper );
|
|
|
|
|
if ( $target->exists() ) {
|
|
|
|
|
// Prefix "CapsCleanup" to bypass the conflict
|
2018-05-24 01:34:01 +00:00
|
|
|
$target = Title::newFromText( 'CapsCleanup/' . $display );
|
2015-06-06 04:42:45 +00:00
|
|
|
}
|
|
|
|
|
$ok = $this->movePage(
|
|
|
|
|
$current,
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid
|
2015-06-06 04:42:45 +00:00
|
|
|
$target,
|
|
|
|
|
'Converting page title to first-letter uppercase',
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
if ( $ok ) {
|
|
|
|
|
$this->progress( 1 );
|
|
|
|
|
if ( $row->page_namespace == $this->namespace ) {
|
|
|
|
|
$talk = $target->getTalkPage();
|
|
|
|
|
$row->page_namespace = $talk->getNamespace();
|
|
|
|
|
if ( $talk->exists() ) {
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->processRowToUppercase( $row );
|
|
|
|
|
return;
|
2015-06-06 04:42:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->progress( 0 );
|
2015-06-06 04:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function processRowToLowercase( $row ) {
|
2005-06-30 00:37:56 +00:00
|
|
|
$current = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
$display = $current->getPrefixedText();
|
|
|
|
|
$upper = $row->page_title;
|
2023-08-31 09:21:12 +00:00
|
|
|
$lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $upper == $lower ) {
|
2009-09-24 04:19:25 +00:00
|
|
|
$this->output( "\"$display\" already lowercase.\n" );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->progress( 0 );
|
|
|
|
|
return;
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-30 00:37:56 +00:00
|
|
|
$target = Title::makeTitle( $row->page_namespace, $lower );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $target->exists() ) {
|
2015-06-06 04:42:45 +00:00
|
|
|
$targetDisplay = $target->getPrefixedText();
|
2009-09-24 04:19:25 +00:00
|
|
|
$this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->progress( 0 );
|
|
|
|
|
return;
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2015-06-06 04:42:45 +00:00
|
|
|
$ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $ok === true ) {
|
2005-06-30 00:37:56 +00:00
|
|
|
$this->progress( 1 );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $row->page_namespace == $this->namespace ) {
|
2005-10-22 20:52:30 +00:00
|
|
|
$talk = $target->getTalkPage();
|
|
|
|
|
$row->page_namespace = $talk->getNamespace();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $talk->exists() ) {
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->processRowToLowercase( $row );
|
|
|
|
|
return;
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2023-03-24 00:27:05 +00:00
|
|
|
$this->progress( 0 );
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
2015-06-06 04:42:45 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Title $current
|
|
|
|
|
* @param Title $target
|
|
|
|
|
* @param string $reason
|
|
|
|
|
* @param bool $createRedirect
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
|
|
|
|
|
$display = $current->getPrefixedText();
|
|
|
|
|
$targetDisplay = $target->getPrefixedText();
|
|
|
|
|
|
|
|
|
|
if ( $this->dryrun ) {
|
|
|
|
|
$this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
|
|
|
|
|
$ok = 'OK';
|
|
|
|
|
} else {
|
2023-08-31 09:21:12 +00:00
|
|
|
$mp = $this->getServiceContainer()->getMovePageFactory()
|
2019-05-01 11:39:45 +00:00
|
|
|
->newMovePage( $current, $target );
|
2015-06-06 04:42:45 +00:00
|
|
|
$status = $mp->move( $this->user, $reason, $createRedirect );
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
$ok = $status->isOK() ? 'OK' : 'FAILED';
|
2015-06-06 04:42:45 +00:00
|
|
|
$this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
|
Maintenance: Print errors from StatusValue objects in a consistent way
Allow Maintenance::error() and Maintenance::fatalError() to take
StatusValue objects. They now print each error message from the
status on a separate line, in English, ignoring on-wiki message
overrides, as wikitext but after parser function expansion.
Thoughts on the previously commonly used methods:
- $status->getMessage( false, false, 'en' )->text()
Almost the same as the new output, but it allows on-wiki message
overrides, and if there is more than one error, it prefixes each
line with a '*' (like a wikitext list).
- $status->getMessage( false, false, 'en' )->plain()
- $status->getWikiText( false, false, 'en' )
As above, but these forms do not expand parser functions
such as {{GENDER:}}.
- print_r( $status->getErrorsArray(), true )
- print_r( $status->getErrors(), true )
These forms output the message keys instead of the message text,
which is not very human-readable.
The error messages are now always printed using error() rather
than output(), which means they go to STDERR rather than STDOUT
and they're printed even with the --quiet flag.
Change-Id: I5b8e7c7ed2a896a1029f58857a478d3f1b4b0589
2024-06-06 23:50:00 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->error( $status );
|
|
|
|
|
}
|
2015-06-06 04:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ok === 'OK';
|
|
|
|
|
}
|
2005-06-30 00:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-05-24 01:34:01 +00:00
|
|
|
$maintClass = CleanupCaps::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|