View source for Action: Admin Replace

{{admin_replace}}

{{toc numerate=1}}

The ##~{{admin_replace}}## action allows administrators to do a global string find-and-replace on all the content pages of a wiki.

This is well suited to performing simple edits such as renaming a link, changing strings, adding templates, or correcting typos.

===Usage===

  A. search & replace form
  B. target matches selection form
  C. replace target text with replacement text


file:find-and-replace-1.png

To replace one text string with another across all regular pages on this wiki, enter the two pieces of text here and then hit "Continue".

file:find-and-replace-2.png

You will then be shown a list of pages that contain the search text, and you can choose the ones in which you want to replace it.

After replacing the first batch of results, it reloads the selection form with the next batch of results.
It remembers all processed and not selected record ids via 'replace_unset' session array and excludes them in the next search query.

file:find-and-replace-3.png

Your name will appear in page histories as the user responsible for any changes.

===Configuration===
%%
{{admin_replace
	[page="PageName"]
	[options=1]
	[lang="en"]
	[mute=1]
	[max=NUMBER]
}}
%%

Default is 50 matches, set max=100 in action for more.
  - average processing time may vary (on a local XAMPP stack 1 second per replaced match), it runs per match up to 12 queries (Change the max=NUMBER if it exceeds the max execution time.)
  - ##max## is limited by get_list_count() -> range: 10 ... 100
  - ##options## showing additional filters like language and categories
  - ##mute## is on by default, else it will send notifications for every watched page it changes (you likely want avoid that)

===Using regular expressions===
If you click on the "Use regular expressions" checkbox, you can use regular expressions[[^ https://en.wikipedia.org/wiki/Regular_expression]], within the search and replacement strings. 

The set of regular expressions allowed is basically a small subset of the PHP and MariaDb/MySQL regular-expression[[^ https://dev.mysql.com/doc/refman/8.0/en/regexp.html#operator_regexp]] 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.)

  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 parentheses, 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====

  Search string: ##123(.*)456##
  Replacement string: ##{$1}123456##

Enclose numbered values in curly brackets.

====Case-insensitive search====

To make your search case-insensitive, use regular expression and the case- insensitive modifier, ##(?i)## [[^ http://www.regular-expressions.info/modifiers.html]]:

  Search string: ##(?i)wackowiki## 
  Replacement string: ##WackoWiki## 

The above will unify the casing of all mentions of ##Wackowiki##/##wackowiki##/##WACKOWIKI## to ##WackoWiki##.

====Tips====

=====Adding content to the top of the page=====
Detect any first character at the beginning of a page:
  Search string: ##^(.)##
 
Prepend my content to the char found a the beginning of a page including a line break:
  Replacement string: %%MyContent
$1%%

=====Adding content at the end of the page =====

Detect any last character at the end of a page:
  Search string: ##(.)$##
 
Prepend my content to the char found a the ending of a page including a line break:
  Replacement string: %%$1
MyContent%%


===ToDo===

  * translate this page to all 6 main languages
  * test and improve it

===See also===
  * ((/Dev/PatchesHacks/MassRegexReplace MassRegexReplace)) - Mass edit using regular expressions