Manual:Pywikibot/replace.py

Replace.py is part of the Pywikipedia bot framework.

This bot replaces text. It will retrieve information on which pages might need changes either from an XML dump or a text file, or only change a single page. To get some more information, use python replace.py -help

If you have Windows, you may omit "python".

Overview
You may use this script basically in two ways:
 * 1) You write all the parameters into the command line, including the text to be replaced and the replacement. This is useful for simple tasks. For example, will search for word "color" only in articles (ns:0), replaces the lower case occurences to colour, asks you each time to confirm the replacement, and uses the default edit comment. Be careful, because there are cases where color must not be changed to colour (e.g. in article Cascading Style Sheets); never run such a replacement automatically unless you are at least 100% sure it is always correct! This is almost the simpliest form of the command (see below at the minimal parameters).   is equivalent to writing the word color into the search bar and is a fast way of gathering articles, but will not find colored and colors.
 * 2) You store the main parameters, including old and new text, exceptions and edit comment in a file. This file may be either fixes.py which is included in your Pywikipedia distribution, but is subject to change at every update, so you have to save it for yourself, or user-fixes.py that designed for personal use, but is not included in Pywiki and may be created with generate_user_files.py. This latter one has a slightly different syntax, but has an example. This is much more efficient and flexible but needs some preparation.

Files
The bot uses three files in addition to the framework:
 * replace.py : the main module
 * fixes.py : a few predefined "fixes"
 * user-fixes.py : a file to add ones own fixes. The file is created nearly empty by generate_user_files.py

Files that may be used for input and/or output:
 * filename.txt : a file with a list of articles if specified with the parameter "-file", or
 * a file in which the bot will save the list of articles for later use (specified with "-save"/"-savenew")


 * filename.xml</tt> : a local XML dump if used with parameter "-xml"
 * replacelog</tt> : the log with a name that may be specified with parameter "-log"

Local
You can run replace.py with the following parameters (for example, ).

Examples
If you want to change templates from the old syntax, e.g., to the new syntax, e.g., download an XML dump file (page table) from http://download.wikimedia.org, then use this command:

python replace.py -xml -regex "" ""

You can match patterns across more than one line:

python replace.py -regex -start:! "First line\nSecond line" ""

You can insert or append text to a page (note the replacement text has an embedded new line):

python replace.py -regex '(?ms)^(.*)$' "\1   > "

If you have a dump called foobar.xml and want to fix typos, e.g. Errror -> Error, use this:

python replace.py -xml:foobar.xml "Errror" "Error"

If you have a page called 'John Doe' and want to convert HTML tags to wiki syntax, use: python replace.py -page:John_Doe -fix:HTML

If you run the bot without arguments you will be prompted multiple times for replacements:

python replace.py -file:blah.txt

The script asks the user before modifying an article. It is recommended to double-check the result to be sure that the bot did not introduce errors (especially with misspelled words). It is possible to specify a set of articles with an external text file containing Wiki links :

plane vehicle train car

The bot is then called using something like :

python replace.py [global-arguments] -file:articles_list.txt "errror" "error"

Rather than specifying regular expressions at the command line, it's preferable to add them to user-fixes.py</tt>

python replace.py -file:articles_list.txt -fix:example2

Example: Replacing multiple paragraphs
The original text of the page Sandbox is: This page is for any tests.

Welcome to the sandbox!

If you want to switch the statement (the second one goes before the first one), you type the following syntax: <pre style="overflow:auto">replace.py -page:Meta:Sandbox -regex "This page is for any tests.\r\n\r\nWelcome to the sandbox!" "Welcome to the sandbox!\n\nThis page is for any tests."

To add a new line we use.

Advanced use of fixes: own functions
To learn how to use your own functions in fixes.py and user-fixes.py and what is this good for, see hu:Szerkesztő:Bináris/Fixes and functions HOWTO.