changeset 1753:2be00a5febbe

Fix and clean up scripts which are no longer working due to storage API change. This resolves issue #246.
author Cheer Xiao <xiaqqaix@gmail.com>
date Mon, 27 Aug 2012 17:30:14 +0800
parents bff62fea804a
children abbbace78848
files MoinMoin/script/account/create.py MoinMoin/script/account/disable.py MoinMoin/script/account/resetpw.py MoinMoin/script/maint/modified_systemitems.py MoinMoin/script/maint/reduce_revisions.py MoinMoin/script/maint/set_meta.py
diffstat 6 files changed, 47 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/account/create.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/account/create.py	Mon Aug 27 17:30:14 2012 +0800
@@ -12,6 +12,7 @@
 from flaskext.script import Command, Option
 
 from MoinMoin import user
+from MoinMoin.app import before_wiki
 
 
 class Create_User(Command):
@@ -30,7 +31,7 @@
     )
 
     def run(self, name, aliasname, email, openid, password):
-        flaskg.unprotected_storage = app.storage
+        before_wiki()
         msg = user.create_user(username=name,
                                password=password,
                                email=email,
--- a/MoinMoin/script/account/disable.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/account/disable.py	Mon Aug 27 17:30:14 2012 +0800
@@ -12,6 +12,7 @@
 from flaskext.script import Command, Option
 
 from MoinMoin import user
+from MoinMoin.app import before_wiki
 
 
 class Disable_User(Command):
@@ -24,13 +25,13 @@
     )
 
     def run(self, name, uid):
-        flaskg.unprotected_storage = app.unprotected_storage
         flags_given = name or uid
         if not flags_given:
             print 'incorrect number of arguments'
             import sys
             sys.exit()
 
+        before_wiki()
         if uid:
             u = user.User(uid)
         elif name:
@@ -40,7 +41,7 @@
             print 'This user "{0!r}" does not exists!'.format(u.name)
             return
 
-        print " {0:<20} {1:!r<25} {2:<35}".format(u.id, u.name, u.email),
+        print " {0:<20} {1:!r<25} {2:<35}".format(u.itemid, u.name, u.email),
         if not u.disabled: # only disable once
             u.disabled = 1
             u.name = u"{0}-{1}".format(u.name, u.id)
--- a/MoinMoin/script/account/resetpw.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/account/resetpw.py	Mon Aug 27 17:30:14 2012 +0800
@@ -13,6 +13,7 @@
 from flaskext.script import Command, Option
 
 from MoinMoin import user
+from MoinMoin.app import before_wiki
 from MoinMoin.util import crypto
 
 
@@ -28,13 +29,13 @@
     )
 
     def run(self, name, uid, password):
-        flaskg.unprotected_storage = app.unprotected_storage
         flags_given = name or uid
         if not flags_given:
             print 'incorrect number of arguments'
             import sys
             sys.exit()
 
+        before_wiki()
         if uid:
             u = user.User(uid)
         elif name:
--- a/MoinMoin/script/maint/modified_systemitems.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/maint/modified_systemitems.py	Mon Aug 27 17:30:14 2012 +0800
@@ -11,23 +11,18 @@
 
 from flask import current_app as app
 from flaskext.script import Command
+
 from MoinMoin.config import IS_SYSITEM, SYSITEM_VERSION
-from MoinMoin.storage.error import NoSuchRevisionError
 
 class Modified_SystemItems(Command):
     description = 'This command can be used to list system items that has been edited in this wiki.'
 
     def run(self):
-        storage = app.unprotected_storage
         edited_sys_items = []
-        for item in storage.iteritems():
-            try:
-                rev = item.get_revision(-1)
-            except NoSuchRevisionError:
-                continue
-            is_sysitem = rev.get(IS_SYSITEM, False)
+        for current_rev in app.storage.documents():
+            is_sysitem = current_rev.meta.get(IS_SYSITEM, False)
             if is_sysitem:
-                version = rev.get(SYSITEM_VERSION)
+                version = current_rev.meta.get(SYSITEM_VERSION)
                 if version is None:
                     # if we don't have the version, it was edited:
                     edited_sys_items.append(item.name)
--- a/MoinMoin/script/maint/reduce_revisions.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/maint/reduce_revisions.py	Mon Aug 27 17:30:14 2012 +0800
@@ -1,6 +1,7 @@
 # Copyright: 2009 MoinMoin:ChristopherDenter
 # Copyright: 2011 MoinMoin:ReimarBauer
 # Copyright: 2011 MoinMoin:ThomasWaldmann
