Revision: 5272 Author: russblau Date: 2008-04-25 15:36:24 +0000 (Fri, 25 Apr 2008)
Log Message: ----------- implemented getlanglinks(); current implementation causes the bot to connect to every site referred to in a language link, which probably isn't desirable.
Modified Paths: -------------- branches/rewrite/pywikibot/data/api.py branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py branches/rewrite/pywikibot/tests/page_tests.py
Modified: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py 2008-04-25 08:32:54 UTC (rev 5271) +++ branches/rewrite/pywikibot/data/api.py 2008-04-25 15:36:24 UTC (rev 5272) @@ -465,6 +465,7 @@ raise AssertionError( "Page %s has neither 'pageid' nor 'missing' attribute" % pagedict['title']) + page._redir = 'redirect' in pagedict if 'lastrevid' in pagedict: page._revid = pagedict['lastrevid'] if 'touched' in pagedict:
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2008-04-25 08:32:54 UTC (rev 5271) +++ branches/rewrite/pywikibot/page.py 2008-04-25 15:36:24 UTC (rev 5272) @@ -520,7 +520,6 @@ # no restricting template found return True
- def put(self, newtext, comment=None, watchArticle=None, minorEdit=True, force=False): """Save the page with the contents of the first argument as the text.
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2008-04-25 08:32:54 UTC (rev 5271) +++ branches/rewrite/pywikibot/site.py 2008-04-25 15:36:24 UTC (rev 5272) @@ -484,21 +484,32 @@ return self._namespaces[num] return self._namespaces[num][0]
+ def getpageinfo(self, page): + """Load page info from api and save in page attributes""" + title = page.title(withSection=False) + query = api.PropertyGenerator( + "info", + inprop="protection|talkid|subjectid", + titles=title.encode(self.encoding())) + for pageitem in query: + if pageitem['title'] != title: + raise RuntimeError( + "getpageinfo: Query on %s returned data on '%s'" + % (page, pageitem['title'])) + api.update_page(page, pageitem) + def page_exists(self, page): """Return True if and only if page is an existing page on site.""" if not hasattr(page, "_pageid"): - query = api.PropertyGenerator( - "info", inprop="protection|talkid|subjectid", - titles=page.title(withSection=False - ).encode(self.encoding())) - for pageitem in query: - if pageitem['title'] != page.title(withSection=False): - raise RuntimeError( - "page_exists: Query on %s returned data on '%s'" - % (page, pageitem['title'])) - page._pageid = pageitem['pageid'] + self.getpageinfo(page) return page._pageid > 0
+ def page_isredirect(self, page): + """Return True if and only if page is a redirect.""" + if not hasattr(page, "_redir"): + self.getpageinfo(page) + return bool(page._redir) + # following group of methods map more-or-less directly to API queries
def getbacklinks(self, page, followRedirects=False, filterRedirects=None, @@ -752,8 +763,27 @@ page._revisions[revision.revid] = revision if latest: page._revid = revision.revid -
+ def getinterwiki(self, page): + # TODO + raise NotImplementedError + + def getlanglinks(self, page): + """Iterate all interlanguage links on page, yielding Link objects.""" + lltitle = page.title(withSection=False) + llquery = api.PropertyGenerator("langlinks", + titles=lltitle.encode(self.encoding()) + ) + for pageitem in llquery: + if pageitem['title'] != lltitle: + raise RuntimeError( + "getlanglinks: Query on %s returned data on '%s'" + % (page, pageitem['title'])) + for linkdata in pageitem['langlinks']: + yield pywikibot.Link(linkdata['*'], + source=pywikibot.Site(linkdata['lang'])) + + #### METHODS NOT IMPLEMENTED YET (but may be delegated to Family object) #### class NotImplementedYet:
Modified: branches/rewrite/pywikibot/tests/page_tests.py =================================================================== --- branches/rewrite/pywikibot/tests/page_tests.py 2008-04-25 08:32:54 UTC (rev 5271) +++ branches/rewrite/pywikibot/tests/page_tests.py 2008-04-25 15:36:24 UTC (rev 5272) @@ -203,6 +203,9 @@
if __name__ == '__main__': try: - unittest.main() - except SystemExit: - pass + try: + unittest.main() + except SystemExit: + pass + finally: + pywikibot.stopme()
pywikipedia-l@lists.wikimedia.org