[Pywikipedia-l] SVN: [5398] trunk/pywikipedia/wikipedia.py
nicdumz at svn.wikimedia.org
nicdumz at svn.wikimedia.org
Sun May 18 14:51:40 UTC 2008
Revision: 5398
Author: nicdumz
Date: 2008-05-18 14:51:39 +0000 (Sun, 18 May 2008)
Log Message:
-----------
Fixing #1966402 "wrong mediawiki_message when not using the default site lang" :
Not fetching anymore every single message through Mediawiki:message_name, fetching instead *once* Special:Allmessages. It is slower indeed, but I think that this is the only way to get the properly localized messages.
Any comments are welcome...
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2008-05-18 12:12:24 UTC (rev 5397)
+++ trunk/pywikipedia/wikipedia.py 2008-05-18 14:51:39 UTC (rev 5398)
@@ -146,6 +146,7 @@
except ValueError:
WIDEBUILD = False
+
# Local exceptions
class Error(Exception):
@@ -4420,54 +4421,43 @@
def mediawiki_message(self, key):
"""Return the MediaWiki message text for key "key" """
- global mwpage, tree
- if key.lower() not in self._mediawiki_messages.keys() \
- and not hasattr(self, "_phploaded"):
- get_throttle()
- mwpage = self.getUrl("%s?title=%s:%s&action=edit"
- % (self.path(), urllib.quote(
- self.namespace(8).replace(' ', '_').encode(
- self.encoding())),
- key))
- tree = BeautifulSoup(mwpage,
- convertEntities=BeautifulSoup.HTML_ENTITIES,
- parseOnlyThese=SoupStrainer("textarea"))
- if tree.textarea is not None and tree.textarea.string is not None:
- value = tree.textarea.string.strip()
- else:
- value = None
- if value:
- self._mediawiki_messages[key.lower()] = value
- else:
- self._mediawiki_messages[key.lower()] = None
- # Fallback in case MediaWiki: page method doesn't work
- if verbose:
- output(
- u"Retrieving mediawiki messages from Special:Allmessages")
- retry_idle_time = 1
- while True:
- get_throttle()
- phppage = self.getUrl(self.get_address("Special:Allmessages")
- + "&ot=php")
- Rphpvals = re.compile(r"(?ms)'([^']*)' => '(.*?[^\\])',")
- count = 0
- for (phpkey, phpval) in Rphpvals.findall(phppage):
- count += 1
- self._mediawiki_messages[str(phpkey).lower()] = phpval
- if count == 0:
- # No messages could be added.
- # We assume that the server is down.
- # Wait some time, then try again.
- output('WARNING: No messages found in Special:Allmessages. Maybe the server is down. Retrying in %i minutes...' % retry_idle_time)
- time.sleep(retry_idle_time * 60)
- # Next time wait longer, but not longer than half an hour
- retry_idle_time *= 2
- if retry_idle_time > 30:
- retry_idle_time = 30
- continue
- break
- self._phploaded = True
+ try:
+ from xml.etree.cElementTree import XML
+ except ImportError:
+ from cElementTree import XML
+ # Allmessages is retrieved once for all in a session
+ if not self._mediawiki_messages:
+ if verbose:
+ output(
+ u"Retrieving mediawiki messages from Special:Allmessages")
+ retry_idle_time = 1
+ while True:
+ get_throttle()
+ xml = self.getUrl(self.get_address("Special:Allmessages")
+ + "&ot=xml")
+ decode = xml.encode(self.encoding())
+ tree = XML(decode)
+ # xml structure is :
+ # <messages lang="fr">
+ # <message name="about">À propos</message>
+ # ...
+ # </messages>
+ for msg in tree.getiterator('message'):
+ self._mediawiki_messages[msg.get('name').lower()] = msg.text
+ if not self._mediawiki_messages:
+ # No messages could be added.
+ # We assume that the server is down.
+ # Wait some time, then try again.
+ output('WARNING: No messages found in Special:Allmessages. Maybe the server is down. Retrying in %i minutes...' % retry_idle_time)
+ time.sleep(retry_idle_time * 60)
+ # Next time wait longer, but not longer than half an hour
+ retry_idle_time *= 2
+ if retry_idle_time > 30:
+ retry_idle_time = 30
+ continue
+ break
+
key = key.lower()
if self._mediawiki_messages[key] is None:
raise KeyError("MediaWiki key '%s' does not exist on %s"
More information about the Pywikipedia-l
mailing list