Rozšíření:Replace Text
![]() Stav rozšíření: stabilní |
|
---|---|
Zavádění | Speciální stránka |
Popis | Poskytuje formulář, který umožňuje správcům nahrazovat řetězce na celé wiki, a to jak pro obsah stránky, tak pro názvy stránek |
Napsal(i) | Yaron Koren <yaron57![]() |
Nejnovější verze | 1.7 (března 2022) |
Zásady kompatibility | Hlavní vývojová větev zachovává zpětnou kompatibilitu. |
MediaWiki | 1.35+ |
Licence | GNU General Public License 2.0 nebo pozdější |
Zdrojový kód | |
|
|
Používání veřejných wikin | 1,812 (Ranked 200th) |
Přeložte rozšíření Replace Text, používá-li lokalizaci z translatewiki.net | |
Problémy | Otevřené úkoly · Nahlásit chybu |
Replace Text (nahradit text) je rozšíření MediaWiki, které poskytuje speciální stránku, stejně jako skript příkazového řádku, umožňující administrátorům provádět globální hledání a nahrazování řetězce na straně serveru v textu i názvech obsahových stránek wiki.
Operace nahrazení textu je odložená operace a je zařazena do fronty úloh.
Můžete spustit runJobs.php
ze složky maintenance
a okamžitě provést změnu a zpracovat frontu úloh.
Instalace
- Stáhněte soubor/y a vložte je do adresáře pojmenovaného
ReplaceText
ve vaší složceextensions/
. - Následující kód přidejte na konec vašeho souboru
LocalSettings.php
:wfLoadExtension( 'ReplaceText' );
- Ve výchozím nastavení mají k funkci Replace Text (nahradit text) přístup pouze členové uživatelské skupiny "
sysop
". Množinu povolených uživatelů můžete přidat nebo upravit úpravou pole$wgGroupPermissions
vLocalSettings.php
. Chcete-li například přidat oprávnění pro uživatele "bureaucrat
" nebo "bot
", přidejte následující:
$wgGroupPermissions['bureaucrat']['replacetext'] = true;
- Dokončeno – Přejděte na stránku Special:Version vaší wiki a zkontrolujte, zda bylo rozšíření úspěšně nainstalováno.
Použití
Replace text definuje speciální stránku na "Special:ReplaceText" (nebo její ekvivalent v jiném jazyce), která zpracovává globální vyhledávání a nahrazování. Také definuje skript replaceAll.php, který dělá totéž z příkazového řádku. Tato dokumentace nejprve pokryje speciální stránku a poté skript příkazového řádku.
Na stránce Special:ReplaceText se nejdříve zobrazí formulář pro administrátora, aby vyplnil podrobnosti o hledání a nahrazení: Administrátor zadá hledaný řetězec a jeho nahrazení, sadu jmenných prostorů, ve kterých se má hledat, a také volbu, zda má být textem na stránce nahrazen obsah, názvy stránek nebo obojí. Volitelně může správce také přidat dva další filtry: Název kategorie, do které musí patřit všechny stránky, a předponu, kterou musí všechny názvy stránek začínat. Když je tento formulář odeslán, zobrazí se seznam buď všech stránek, které obsahují daný hledaný řetězec, nebo všech stránek, jejichž názvy obsahují hledaný řetězec, nebo obojí. Vedle názvu každé stránky je zaškrtávací políčko pro každou stránku, takže lze zrušit výběr stránek, které uživatel nahradit nechce. Uživatel pak může kliknout na "Replace" a provést skutečnou výměnu. Jakmile je nahrazení textu provedeno, nelze jej přímo vrátit zpět. I když vždy můžete spustit "reverzní" nahrazení, nahrazující nový řetězec původním řetězcem. Z tohoto důvodu, pokud je náhradní řetězec prázdný nebo se jedná o řetězec, který již na wiki existuje, se zobrazí uživateli varovná zpráva.
Pokud je hledaný řetězec na stránce obsažen vícekrát, bude nahrazen každý takový výskyt. Nahrazení každé stránky se zobrazí jako (malá) úprava wiki, přičemž správce, který provedl nahrazení textu, je uživatel, který provedl úpravu, a shrnutí úprav, které se zobrazí jako "Text replace: 'search string' to 'replacement string'" (nahrazení textu: 'hledaný řetězec' na 'náhradní řetězec').
Název stránky nelze přesunout do názvu, který již na wiki existuje, nebo do titulku, který má více řádků. Stránky, které nelze přesunout, budou jednoduše uvedeny na stránce "select" (vybrat) jako nepřemístitelné, bez zaškrtávacího políčka vedle nich.
Vyhledávání rozlišuje malá a velká písmena. Viz níže, jak nastavit, aby se nerozlišovala malá a velká písmena.
Důležité poznámky
- ReplaceText ve výchozím nastavení zpracovává pouze 250 stránek najednou. Chcete-li to změnit, můžete v LocalSettings.php nastavit
$wgReplaceTextResultsLimit
na vyšší hodnotu. Můžete však být omezenimax_input_vars
PHP. Pokud rozšíření hlásí takové varování, možná ho budete muset změnit. - Všechny náhrady jsou umístěny do fronty úloh. Pokud tedy musíte zpracovat velké množství stránek, budete muset spustit Replace Text a poté spustit "
runJobs.php
" (potenciálně vícekrát).
Použití regulárních výrazů
Pokud používáte databázi MySQL nebo PostgreSQL, uvidíte ve formuláři Special:ReplaceText zaškrtávací políčko "Use regular expressions" (použít regulární výrazy). Toto zaškrtávací políčko se nezobrazuje u databází SQLite, protože SQLite postrádá nativní operátor "REGEXP".
Pokud kliknete na toto zaškrtávací políčko, můžete v rámci vyhledávacích a nahrazovacích řetězců použít regular expressions neboli regulární výrazy. 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:
- Search string:
a(.*)c
- Replacement string:
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.
More complex regexps
- Search string:
hello([0-9]*)\.([0-9]*)
- Replacement string:
$1,$2
This would replace "hello222.555" with "222,555" and "hello2.55" with "2,55"
First the word "hello" is matched. In brackets follows an expression to search for any digits "0-9". The following asterisk finds multiple matches of the preceding element. 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.
Replacements followed by numbers
The following will not work:
- Search string:
123(.*)456
- Replacement string:
$1123456
That's because the search will think the number of the replacement is 1123456, not 1. Instead, you should have the following:
- Replacement string:
{$1}123456
In general, you can always enclose numbered values in curly brackets.
Case-insensitive search
To make your search case-insensitive, use regular expression and the case-insensitive modifier, (?i)
:
- Search string:
(?i)iphone
- Replacement string:
iPhone
The above will unify the casing of all mentions of iPhone/iphone/IPHONE to iPhone.
Command-line script: 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.
Other possible arguments/flags for this script are:
--dry-run
- only find the search texts, without doing any actual replacements--regex
- regular expressions should be used (default is false)--user
- the user to attribute the replacements to (default is the user with ID 1)--yes
- skip all prompts with an assumed "yes"--summary
- set a custom edit summary for the replacement edits--rename
- rename page titles instead of replacing contents--no-announce
- do not announce edits on Special:RecentChanges or watchlists--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
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".)
Přiřazení náhrad uživateli
Ve výchozím nastavení jsou všechna nahrazení připsána v historii stránek uživateli, který je inicioval. Místo toho však můžete nechat náhrady připsat předem nastavenému uživateli – například účtu robota – přidáním řádku jako je tento do LocalSettings.php pod zahrnutím Replace Text:
$wgReplaceTextUser = "MyReplaceTextBot";
Zadané uživatelské jméno již musí mít účet na wiki. Bude přidáno do pole MediaWiki s uživatelskými jmény, ve kterých nemusí být zaregistrováno nebo přihlášeno.
Autoři
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.
Historie verzí
Replace Text is currently at version 1.7. See the entire version history.
Snímky obrazovky
Níže jsou obrázky pracovního postupu 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.
Známé problémy
- 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
andsuhosin.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.
- 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
Překlad
Translation of Replace Text is done through translatewiki.net. The translation for this extension can be found here. 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.
Související odkazy
- Extension:MassEditRegex - A similar extension using regular expressions to edit multiple pages at once
Toto rozšíření je zahrnuto v následujících balíčcích a/nebo wiki farmách: Toto není autoritativní seznam. Některé wiki farmy/hostitelé mohou obsahovat tento extension, i když zde nejsou uvedeny. Pro potvrzení se vždy obraťte na své wiki farmy/hostitele nebo balíček. |