diff -r 38fdb5acd7c5 -r c199b2ad0c31 0.11/wikirename/script.py
--- a/0.11/wikirename/script.py	Wed Nov 03 11:34:03 2010 +0100
+++ b/0.11/wikirename/script.py	Tue Nov 09 14:12:20 2010 +0100
@@ -19,15 +19,27 @@
 def main(*argv):
     parser = optparse.OptionParser(usage='Usage: %prog old-name new-name trac-env', version='RenamePage 2.0')
     parser.add_option('-d','--debug',help='Activate debugging', action='store_true', default=False)
+    parser.add_option('--dry-run',help='dry-run', action='store_true', default=False)
+    parser.add_option('-t', '--target', help='trac target env') 
+    parser.add_option('--remove',help='remove wiki page from old env (only '
+                      'evaluated with a new --target env)',
+                      action='store_true', default=False)
     (options, args) = parser.parse_args(list(argv[1:]))
     if len(args) < 3:
         parser.error("Not enough arguments")
 
     oldname = args[0]
     newname = args[1]
-    envpath = args[2]
+    envpath = os.path.normpath(args[2])
     env = Environment(envpath)
-    rename_page(env, oldname, newname, username(), '127.0.0.1', debug=options.debug)
+    newenv = None
+    if options.target:
+        newenvpath = os.path.normpath(options.target)
+        if newenvpath != envpath:
+            newenv = Environment(newenvpath)
+    rename_page(env, oldname, newname, username(), '127.0.0.1',
+                debug=options.debug, newenv=newenv,
+                newenv_keepold=not(options.remove), dry_run=options.dry_run)
 
 def run():
     main(*sys.argv)
diff -r 38fdb5acd7c5 -r c199b2ad0c31 0.11/wikirename/util.py
--- a/0.11/wikirename/util.py	Wed Nov 03 11:34:03 2010 +0100
+++ b/0.11/wikirename/util.py	Tue Nov 09 14:12:20 2010 +0100
@@ -5,6 +5,7 @@
 import time
 import optparse
 import urllib
+import shutil
 
 from trac.core import *
 from trac.env import *
@@ -15,21 +16,33 @@
     pass
     
 def print_(s, *args): 
-    print s%args
+    print s % args
     
 def log_intercept(log):
     def out(s, *args):
         log('WikiRenamePlugin: '+s, *args)
     return out
 
-def rename_page(env, oldname, newname, user, ip, debug=False, db=None):
-    """Rename a wiki page from oldname to newname, using env as the environment."""
+def rename_page(env, oldname, newname, user, ip, debug=False, db=None,
+                newenv=None, newdb=None, newenv_keepold=True, dry_run=False):
+    """Rename a wiki page from oldname to newname, using env as the
+    environment. If a new target env newenv is given, this will move the wiki
+    page with its history & attachments to the new trac environment."""
     handle_commit = False
     if not db:
         db = env.get_db_cnx()
         handle_commit = True
     cursor = db.cursor()
-    
+  
+    handle_targetdb_commit = False 
+    if newenv and newenv is not env:
+        if not newdb:
+            newdb = newenv.get_db_cnx()
+            handle_targetdb_commit = True
+        newcursor = newdb.cursor()
+    else:
+        newcursor = cursor
+ 
     if debug is False:
         debug = pass_
     elif debug is True:
@@ -37,6 +50,9 @@
     else:
         debug = log_intercept(debug)
 
+    debug('env: %s', env)
+    debug('newenv: %s', newenv)
+
     sqlbase = ' FROM wiki w1, ' + \
         '(SELECT name, MAX(version) AS max_version FROM wiki GROUP BY name) w2 ' + \
         'WHERE w1.version = w2.max_version AND w1.name = w2.name '
@@ -50,41 +66,102 @@
     if not row:
         raise TracError, 'Page not found'
     
-    new_wiki_page = (newname,row[0]+1,int(time.time()),user,ip,row[1],'Name changed from %s to %s'%(oldname,newname),0)
+    new_wiki_page = (newname, row[0]+1, int(time.time()), user, ip, row[1], 
+                     'Name changed from %s to %s' % (oldname,newname), 0)
 
     # Create a new page with the needed comment
     debug('Inserting new page %r', new_wiki_page)
-    cursor.execute('INSERT INTO wiki (name,version,time,author,ipnr,text,comment,readonly) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)', new_wiki_page)
+    if not dry_run:
+        newcursor.execute(
+            'INSERT INTO wiki '
+            '(name, version, time, author, ipnr, text, comment, readonly) '
+            'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', new_wiki_page)
 
     # Move all the old versions of the page
     debug("Moving all old versions of page")
