Extension:Replace Text

Replace Text is an extension to MediaWiki that provides a special page to allow administrators to do a global string find-and-replace on both the text and titles of the wiki's content pages.

Description
An administrator submits a search string and its replacement, as well as choosing whether to replace text within page contents, page titles, or both. 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; with a checkbox for each, so that they can uncheck 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 first asked to confirm their action before being shown the list of possible pages, since this action may not be easily undoable.

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. Pages that cannot be moved will be simply listed on the "select" page as being unmoveable, without a checkbox near them.

The search is case-sensitive, and does not currently allow regular expressions. The text of talk pages and "MediaWiki:" pages are not modified (though their titles can be modified). Talk pages in namespaces other than the main one (like "Template talk") can have their text modified; this is a known bug.

Note: 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. Jobs get activated every time a page is viewed on the wiki; to speed up the process (or slow it down), you change the number of jobs run when a page is viewed; the default is 1. For information on how to change it, see the $wgJobRunRate page.

Attention: If your revisions are compressed (that is, if $wgCompressRevisions is enabled in LocalSettings.php) then ReplaceText will not work because it makes use of SQL queries that can't search compressed text.

Code and download
You can download the Replace Text code in either one of these two compressed files:


 * replace_text_0.5.2.tar.gz
 * replace_text_0.5.2.zip

You can also download the code directly via SVN from the MediaWiki source code repository, at http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/ReplaceText/. From a command line, you can call the following:

To view the code online, including version history for each file, you can go here.

Installation
After you've obtained a 'ReplaceText' directory (either by extracting a compressed file or downloading via SVN), place this directory within the main MediaWiki 'extensions' directory. Then, anywhere in the file 'LocalSettings.php' in the main MediaWiki directory, add the following lines:

Author
Replace Text was written by Yaron Koren, reachable at yaron57 -at- gmail.com.

Version
Replace Text is currently at version 0.5.2.

The version history is:


 * 0.1 - April 29, 2008 - Initial version
 * 0.1.1 - May 7, 2008 - Language support added for Arabic, Dutch, French, Javanese, Malayalam, Norwegian Bokmål, Occitan, Portuguese, Serbian Cyrillic, Slovak, Swedish and Thai
 * 0.2 - May 12, 2008 - Replacement turned into two-step process, asking user to select among list of pages; actual replacement implemented as MediaWiki jobs; language support added for Traditional Chinese, Esperanto, Farsi, German, Khmer and Marathi
 * 0.2.1 - May 14, 2008 - Retrieval of matching pages done through SQL call, instead of manually; language support added for Bulgarian, Taiwanese Chinese and Luxembourgish
 * 0.2.2 - May 27, 2008 - Fixed handling of strings containing single quotation marks and target strings at the beginning of the page; set all replacements to be minor edits; pages are displayed in alphabetical order; language support added for Telugu
 * 0.2.3 - June 24, 2008 - Fixed handling of regular quotation marks; language support added for Czech, Galician, Polish and Vietnamese
 * 0.2.4 - August 6, 2008 - Updated initialization to use autoloading of classes and language values; bug fix so that replacement job does not override identity of logged-in user; title of page added as hidden field in forms; new 'replacetext' permission type added; "invert selections" link added to let user invert checkboxes in form; language support added for Brazilian Portuguese
 * 0.2.5 - September 2, 2008 - Fixed handling of strings containing backslashes; "invert selections" link only shown if there are more than five items
 * 0.2.6 - September 5, 2008 - Language aliases added
 * 0.2.7 - September 18, 2008 - Access control improved; language support added for Egyptian Arabic
 * 0.2.8 - October 1, 2008 - Handling of apostrophes re-fixed; language support added for Interlingua and Ripuarian
 * 0.3 - November 5, 2008 - Added handling for moving pages; language support added for Croatian and Russian
 * 0.3.1 - January 8, 2009 - Support for singulars and plurals in user messages; other code improvements; language support added for Basque, Finnish, Hebrew, Norwegian Nynorsk, Romanian, Spanish, Tagalog, Upper Sorbian and Volapük
 * 0.3.2 - January 18, 2009 - Fixed major bug introduced in 0.3.1; added "return to form" link at the end of the replacement process
 * 0.4 - January 22, 2009 - Added option to not keep the old title as a redirect for moved pages; language support added for Japanese and Lower Sorbian
 * 0.4.1 - April 7, 2009 - Fixed handling of HTML-escaped strings like "&amp;"; language support added for Belarusian and Bosnian
 * 0.5 - April 27, 2009 - Replacement of text within pages (as opposed to in page titles) made optional; HTML-printing code changed to use MediaWiki functions; language support added for Greek, Swiss German, Italian and Turkish
 * 0.5.1 - April 30, 2009 - Inputs for strings changed to textareas, to allow multi-line values; various small bug fixes
 * 0.5.2 - May 5, 2009 - Appearance of results page fixed; handling of backslashes fixed; "watch these pages" unchecked by default

Screenshots
Below are images of the workflow of 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:



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.

Code structure
The following are the files in the Replace Text extension:


 * README - description of the extension
 * ReplaceText.js - Javascript needed for the forms
 * ReplaceText.php - main file
 * ReplaceTextJob.php - class for the MediaWiki job that does the text replacement
 * ReplaceText.alias.php - special-page aliases
 * ReplaceText.i18n.php - language file
 * SpecialReplaceText.php - the "Special" page that provides the interface for the replacement

Languages supported
Replace Text supports Arabic, Egyptian Arabic, Bulgarian, Taiwanese Chinese, Traditional Chinese, Czech, Dutch, English, Finnish, French, German, Greek, Hebrew, Italian, Japanese, Javanese, Khmer, Marathi, Persian, Polish, Portuguese, Brazilian Portuguese, Romanian, Russian, Slovak, Spanish, Swedish, Tagalog, Telugu, Thai, Turkish, Vietnamese and many other languages.

Bugs and feature requests
Send any bug reports and requests to Yaron Koren, at yaron57 -at- gmail.com.

Contributing patches to the project
If you found some bug and fixed it, please create a patch by going to the "ReplaceText" directory, and typing:

svn diff >descriptivename.patch

Then send this patch, with a description, to Yaron Koren.

Translating
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.