I could have sworn that there was already something that did this,
but recently I needed to search and replace on a long list of wiki
pages and I couldn't find anything, so I wrote a script. It needs
some work but here's the basic code (it only works in the main
namespace right now):
<?php
/* searchreplace.php
Find and replace using preg_replace from a list of pagenames
*/
# get the options from the command line
# argv[0] is the script name
if (!isset($argv[1])){
echo "USAGE:
php searchreplace.php -c <config file> -p <pagename file>
config file defines $pattern, $replacement, $wikipath if you don't
want to deal with regex from the shell
php searchreplace.php -w <path to wiki> -p <pagename file> list -f
<find regex> -r <replace text>
\n\n
pagename file is a list of page names\n\n";
exit;
}
$options = getopt('c: w:p:f:r:');
$page_list = $options['p'];
if (isset($options['c'])){
require_once($options['c']);
}else{
$wikipath = $options['w'];
$pattern = $options['f'];
$replacement = $options['r'];
}
print_r ($options);
# check that they are OK
if (is_file("$wikipath/AdminSettings.php")) {
require_once ("$wikipath/AdminSettings.php");
require_once ("$wikipath/maintenance/commandLine.inc");
}else{
die ("can't find AdminSettings.php - bad wiki path or
AdminSettings.php doesn't exist");
}
$infile = fopen ($page_list, 'r');
if (!$infile) die ("can't open $file\n");
while (!feof($infile)){
$page_name = trim(fgets($infile, 4096));
if (trim($page_name) != ''){
$title = Title::newFromText($page_name);
if (!is_object($title)) {
echo "can't find $page_name\n";
continue;
}
$old_page = get_wiki_text($page_name);
$new_page = preg_replace($pattern, "$replacement", $old_page); echo
"$pattern \n\n $replacement \n\n"; echo "$page_name\n\n$old_page\n\n
$new_page\n\n";
$new_page = str_replace('\n',"\n",$new_page);
$article = new Article($title);
if (isset ($new_page) && $new_page != '') $article->doEdit
( "$new_page", 'Edited by wikibot', EDIT_UPDATE | EDIT_FORCE_BOT );
}
}
function get_wiki_text($page_name, $ns = 'NS_MAIN'){
$title = Title::newFromText($page_name, $ns);
$revision = Revision::newFromTitle($title);
if (! $revision) return false;
return trim($revision->getText());
}
?>
Usage example:
trimer:/usr/local/phpwikibots jimhu$ php5 searchreplace.php -c
searchreplace/config_test.php -p searchreplace/test.txt
searchreplace/test.txt is
Sandbox
searchreplace/config_test.php is
<?php
$wikipath = "path to the wiki";
$pattern = "/foo/";
$replacement = "bar";
?>
On Nov 15, 2007, at 9:45 AM, Reizer Gábor wrote:
Hi,
I have a lot of tables in the following format:
{| class="wikitable"
...
|}
I want to replace these with the following:
{|border="2" cellspacing="0" cellpadding="4"
...
|}
How can I do this? What could be the quickest solution?
Regards,
Gabor
_______________________________________________
MediaWiki-l mailing list
MediaWiki-l(a)lists.wikimedia.org
http://lists.wikimedia.org/mailman/listinfo/mediawiki-l
=====================================
Jim Hu
Associate Professor
Dept. of Biochemistry and Biophysics
2128 TAMU
Texas A&M Univ.
College Station, TX 77843-2128
979-862-4054