Расширение:Replace Text

This page is a translated version of the page Extension:Replace Text and the translation is 34% complete.
Outdated translations are marked like this.
Это extension поставляется с MediaWiki 1.31 и выше. Таким образом, вам не нужно загружать его снова. Тем не менее, вы всё равно должны следовать другим инструкциям.
Справка по расширениям MediaWiki
ReplaceText
Статус релиза: стабильно
Реализация Служебная страница
Описание Предоставляет форму, позволяющую администраторам выполнять замену строк во всей вики, как для содержимого, так и для заголовков страниц
Автор(ы) Yaron Koren <yaron57@gmail.com> и Niklas Laxström
Последняя версия 1.8
Политика совместимости Snapshots releases along with MediaWiki. Master is not backward compatible.
MediaWiki 1.31+
Composer mediawiki/replace-text
Лицензия GNU General Public License 2.0 или позднее
Скачать
  • $wgReplaceTextResultsLimit
  • replacetext
Использование общедоступными вики 1,812 (Ranked 200th)
Переведите расширение Replace Text, если оно доступно на translatewiki.net
Проблемы Открытые задачи · Сообщить об ошибке

Replace Text — это расширение MediaWiki, которое предоставляет служебную страницу, а также сценарий (скрипт) командной строки, позволяющий администраторам выполнять поиск и замену глобальной строки на стороне сервера как в тексте, так и в заголовках страниц содержимого вики.

Операция замены текста является отложенной операцией и помещается в очередь заданий. Вы можете запустить runJobs.php из папки maintenance, чтобы немедленно выполнить замену и обработать очередь заданий.

Установка

  • Скачайте и распакуйте файл(ы) в папку с названием ReplaceText в вашей папке extensions/.
    Вместо этого разработчикам и соавторам кода следует установить расширение из Git, используя:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/ReplaceText
  • Добавьте следующий код в конце вашего файла LocalSettings.php :
    wfLoadExtension( 'ReplaceText' );
    
  • По умолчанию только члены группы пользователей «sysop» имеют доступ к функциональности Replace Text. Вы можете добавить или изменить набор разрешённых пользователей, изменив массив $wgGroupPermissions в LocalSettings.php. Например, чтобы добавить разрешение для пользователей «bureaucrat» или «bot», Вы должны добавить следующее:
$wgGroupPermissions['bureaucrat']['replacetext'] = true;
  •   Готово – Перейдите на страницу Special:Version на своей вики, чтобы удостовериться в том, что расширение успешно установлено.

Использование

Replace Text defines a special page, at "Special:ReplaceText" (or its equivalent in another language), that handles global searching and replacing; it also defines a script, replaceAll.php, that does the same thing from the command line. This documentation will cover the special page first, then the command-line script.

The page Special:ReplaceText initially displays a form for an administrator to fill out the search-and-replace details: the administrator enters a search string and its replacement, the set of namespaces on which to search, as well as choosing whether to replace text within page contents, page titles, or both. Optionally, the administrator can also add two additional filters: the name of a category that all pages must belong to, and a prefix that all page names must start with. When this form is submitted, they are then shown a list of either all the pages that contain that search string, or all the pages whose titles contain the search string, or both. Next to each page name is a checkbox for each, so that they can unselect whichever pages they don't want replaced. The user then can hit "Replace" to do the actual replacement. Once a text replace is done, it is not directly undoable; though you can always run a "reverse" replace, replacing the new string with the original string. For that reason, if the replacement string is blank or is a string that already exists in the wiki, the user is shown a warning message.

If the search string is contained multiple times on a page, every such instance is replaced. Every page's replacements shows up as a (minor) wiki edit, with the administrator who performed the text replacement as the user who made the edit and an edit summary that looks like "Text replace: 'search string' to 'replacement string'".

A page's title cannot be moved to a title that already exists in the wiki, or to a title that is multiple lines. Pages that cannot be moved will be simply listed on the "select" page as being unmovable, without a checkbox near them.

The search is case-sensitive. See below for how to make it case-insensitive.

Что нужно знать

  1. По умолчанию Replace Text обрабатывает только 250 страниц одновременно. Чтобы изменить это, вы можете установить для $wgReplaceTextResultsLimit большее значение в Вашем LocalSettings.php. However, you may be limited by PHP's max_input_vars; if the extension reports such a warning, you may need to change it.
  2. All of the replacements are placed in the job queue. So if you have to process a large number of pages, you will need to run Replace Text and then run "runJobs.php" (potentially multiple times).

Использование регулярных выражений

If you are using a MySQL or PostgreSQL database, you will see a "Use regular expressions" checkbox within the Special:ReplaceText form. This checkbox does not appear for SQLite databases, because SQLite lacks a native "REGEXP" operator.

