[Pywikipedia-l] SVN: [5983] trunk/pywikipedia/wikipedia.py

filnik at svn.wikimedia.org filnik at svn.wikimedia.org
Thu Oct 16 16:49:37 UTC 2008


Revision: 5983
Author:   filnik
Date:     2008-10-16 16:49:37 +0000 (Thu, 16 Oct 2008)

Log Message:
-----------
New function: pageAPInfo() to know if a page is a redirect or it's empty calling the API to improve the performance in some situations

Modified Paths:
--------------
    trunk/pywikipedia/wikipedia.py

Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py	2008-10-16 16:49:27 UTC (rev 5982)
+++ trunk/pywikipedia/wikipedia.py	2008-10-16 16:49:37 UTC (rev 5983)
@@ -885,6 +885,33 @@
             return False
         return True
 
+    def pageAPInfo(self):
+        """Return True if page exists on the wiki,
+           Raise IsRedirectPage if it's a redirect
+           Raise NoPage if the page doesn't exist
+
+        Using the API should be a lot faster.
+        Function done in order to improve the scripts performance.
+
+        """
+        params = {
+            'action'    :'query',
+            'prop'      :'info',
+            'titles'    :self.title(),
+            }
+        data = query.GetData(params,
+                        useAPI = True, encodeTitle = False)        
+        pageid = data['query']['pages'].keys()[0]
+        if data['query']['pages'][pageid].keys()[0] == 'lastrevid':
+            return True
+        elif data['query']['pages'][pageid].keys()[0] == 'redirect':
+            raise IsRedirectPage        
+        else:
+            # should not exists, OR we have problems.
+            # better double check in this situations
+            x = self.get()                
+            return True # if we reach this point, we had no problems.
+
     def isRedirectPage(self):
         """Return True if this is a redirect, False if not or not existing."""
         try:





More information about the Pywikipedia-l mailing list