Revision: 5903 Author: russblau Date: 2008-09-17 14:50:45 +0000 (Wed, 17 Sep 2008)
Log Message: ----------- leverage use of Link objects
Modified Paths: -------------- branches/rewrite/pywikibot/page.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2008-09-17 12:15:09 UTC (rev 5902) +++ branches/rewrite/pywikibot/page.py 2008-09-17 14:50:45 UTC (rev 5903) @@ -1439,7 +1439,7 @@
""" self._text = text - self.source = source + self._source = source self._defaultns = defaultNamespace
def parse(self): @@ -1451,9 +1451,9 @@ else: self._anchor = None
- if self.source is None: - self.source = pywikibot.Site() - self._site = self.source + if self._source is None: + self._source = pywikibot.Site() + self._site = self._source
# Clean up the name, it can come from anywhere. # Convert HTML entities to unicode @@ -1580,7 +1580,7 @@ # Can't make a link to a namespace alone... # "empty" local links can only be self-links # with a fragment identifier. - if not t and self._site == self.source and self._namespace != 0: + if not t and self._site == self._source and self._namespace != 0: raise ValueError("Invalid link (no page title): '%s'" % self._text)
self._title = t @@ -1617,8 +1617,38 @@ self.parse() return self._anchor
+ def astext(self, onsite=None): + """Return a text representation of the link.
+ @param onsite: if specified, present as a (possibly interwiki) link + from the given site; otherwise, present as an internal link on + the source site.
+ """ + if onsite is None: + onsite = self.site + title = self.title + if self.namespace: + title = onsite.namespace(self.namespace) + ":" + title + if self.section: + title = title + "#" + self.section + if onsite == self.site: + return u'[[%s]]' % title + if onsite.family == self.site.family: + return u'[[%s:%s]]' % (self.site.code, title) + if self.site.family.name == self.site.code: + # use this form for sites like commons, where the + # code is the same as the family name + return u'[[%s:%s]]' % (self.site.code, + title) + return u'[[%s:%s:%s]]' % (self.site.family.name, + self.site.code, + title) + + def __str__(self): + return self.astext() + + # Utility functions for parsing page titles
def html2unicode(text, ignore = []):
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2008-09-17 12:15:09 UTC (rev 5902) +++ branches/rewrite/pywikibot/site.py 2008-09-17 14:50:45 UTC (rev 5903) @@ -306,46 +306,17 @@ Use optional Site argument 'othersite' to generate an interwiki link.
""" - # TODO convert to Link method, deprecate - if othersite and othersite.code != self.code: - return u'[[%s:%s]]' % (self.code, title) - else: - return u'[[%s]]' % title + logger.debug("Site.linkto() method is deprecated; use pywikibot.Link") + return pywikibot.Link(title, self).astext(othersite)
def isInterwikiLink(self, s): """Return True if s is in the form of an interwiki link.
- Interwiki links have the form "foo:bar" or ":foo:bar" where foo is a - known language code or family. Called recursively if the first part - of the link refers to this site's own family and/or language. Do - not include brackets around the link! + If a link object constructed using "s" as the link text parses as + belonging to a different site, this method returns True.
""" - # TODO: convert to Link method - s = s.strip().lstrip(":") - if not ':' in s: - return False - first, rest = s.split(':',1) - # interwiki codes are case-insensitive - first = first.lower().strip() - # commons: forwards interlanguage links to wikipedia:, etc. - if self.family.interwiki_forward: - interlangTargetFamily = pywikibot.Family(self.family.interwiki_forward) - else: - interlangTargetFamily = self.family - if self.ns_index(first): - return False - if first in interlangTargetFamily.langs: - if first == self.code: - return self.isInterwikiLink(rest) - else: - return True - if first in self.family.get_known_families(site = self): - if first == self.family.name: - return self.isInterwikiLink(rest) - else: - return True - return False + return (pywikibot.Link(s, self).site != self)
def redirectRegex(self): """Return a compiled regular expression matching on redirect pages. @@ -916,6 +887,7 @@ % (page.title(withSection=False, asLink=True), item['title'])) api.update_page(page, item) + logging.debug(str(item)) return item[tokentype + "token"]
# following group of methods map more-or-less directly to API queries