Action: Admin Replace
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.
This action is reserved for administrators. For non-administrators it produces no output at all.
1. Usage
- search & replace form
- target matches selection form
- replace target text with replacement text
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".
The form also offers the following options:
- replace in the body of pages (
edit_pages), - replace in page titles (
edit_titles), - replace in comments (
edit_comments), - use regular expressions (
use_regex), - a minor edit flag (only if minor edits are enabled in the configuration),
- an optional edit note (if left empty, a note with the search and replacement strings is generated automatically).
Notes on the scope of the search:
- Deleted pages are never touched.
- Comments are only searched and replaced when the comment option is ticked; if the "pages" option is ticked as well, both pages and comments appear in the selection list.
- Page titles are only searched when the title option is ticked; if body and title are selected, a page is listed when either matches.
- The search string must be at least 3 characters long.
- If search string and replacement string are identical, the action stops with a hint.
- An empty replacement string is allowed, but a warning is displayed (it removes the text).
- If the search or replacement string is longer than 200 characters, replacing titles is refused (the title field in the database is limited to 250 bytes).
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. When regular expressions are used, the pages just processed are added to this list as well, so that they cannot match a second time. The "Cancel" button resets the form and the session data.
Your name will appear in page histories as the user responsible for any changes, together with the automatically generated edit note (search string and replacement string).
2. Configuration
{{admin_replace
[page="PageName"]
[options=1]
[lang="en"]
[mute=1]
[max=NUMBER]
}}
These are the parameters of the action; all of them are optional:
-
page- cluster (tag) whose pages are processed. Default is"/", i.e. the whole wiki;page=""is treated the same way. The cluster page itself, all its subpages and the comments attached to them are included. -
options-1shows additional filters like language and categories. Default is0. -
lang- process only pages of this language. Default is""(all languages). An unknown language code is ignored (with a message). -
mute-1(default) saves the changes without sending notifications. Withmute=0a watch notification is sent for every watched page that was changed (you likely want to avoid that). -
max- number of matches shown per batch. Default is50; the effective range is limited by the list length setting (roughly 10 ... 100).
- 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.)
-
maxis limited by get_list_count() -> range: 10 ... 100 -
optionsshowing additional filters like language and categories -
muteis on by default, else it will send notifications for every watched page it changes (you likely want avoid that)
To display the built-in help of this action use {{admin_replace help=1}}.
3. Using regular expressions
If you click on the "Use regular expressions" checkbox, you can use regular expressions[1], within the search and replacement strings.
Please note: the search is performed by the database, the replacement by PHP.
- The list of matching pages is found with the regular expression support of the database (MySQL 8 uses ICU, MariaDB uses PCRE).
- The actual replacement is done with PHP
preg_replace()using the modifiers/Uu: U = ungreedy, u = UTF-8. - Therefore a pattern must be understood by both engines; this is the reason why only a small subset of the PHP and MariaDB/MySQL regular-expression set[2] can be used reliably.
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.)
a(.*)cReplacement string:
ac\$1This 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.
Further details worth knowing:
- Because of the
Umodifier, quantifiers are lazy:.*matches the *shortest* possible text, not the longest. If you need the greedy behaviour, write it explicitly (e.g..*+) and test it carefully. - In the search string backreferences are written with a backslash (
\1), in the replacement string with the PHP syntax (\$1or\${1}). - Without the "Use regular expressions" checkbox the replacement is a plain, case-sensitive literal replacement (all occurrences). In that mode
$1is ordinary text, not a backreference. - The characters
/and\must be escaped in the search string, because the pattern is delimited by/in PHP.
3.1. More complex regexps
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.
3.2. Replacements followed by numbers
123(.*)456Replacement string:
{\$1}123456Enclose numbered values in curly brackets.
3.3. Case-insensitive search
To make your search case-insensitive, use regular expression and the case-insensitive modifier, (?i) [3]:
(?i)wackowiki Replacement string:
WackoWiki
The above will unify the casing of all mentions of Wackowiki/wackowiki/WACKOWIKI to WackoWiki.
3.4. Tips
3.4.1. Adding content to the top of the page
Detect any first character at the beginning of a page:
^(.)Prepend my content to the char found at the beginning of a page including a line break:
MyContent $1
Paste only the text between the markers into the replacement field of the form (the %% are just wiki code markers). The form converts Windows line breaks (\r\n) into Unix line breaks (\n).
3.4.2. Adding content at the end of the page
Detect any last character at the end of a page:
(.)$Prepend my content to the char found at the ending of a page including a line break:
\$1 MyContent
Caveat: in both the database and in PHP, the "\$" may match the position before a final newline; if a page ends with a line break, the last matched character can therefore be that line break. Check the result on a test page first.
4. ToDo
- translate this page to all 6 main languages
- test and improve it
5. See also
- MassRegexReplace - Mass edit using regular expressions
- Binary search - search on all pages using exact or regex search
Footnotes:
Files
| find-and-replace-0.10.zip | admin_replace action version 0.10 | 10.3 KiB | 04/04/2023 11:07 | |
| find-and-replace-1.png | search & replace form | 40.0 KiB | 03/21/2023 11:34 | |
| find-and-replace-2.png | target matches selection form | 93.7 KiB | 03/21/2023 11:34 | |
| find-and-replace-3.png | replace target text with replacement text | 32.7 KiB | 03/21/2023 11:34 |


