Revision: 7989 Author: russblau Date: 2010-03-11 19:16:15 +0000 (Thu, 11 Mar 2010)
Log Message: ----------- Get magic word localizations from the API instead of storing them in family files.
Modified Paths: -------------- branches/rewrite/pywikibot/family.py branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/family.py =================================================================== --- branches/rewrite/pywikibot/family.py 2010-03-11 17:44:03 UTC (rev 7988) +++ branches/rewrite/pywikibot/family.py 2010-03-11 19:16:15 UTC (rev 7989) @@ -490,7 +490,9 @@
# A list with the name for cross-project cookies. # default for wikimedia centralAuth extensions. - self.cross_projects_cookies = ['centralauth_Session', 'centralauth_Token', 'centralauth_User'] + self.cross_projects_cookies = ['centralauth_Session', + 'centralauth_Token', + 'centralauth_User'] self.cross_projects_cookie_username = 'centralauth_User'
# A list with the name in the cross-language flag permissions @@ -676,226 +678,9 @@ return self.disambiguationTemplates[fallback] else: raise KeyError( - "ERROR: title for disambig template in language %(language_code)s unknown" +"ERROR: title for disambig template in language %(language_code)s unknown" % {'language_code': code})
- # Localised magic words for language code 'xyz' can be found in - # the MediaWiki source code in the file - # /mediawiki/trunk/phase3/languages/messages/MessagesXyz.php - # in the 'magicwords' array - - # Localised redirect codes - - # Note that redirect codes are case-insensitive, so it is enough - # to enter the code in lowercase here. - - # When creating a redirect page, only the first item is looked for. - # When matching for redirects, default 'redirect' is always inserted - # => if default redirect keyword used for a language is not 'redirect', - # it is not necessary to add 'redirect' at the end of the list - redirect = { - 'ab': [u'перенаправление', u'перенапр', u'redirect'], - 'ace': [u'alih'], - 'af': [u'aanstuur'], - 'aln': [u'ridrejto'], - 'als': [u'weiterleitung'], - 'an': [u'redirección'], - 'ar': [u'تحويل'], - 'arn': [u'redirección'], - 'arz': [u'تحويل'], - 'av': [u'перенаправление', u'перенапр'], - 'ay': [u'redirección'], - 'ba': [u'перенаправление', u'перенапр'], - 'bar': [u'weiterleitung'], - 'bat-smg': [u'peradresavimas'], - 'bcc': [u'تغییرمسیر'], - 'be-tarask': [u'перанакіраваньне'], - 'be-x-old': [u'перанакіраваньне'], - 'bg': [u'виж', u'пренасочване'], - 'bm': [u'redirection'], - 'bqi': [u'تغییرمسیر'], - 'br': [u'adkas'], - 'bug': [u'alih'], - 'bs': [u'preusmjeri'], - 'cbk-zam': [u'redirección'], - 'ce': [u'перенаправление', u'перенапр'], - 'cs': [u'přesměruj'], - 'cu': [u'прѣнаправлєниѥ'], - 'cv': [u'перенаправление', u'перенапр'], - 'cy': [u'ail-cyfeirio', u'ailgyfeirio'], - 'de': [u'weiterleitung'], - 'de-at': [u'weiterleitung'], - 'de-ch': [u'weiterleitung'], - 'de-formal': [u'weiterleitung'], - 'dsb': [u'weiterleitung'], - 'el': [u'ανακατευθυνση'], - 'eml': [u'rinvia', u'rinvio'], - 'eo': [u'alidirektu'], - 'es': [u'redirección'], - 'et': [u'suuna'], - 'eu': [u'birzuzendu'], - 'fa': [u'تغییرمسیر'], - 'ff': [u'redirection'], - 'fi': [u'ohjaus', u'uudelleenohjaus'], - 'fiu-vro': [u'saadaq'], - 'fr': [u'redirection'], - 'frp': [u'redirèccion', u'redirection'], - 'fur': [u'rinvia', u'rinvio'], - 'ga': [u'athsheoladh'], - 'gag': [u'yönlendirme'], - 'gl': [u'redirección'], - 'glk': [u'تغییرمسیر'], - 'gn': [u'redirección'], - 'gsw': [u'weiterleitung'], - 'he': [u'הפניה'], - 'hr': [u'preusmjeri'], - 'hsb': [u'weiterleitung'], - 'ht': [u'redirection'], - 'hu': [u'átirányítás'], - 'hy': [u'վերահղում'], - 'id': [u'alih'], - 'inh': [u'перенаправление', u'перенапр'], - 'is': [u'tilvísun'], - 'it': [u'rinvia', u'rinvio'], - 'ja': [u'転送', u'リダイレクト'], - 'jv': [u'alih'], - 'ka': [u'გადამისამართება'], - 'kaa': [u'aýdaw', u'айдау'], - 'kk': [u'айдау'], - 'kk-arab': [u'ايداۋ'], - 'kk-cyrl': [u'АЙДАУ'], - 'kk-latn': [u'aýdaw', u'айдау'], - 'km': [u'\u1794\u1789\u17d2\u1787\u17bc\u1793\u1794\u1793\u17d2\u178f', - u'\u1794\u17d2\u178f\u17bc\u179a\u1791\u17b8\u178f\u17b6\u17c6\u1784', - u'\u1794\u17d2\u178a\u17bc\u179a\u1785\u17c6\u178e\u1784\u1787\u17be\u1784', - u'ប្តូរទីតាំងទៅ'], - 'ko': [u'넘겨주기'], - 'ksh': [u'ömleide op', u'ömleidung'], - 'kv': [u'перенаправление', u'перенапр'], - 'lad': [u'redirección'], - 'lb': [u'weiterleitung'], - 'lbe': [u'перенаправление', u'перенапр'], - 'li': [u'doorverwijzing'], - 'lij': [u'rinvia', u'rinvio'], - 'lld': [u'rinvia', u'rinvio'], - 'lmo': [u'rinvia', u'rinvio'], - 'ln': [u'redirection'], - 'lt': [u'peradresavimas'], - 'map-bms': [u'alih'], - 'mg': [u'redirection'], - 'mhr': [u'перенаправление', u'перенапр'], - 'mk': [u'пренасочување', u'види'], - 'ml': [u'തിരിച്ചുവിടുക', u'തിരിച്ചുവിടല്'], - 'mo': [u'redirecteaza'], - 'mr': [u'पुनर्निर्देशन'], - 'mt': [u'rindirizza'], - 'mwl': [u'ancaminar'], - 'myv': [u'перенаправление', u'перенапр'], - 'mzn': [u'تغییرمسیر'], - 'nah': [u'redirección'], - 'nap': [u'rinvia'], - 'nds': [u'wiederleiden', u'weiterleitung'], - 'nds-nl': [u'deurverwiezing', u'doorverwijzing'], - 'new': [u'पुनर्निर्देश'], - 'nl': [u'doorverwijzing'], - 'nn': [u'omdiriger'], - 'no': [u'omdirigering'], - 'oc': [u'redireccion'], - 'os': [u'рарвыст', u'перенаправление', u'перенапр'], - 'pdc': [u'weiterleitung'], - 'pl': [u'patrz', u'przekieruj', u'tam'], - 'pms': [u'rinvia', u'rinvio'], - 'pt': [u'redirecionamento'], - 'pt-br': [u'redirecionamento'], - 'qu': [u'pusapuna', u'redirección'], - 'rmy': [u'redirecteaza'], - 'ro': [u'redirecteaza'], - 'ru': [u'перенаправление', u'перенапр'], - 'sa': [u'पुनर्निदेशन'], - 'sah': [u'перенаправление', u'перенапр'], - 'scn': [u'rinvia', u'rinvio'], - 'sd': [u'چوريو'], - 'sg': [u'redirection'], - 'shi': [u'تحويل'], - 'si': [u'යළියොමුව'], - 'sk': [u'presmeruj'], - 'sl': [u'preusmeritev'], - 'sli': [u'weiterleitung'], - 'sq': [u'ridrejto'], - 'sr': [u'преусмери', u'преусмери'], - 'sr-ec': [u'преусмери'], - 'sr-el': [u'preusmeri'], - 'srn': [u'stir', u'doorverwijzing'], - 'stq': [u'weiterleitung'], - 'su': [u'alih'], - 'sv': [u'omdirigering'], - 'szl': [u'patrz', u'przekieruj', u'tam'], - 'ta': [u'வழிமாற்று'], - 'te': [u'దారిమార్పు'], - 'th': [u'เปลี่ยนทาง'], - 'tr': [u'yönlendirme'], - 'tt': [u'yünältü'], - 'tt-latn': [u'yünältü'], - 'tt-cyrl': [u'перенаправление', u'перенапр'], - 'ty': [u'redirection'], - 'udm': [u'перенаправление', u'перенапр'], - 'uk': [u'перенаправлення', u'перенаправление', u'перенапр'], - 'vec': [u'rinvia', u'rinvio'], - 'vep': [u'suuna'], - 'vi': [u'đổi', u'đổi'], - 'vls': [u'doorverwijzing'], - 'vro': [u'saadaq', u'suuna'], - 'wa': [u'redirection'], - 'wo': [u'redirection'], - 'yi': [u'ווייטערפירן', u'הפניה'], - 'zea': [u'doorverwijzing'] - } - - # So can be pagename code - pagename = { - 'bg': [u'СТРАНИЦА'], - 'he': [u'שם הדף'], - 'kk': [u'БЕТАТАУЫ'], - 'nn': ['SIDENAMN', 'SIDENAVN'], - 'ru': [u'НАЗВАНИЕСТРАНИЦЫ'], - 'sr': [u'СТРАНИЦА'], - 'tt': [u'BİTİSEME'] - } - - pagenamee = { - 'he': [u'שם הדף מקודד'], - 'kk': [u'БЕТАТАУЫ2'], - 'nn': ['SIDENAMNE', 'SIDENAVNE'], - 'ru': [u'НАЗВАНИЕСТРАНИЦЫ2'], - 'sr': [u'СТРАНИЦЕ'] - } - - def pagenamecodes(self, code): - pos = ['PAGENAME'] - pos2 = [] - if code in self.pagename: - pos = pos + self.pagename[code] - elif code == 'als': - return self.pagenamecodes('de') - elif code == 'bm': - return self.pagenamecodes('fr') - for p in pos: - pos2 += [p, p.lower()] - return pos2 - - def pagename2codes(self, code): - pos = ['PAGENAME'] - pos2 = [] - if code in self.pagenamee: - pos = pos + self.pagenamee[code] - elif code == 'als': - return self.pagename2codes('de') - elif code == 'bm': - return self.pagename2codes('fr') - for p in pos: - pos2 += [p, p.lower()] - return pos2 - # Methods def protocol(self, code): """
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2010-03-11 17:44:03 UTC (rev 7988) +++ branches/rewrite/pywikibot/site.py 2010-03-11 19:16:15 UTC (rev 7989) @@ -261,17 +261,22 @@ normalizeNamespace = ns_normalize # for backwards-compatibility
def redirect(self, default=True): - """Return the localized redirect tag for the site. + """Return list of localized redirect tags for the site.
If default is True, falls back to 'REDIRECT' if the site has no special redirect tag.
""" - if default: - return self.family.redirect.get(self.code, [u"REDIRECT"])[0] - else: - return self.family.redirect.get(self.code, None) + return [u"REDIRECT"]
+ def pagenamecodes(self, default=True): + """Return list of localized PAGENAME tags for the site.""" + return [u"PAGENAME"] + + def pagename2codes(self, default=True): + """Return list of localized PAGENAMEE tags for the site.""" + return [u"PAGENAMEE"] + def lock_page(self, page, block=True): """Lock page for writing. Must be called before writing any page.
@@ -335,22 +340,14 @@ """ return (pywikibot.Link(s, self).site != self)
- def redirectRegex(self): + def redirectRegex(self, pattern=None): """Return a compiled regular expression matching on redirect pages.
Group 1 in the regex match object will be the target title.
""" - #TODO: is this needed, since the API identifies redirects? - # (maybe, the API can give false positives) - default = 'REDIRECT' - try: - keywords = set(self.family.redirect[self.code]) - keywords.add(default) - pattern = r'(?:' + '|'.join(keywords) + ')' - except KeyError: - # no localized keyword for redirects - pattern = r'%s' % default + if pattern is None: + pattern = "REDIRECT" # A redirect starts with hash (#), followed by a keyword, then # arbitrary stuff, then a wikilink. The wikilink may contain # a label, although this is not useful. @@ -847,6 +844,70 @@ ts = self.getcurrenttimestamp() return pywikibot.Timestamp.fromtimestampformat(ts)
+ def getmagicwords(self, word): + """Return list of localized "word" magic words for the site.""" + if not hasattr(self, "_magicwords"): + sirequest = api.Request( + site=self, + action="query", + meta="siteinfo", + siprop="magicwords" + ) + try: + sidata = sirequest.submit() + assert 'query' in sidata, \ + "API siteinfo response lacks 'query' key" + sidata = sidata['query'] + assert 'magicwords' in sidata, \ + "API siteinfo response lacks 'magicwords' key" + self._magicwords = dict((item["name"], item["aliases"]) + for item in sidata["magicwords"]) + + except api.APIError: + # hack for older sites that don't support 1.13 properties + # probably should delete if we're not going to support pre-1.13 + self._magicwords = {} + + if word in self._magicwords: + return self._magicwords[word] + else: + return [word] + + def redirect(self, default=True): + """Return the preferred localized #REDIRECT keyword. + + Argument is ignored (but maintained for backwards-compatibility. + + """ + # return the magic word without the preceding '#' character + return self.getmagicwords("redirect")[0].lstrip("#") + + def redirectRegex(self): + """Return a compiled regular expression matching on redirect pages. + + Group 1 in the regex match object will be the target title. + + """ + #TODO: is this needed, since the API identifies redirects? + # (maybe, the API can give false positives) + try: + keywords = set(s.lstrip("#") + for s in self.getmagicwords("redirect")) + keywords.add("REDIRECT") # just in case + pattern = "(?:" + "|".join(keywords) + ")" + except KeyError: + # no localized keyword for redirects + pattern = None + return BaseSite.redirectRegex(self, pattern) + + def pagenamecodes(self, default=True): + """Return list of localized PAGENAME tags for the site.""" + return self.getmagicwords("pagename") + + def pagename2codes(self, default=True): + """Return list of localized PAGENAMEE tags for the site.""" + return self.getmagicwords("pagenamee") + def _getsiteinfo(self): """Retrieve siteinfo and namespaces from site.""" sirequest = api.Request(