http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9530
Revision: 9530 Author: valhallasw Date: 2011-09-18 16:32:57 +0000 (Sun, 18 Sep 2011) Log Message: ----------- Added support for older (tested with 1.11) MW wikis.
Follow-up for r9528
Further fix for https://sourceforge.net/tracker/?func=detail&atid=603138&aid=3400045...
Modified Paths: -------------- trunk/pywikipedia/generate_family_file.py
Modified: trunk/pywikipedia/generate_family_file.py =================================================================== --- trunk/pywikipedia/generate_family_file.py 2011-09-18 15:26:53 UTC (rev 9529) +++ trunk/pywikipedia/generate_family_file.py 2011-09-18 16:32:57 UTC (rev 9530) @@ -176,12 +176,26 @@ self.f = family.Family()
def addfromwiki(self, w): - data = json.load(urlopen(w.api + "?action=query&format=json&meta=siteinfo&siprop=namespaces|namespacealiases"))['query'] - for ns in data['namespaces'].itervalues(): - self.add(ns['id'], w.lang, ns['*']) - for ns in data['namespacealiases']: - self.add(ns['id'], w.lang, ns['*']) + jdata = json.load(urlopen(w.api + "?action=query&format=json&meta=siteinfo&siprop=namespaces|namespacealiases")) + if 'query' not in jdata: + jdata = json.load(urlopen(w.api + "?action=query&format=json&meta=siteinfo&siprop=namespaces")) + if 'query' not in jdata: + jdata = {'query': {}} # empty
+ data = jdata['query'] + + if 'namespaces' in data: + for ns in data['namespaces'].itervalues(): + self.add(ns['id'], w.lang, ns['*']) + else: + print "\n*** Notice: cannot retrieve namespaces for %s" % w.lang + + if 'namespacealiases' in data: + for ns in data['namespacealiases']: + self.add(ns['id'], w.lang, ns['*']) + else: + print "\n*** Notice: cannot retrieve namespace aliases for %s" % w.lang + def add(self, ns, lang, translation): """ Contains logic for determining whether to define a namespace or not """ ns = int(ns) @@ -248,11 +262,20 @@ self.version = self.REwgVersion.search(data).groups()[0] except AttributeError: self.version = None + self.server = self.REwgServer.search(data).groups()[0] self.scriptpath = self.REwgScriptPath.search(data).groups()[0] self.articlepath = self.REwgArticlePath.search(data).groups()[0] self.lang = self.REwgContentLanguage.search(data).groups()[0]
+ if self.version == None: + # try to get version using api + try: + d = json.load(urlopen(self.api + "?version&format=json")) + self.version = filter(lambda x: x.startswith("MediaWiki"), [l.strip() for l in d['error']['*'].split("\n")])[0].split()[1] + except Exception: + pass + def _parse_post_117(self, bs): apipath = bs.find("link", rel='EditURI')['href'].split("?")[0] info = json.load(urlopen(apipath + "?action=query&meta=siteinfo&format=json"))['query']['general']