2018-05-03 20:07:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-09-06 18:07:05 +00:00
|
|
|
* Replace text in pages or page titles.
|
2018-05-03 20:07:07 +00:00
|
|
|
*
|
|
|
|
* Copyright © 2014 NicheWork, LLC
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2022-09-08 18:41:13 +00:00
|
|
|
* https://www.gnu.org/copyleft/gpl.html
|
2018-05-03 20:07:07 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @category Maintenance
|
|
|
|
* @package ReplaceText
|
|
|
|
* @author Mark A. Hershberger <mah@nichework.com>
|
2018-09-06 18:07:05 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2018-05-03 20:07:07 +00:00
|
|
|
* @link https://www.mediawiki.org/wiki/Extension:Replace_Text
|
|
|
|
*
|
|
|
|
*/
|
2021-04-08 02:12:20 +00:00
|
|
|
namespace MediaWiki\Extension\ReplaceText;
|
|
|
|
|
2024-10-20 09:32:06 +00:00
|
|
|
use MediaWiki\Maintenance\Maintenance;
|
2024-01-05 20:26:44 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-04-08 02:12:20 +00:00
|
|
|
use MWException;
|
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' ) ?: __DIR__ . '/../../..';
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( !is_readable( "$IP/maintenance/Maintenance.php" ) ) {
|
|
|
|
die( "MW_INSTALL_PATH needs to be set to your MediaWiki installation.\n" );
|
|
|
|
}
|
2021-04-03 22:00:48 +00:00
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2018-05-03 20:07:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintenance script that replaces text in pages
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
* @SuppressWarnings(StaticAccess)
|
|
|
|
* @SuppressWarnings(LongVariable)
|
|
|
|
*/
|
|
|
|
class ReplaceAll extends Maintenance {
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var User */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $user;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var string[] */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $target;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var string[] */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $replacement;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var int[] */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $namespaces;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var string */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $category;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var string */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $prefix;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var int */
|
2023-09-19 10:17:58 +00:00
|
|
|
private $pageLimit;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var bool[] */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $useRegex;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var bool */
|
2018-05-03 20:07:07 +00:00
|
|
|
private $defaultContinue;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var bool */
|
2023-08-08 15:39:55 +00:00
|
|
|
private $botEdit;
|
2024-08-20 21:46:31 +00:00
|
|
|
/** @var bool */
|
2018-09-06 18:07:05 +00:00
|
|
|
private $rename;
|
2018-05-03 20:07:07 +00:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addDescription( 'CLI utility to replace text wherever it is ' .
|
|
|
|
'found in the wiki.' );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addArg( 'target', 'Target text to find.', false );
|
|
|
|
$this->addArg( 'replace', 'Text to replace.', false );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'dry-run', 'Only find the texts, don\'t replace.',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, false, 'n' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'regex', 'This is a regex (false).',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, false, 'r' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'user', 'The user to attribute this to (uid 1).',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, true, 'u' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'yes', 'Skip all prompts with an assumed \'yes\'.',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, false, 'y' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'summary', 'Alternate edit summary. (%r is where to ' .
|
|
|
|
' place the replacement text, %f the text to look for.)',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, true, 's' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'nsall', 'Search all canonical namespaces (false). ' .
|
|
|
|
'If true, this option overrides the ns option.', false, false, 'a' );
|
|
|
|
$this->addOption( 'ns', 'Comma separated namespaces to search in ' .
|
|
|
|
'(Main) .', false, true );
|
|
|
|
$this->addOption( 'category', 'Search only pages within this category.',
|
2022-08-11 23:22:41 +00:00
|
|
|
false, true, 'c' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'prefix', 'Search only pages whose names start with this string.',
|
2022-08-11 23:22:41 +00:00
|
|
|
false, true, 'p' );
|
2023-09-19 10:17:58 +00:00
|
|
|
$this->addOption( 'pageLimit', 'Maximum number of pages to return from the search.',
|
|
|
|
false, true, 'p' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'replacements', 'File containing the list of ' .
|
|
|
|
'replacements to be made. Fields in the file are tab-separated. ' .
|
|
|
|
'See --show-file-format for more information.', false, true, 'f' );
|
|
|
|
$this->addOption( 'show-file-format', 'Show a description of the ' .
|
|
|
|
'file format to use with --replacements.', false, false );
|
2023-08-08 15:39:55 +00:00
|
|
|
$this->addOption( 'bot-edit', 'Mark changes as bot edits.',
|
|
|
|
false, false, 'b' );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'debug', 'Display replacements being made.', false, false );
|
|
|
|
$this->addOption( 'listns', 'List out the namespaces on this wiki.',
|
2018-05-03 20:07:07 +00:00
|
|
|
false, false );
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->addOption( 'rename', 'Rename page titles instead of replacing contents.',
|
2018-09-06 18:07:05 +00:00
|
|
|
false, false );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2021-07-05 18:48:19 +00:00
|
|
|
$this->requireExtension( 'Replace Text' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getUser() {
|
2022-09-08 12:45:06 +00:00
|
|
|
$userReplacing = $this->getOption( 'user', 1 );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2023-10-18 20:20:20 +00:00
|
|
|
$userFactory = $this->getServiceContainer()->getUserFactory();
|
2018-05-03 20:07:07 +00:00
|
|
|
$user = is_numeric( $userReplacing ) ?
|
2021-10-22 14:50:16 +00:00
|
|
|
$userFactory->newFromId( $userReplacing ) :
|
|
|
|
$userFactory->newFromName( $userReplacing );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2024-01-05 20:26:44 +00:00
|
|
|
if ( !$user instanceof User ) {
|
2019-12-28 18:37:40 +00:00
|
|
|
$this->fatalError(
|
|
|
|
"Couldn't translate '$userReplacing' to a user."
|
2018-05-03 20:07:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTarget() {
|
|
|
|
$ret = $this->getArg( 0 );
|
2022-09-08 12:11:58 +00:00
|
|
|
if ( $ret === null ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->fatalError( 'You have to specify a target.' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
return [ $ret ];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getReplacement() {
|
|
|
|
$ret = $this->getArg( 1 );
|
2022-09-08 12:11:58 +00:00
|
|
|
if ( $ret === null ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->fatalError( 'You have to specify replacement text.' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
return [ $ret ];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getReplacements() {
|
2022-09-08 12:45:06 +00:00
|
|
|
$file = $this->getOption( 'replacements' );
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( !$file ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_readable( $file ) ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
throw new MWException( 'File does not exist or is not readable: '
|
2018-05-03 20:07:07 +00:00
|
|
|
. "$file\n" );
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
$handle = fopen( $file, 'r' );
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( $handle === false ) {
|
|
|
|
throw new MWException( "Trouble opening file: $file\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->defaultContinue = true;
|
2024-03-11 21:44:46 +00:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
|
2018-05-03 20:07:07 +00:00
|
|
|
while ( ( $line = fgets( $handle ) ) !== false ) {
|
|
|
|
$field = explode( "\t", substr( $line, 0, -1 ) );
|
|
|
|
if ( !isset( $field[1] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->target[] = $field[0];
|
|
|
|
$this->replacement[] = $field[1];
|
2021-09-08 20:53:12 +00:00
|
|
|
$this->useRegex[] = isset( $field[2] );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldContinueByDefault() {
|
|
|
|
if ( !is_bool( $this->defaultContinue ) ) {
|
|
|
|
$this->defaultContinue =
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->getOption( 'yes' ) ?
|
2018-05-03 20:07:07 +00:00
|
|
|
true :
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
return $this->defaultContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSummary( $target, $replacement ) {
|
|
|
|
$msg = wfMessage( 'replacetext_editsummary', $target, $replacement )->
|
|
|
|
plain();
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $this->getOption( 'summary' ) !== null ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
$msg = str_replace( [ '%f', '%r' ],
|
2023-06-07 13:58:56 +00:00
|
|
|
[ $target, $replacement ],
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->getOption( 'summary' ) );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function listNamespaces() {
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "Index\tNamespace\n" );
|
2023-10-18 20:20:20 +00:00
|
|
|
$nsList = $this->getServiceContainer()->getNamespaceInfo()->getCanonicalNamespaces();
|
2018-05-03 20:07:07 +00:00
|
|
|
ksort( $nsList );
|
|
|
|
foreach ( $nsList as $int => $val ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $val == '' ) {
|
|
|
|
$val = '(main)';
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( " $int\t$val\n" );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function showFileFormat() {
|
2020-03-31 22:20:04 +00:00
|
|
|
$text = <<<EOF
|
2018-05-03 20:07:07 +00:00
|
|
|
|
|
|
|
The format of the replacements file is tab separated with three fields.
|
|
|
|
Any line that does not have a tab is ignored and can be considered a comment.
|
|
|
|
|
|
|
|
Fields are:
|
|
|
|
|
|
|
|
1. String to search for.
|
|
|
|
2. String to replace found text with.
|
|
|
|
3. (optional) The presence of this field indicates that the previous two
|
|
|
|
are considered a regular expression.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
This is a comment
|
|
|
|
TARGET REPLACE
|
|
|
|
regex(p*) Count the Ps; \\1 true
|
|
|
|
|
|
|
|
|
|
|
|
EOF;
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( $text );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getNamespaces() {
|
2022-09-08 12:45:06 +00:00
|
|
|
$nsall = $this->getOption( 'nsall' );
|
|
|
|
$ns = $this->getOption( 'ns' );
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( !$nsall && !$ns ) {
|
|
|
|
$namespaces = [ NS_MAIN ];
|
|
|
|
} else {
|
2023-10-18 20:20:20 +00:00
|
|
|
$canonical = $this->getServiceContainer()->getNamespaceInfo()->getCanonicalNamespaces();
|
2022-09-08 12:45:06 +00:00
|
|
|
$canonical[NS_MAIN] = '_';
|
2018-05-03 20:07:07 +00:00
|
|
|
$namespaces = array_flip( $canonical );
|
|
|
|
if ( !$nsall ) {
|
|
|
|
$namespaces = array_map(
|
2021-05-13 05:12:19 +00:00
|
|
|
static function ( $n ) use ( $canonical, $namespaces ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( is_numeric( $n ) ) {
|
|
|
|
if ( isset( $canonical[ $n ] ) ) {
|
|
|
|
return intval( $n );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( isset( $namespaces[ $n ] ) ) {
|
|
|
|
return $namespaces[ $n ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2022-09-08 12:45:06 +00:00
|
|
|
}, explode( ',', $ns ) );
|
2018-05-03 20:07:07 +00:00
|
|
|
$namespaces = array_filter(
|
|
|
|
$namespaces,
|
2021-05-13 05:12:19 +00:00
|
|
|
static function ( $val ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
return $val !== null;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $namespaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCategory() {
|
2022-08-11 23:22:41 +00:00
|
|
|
return $this->getOption( 'category' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getPrefix() {
|
2022-08-11 23:22:41 +00:00
|
|
|
return $this->getOption( 'prefix' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 10:17:58 +00:00
|
|
|
private function getPageLimit() {
|
|
|
|
return $this->getOption( 'pageLimit' );
|
|
|
|
}
|
|
|
|
|
2018-05-03 20:07:07 +00:00
|
|
|
private function useRegex() {
|
2022-09-08 12:45:06 +00:00
|
|
|
return [ $this->getOption( 'regex' ) ];
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
private function getRename() {
|
|
|
|
return $this->hasOption( 'rename' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
private function listTitles( $titles, $target, $replacement, $regex, $rename ) {
|
2023-05-19 12:58:45 +00:00
|
|
|
$skippedTitles = [];
|
|
|
|
foreach ( $titles as $prefixedText => $title ) {
|
|
|
|
if ( $title === null ) {
|
|
|
|
$skippedTitles[] = $prefixedText;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( $rename ) {
|
2021-04-08 02:12:20 +00:00
|
|
|
$newTitle = Search::getReplacedTitle( $title, $target, $replacement, $regex );
|
2018-09-06 18:07:05 +00:00
|
|
|
// Implicit conversion of objects to strings
|
2022-09-08 18:35:00 +00:00
|
|
|
$this->output( "$title\t->\t$newTitle\n" );
|
2018-09-06 18:07:05 +00:00
|
|
|
} else {
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "$title\n" );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
2023-05-19 12:58:45 +00:00
|
|
|
|
|
|
|
if ( $skippedTitles ) {
|
|
|
|
$this->output( "\nExtension hook filtered out the following titles from being moved:\n" );
|
|
|
|
foreach ( $skippedTitles as $prefixedTitle ) {
|
|
|
|
$this->output( "$prefixedTitle\n" );
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
private function replaceTitles( $titles, $target, $replacement, $useRegex, $rename ) {
|
|
|
|
foreach ( $titles as $title ) {
|
|
|
|
$params = [
|
2018-05-03 20:07:07 +00:00
|
|
|
'target_str' => $target,
|
|
|
|
'replacement_str' => $replacement,
|
|
|
|
'use_regex' => $useRegex,
|
|
|
|
'user_id' => $this->user->getId(),
|
|
|
|
'edit_summary' => $this->getSummary( $target, $replacement ),
|
2023-08-08 15:39:55 +00:00
|
|
|
'botEdit' => $this->botEdit
|
2018-05-03 20:07:07 +00:00
|
|
|
];
|
2018-09-06 18:07:05 +00:00
|
|
|
|
|
|
|
if ( $rename ) {
|
|
|
|
$params[ 'move_page' ] = true;
|
|
|
|
$params[ 'create_redirect' ] = false;
|
|
|
|
$params[ 'watch_page' ] = false;
|
|
|
|
}
|
|
|
|
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "Replacing on $title... " );
|
2024-02-11 18:44:30 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
$job = new Job( $title, $params,
|
|
|
|
$services->getMovePageFactory(),
|
|
|
|
$services->getPermissionManager(),
|
|
|
|
$services->getUserFactory(),
|
|
|
|
$services->getWatchlistManager(),
|
|
|
|
$services->getWikiPageFactory()
|
|
|
|
);
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( $job->run() !== true ) {
|
|
|
|
$this->error( "Trouble on the page '$title'." );
|
|
|
|
}
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "done.\n" );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getReply( $question ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
$reply = '';
|
2018-05-03 20:07:07 +00:00
|
|
|
if ( $this->shouldContinueByDefault() ) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-08 12:45:06 +00:00
|
|
|
while ( $reply !== 'y' && $reply !== 'n' ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
$reply = $this->readconsole( "$question (Y/N) " );
|
|
|
|
$reply = substr( strtolower( $reply ), 0, 1 );
|
|
|
|
}
|
2022-09-08 12:45:06 +00:00
|
|
|
return $reply === 'y';
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function localSetup() {
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $this->getOption( 'listns' ) ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
$this->listNamespaces();
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $this->getOption( 'show-file-format' ) ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
$this->showFileFormat();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->user = $this->getUser();
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( !$this->getReplacements() ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
$this->target = $this->getTarget();
|
|
|
|
$this->replacement = $this->getReplacement();
|
|
|
|
$this->useRegex = $this->useRegex();
|
|
|
|
}
|
|
|
|
$this->namespaces = $this->getNamespaces();
|
|
|
|
$this->category = $this->getCategory();
|
|
|
|
$this->prefix = $this->getPrefix();
|
2023-09-19 10:17:58 +00:00
|
|
|
$this->pageLimit = $this->getPageLimit();
|
2018-09-06 18:07:05 +00:00
|
|
|
$this->rename = $this->getRename();
|
|
|
|
|
2018-05-03 20:07:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function execute() {
|
2023-08-08 15:39:55 +00:00
|
|
|
$this->botEdit = false;
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( !$this->localSetup() ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( $this->namespaces === [] ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->fatalError( 'No matching namespaces.' );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2024-03-16 11:32:30 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
$hookHelper = new HookHelper( $services->getHookContainer() );
|
2024-02-11 13:21:52 +00:00
|
|
|
$search = new Search(
|
2024-03-16 11:32:30 +00:00
|
|
|
$services->getMainConfig(),
|
|
|
|
$services->getDBLoadBalancerFactory()
|
2024-02-11 13:21:52 +00:00
|
|
|
);
|
2022-10-23 10:31:07 +00:00
|
|
|
foreach ( $this->target as $index => $target ) {
|
2018-09-06 18:07:05 +00:00
|
|
|
$replacement = $this->replacement[$index];
|
|
|
|
$useRegex = $this->useRegex[$index];
|
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $this->getOption( 'debug' ) ) {
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "Replacing '$target' with '$replacement'" );
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( $useRegex ) {
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->output( ' as regular expression' );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
2022-09-08 12:45:06 +00:00
|
|
|
$this->output( ".\n" );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->rename ) {
|
2024-02-11 13:21:52 +00:00
|
|
|
$res = $search->getMatchingTitles(
|
2018-09-06 18:07:05 +00:00
|
|
|
$target,
|
|
|
|
$this->namespaces,
|
|
|
|
$this->category,
|
|
|
|
$this->prefix,
|
2023-09-19 10:17:58 +00:00
|
|
|
$this->pageLimit,
|
2018-09-06 18:07:05 +00:00
|
|
|
$useRegex
|
|
|
|
);
|
2023-08-15 20:40:53 +00:00
|
|
|
$titlesToProcess = $hookHelper->filterPageTitlesForRename( $res );
|
2018-09-06 18:07:05 +00:00
|
|
|
} else {
|
2024-02-11 13:21:52 +00:00
|
|
|
$res = $search->doSearchQuery(
|
2018-09-06 18:07:05 +00:00
|
|
|
$target,
|
|
|
|
$this->namespaces,
|
|
|
|
$this->category,
|
|
|
|
$this->prefix,
|
2023-09-19 10:17:58 +00:00
|
|
|
$this->pageLimit,
|
2018-09-06 18:07:05 +00:00
|
|
|
$useRegex
|
|
|
|
);
|
2023-08-15 20:40:53 +00:00
|
|
|
$titlesToProcess = $hookHelper->filterPageTitlesForEdit( $res );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 12:58:45 +00:00
|
|
|
if ( count( $titlesToProcess ) === 0 ) {
|
2019-12-28 18:37:40 +00:00
|
|
|
$this->fatalError( 'No targets found to replace.' );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
if ( $this->getOption( 'dry-run' ) ) {
|
2023-05-19 12:58:45 +00:00
|
|
|
$this->listTitles( $titlesToProcess, $target, $replacement, $useRegex, $this->rename );
|
2018-09-06 18:07:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-06 18:46:03 +00:00
|
|
|
if ( !$this->shouldContinueByDefault() ) {
|
2023-05-19 12:58:45 +00:00
|
|
|
$this->listTitles( $titlesToProcess, $target, $replacement, $useRegex, $this->rename );
|
2018-09-06 18:07:05 +00:00
|
|
|
if ( !$this->getReply( 'Replace instances on these pages?' ) ) {
|
2018-05-03 20:07:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 18:07:05 +00:00
|
|
|
|
2022-09-08 12:45:06 +00:00
|
|
|
$comment = '';
|
|
|
|
if ( $this->getOption( 'user', null ) === null ) {
|
|
|
|
$comment = ' (Use --user to override)';
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
2023-08-08 15:39:55 +00:00
|
|
|
if ( $this->getOption( 'bot-edit', false ) ) {
|
|
|
|
$this->botEdit = true;
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
if ( !$this->getReply(
|
2024-01-05 20:26:44 +00:00
|
|
|
"Attribute changes to the user '{$this->user->getName()}'?$comment"
|
2018-09-06 18:07:05 +00:00
|
|
|
) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:58:45 +00:00
|
|
|
$this->replaceTitles( $titlesToProcess, $target, $replacement, $useRegex, $this->rename );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 02:12:20 +00:00
|
|
|
$maintClass = ReplaceAll::class;
|
2018-05-03 20:07:07 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|