http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9902
Revision: 9902
Author: drtrigon
Date: 2012-02-17 11:34:20 +0000 (Fri, 17 Feb 2012)
Log Message:
-----------
Adding capabilities of DrTrigonBot 'wikipedia' script; 'getParsedString'
and 'getExpandedString'
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2012-02-16 22:44:36 UTC (rev 9901)
+++ trunk/pywikipedia/wikipedia.py 2012-02-17 11:34:20 UTC (rev 9902)
@@ -4807,8 +4807,11 @@
versionnumber: Return int identifying the MediaWiki version.
live_version: Return version number read from Special:Version.
checkCharset(charset): Warn if charset doesn't match family file.
- server_time : returns server time (currently userclock depending)
+ server_time: returns server time (currently userclock depending)
+ getParsedString: Parses the string with API and returns html content.
+ getExpandedString: Expands the string with API and returns wiki content.
+
linktrail: Return regex for trailing chars displayed as part of a link.
disambcategory: Category in which disambiguation pages are listed.
@@ -7581,6 +7584,67 @@
files.append(image)
return files
+ def getParsedString(self, string, keeptags = [u'*']):
+ """Parses the string with API and returns html content.
+
+ @param string: String that should be parsed.
+ @type string: string
+ @param keeptags: Defines which tags (wiki, HTML) should NOT be removed.
+ @type keeptags: list
+
+ Returns the string given, parsed through the wiki parser.
+ """
+
+ if not self.has_api():
+ raise Exception('parse: no API: not implemented')
+
+ # call the wiki to get info
+ params = {
+ u'action' : u'parse',
+ u'text' : string,
+ }
+
+ pywikibot.get_throttle()
+ pywikibot.output(u"Parsing string through the wiki parser via API.")
+
+ result = query.GetData(params, self)
+ r = result[u'parse'][u'text'][u'*']
+
+ # disable/remove comments
+ r = pywikibot.removeDisabledParts(r, tags = ['comments']).strip()
+
+ # disable/remove ALL tags
+ if not (keeptags == [u'*']):
+ r = removeHTMLParts(r, keeptags = keeptags).strip()
+
+ return r
+
+ def getExpandedString(self, string):
+ """Expands the string with API and returns wiki content.
+
+ @param string: String that should be expanded.
+ @type string: string
+
+ Returns the string given, expanded through the wiki parser.
+ """
+
+ if not self.has_api():
+ raise Exception('expandtemplates: no API: not implemented')
+
+ # call the wiki to get info
+ params = {
+ u'action' : u'expandtemplates',
+ u'text' : string,
+ }
+
+ pywikibot.get_throttle()
+ pywikibot.output(u"Expanding string through the wiki parser via API.")
+
+ result = query.GetData(params, self)
+ r = result[u'expandtemplates'][u'*']
+
+ return r
+
# Caches to provide faster access
_sites = {}
_namespaceCache = {}