Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/80789


Change subject: Implement a simple preloading generator for ItemPages
......................................................................

Implement a simple preloading generator for ItemPages

This was not added to pagegenerators since it works
in a very narrow set of cases, and isn't what the
user expects.

Change-Id: I05917eb982f41dcdc92d8e45292ba6c27eac47f7
---
M pywikibot/site.py
1 file changed, 25 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/89/80789/1

diff --git a/pywikibot/site.py b/pywikibot/site.py
index bca3feb..66dc2de 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -3398,6 +3398,31 @@
             raise pywikibot.data.api.APIError, data['errors']
         return data['entities']
 
+    def preloaditempages(self, pagelist, groupsize=50):
+        """Yields ItemPages with content prefilled.
+
+        Note that [at least in current implementation] pages may be iterated
+        in a different order than in the underlying pagelist.
+
+        This only works if we've initalized the page using the ID. If it
+        was created with ItemPage.fromPage, this is useless.
+
+        @param pagelist: an iterable that yields ItemPage objects
+        @param groupsize: how many pages to query at a time
+        @type groupsize: int
+        """
+        from pywikibot.tools import itergroup
+        for sublist in itergroup(pagelist, groupsize):
+            pages = [str(p.getID() for p in sublist)]
+            cache = dict((p.getID(), p) for p in sublist)
+            req = api.Request(action='wbgetentities', ids='|'.join(pages))
+            data = req.submit()
+            for qid in data['entities']:
+                if not qid in cache:
+                    continue  # This should never be possible...
+                cache[qid]._content = data['entities'][qid]
+                yield cache[qid]
+
     def getPropertyType(self, prop):
         """
         This is used sepecifically because we can cache

-- 
To view, visit https://gerrit.wikimedia.org/r/80789
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I05917eb982f41dcdc92d8e45292ba6c27eac47f7
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to