<?php if (!defined('PmWiki')) exit();
/*
	isorename.php  Copyright 2007 Hans Bracker
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published
	by the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	Install normally and call with 
	?action=isorename  - check all files and rename where necessary
	?action=isorename&pattern=Group.*  - rename only files in Group
	Other wildcard patterns are allowed for patter= parameter
	action=isorename&backup=1 - the original file will be copied with -BAK extension first
	action=isorename&test - test renaming without actually renaming any files
	Add (:messages:) markup to page to receive feedback
	
	version 2007-11-21
*/

$HandleAuth['isorename'] = 'admin';
$HandleActions['isorename'] = 'HandleIsoRename';

# rename pages with new ISO 8859 pagename patterns
function HandleIsoRename ($pagename, $auth) {
	global $WikiDir, $MessagesFmt;
	$req = RequestArgs();
	if (isset($req['pattern'])) $pat = $req['pattern'];
	$matches = MatchPageNames(ListPages("/^.*/"),$pat);	
   foreach ($matches as $i=>$pname) { 
		$newname = MakePageName($pagename, $pname);
		if($pname==$newname) continue;
		else {
			$page = RetrieveAuthPage($pname, $auth, true, READPAGE_CURRENT);   
			$pagefile = $WikiDir->pagefile($pname);
			$newfile = preg_replace('/'.$pname.'/', $newname, $pagefile);
			if (!isset($req['test'])) {
				if (isset($req['backup'])) @copy($pagefile,"$pagefile-BAK");
				@rename($pagefile,"$newfile");
			}
			$MessagesFmt[] = "<div class='wikimessage'>$[Renamed $pname => $newname]</div>";
   	}
   }
	HandleBrowse($pagename);
	return '';
} //}}}