http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11326
Revision: 11326
Author: xqt
Date: 2013-04-03 19:39:46 +0000 (Wed, 03 Apr 2013)
Log Message:
-----------
Merge templatesWithParams() with textlib.extract_templates_and_params();
Keep templatesWithParams() kompatible with previous releases depending on asDict parameter
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/textlib.py
Modified: trunk/pywikipedia/pywikibot/textlib.py
===================================================================
--- trunk/pywikipedia/pywikibot/textlib.py 2013-04-03 18:34:33 UTC (rev 11325)
+++ trunk/pywikipedia/pywikibot/textlib.py 2013-04-03 19:39:46 UTC (rev 11326)
@@ -828,17 +828,24 @@
# Functions dealing with templates
#----------------------------------
-def extract_templates_and_params(text):
- """Return list of template calls found in text.
+def extract_templates_and_params(text, asDict=False):
+ """Return a list of templates found in text.
Return value is a list of tuples. There is one tuple for each use of a
- template in the page, with the template title as the first entry and a
- dict of parameters as the second entry. Parameters are indexed by
- strings; as in MediaWiki, an unnamed parameter is given a parameter name
- with an integer value corresponding to its position among the unnnamed
- parameters, and if this results multiple parameters with the same name
- only the last value provided will be returned.
+ template in the page, with the template title as the first entry and
+ either a list of parameters or a dict of parameters as the second entry
+ which depends on asDict method parameter.
+ If asDict is True the parameters is a dict, and they are indexed by strings;
+ as in MediaWiki, an unnamed parameter is given a parameter name with an
+ integer value corresponding to its position among the unnamed parameters,
+ and if this results multiple parameters with the same name, only the last
+ value provided will be returned.
+ @param text: The wikitext from which templates are extracted
+ @type text: unicode or string
+ @param asDict: If True, return parameters as list, else as dict
+ @type asDict: bool
+
"""
# remove commented-out stuff etc.
thistxt = removeDisabledParts(text)
@@ -907,6 +914,35 @@
# Doesn't detect templates whose name changes,
# or templates whose name contains math tags
continue
+
+ # {{#if: }}
+ if name.startswith('#'):
+ continue
+
+## TODO: merged from wikipedia.py - implement the following
+## if self.site().isInterwikiLink(name):
+## continue
+## # {{DEFAULTSORT:...}}
+## defaultKeys = self.site().versionnumber() > 13 and \
+## self.site().getmagicwords('defaultsort')
+## # It seems some wikis does not have this magic key
+## if defaultKeys:
+## found = False
+## for key in defaultKeys:
+## if name.startswith(key):
+## found = True
+## break
+## if found: continue
+##
+## try:
+## name = Page(self.site(), name).title()
+## except InvalidTitle:
+## if name:
+## output(
+## u"Page %s contains invalid template name {{%s}}."
+## % (self.title(), name.strip()))
+## continue
+
# Parameters
paramString = m.group('params')
params = {}
@@ -925,7 +961,7 @@
markedParams = paramString.split('|')
# Replace markers
for param in markedParams:
- if "=" in param:
+ if asDict and "=" in param:
param_name, param_val = param.split("=", 1)
else:
param_name = unicode(numbered_param)
@@ -946,7 +982,10 @@
params[param_name.strip()] = param_val.strip()
# Add it to the result
- result.append((name, params))
+ if asDict:
+ result.append((name, params))
+ else:
+ result.append((name, params.values()))
return result
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11323
Revision: 11323
Author: xqt
Date: 2013-04-03 16:18:30 +0000 (Wed, 03 Apr 2013)
Log Message:
-----------
Fix bug #3609816 by setting parameters for wbeditentity to non-plural form,
patch submitted by legoktm
Modified Paths:
--------------
branches/rewrite/pywikibot/page.py
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2013-04-01 16:43:54 UTC (rev 11322)
+++ branches/rewrite/pywikibot/page.py 2013-04-03 16:18:30 UTC (rev 11323)
@@ -2199,26 +2199,36 @@
self.repo = self.site.data_repository()
self._isredir = False # Wikibase pages cannot be a redirect
- def __defined_by(self):
+ def __defined_by(self, singular=False):
"""
- returns the parameters needed by the API
- to identify an item.
- Once an item's "p/q##" is looked up, that
- will be used for all future requests.
+ returns the parameters needed by the API to identify an item.
+ Once an item's "p/q##" is looked up, that will be used for all future
+ requests.
+ @param singular: Whether the parameter names should use the singular
+ form
+ @type singular: bool
"""
params = {}
+ if singular:
+ id = 'id'
+ site = 'site'
+ title = 'title'
+ else:
+ id = 'ids'
+ site = 'sites'
+ title = 'titles'
#id overrides all
if hasattr(self, 'id'):
- params['ids'] = self.id
+ params[id] = self.id
return params
#the rest only applies to ItemPages, but is still needed here.
if isinstance(self.site, pywikibot.site.DataSite):
- params['ids'] = self.title(withNamespace=False)
+ params[id] = self.title(withNamespace=False)
elif isinstance(self.site, pywikibot.site.BaseSite):
- params['sites'] = self.site.dbName()
- params['titles'] = self.title()
+ params[site] = self.site.dbName()
+ params[title] = self.title()
else:
raise pywikibot.exceptions.BadTitle
return params
@@ -2326,7 +2336,8 @@
baserevid = self.lastrevid
else:
baserevid = None
- updates = self.repo.editEntity(self.__defined_by(), data, baserevid=baserevid, **kwargs)
+ updates = self.repo.editEntity(self.__defined_by(singular=True), data,
+ baserevid=baserevid, **kwargs)
self.lastrevid = updates['entity']['lastrevid']
def editLabels(self, labels, **kwargs):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11322
Revision: 11322
Author: xqt
Date: 2013-04-01 16:43:54 +0000 (Mon, 01 Apr 2013)
Log Message:
-----------
use findmarker() for extract_templates_and_params(), update from trunk r11321
Modified Paths:
--------------
branches/rewrite/pywikibot/textlib.py
Modified: branches/rewrite/pywikibot/textlib.py
===================================================================
--- branches/rewrite/pywikibot/textlib.py 2013-04-01 16:42:32 UTC (rev 11321)
+++ branches/rewrite/pywikibot/textlib.py 2013-04-01 16:43:54 UTC (rev 11322)
@@ -108,7 +108,7 @@
dontTouchRegexes = []
for exc in exceptions:
- if isinstance(exc, str) or isinstance(exc, unicode):
+ if isinstance(exc, basestring):
# assume it's a reference to the exceptionRegexes dictionary
# defined above.
if exc in exceptionRegexes:
@@ -274,13 +274,13 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(text, '@@', '@')
+ marker = findmarker(text)
text = text[:index] + marker + text[index:]
text = removeDisabledParts(text, tags)
return (marker not in text)
-def findmarker(text, startwith=u'@', append=None):
+def findmarker(text, startwith=u'@@', append=None):
# find a string which is not part of text
if not append:
append = u'@'
@@ -446,7 +446,7 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(oldtext, u'@@')
+ marker = findmarker(oldtext)
if site is None:
site = pywikibot.getSite()
separator = site.family.interwiki_text_separator
@@ -708,7 +708,7 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(oldtext, u'@@')
+ marker = findmarker(oldtext)
if site is None:
site = pywikibot.getSite()
if site.sitename() == 'wikipedia:de' and "{{Personendaten" in oldtext:
@@ -846,24 +846,16 @@
thistxt = removeDisabledParts(text)
# marker for inside templates or parameters
- marker = u'@@'
- while marker in thistxt:
- marker += u'@'
+ marker = findmarker(thistxt)
# marker for links
- marker2 = u'##'
- while marker2 in thistxt:
- marker2 += u'#'
+ marker2 = findmarker(thistxt, u'##', u'#')
# marker for math
- marker3 = u'%%'
- while marker3 in thistxt:
- marker3 += u'%'
+ marker3 = findmarker(thistxt, u'%%', u'%')
# marker for value parameter
- marker4 = u'§§'
- while marker4 in thistxt:
- marker4 += u'§'
+ marker4 = findmarker(thistxt, u'§§', u'§')
result = []
Rtemplate = re.compile(
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11321
Revision: 11321
Author: xqt
Date: 2013-04-01 16:42:32 +0000 (Mon, 01 Apr 2013)
Log Message:
-----------
use findmarker() for extract_templates_and_params()
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/textlib.py
Modified: trunk/pywikipedia/pywikibot/textlib.py
===================================================================
--- trunk/pywikipedia/pywikibot/textlib.py 2013-04-01 15:05:38 UTC (rev 11320)
+++ trunk/pywikipedia/pywikibot/textlib.py 2013-04-01 16:42:32 UTC (rev 11321)
@@ -108,7 +108,7 @@
dontTouchRegexes = []
for exc in exceptions:
- if isinstance(exc, str) or isinstance(exc, unicode):
+ if isinstance(exc, basestring):
# assume it's a reference to the exceptionRegexes dictionary
# defined above.
if exc in exceptionRegexes:
@@ -274,13 +274,13 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(text, '@@', '@')
+ marker = findmarker(text)
text = text[:index] + marker + text[index:]
text = removeDisabledParts(text, tags)
return (marker not in text)
-def findmarker(text, startwith=u'@', append=None):
+def findmarker(text, startwith=u'@@', append=None):
# find a string which is not part of text
if not append:
append = u'@'
@@ -446,7 +446,7 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(oldtext, u'@@')
+ marker = findmarker(oldtext)
if site is None:
site = pywikibot.getSite()
separator = site.family.interwiki_text_separator
@@ -706,7 +706,7 @@
"""
# Find a marker that is not already in the text.
- marker = findmarker(oldtext, u'@@')
+ marker = findmarker(oldtext)
if site is None:
site = pywikibot.getSite()
if site.sitename() == 'wikipedia:de' and "{{Personendaten" in oldtext:
@@ -844,24 +844,16 @@
thistxt = removeDisabledParts(text)
# marker for inside templates or parameters
- marker = u'@@'
- while marker in thistxt:
- marker += u'@'
+ marker = findmarker(thistxt)
# marker for links
- marker2 = u'##'
- while marker2 in thistxt:
- marker2 += u'#'
+ marker2 = findmarker(thistxt, u'##', u'#')
# marker for math
- marker3 = u'%%'
- while marker3 in thistxt:
- marker3 += u'%'
+ marker3 = findmarker(thistxt, u'%%', u'%')
# marker for value parameter
- marker4 = u'§§'
- while marker4 in thistxt:
- marker4 += u'§'
+ marker4 = findmarker(thistxt, u'§§', u'§')
result = []
Rtemplate = re.compile(
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11318
Revision: 11318
Author: xqt
Date: 2013-04-01 13:58:49 +0000 (Mon, 01 Apr 2013)
Log Message:
-----------
use the right code for plural_rules dict, update from trunk r11317
Modified Paths:
--------------
branches/rewrite/pywikibot/i18n.py
Modified: branches/rewrite/pywikibot/i18n.py
===================================================================
--- branches/rewrite/pywikibot/i18n.py 2013-04-01 13:56:43 UTC (rev 11317)
+++ branches/rewrite/pywikibot/i18n.py 2013-04-01 13:58:49 UTC (rev 11318)
@@ -277,9 +277,11 @@
for alt in _altlang(code) + ['_default', 'en']:
if alt in xdict:
trans = xdict[alt]
+ code = alt
break
else:
trans = xdict.values()[0]
+ code = xdict.keys()[0]
if not trans:
return # return None if we have no translation found
if parameters is None:
@@ -297,7 +299,6 @@
num = int(parameters)
else:
num = parameters
- # we only need the lang or _default, not a _altlang code
# TODO: check against plural_rules[lang]['nplurals']
try:
index = plural_rules[code]['plural'](num)
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11317
Revision: 11317
Author: xqt
Date: 2013-04-01 13:56:43 +0000 (Mon, 01 Apr 2013)
Log Message:
-----------
use the right code for plural dict
Modified Paths:
--------------
trunk/pywikipedia/pywikibot/i18n.py
Modified: trunk/pywikipedia/pywikibot/i18n.py
===================================================================
--- trunk/pywikipedia/pywikibot/i18n.py 2013-04-01 13:09:37 UTC (rev 11316)
+++ trunk/pywikipedia/pywikibot/i18n.py 2013-04-01 13:56:43 UTC (rev 11317)
@@ -277,9 +277,11 @@
for alt in _altlang(code) + ['_default', 'en']:
if alt in xdict:
trans = xdict[alt]
+ code = alt
break
else:
trans = xdict.values()[0]
+ code = xdict.keys()[0]
if not trans:
return # return None if we have no translation found
if parameters is None:
@@ -300,7 +302,6 @@
num = int(parameters)
else:
num = parameters
- # we only need the lang or _default, not a _altlang code
# TODO: check against plural_rules[lang]['nplurals']
try:
index = plural_rules[code]['plural'](num)