changeset 784:22350c783ec5 pytest2

add backend.documents(), simplify tags code (by using .search and .documents)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 28 Aug 2011 00:26:05 +0200
parents 66c5f8bdfb2b
children e0c61257b6c4
files MoinMoin/apps/frontend/views.py MoinMoin/storage/backends/indexing.py
diffstat 2 files changed, 28 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Sat Aug 27 22:27:44 2011 +0200
+++ b/MoinMoin/apps/frontend/views.py	Sun Aug 28 00:26:05 2011 +0200
@@ -49,7 +49,7 @@
 from MoinMoin.items import Item, NonExistent
 from MoinMoin.items import ROWS_META, COLS, ROWS_DATA
 from MoinMoin import config, user, util, wikiutil
-from MoinMoin.config import ACTION, COMMENT, CONTENTTYPE, ITEMLINKS, ITEMTRANSCLUSIONS, NAME, CONTENTTYPE_GROUPS, MTIME
+from MoinMoin.config import ACTION, COMMENT, CONTENTTYPE, ITEMLINKS, ITEMTRANSCLUSIONS, NAME, CONTENTTYPE_GROUPS, MTIME, TAGS
 from MoinMoin.util import crypto
 from MoinMoin.util.interwiki import url_for_item
 from MoinMoin.security.textcha import TextCha, TextChaizedForm, TextChaValid
@@ -1815,13 +1815,18 @@
     """
     show a list or tag cloud of all tags in this wiki
     """
-    counts_tags_names = flaskg.storage.all_tags()
     item_name = request.values.get('item_name', '') # actions menu puts it into qs
-    if counts_tags_names:
-        # sort by tag name
-        counts_tags_names = sorted(counts_tags_names, key=lambda e: e[1])
+    docs = flaskg.storage.documents(all_revs=False, wikiname=app.cfg.interwikiname)
+    tags_counts = {}
+    for doc in docs:
+        tags = doc.get(TAGS, [])
+        logging.debug("name %s rev %s tags %s" % (doc[NAME], doc["rev_no"], tags))
+        for tag in tags:
+            tags_counts[tag] = tags_counts.setdefault(tag, 0) + 1
+    tags_counts = sorted(tags_counts.items())
+    if tags_counts:
         # this is a simple linear scaling
-        counts = [e[0] for e in counts_tags_names]
+        counts = [count for tags, count in tags_counts]
         count_min = min(counts)
         count_max = max(counts)
         weight_max = 9.99
@@ -1829,11 +1834,11 @@
             scale = weight_max / 2
         else:
             scale = weight_max / (count_max - count_min)
-        def cls(count, tag):
+        def cls(count):
             # return the css class for this tag
             weight = scale * (count - count_min)
             return "weight%d" % int(weight)  # weight0, ..., weight9
-        tags = [(cls(count, tag), tag) for count, tag, names in counts_tags_names]
+        tags = [(cls(count), tag) for tag, count in tags_counts]
     else:
         tags = []
     return render_template("global_tags.html",
@@ -1847,10 +1852,11 @@
     """
     show all items' names that have tag <tag>
     """
-    item_names = flaskg.storage.tagged_items(tag)
+    query = And([Term("wikiname", app.cfg.interwikiname), Term(TAGS, tag), ])
+    docs = flaskg.storage.search(query, all_revs=False, sortedby="name_exact", limit=None)
+    item_names = [doc[NAME] for doc in docs]
     return render_template("item_link_list.html",
                            headline=_("Items tagged with %(tag)s", tag=tag),
                            item_name=tag,
                            item_names=item_names)
 
-
--- a/MoinMoin/storage/backends/indexing.py	Sat Aug 27 22:27:44 2011 +0200
+++ b/MoinMoin/storage/backends/indexing.py	Sun Aug 28 00:26:05 2011 +0200
@@ -66,17 +66,8 @@
     def search_page(self, q, all_revs=False, pagenum=1, pagelen=10, **kw):
         return self._index.search_page(q, all_revs=all_revs, pagenum=pagenum, pagelen=pagelen, **kw)
 
-    def all_tags(self):
-        """
-        Return a unsorted list of tuples (count, tag, tagged_itemnames) for all tags.
-        """
-        return self._index.all_tags()
-
-    def tagged_items(self, tag):
-        """
-        Return a list of item names of items that are tagged with <tag>.
-        """
-        return self._index.tagged_items(tag)
+    def documents(self, all_revs=False, **kw):
+        return self._index.documents(all_revs=all_revs, **kw)
 
 
 class IndexingItemMixin(object):
@@ -305,20 +296,14 @@
             for hit in searcher.search_page(q, pagenum, pagelen=pagelen, **kw):
                 yield hit.fields()
 
-    def all_tags(self):
-        with self.index_object.latest_revisions_index.searcher() as latest_revs_searcher:
-            docs = latest_revs_searcher.documents(wikiname=self.wikiname)
-            tags_names = {}
-            for doc in docs:
-                tags = doc.get(TAGS, [])
-                logging.debug("name %s rev %s tags %s" % (doc[NAME], doc["rev_no"], tags))
-                for tag in tags:
-                    tags_names.setdefault(tag, []).append(doc[NAME])
-            counts_tags_names = [(len(names), tag, names) for tag, names in tags_names.items()]
-            return counts_tags_names
+    def documents(self, all_revs=False, **kw):
+        if all_revs:
+            ix = self.index_object.all_revisions_index
+        else:
+            ix = self.index_object.latest_revisions_index
+        with ix.searcher() as searcher:
+            # Note: callers must consume everything we yield, so the for loop
+            # ends and the "with" is left to close the index files.
+            for doc in searcher.documents(**kw):
+                yield doc
 
-    def tagged_items(self, tag):
-        with self.index_object.latest_revisions_index.searcher() as latest_revs_searcher:
-            docs = latest_revs_searcher.documents(tags=tag, wikiname=self.wikiname)
-            return [doc[NAME] for doc in docs]
-