1. marchael
  2. moin-2.0

Commits

Thomas Waldmann  committed 3388522

add query_parser building method to storage, use index-based search in some scripts

moin maint_reduce_revisions and maint_set_meta now have a --query option that takes
a whoosh query

whoosh can not do regex yet, but you can use wildcards. also, you can search in
other fields than in name_exact (which is the default).

remove MoinMoin.storage.serialization.TermMatch - it is not used and we need
to solve this differently.

deprecate MoinMoin.storage.terms, use whoosh indexed search in the future.

  • Participants
  • Parent commits 21a6765
  • Branches pytest2

Comments (0)

Files changed (5)

File MoinMoin/script/maint/reduce_revisions.py

View file
  • Ignore whitespace
 # Copyright: 2009 MoinMoin:ChristopherDenter
 # Copyright: 2011 MoinMoin:ReimarBauer
+# Copyright: 2011 MoinMoin:ThomasWaldmann
 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
 
 """
-    MoinMoin - Reduce Revisions of a backend
+MoinMoin - Reduce Revisions of a backend
 
-    This script removes all revisions but the last one from all selected items.
+This script removes all revisions but the last one from all selected items.
 """
 
 
-import re
 from flask import current_app as app
 from flaskext.script import Command, Option
 
-from MoinMoin.storage.terms import NameRE
+from MoinMoin.config import NAME
 
 
 class Reduce_Revisions(Command):
     description = "This command can be used to remove all revisions but the last one from all selected items."
     option_list = (
-        Option('--pattern', '-p', required=False, dest='pattern', type=unicode, default=".*",
-               help="You can limit the operation on certain items whose names match the given pattern."),
+        Option('--query', '-q', dest="query", type=unicode, default='',
+               help='Only perform the operation on items found by the given query.'),
     )
 
-    def run(self, pattern):
+    def run(self, query):
         storage = app.unprotected_storage
-        query = NameRE(re.compile(pattern))
-        # If no pattern is given, the default regex will match every item.
-        for item in storage.search_items(query):
+        if query:
+            qp = storage.query_parser("name_exact", all_revs=False)
+            q = qp.parse(query)
+        else:
+            q = Every()
+        results = storage.search(q, all_revs=False, 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 %r revision %d." % (item_name, revno)
                     rev.destroy()
 
         print "Finished reducing backend."

File MoinMoin/script/maint/set_meta.py

View file
  • Ignore whitespace
 # Copyright: 2009 MoinMoin:ChristopherDenter
 # Copyright: 2011 MoinMoin:ReimarBauer
+# Copyright: 2011 MoinMoin:ThomasWaldmann
 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
 
 """
-    MoinMoin - Set Metadata of a revision
+MoinMoin - Set Metadata of a revision
 
-    This script duplicates the last revision of the selected item
-    and sets or removes metadata.
+This script duplicates the last revision of the selected item
+and sets or removes metadata.
 """
 
 
-import re
 from ast import literal_eval
 from shutil import copyfileobj
 
 from flask import g as flaskg
 from flaskext.script import Command, Option
 
+from MoinMoin.config import NAME
 from MoinMoin.script import fatal
-from MoinMoin.storage.terms import NameRE
 from MoinMoin.storage.error import NoSuchRevisionError
 
+
 class Set_Meta(Command):
     description = "This command can be used to set meta data of a new revision."
     option_list = (
         Option('--key', '-k', required=False, dest='key', type=unicode,
                help="The key you want to set/change in the new revision"),
-        Option('--value', '-v', dest="text", type=unicode,
+        Option('--value', '-v', dest="value", type=unicode,
                help='The value to set for the given key.'),
         Option('--remove', '-r', dest="remove", action='store_true', default=False,
                help='If you want to delete the key given, add this flag.'),
-        Option('--pattern', '-p', dest="pattern", type=unicode, default='.*',
-               help='Only perform the operation on items whose names match the pattern.')
+        Option('--query', '-q', dest="query", type=unicode, default='',
+               help='Only perform the operation on items found by the given query.')
     )
 
-    def run(self, key, text, remove, pattern):
+    def run(self, key, value, remove, query):
         storage = app.unprotected_storage
 
-        if not ((key and text) or (key and remove)) or (key and text and remove):
+        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).")
 
-        query = NameRE(re.compile(pattern))
-        for item in storage.search_items(query):
+        if query:
+            qp = storage.query_parser("name_exact", all_revs=False)
+            q = qp.parse(query)
+        else:
+            q = Every()
+        results = storage.search(q, all_revs=False, 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:
 
             if not remove:
                 # Set or overwrite given metadata key with text
-                value = literal_eval(text)
-                next_rev[key] = value
+                next_rev[key] = literal_eval(value)
+                print "Processing %r, setting %s=%r." % (item_name, key, value)
+            else:
+                print "Processing %r, removing %s." % (item_name, key)
+
             item.commit()
 

File MoinMoin/storage/backends/indexing.py

View file
  • Ignore whitespace
         item.publish_metadata()
         return item
 
+    def query_parser(self, default_field, all_revs=False):
+        return self._index.query_parser(default_field, all_revs=all_revs)
+
     def search(self, q, all_revs=False, **kw):
         return self._index.search(q, all_revs=all_revs, **kw)
 
     # TODO by intercepting write() to index data written to a revision
 
 from whoosh.writing import AsyncWriter
+from whoosh.qparser import QueryParser
+
 from MoinMoin.search.indexing import WhooshIndex
 
 class ItemIndex(object):
                 logging.debug("Latest revisions: removing %d", latest_doc_number)
                 async_writer.delete_document(latest_doc_number)
 
+    def query_parser(self, default_field, all_revs=False):
+        if all_revs:
+            schema = self.index_object.all_revisions_schema
+        else:
+            schema = self.index_object.latest_revisions_schema
+        return QueryParser(default_field, schema=schema)
+
     def search(self, q, all_revs=False, **kw):
         if all_revs:
             ix = self.index_object.all_revisions_index

File MoinMoin/storage/serialization.py

View file
  • Ignore whitespace
         return item is not None and item.name in self.item_names
 
 
-class TermMatch(XMLSelectiveGenerator):
-    def __init__(self, out, term):
-        self.term = term  # see MoinMoin.storage.terms
-        XMLSelectiveGenerator.__init__(self, out)
-
-    def shall_serialize(self, item=None, rev=None,
-                        revno=None, current_revno=None):
-        if item is not None:
-            self.term.prepare()
-            return self.term.evaluate(item)
-        return False
-
-
 def serialize(obj, xmlfile, xmlgen_cls=XMLSelectiveGenerator, *args, **kwargs):
     """
     Serialize <obj> to <xmlfile>.
 
     The default value of <xmlgen_cls> will just serialize everything. Alternatively,
     use some of XMLSelectiveGenerator child classes to do selective serialization,
-    e.g. of just a list of items or just of items that match some search term.
+    e.g. of just a list of items or just a subset of the revisions.
 
     :arg obj: object to serialize (must mix in Serializable)
     :arg xmlfile: output file (file-like or filename)

File MoinMoin/storage/terms.py

View file
  • Ignore whitespace
 """
     MoinMoin - search expression object representation
 
+    DEPRECATED - please use storage.search with whoosh queries.
+
     This module defines the possible search terms for a query to the
     storage backend. This is used, for example, to implement searching,
     page lists etc.