Replace Text – Search and replace in your wiki

CCO Public Domain via Pixabay.

When working with longer texts, it is not unusual to have to change a common term because it has been written incorrectly or because it is no longer up-to-date. Common word processing software packages like MS Word have a “search and replace” function so that you do not have to go through the whole text and change everything by hand.

There is a similar tool for MediaWiki users called ReplaceText. This is a small but powerful MediaWiki extension allowing you to search for specific combinations of characters, whether it is text, code or spaces, and to replace them both in wiki articles and in article titles. It is also possible to use complex algorithms which recognise patterns in the form of regular expressions so that you can undertake more sophisticated search-and-replace tasks.

ReplaceText can be used in two ways: very simply via its own special page with an input window, or via the command-line. To use this latter option, you have to run the script replaceAll.php on the wiki server. This can take many arguments and make targeted replacements very quickly. Working at the server level, however, requires special permissions which should be restricted to users already possessing technical knowledge.

ReplaceText with the special page

ReplaceText generates a special page which you can find on the “Special pages” page under “Data and tools”.

ReplaceText can be found as a special page under “Data and tools”.

The special page is shown in bold, which means that only users with administrator permissions can access it. This is a good idea because one can cause quite a lot of damage with this tool “at the touch of a button”. The special page linked to works like a small assistant leading you through this easy procedure:

In the 1st step, you enter the old text you want to replace and the new text that should replace it. You can also work with diverse filters here and restrict the replacement operation to specific namespaces or categories. You also have the possibility to restrict the function to article contents or article titles.

The first ReplaceText form

In the 2nd step the assistant lists all the articles and article titles in which it has found the text you want replaced. This gives you the opportunity to exclude specific places from the replacement operation. The process is only started when you confirm by clicking on “Replace”.

The second form with the selection of articles

In the 3rd step the system confirms what has been replaced and the number of pages affected.

Confirming the replacement operation

Using regular expressions

Regular expressions (RegExp or Regex for short) are used as filter criteria in the search function, to check strings of characters for specific sequences. With the help of regular expressions, it is, for example, possible to filter all words which begin with B and end in E without defining the letters that occur in between.

To use regular expressions when replacing text in the wiki, you need to tick the relevant checkbox in the first form, activating them. You can only use a subset of the regular expressions from PHP and MySQL. The following characters can be used:

( ) . * + ? [ ] |  and $1, $2 etc.

If you need these characters to be interpreted as real parts of the text, and not as special characters in a regular expression, then you will need to put a “\” character just in front (see the second example below where the dot character is interpreted as a full-stop).

Examples:

Search string Replace string Behaviour
\[\[Kk]ategorie:(.*?)\]\]
[[Category:$1]]
Replaces all German category tags with English category tags
([0-9]*)\.([0-9]*)
$1,$2
Replaces the dot between two sequences of numbers with a comma, for example:
2465.8924 to 2465,8924

What happens when replacing in the wiki?

MediaWiki is a very special piece of software with many small units, which raises many questions when an extension like ReplaceText is used. For example, should the replacement show up in the history, and if so, how? How can one undo the process? And there are many more. The author of the extension, Yaron, has taken these special circumstances into consideration and, in my view, solved them very well:

  • Every individual replacement process shows as a “minor edit” in the history of a wiki article. By default, the author of the replacement will be set as the person who started the process. One can, however, assign the action to a bot.
  • When a page title is changed, the article is effectively moved, and then one can, if desired, set up a redirect.
  • There is no individual function to undo the replacement. One can, of course, “reverse” the process using Replace Text again. However, one has to take into account that sequences of letters that were already there might be substituted.

Where can I download the function?

As we mentioned above, the extension was penned by the experienced MediaWiki developer Yaron Koren, who is, for example, also responsible for the extension Semantic Forms. ReplaceText is not contained in the standard version of MediaWiki, but can be downloaded from mediawiki.org and installed later on.

If you have installed BlueSpice, the function is already integrated and can be used straight away.

Further information on ReplaceText:
– Mediawiki.org
– BlueSpice HelpdeskReplaceText

You Might Also Like

Leave a Reply