MediaWiki批量页面重命名

I need to rename all pages matching a certain pattern in MediaWiki. For any page containing XXXXX, it must be renamed to YYYYY. This includes page content as well as page names. For example, the wiki URL http://wiki.example.org/TfrmXXX_Rates should be renamed to http://wiki.example.org/TfrmYYY_Rates, and any text containing XXXXX should be replaced with YYYYY.

The Replace Text extension cannot be used for this, because it can only modify page content. I also need to rename the pages, and all references to them (including interwiki links).

What I have tried is this: dump the MySQL database, replace all occurrences of XXXX to YYYY, then drop and recreate the wiki database with the modified SQL script, and finally run the maintenance/rebuildall.php script (provided by MediaWiki).

The problem is that it does not work. When I search for a keyword, MediaWiki shows some hits with related page content. But when I open the URL, it shows "no content yet". I suspect that the reason for this is that some of the data is stored in PHP serialized form, and that cannot be replaced easily with a text search/replace.

So the question is: how do I rename all references in a MediaWiki database, including content and page names?

I could finish the task. Here are the steps:

  • Backup your database
  • Execute this to export all page names:

select page_title into outfile '/tmp/pagenames.txt' from page;

  • I have used a program to replace all occurences that needs to be replaced, and construct another text file with the format

oldXXXname|newXXXname

miken's solution is equally good, if you need to replace just a single word. If you need to replace XXX_1 XXX_2 XXX and other words that are prefixes of each other, then you need to write a program for this, and carefully select the order of replacements so they don't conflict with each other.

  • Then run

    php mediawiki/maintenance/moveBatch.php --noredirects /tmp/constructed.txt

  • Then use this kind of SQL to replace all other text references:

    use wiki_db; begin; update text set old_text = replace(replace(....replace( convert(old_text using utf8 ), 'XXX_1', 'YYYY' ), 'XXX_2', 'YYYY' ), 'XXX_3', 'YYYY' ... ), 'XXXX_2', 'YYYY_or_whatever' );

  • Finally, run

    php mediawiki/maintenance/rebuildall.php

I did not accept miken's answer only because it was just half of the solution.

Mediawiki is shipped with a maintenance script to do the renaming of pages and any links to them. You can run this query from MySQL to populate a file called /tmp/names.txt:

SELECT CONCAT(page_title, '|', REPLACE(page_title, 'XXXXX', 'YYYYY')) INTO OUTFILE '/tmp/names.txt' FROM page WHERE page_title LIKE '%XXXXX%';

This will give you this file:

TfrmXXX_Rates|TfrmYYY_Rates
TfrmXXX_Other|TformYYY_Other
...

Then you should be able to run this command:

php /path/to/mediawiki/maintenance/moveBatch.php --noredirects /tmp/names.txt

The Replace Text extension should work for any other references in the text that aren't links.