If you click on this checkbox, you can use regular expressions, or regexps, within the search and replacement strings. The set of regular expressions allowed is basically a small subset of the PHP and MySQL/PostgreSQL regular-expression set. The special characters that one can use in the search string are "( ) . * + ? [ ] |", and within the replacement string one can use values like $1, $2 etc. (To use these as regular characters, you just need to escape them by adding a "\" beforehand - which you would also need to do with the "/" character.) This section will not give a tutorial on using regular expressions (the Wikipedia article is a good place to start for that, as is this page on MySQL regexps), but here is the basic example listed in the inline explanation:

Искомая строка - a(.*)c
Заменяющая строка - ac$1

This would look for pages containing the letter 'a', the letter 'c', and any text in between (signified by the ".*"). It would then put that middle text after the 'a' and 'c' - the "$1" in the replacement string refers to the first element of the search string contained within parentheses (in this case, there's only one).

For every page for which the replace is actually called, the replacement would happen for every occurrence of the search string, not just one - just as happens with regular, non-regular-expression search.

Более сложные регулярные выражения

Искомая строка - hello([0-9]*)\.([0-9]*)
Заменяющая строка - $1,$2

Это заменит «hello222.555» на «222,555» и «hello2.55" with "2,55»

Сначала сопоставляется слово «hello». В скобках следует выражение для поиска любых цифр «0-9». Следующая звёздочка находит несколько совпадений предыдущего элемента. The brackets and the asterisk get enclosed with parantheses, so we match the complete term and transfer it to $1. The "\." indicates that it's a true dot, and not a special character. The second expression within parentheses is matched and put into $2.

Замены с последующими цифрами

Следующее не будет работать:

Искомая строка - 123(.*)456
Заменяющая строка - $1123456

That's because the search will think the number of the replacement is 1123456, not 1. Instead, you should have the following:

Заменяющая строка - {$1}123456

In general, you can always enclose numbered values in curly brackets.

Поиск без учёта регистра

To make your search case-insensitive, use regular expression and the case-insensitive modifier, (?i):

Искомая строка - (?i)iphone
Заменяющая строка - iPhone

The above will unify the casing of all mentions of iPhone/iphone/IPHONE to iPhone.

Encapsulating timestamps

If you have text that contains timestamps (such as from a video transcript) in the form hh:mm:ss where the hh and mm are not zero-padded and hh: may not even be present - such as:

0:03
Blah blah blah
11:21
Blah blah blah
2:31:54
Blah blah blah

and you would like to encapsulate the timestamps in, say, span tags. You could do something like:

  • Original text: ((([01]?\d|2[0-3]):)?[0-5]?\d:[0-5]\d)
  • Replacement Text: <span class="timespan">$0</span>

This would result in:

<span class="timespan">0:03</span>
Blah blah blah
<span class="timespan">11:21</span>
Blah blah blah
<span class="timespan">2:31:54</span>
Blah blah blah

Сценарий командной строки: replaceAll.php

Replace Text offers a script, replaceAll.php, in the extensions/ReplaceText/maintenance directory, which can be run from the command line, and which does all the same replacements that the page Special:ReplaceText does. The first two arguments for this script should be the search text and the replacement text; you can call this script simply like the following:

php replaceAll.php "Leningrad" "St. Petersburg"

If you use numbered values like $1 in the replacement string, you have to either escape them inside double quotes like "ac\$1" or use single quotes. Otherwise the shell will put blanks in their place as it thinks they are variables.

Другие возможные аргументы/флаги для этого сценария:

Описание По умолчанию
--dry-run only find the search texts, without doing any actual replacements
--regex следует использовать регулярные выражения false
--user the user to attribute the replacements to пользователь с ИД 1
--yes пропустить все запросы с предполагаемым «да»
--summary set a custom edit summary for the replacement edits
--rename rename page titles instead of replacing contents
--bot-edit mark all replacements as bot edits
--ns set a comma-separated list of the namespaces to search
--nsall search on all the namespaces; overrides any value set for "--ns"
--replacements the name of a file holding a tab-separated list of replacements to make
--debug display the replacements being made
--category search only pages within this category
--prefix search only pages whose names start with this string

Additionally, there are two flags that strictly print out helpful information unrelated to a specific text replacement:

Описание По умолчанию
--show-file-format show a description of the file format to use with --replacements
--listns show a list of all namespaces on this wiki

(You can also see all of these options by calling "php replaceAll.php --help".)

Assigning replacements to a user

By default, all replacements are credited in the page history to the user who initiated them. However, you can instead have replacements be credited to a pre-set user -- such as a bot account -- by adding a line like this to LocalSettings.php, below the inclusion of Replace Text:

$wgReplaceTextUser = "MyReplaceTextBot";

Указанное имя пользователя уже должно быть учётной записью на вики. It will be added to MediaWiki's array of usernames which may not be registered or logged in from.

Авторы

Replace Text was mostly written by Yaron Koren, reachable at yaron57 gmail.com. Important contributions were also made by Niklas Laxström, Ankit Garg (who contributed much of the regular expression code), Mark Hershberger (who wrote the command-line script), Brent Laabs, Cindy Cicalese, Nikhil Kumar, Umherirrender, James Forrester, Fomafix, Marijn van Wezel, Sahaj Khandelwal and others.

История версий

Replace Text в настоящее время имеет версию 1.8. See the entire version history .

Скриншоты

Ниже приведены изображения рабочего процесса Replace Text. First, the initial page in which "George F. Will" and "George Will" are entered for the target string and replacement string, respectively, and replacing text in page titles is specified as well, on Discourse DB:

 

Note: the check "All" and "None" boxes only show up when using the Vector skin, or skins based off of it.

And here is what the page looks like after the user hits "Continue":

 

Hitting "Replace" would replace this value in all of the listed pages, as well as moving the page at the end to its new value.

Известные проблемы

  • The replacement actions themselves are structured as MediaWiki "jobs", to ensure that the system is not overloaded if the user wants to do many at the same time. This means that a large set of replacements will not be done immediately, and may take minutes, hours or even longer to complete. Normally, jobs get activated every time a page is viewed on the wiki; to speed up the process (or slow it down), you can change the number of jobs run when a page is viewed; the default is 1. Thus a Replace Text request can stall soon after it starts if there is not enough front-end activity on the wiki to outrun the other sources of new batch jobs. For information on how to change the default, see the $wgJobRunRate page. To force the wiki to run all jobs immediately, go to the shell and execute the script runJobs.php ; there generally isn't a way to do this from the wiki's front end.
  • For getting the jobs to run on a seldom-used wiki, for instance a working wiki for a small hobby project team, you can for instance force a refresh of the "All pages" special page. This can be done for instance by using the following shell script on Unix:
#!/bin/bash
# $0 is the script name, $1 id the first ARG, $2 is second...
ITERATIONS=$1
WAIT=$2

n=1
while [ $n -le $ITERATIONS ]
do
	echo "Iteration $n/$ITERATIONS"
	# Getting a special page that must be regenerated forces the wiki's queued jobs to be run, the resulting download is discarded
	wget -nv --output-document=/dev/null http://yourwiki.tld/index.php/Special:AllPages
	echo "  wget OK"
	sleep $WAIT
	echo "  wait OK"
	n=$(( n+1 ))
done
  • If you are still having a problem with getting Replace Text's jobs to run, adding the following to LocalSettings.php may fix the problem:
$wgRunJobsAsync = false;
  • In order for redirects not to be created on page moves (i.e. if the "Save the old titles as redirects to the new titles" checkbox is unchecked), you will have to give the relevant user the 'suppressredirect' permission. Assuming you've given the replacetext permission to a trusted-editor user group, you would need to add the following to LocalSettings.php:
$wgGroupPermissions['trusted-editor']['suppressredirect'] = true;
  • If your revisions are compressed (that is, if $wgCompressRevisions is enabled in LocalSettings.php or the script compressOld.php has been executed with the option -t gzip) then ReplaceText will not work, because it makes use of SQL queries that can't search compressed text. (see the MassEditRegex extension as an alternative in this case.)
  • If you have a large number of replacements, some of them may not get done; or you may see an error message that reads "You must select at least one namespace". In both cases, that's due to a limit on the number of form inputs that PHP accepts. This can be fixed in a number of ways:
    • If you have the Suhosin PHP extension installed on your server, that may be source of the problem. You can increase the value of the variables suhosin.post.max_vars and suhosin.request.max_vars; or simply try temporarily disabling Suhosin while you do the replacements.
    • The PHP setting max_input_vars (available since PHP 5.3.9) also affects the number of replacements you can do at any time - you can try setting this to a very large number, like 3000.

Перевод

Translation of Replace Text is done through translatewiki.net. The translation for this extension is on translatewiki. To add language values or change existing ones, you should create an account on translatewiki.net, then request permission from the administrators to translate a certain language or languages on this page (this is a very simple process). Once you have permission for a given language, you can log in and add or edit whatever messages you want to in that language.


См. также

  • Extension:MassEditRegex - Аналогичное расширение, использующее регулярные выражения для одновременного редактирования нескольких страниц