+# Copyright: 2012 MoinMoin:CheerXiao
 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
 
 """
@@ -13,7 +14,9 @@
 from flask import current_app as app
 from flaskext.script import Command, Option
 
-from MoinMoin.config import NAME, NAME_EXACT
+from whoosh.query import Every
+
+from MoinMoin.config import NAME, NAME_EXACT, REVID
 
 
 class Reduce_Revisions(Command):
@@ -24,21 +27,20 @@
     )
 
     def run(self, query):
-        storage = app.unprotected_storage
         if query:
-            qp = storage.query_parser([NAME_EXACT, ])
-            q = qp.parse(query)
+            qp = app.storage.query_parser([NAME_EXACT, ])
+            q = qp.parse(query_text)
         else:
             q = Every()
-        results = storage.search(q, limit=None)
-        for result in results:
-            item_name = result[NAME]
-            item = storage.get_item(item_name)
-            current_revno = item.next_revno - 1
-            for revno in item.list_revisions():
-                if revno < current_revno:
-                    rev = item.get_revision(revno)
-                    print "Destroying {0!r} revision {1}.".format(item_name, revno)
-                    rev.destroy()
+
+        for current_rev in app.storage.search(q, limit=None):
+            current_name = current_rev.meta[NAME]
+            current_revid = current_rev.meta[REVID]
+            for rev in current_rev.item.iter_revs():
+                revid = rev.meta[REVID]
+                if revid != current_revid:
+                    name = rev.meta[NAME]
+                    print "Destroying {0!r} revision {1}.".format(name, revid)
+                    current_rev.item.destroy_revision(revid)
 
         print "Finished reducing backend."
--- a/MoinMoin/script/maint/set_meta.py	Sun Aug 26 14:03:02 2012 -0700
+++ b/MoinMoin/script/maint/set_meta.py	Mon Aug 27 17:30:14 2012 +0800
@@ -1,6 +1,7 @@
 # Copyright: 2009 MoinMoin:ChristopherDenter
 # Copyright: 2011 MoinMoin:ReimarBauer
 # Copyright: 2011 MoinMoin:ThomasWaldmann
+# Copyright: 2012 MoinMoin:CheerXiao
 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
 
 """
@@ -12,15 +13,14 @@
 
 
 from ast import literal_eval
-from shutil import copyfileobj
 
 from flask import current_app as app
-from flask import g as flaskg
 from flaskext.script import Command, Option
 
+from whoosh.query import Every
+
 from MoinMoin.config import NAME, NAME_EXACT
 from MoinMoin.script import fatal
-from MoinMoin.storage.error import NoSuchRevisionError
 
 
 class Set_Meta(Command):
@@ -37,42 +37,29 @@
     )
 
     def run(self, key, value, remove, query):
-        storage = app.unprotected_storage
-
         if not ((key and value) or (key and remove)) or (key and value and remove):
             fatal("You need to either specify a proper key/value pair or "
                   "only a key you want to delete (with -r set).")
 
+        if not remove:
+            try:
+                value = literal_eval(value)
+            except ValueError:
+                fatal("You need to specify a valid Python literal as the argument")
+
         if query:
-            qp = storage.query_parser([NAME_EXACT, ])
-            q = qp.parse(query)
+            qp = app.storage.query_parser([NAME_EXACT, ])
+            q = qp.parse(query_text)
         else:
             q = Every()
-        results = storage.search(q, limit=None)
-        for result in results:
-            item_name = result[NAME]
-            item = storage.get_item(item_name)
-            try:
-                last_rev = item.get_revision(-1)
-            except NoSuchRevisionError:
-                last_rev = None
-
-            next_rev = item.create_revision(item.next_revno)
 
-            if last_rev:
-                # Copy data.
-                copyfileobj(last_rev, next_rev)
-                # Copy metadata:
-                for k, v in last_rev.iteritems():
-                    if remove and k == key:
-                        continue
-                    next_rev[k] = v
-
-            if not remove:
-                # Set or overwrite given metadata key with text
-                next_rev[key] = literal_eval(value)
-                print "Processing {0!r}, setting {1}={2!r}.".format(item_name, key, value)
+        for current_rev in app.storage.search(q, limit=None):
+            name = current_rev.meta[NAME]
+            newmeta = dict(current_rev.meta)
+            if remove:
+                newmeta.pop(key)
+                print "Processing {0!r}, removing {1}.".format(name, key)
             else:
-                print "Processing {0!r}, removing {1}.".format(item_name, key)
-
-            item.commit()
+                newmeta[key] = value
+                print "Processing {0!r}, setting {1}={2!r}.".format(name, key, value)
+            current_rev.item.store_revision(newmeta, current_rev.data)