Revision: 8581 Author: xqt Date: 2010-09-19 20:47:22 +0000 (Sun, 19 Sep 2010)
Log Message: ----------- update from rewrite
Modified Paths: -------------- trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2010-09-19 20:44:50 UTC (rev 8580) +++ trunk/pywikipedia/wikipedia.py 2010-09-19 20:47:22 UTC (rev 8581) @@ -940,7 +940,7 @@ self.latestRevision())
def latestRevision(self): - """Return the latest revision id for this page.""" + """Return the current revision id for this page.""" if not self._permalink: # When we get the page with getall, the permalink is received # automatically @@ -949,7 +949,31 @@ if hasattr(self, '_getexception'): raise self._getexception return int(self._permalink) + def userName(self): + """Return name or IP address of last user to edit page.
+ Returns None unless page was retrieved with getAll(). + + """ + return self._userName + + def isIpEdit(self): + """Return True if last editor was unregistered. + + Returns None unless page was retrieved with getAll() or _getEditPage(). + + """ + return self._ipedit + + def editTime(self): + """Return timestamp (in MediaWiki format) of last revision to page. + + Returns None if last edit time is unknown. + + """ + return self._editTime + + def previousRevision(self): """Return the revision id for the previous revision of this Page.""" vh = self.getVersionHistory(revCount=2) @@ -1052,19 +1076,50 @@
def isStaticRedirect(self, force=False): """Return True if this is a redirect containing the magic word - __STATICREDIRECT__, False if not or not existing.""" + __STATICREDIRECT__, False if not or not existing. + + """ + found = False if self.isRedirectPage(): staticKeys = self.site().getmagicwords('staticredirect') text = self.get(get_redirect=True, force=force) if staticKeys: - found = False for key in staticKeys: if key in text: found = True break - if found: return True - return False + return found
+ def isCategoryRedirect(self, text=None): + """Return True if this is a category redirect page, False otherwise.""" + + if not self.isCategory(): + return False + if not hasattr(self, "_catredirect"): + if not text: + try: + text = self.get(get_redirect=True) + except NoPage: + return False + catredirs = self.site().category_redirects() + for (t, args) in self.templatesWithParams(thistxt=text): + template = Page(self.site(), t, defaultNamespace=10 + ).titleWithoutNamespace() # normalize title + if template in catredirs: + # Get target (first template argument) + self._catredirect = self.site().namespace(14) + ":" + args[0] + break + else: + self._catredirect = False + return bool(self._catredirect) + + def getCategoryRedirectTarget(self): + """If this is a category redirect, return the target category title.""" + if self.isCategoryRedirect(): + import catlib + return catlib.Category(self.site(), self._catredirect) + raise IsNotRedirectPage + def isEmpty(self): """Return True if the page text has less than 4 characters.
@@ -1085,6 +1140,94 @@ ns = self.namespace() return ns >= 0 and ns % 2 == 1
+ def toggleTalkPage(self): + """Return other member of the article-talk page pair for this Page. + + If self is a talk page, returns the associated content page; + otherwise, returns the associated talk page. + Returns None if self is a special page. + + """ + ns = self.namespace() + if ns < 0: # Special page + return None + if self.isTalkPage(): + ns -= 1 + else: + ns += 1 + + if ns == 6: + return ImagePage(self.site(), self.titleWithoutNamespace()) + + return Page(self.site(), self.titleWithoutNamespace(), defaultNamespace=ns) + + def isCategory(self): + """Return True if the page is a Category, False otherwise.""" + return self.namespace() == 14 + + def isImage(self): + """Return True if this is an image description page, False otherwise.""" + return self.namespace() == 6 + + def isDisambig(self): + """Return True if this is a disambiguation page, False otherwise. + + Relies on the presence of specific templates, identified in + the Family file or on a wiki page, to identify disambiguation + pages. + + By default, loads a list of template names from the Family file; + if the value in the Family file is None, looks for the list on + [[MediaWiki:Disambiguationspage]]. + + """ + if not hasattr(self, "_isDisambig"): + if not hasattr(self._site, "_disambigtemplates"): + distl = self._site.family.disambig(self._site.lang) + if distl is None: + try: + disambigpages = Page(self._site, + "MediaWiki:Disambiguationspage") + self._site._disambigtemplates = set( + link.titleWithoutNamespace() + for link in disambigpages.linkedPages() + if link.namespace() == 10 + ) + except NoPage: + self._site._disambigtemplates = set(['Disambig']) + else: + # Normalize template capitalization + self._site._disambigtemplates = set( + t[:1].upper() + t[1:] for t in distl + ) + disambigInPage = self._site._disambigtemplates.intersection(self.templates()) + self._isDisambig = self.namespace() != 10 and len(disambigInPage) > 0 + return self._isDisambig + + def canBeEdited(self): + """Return bool indicating whether this page can be edited. + + This returns True if and only if: + - page is unprotected, and bot has an account for this site, or + - page is protected, and bot has a sysop account for this site. + + """ + try: + self.get() + except: + pass + if self.editRestriction == 'sysop': + userdict = config.sysopnames + else: + userdict = config.usernames + try: + userdict[self.site().family.name][self.site().lang] + return True + except: + # We don't have a user account for that wiki, or the + # page is locked and we don't have a sysop account. + return False + def botMayEdit(self, username): """Return True if this page allows bots to edit it.
@@ -1118,14 +1261,14 @@ if len(template[1]) == 0: return True else: - (type, bots) = template[1][0].split('=', 1) + (ttype, bots) = template[1][0].split('=', 1) bots = bots.split(',') - if type == 'allow': + if ttype == 'allow': if 'all' in bots or username in bots: return True else: return False - if type == 'deny': + if ttype == 'deny': if 'all' in bots or username in bots: return False else: @@ -1133,30 +1276,6 @@ # no restricting template found return True
- def userName(self): - """Return name or IP address of last user to edit page. - - Returns None unless page was retrieved with getAll(). - - """ - return self._userName - - def isIpEdit(self): - """Return True if last editor was unregistered. - - Returns None unless page was retrieved with getAll() or _getEditPage(). - - """ - return self._ipedit - - def editTime(self): - """Return timestamp (in MediaWiki format) of last revision to page. - - Returns None if last edit time is unknown. - - """ - return self._editTime - def namespace(self): """Return the number of the namespace of the page.
@@ -1166,79 +1285,6 @@ """ return self._namespace
- def isCategory(self): - """Return True if the page is a Category, False otherwise.""" - return self.namespace() == 14 - - def isImage(self): - """Return True if this is an image description page, False otherwise.""" - return self.namespace() == 6 - - def isCategoryRedirect(self, text=None): - """Return True if this is a category redirect page, False otherwise.""" - - if not self.isCategory(): - return False - if not hasattr(self, "_catredirect"): - if not text: - try: - text = self.get(get_redirect=True) - except NoPage: - return False - catredirs = self.site().category_redirects() - for (t, args) in self.templatesWithParams(thistxt=text): - template = Page(self.site(), t, defaultNamespace=10 - ).titleWithoutNamespace() # normalize title - if template in catredirs: - # Get target (first template argument) - self._catredirect = self.site().namespace(14) + ":" + args[0] - break - else: - self._catredirect = False - return bool(self._catredirect) - - def getCategoryRedirectTarget(self): - """If this is a category redirect, return the target category title.""" - if self.isCategoryRedirect(): - import catlib - return catlib.Category(self.site(), self._catredirect) - raise IsNotRedirectPage - - def isDisambig(self): - """Return True if this is a disambiguation page, False otherwise. - - Relies on the presence of specific templates, identified in - the Family file or on a wiki page, to identify disambiguation - pages. - - By default, loads a list of template names from the Family file; - if the value in the Family file is None, looks for the list on - [[MediaWiki:Disambiguationspage]]. - - """ - if not hasattr(self, "_isDisambig"): - if not hasattr(self._site, "_disambigtemplates"): - distl = self._site.family.disambig(self._site.lang) - if distl is None: - try: - disambigpages = Page(self._site, - "MediaWiki:Disambiguationspage") - self._site._disambigtemplates = set( - link.titleWithoutNamespace() - for link in disambigpages.linkedPages() - if link.namespace() == 10 - ) - except NoPage: - self._site._disambigtemplates = set(['Disambig']) - else: - # Normalize template capitalization - self._site._disambigtemplates = set( - t[:1].upper() + t[1:] for t in distl - ) - disambigInPage = self._site._disambigtemplates.intersection(self.templates()) - self._isDisambig = self.namespace() != 10 and len(disambigInPage) > 0 - return self._isDisambig - def getReferences(self, follow_redirects=True, withTemplateInclusion=True, onlyTemplateInclusion=False, redirectsOnly=False, internal = False): """Yield all pages that link to the page by API @@ -2138,29 +2184,6 @@
return response.code, response.msg, data
- def canBeEdited(self): - """Return bool indicating whether this page can be edited. - - This returns True if and only if: - * page is unprotected, and bot has an account for this site, or - * page is protected, and bot has a sysop account for this site. - """ - try: - self.get() - except: - pass - if self.editRestriction == 'sysop': - userdict = config.sysopnames - else: - userdict = config.usernames - try: - userdict[self.site().family.name][self.site().lang] - return True - except: - # We don't have a user account for that wiki, or the - # page is locked and we don't have a sysop account. - return False - def protection(self): """Return list of dicts of this page protection level. like: [{u'expiry': u'2010-05-26T14:41:51Z', u'type': u'edit', u'level': u'autoconfirmed'}, {u'expiry': u'2010-05-26T14:41:51Z', u'type': u'move', u'level': u'sysop'}] @@ -2179,28 +2202,6 @@ data=datas['query']['pages'].values()[0]['protection'] return data
- - def toggleTalkPage(self): - """Return the other member of the article-talk page pair for this Page. - - If self is a talk page, returns the associated content page; otherwise, - returns the associated talk page. - Returns None if self is a special page. - - """ - ns = self.namespace() - if ns < 0: # Special page - return None - if self.isTalkPage(): - ns -= 1 - else: - ns += 1 - - if ns == 6: - return ImagePage(self.site(), self.titleWithoutNamespace()) - - return Page(self.site(), self.titleWithoutNamespace(), defaultNamespace=ns) - def interwiki(self): """Return a list of interwiki links in the page text.