-    cursor.execute('UPDATE wiki SET name=%s WHERE name=%s', (newname,oldname))
-
+    if not newenv:
+        cursor.execute('UPDATE wiki SET name=%s WHERE name=%s', (newname,oldname))
+    else:
+        sql = 'SELECT name, version, time, author, ipnr, text, comment, ' \
+              ' readonly FROM wiki where name = %s'
+        cursor.execute(sql, (oldname,))
+        rows = cursor.fetchall()
+        for page_version in rows:
+            debug('Inserting page version %r as %r', page_version, newname)
+            page_version = list(page_version)
+            page_version[0] = newname
+            page_version = tuple(page_version)
+            if not dry_run: 
+                newcursor.execute(
+                    'INSERT INTO wiki '
+                    '(name, version, time, author, ipnr, text, comment, '
+                    'readonly) '
+                    'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', page_version)
+        if not newenv_keepold:
+            debug('Deleting page %s from old env %s', oldname, env.path)
+            if not dry_run:
+                cursor.execute('DELETE FROM wiki WHERE name=%s', (oldname,))
     # Move any attachments that are on the page
     debug("Moving all attachments in database")
-    cursor.execute('UPDATE attachment SET id=%s WHERE type=%s AND id=%s', (newname,'wiki',oldname))
+    if not newenv:
+        cursor.execute('UPDATE attachment SET id=%s WHERE type=%s AND id=%s',
+                       (newname, 'wiki', oldname))
+        no_of_attachments = cursor.rowcount
+    else:
+        cursor.execute('SELECT * from attachment WHERE type=%s AND id=%s',
+                       ('wiki', oldname))
+        rows = cursor.fetchall()
+        no_of_attachments = len(rows)
+        for attachment in rows:
+            debug('Adding attachment %r as %r', attachment, newname)
+            attachment = list(attachment)
+            attachment[1] = newname
+            attachment = tuple(attachment)
+            if not dry_run:
+                newcursor.execute('INSERT INTO attachment VALUES (%s, %s, %s, '
+                                  '%s, %s, %s, %s, %s)', attachment)
+        if not newenv_keepold:
+            debug('Deleting page %s attachments from old env %s', oldname,
+                  env.path)
+            if not dry_run:
+                cursor.execute(
+                    'DELETE FROM attachment WHERE type=%s AND id=%s',
+                    ('wiki', oldname))
 
-    debug("Found %s attachments on that page", cursor.rowcount)
-    if cursor.rowcount > 0:
+    debug("Found %s attachments on that page", no_of_attachments)
+    if dry_run or no_of_attachments > 0:
         # Change the directory where the attachments are stored, if there were any
         debug('Moving all attachments on file system')
-        from_path = os.path.join(env.path, 'attachments', 'wiki', urllib.quote(oldname))
-        to_path = os.path.join(env.path, 'attachments', 'wiki', urllib.quote(newname))
+        from_path = os.path.join(env.path, 'attachments', 'wiki', 
+                                 urllib.quote(oldname))
+        if not newenv:
+            to_path = os.path.join(env.path, 'attachments', 'wiki',
+                                   urllib.quote(newname))
+        else:
+            to_path = os.path.join(newenv.path, 'attachments', 'wiki',
+                                   urllib.quote(newname))
         debug('Moving from %r to %r', from_path, to_path)
-        os.renames(from_path, to_path)
+        if not dry_run:
+            if newenv is None or not newenv_keepold:
+                os.renames(from_path, to_path)
+            else:
+               shutil.copytree(from_path, to_path)
 
     # Get a list of all wiki pages containing links to the old page
-    debug("Trying to fix links")
-    sql = 'SELECT w1.version,w1.name,w1.text' + sqlbase + 'AND w1.text like %s'
-    debug('Running query %r', sql)
-    cursor.execute(sql, ('%[wiki:'+oldname+'%',))
+    if not newenv:
+        debug("Trying to fix links")
+        sql = 'SELECT w1.version,w1.name,w1.text' + sqlbase + 'AND w1.text like %s'
+        debug('Running query %r', sql)
+        cursor.execute(sql, ('%[wiki:'+oldname+'%',))
 
-    # Rewrite all links to the old page, such as to point to the new page
-    for row in list(cursor):
-        debug("Found a page with a backlink in it: %s (v%s)", row[1], row[0])
-        newtext = re.sub('\[wiki:%s'%oldname,'[wiki:%s'%newname,row[2])
-        cursor.execute('UPDATE wiki SET text=%s WHERE name=%s AND version=%s', (newtext,row[1],row[0]))
+        # Rewrite all links to the old page, such as to point to the new page
+        for row in list(cursor):
+            debug("Found a page with a backlink in it: %s (v%s)", row[1], row[0])
+            newtext = re.sub('\[wiki:%s'%oldname,'[wiki:%s'%newname,row[2])
+            cursor.execute('UPDATE wiki SET text=%s WHERE name=%s AND version=%s', (newtext,row[1],row[0]))
 
+    if handle_targetdb_commit:
+        newdb.commit()
     if handle_commit:
         db.commit()
 
