2018-05-03 20:07:07 +00:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?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.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// @codingStandardsIgnoreStart
|
2019-03-11 03:34:48 +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" );
|
|
|
|
}
|
|
|
|
require_once ( "$IP/maintenance/Maintenance.php" );
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintenance script that replaces text in pages
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
* @SuppressWarnings(StaticAccess)
|
|
|
|
* @SuppressWarnings(LongVariable)
|
|
|
|
*/
|
|
|
|
class ReplaceAll extends Maintenance {
|
|
|
|
private $user;
|
|
|
|
private $target;
|
|
|
|
private $replacement;
|
|
|
|
private $namespaces;
|
|
|
|
private $category;
|
|
|
|
private $prefix;
|
|
|
|
private $useRegex;
|
|
|
|
private $titles;
|
|
|
|
private $defaultContinue;
|
|
|
|
private $doAnnounce;
|
2018-09-06 18:07:05 +00:00
|
|
|
private $rename;
|
2018-05-03 20:07:07 +00:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2019-08-19 18:33: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
|
|
|
|
|
|
|
$this->addArg( "target", "Target text to find.", false );
|
|
|
|
$this->addArg( "replace", "Text to replace.", false );
|
|
|
|
|
|
|
|
$this->addOption( "dry-run", "Only find the texts, don't replace.",
|
|
|
|
false, false, 'n' );
|
|
|
|
$this->addOption( "regex", "This is a regex (false).",
|
|
|
|
false, false, 'r' );
|
|
|
|
$this->addOption( "user", "The user to attribute this to (uid 1).",
|
|
|
|
false, true, 'u' );
|
|
|
|
$this->addOption( "yes", "Skip all prompts with an assumed 'yes'.",
|
|
|
|
false, false, 'y' );
|
2018-09-03 14:30:30 +00:00
|
|
|
$this->addOption( "summary", "Alternate edit summary. (%r is where to " .
|
2018-05-03 20:07:07 +00:00
|
|
|
" place the replacement text, %f the text to look for.)",
|
|
|
|
false, true, 's' );
|
|
|
|
$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( "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 );
|
|
|
|
$this->addOption( "no-announce", "Do not announce edits on Special:RecentChanges or " .
|
|
|
|
"watchlists.", false, false, "m" );
|
|
|
|
$this->addOption( "debug", "Display replacements being made.", false, false );
|
|
|
|
$this->addOption( "listns", "List out the namespaces on this wiki.",
|
|
|
|
false, false );
|
2018-09-06 18:07:05 +00:00
|
|
|
$this->addOption( 'rename', "Rename page titles instead of replacing contents.",
|
|
|
|
false, false );
|
2018-05-03 20:07:07 +00:00
|
|
|
|
|
|
|
// MW 1.28
|
|
|
|
if ( method_exists( $this, 'requireExtension' ) ) {
|
|
|
|
$this->requireExtension( 'Replace Text' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUser() {
|
|
|
|
$userReplacing = $this->getOption( "user", 1 );
|
|
|
|
|
|
|
|
$user = is_numeric( $userReplacing ) ?
|
|
|
|
User::newFromId( $userReplacing ) :
|
|
|
|
User::newFromName( $userReplacing );
|
|
|
|
|
|
|
|
if ( get_class( $user ) !== '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 );
|
|
|
|
if ( !$ret ) {
|
2019-12-28 18:37:40 +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 );
|
|
|
|
if ( !$ret ) {
|
2019-12-28 18:37:40 +00:00
|
|
|
$this->fatalError( "You have to specify replacement text." );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
return [ $ret ];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getReplacements() {
|
|
|
|
$file = $this->getOption( "replacements" );
|
|
|
|
if ( !$file ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_readable( $file ) ) {
|
|
|
|
throw new MWException( "File does not exist or is not readable: "
|
|
|
|
. "$file\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$handle = fopen( $file, "r" );
|
|
|
|
if ( $handle === false ) {
|
|
|
|
throw new MWException( "Trouble opening file: $file\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->defaultContinue = true;
|
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
while ( ( $line = fgets( $handle ) ) !== false ) {
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
$field = explode( "\t", substr( $line, 0, -1 ) );
|
|
|
|
if ( !isset( $field[1] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->target[] = $field[0];
|
|
|
|
$this->replacement[] = $field[1];
|
|
|
|
$this->useRegex[] = isset( $field[2] ) ? true : false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldContinueByDefault() {
|
|
|
|
if ( !is_bool( $this->defaultContinue ) ) {
|
|
|
|
$this->defaultContinue =
|
|
|
|
$this->getOption( "yes" ) ?
|
|
|
|
true :
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
return $this->defaultContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSummary( $target, $replacement ) {
|
|
|
|
$msg = wfMessage( 'replacetext_editsummary', $target, $replacement )->
|
|
|
|
plain();
|
|
|
|
if ( $this->getOption( "summary" ) !== null ) {
|
|
|
|
$msg = str_replace( [ '%f', '%r' ],
|
|
|
|
[ $this->target, $this->replacement ],
|
|
|
|
$this->getOption( "summary" ) );
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function listNamespaces() {
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "Index\tNamespace\n" );
|
2018-05-03 20:07:07 +00:00
|
|
|
$nsList = MWNamespace::getCanonicalNamespaces();
|
|
|
|
ksort( $nsList );
|
|
|
|
foreach ( $nsList as $int => $val ) {
|
|
|
|
if ( $val == "" ) {
|
|
|
|
$val = "(main)";
|
|
|
|
}
|
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() {
|
|
|
|
$nsall = $this->getOption( "nsall" );
|
|
|
|
$ns = $this->getOption( "ns" );
|
|
|
|
if ( !$nsall && !$ns ) {
|
|
|
|
$namespaces = [ NS_MAIN ];
|
|
|
|
} else {
|
|
|
|
$canonical = MWNamespace::getCanonicalNamespaces();
|
|
|
|
$canonical[NS_MAIN] = "_";
|
|
|
|
$namespaces = array_flip( $canonical );
|
|
|
|
if ( !$nsall ) {
|
|
|
|
$namespaces = array_map(
|
|
|
|
function ( $n ) use ( $canonical, $namespaces ) {
|
|
|
|
if ( is_numeric( $n ) ) {
|
|
|
|
if ( isset( $canonical[ $n ] ) ) {
|
|
|
|
return intval( $n );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( isset( $namespaces[ $n ] ) ) {
|
|
|
|
return $namespaces[ $n ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}, explode( ",", $ns ) );
|
|
|
|
$namespaces = array_filter(
|
|
|
|
$namespaces,
|
|
|
|
function ( $val ) {
|
|
|
|
return $val !== null;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $namespaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getCategory() {
|
2018-09-06 18:07:05 +00:00
|
|
|
return null;
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getPrefix() {
|
2018-09-06 18:07:05 +00:00
|
|
|
return null;
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function useRegex() {
|
|
|
|
return [ $this->getOption( "regex" ) ];
|
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
foreach ( $titles as $title ) {
|
|
|
|
if ( $rename ) {
|
|
|
|
$newTitle = ReplaceTextSearch::getReplacedTitle( $title, $target, $replacement, $regex );
|
|
|
|
// Implicit conversion of objects to strings
|
|
|
|
$this->output( "$title -> $newTitle\n" );
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ),
|
2018-09-06 18:07:05 +00:00
|
|
|
'doAnnounce' => $this->doAnnounce
|
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... " );
|
2018-09-06 18:07:05 +00:00
|
|
|
$job = new ReplaceTextJob( $title, $params );
|
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 ) {
|
|
|
|
$reply = "";
|
|
|
|
if ( $this->shouldContinueByDefault() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
while ( $reply !== "y" && $reply !== "n" ) {
|
|
|
|
$reply = $this->readconsole( "$question (Y/N) " );
|
|
|
|
$reply = substr( strtolower( $reply ), 0, 1 );
|
|
|
|
}
|
|
|
|
return $reply === "y";
|
|
|
|
}
|
|
|
|
|
|
|
|
private function localSetup() {
|
|
|
|
if ( $this->getOption( "listns" ) ) {
|
|
|
|
$this->listNamespaces();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( $this->getOption( "show-file-format" ) ) {
|
|
|
|
$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();
|
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() {
|
|
|
|
global $wgShowExceptionDetails;
|
|
|
|
$wgShowExceptionDetails = true;
|
|
|
|
|
|
|
|
$this->doAnnounce = true;
|
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 === [] ) {
|
2019-12-28 18:37:40 +00:00
|
|
|
$this->fatalError( "No matching namespaces." );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
2018-05-03 20:07:07 +00:00
|
|
|
|
2018-09-06 18:07:05 +00:00
|
|
|
foreach ( array_keys( $this->target ) as $index ) {
|
|
|
|
$target = $this->target[$index];
|
|
|
|
$replacement = $this->replacement[$index];
|
|
|
|
$useRegex = $this->useRegex[$index];
|
|
|
|
|
|
|
|
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 ) {
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( " as regular expression." );
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
2020-03-31 22:20:04 +00:00
|
|
|
$this->output( "\n" );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->rename ) {
|
|
|
|
$res = ReplaceTextSearch::getMatchingTitles(
|
|
|
|
$target,
|
|
|
|
$this->namespaces,
|
|
|
|
$this->category,
|
|
|
|
$this->prefix,
|
|
|
|
$useRegex
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$res = ReplaceTextSearch::doSearchQuery(
|
|
|
|
$target,
|
|
|
|
$this->namespaces,
|
|
|
|
$this->category,
|
|
|
|
$this->prefix,
|
|
|
|
$useRegex
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$titles = new TitleArrayFromResult( $res );
|
|
|
|
|
|
|
|
if ( count( $titles ) === 0 ) {
|
2019-12-28 18:37:40 +00:00
|
|
|
$this->fatalError( 'No targets found to replace.' );
|
2018-09-06 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->getOption( "dry-run" ) ) {
|
|
|
|
$this->listTitles( $titles, $target, $replacement, $useRegex, $this->rename );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-06 18:46:03 +00:00
|
|
|
if ( !$this->shouldContinueByDefault() ) {
|
|
|
|
$this->listTitles( $titles, $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
|
|
|
|
|
|
|
$comment = "";
|
|
|
|
if ( $this->getOption( "user", null ) === null ) {
|
|
|
|
$comment = " (Use --user to override)";
|
|
|
|
}
|
|
|
|
if ( $this->getOption( "no-announce", false ) ) {
|
|
|
|
$this->doAnnounce = false;
|
|
|
|
}
|
|
|
|
if ( !$this->getReply(
|
|
|
|
"Attribute changes to the user '{$this->user}'?$comment"
|
|
|
|
) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->replaceTitles(
|
|
|
|
$titles, $target, $replacement, $useRegex, $this->rename
|
|
|
|
);
|
2018-05-03 20:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = "ReplaceAll";
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|