Revision: 7834
Author: xqt
Date: 2009-12-28 10:32:45 +0000 (Mon, 28 Dec 2009)
Log Message:
-----------
Fix for #2920675
Modified Paths:
--------------
trunk/pywikipedia/cosmetic_changes.py
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/cosmetic_changes.py
===================================================================
--- trunk/pywikipedia/cosmetic_changes.py 2009-12-28 09:24:21 UTC (rev 7833)
+++ trunk/pywikipedia/cosmetic_changes.py 2009-12-28 10:32:45 UTC (rev 7834)
@@ -186,14 +186,43 @@
nn_iw_msg = u'<!--interwiki (no, sv, da first; then other languages alphabetically by name)-->'
+# This is from interwiki.py;
+# move it to family file and implement global instances
+moved_links = {
+ 'ca' : (u'ús de la plantilla', u'/ús'),
+ 'cs' : (u'dokumentace', u'/doc'),
+ 'de' : (u'dokumentation', u'/Meta'),
+ 'en' : ([u'documentation',
+ u'template documentation',
+ u'template doc',
+ u'doc',
+ u'documentation, template'], u'/doc'),
+ 'es' : ([u'documentación', u'documentación de plantilla'], u'/doc'),
+ 'fr' : (u'/documentation', u'/Documentation'),
+ 'hu' : (u'sablondokumentáció', u'/doc'),
+ 'id' : (u'template doc', u'/doc'),
+ 'ja' : (u'documentation', u'/doc'),
+ 'ka' : (u'თარგის ინფო', u'/ინფო'),
+ 'ko' : (u'documentation', u'/설명문서'),
+ 'ms' : (u'documentation', u'/doc'),
+ 'pl' : (u'dokumentacja', u'/opis'),
+ 'pt' : ([u'documentação', u'/doc'], u'/doc'),
+ 'ro' : (u'documentaţie', u'/doc'),
+ 'ru' : (u'doc', u'/doc'),
+ 'sv' : (u'dokumentation', u'/dok'),
+ 'vi' : (u'documentation', u'/doc'),
+ 'zh' : ([u'documentation', u'doc'], u'/doc'),
+}
+
class CosmeticChangesToolkit:
- def __init__(self, site, debug=False, redirect=False, namespace=None):
+ def __init__(self, site, debug=False, redirect=False, namespace=None, pageTitle=None):
self.site = site
self.debug = debug
self.redirect = redirect
self.namespace = namespace
self.template = (self.namespace == 10)
self.talkpage = self.namespace >= 0 and self.namespace % 2 == 1
+ self.title = pageTitle
def change(self, text):
"""
@@ -260,7 +289,17 @@
categories = pywikibot.getCategoryLinks(text, site = self.site)
if not self.talkpage:# and pywikibot.calledModuleName() <> 'interwiki':
- interwikiLinks = pywikibot.getLanguageLinks(text, insite = self.site)
+ subpage = False
+ if self.template:
+ loc = None
+ try:
+ tmpl, loc = moved_links[self.site.lang]
+ del tmpl
+ except KeyError:
+ pass
+ if loc != None and loc in self.title:
+ subpage = True
+ interwikiLinks = pywikibot.getLanguageLinks(text, insite=self.site, template_subpage=subpage)
# Removing the interwiki
text = pywikibot.removeLanguageLinks(text, site = self.site)
@@ -301,7 +340,7 @@
pywikibot.output(u'%s' %element.strip())
# Adding the interwiki
if interwikiLinks != None:
- text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site = self.site, template = self.template)
+ text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site = self.site, template = self.template, template_subpage = subpage)
return text
def translateAndCapitalizeNamespaces(self, text):
@@ -560,7 +599,7 @@
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
- ccToolkit = CosmeticChangesToolkit(page.site(), debug = True, namespace = page.namespace())
+ ccToolkit = CosmeticChangesToolkit(page.site(), debug = True, namespace = page.namespace(), pageTitle=page.title())
changedText = ccToolkit.change(page.get())
if changedText.strip() != page.get().strip():
if not self.acceptall:
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2009-12-28 09:24:21 UTC (rev 7833)
+++ trunk/pywikipedia/wikipedia.py 2009-12-28 10:32:45 UTC (rev 7834)
@@ -1628,7 +1628,7 @@
if verbose:
output(u'Cosmetic Changes for %s-%s enabled.' % (self.site().family.name, self.site().lang))
import cosmetic_changes
- ccToolkit = cosmetic_changes.CosmeticChangesToolkit(self.site(), redirect=self.isRedirectPage(), namespace = self.namespace())
+ ccToolkit = cosmetic_changes.CosmeticChangesToolkit(self.site(), redirect=self.isRedirectPage(), namespace = self.namespace(), pageTitle=self.title())
newtext = ccToolkit.change(newtext)
if comment and old.strip().replace('\r\n', '\n') != newtext.strip().replace('\r\n', '\n'):
comment += translate(self.site(), cosmetic_changes.msg_append)
@@ -4587,7 +4587,7 @@
# or change links to a different project, or any that are formatted
# as in-line interwiki links (e.g., "[[:es:Articulo]]". (CONFIRM)
-def getLanguageLinks(text, insite = None, pageLink = "[[]]"):
+def getLanguageLinks(text, insite = None, pageLink="[[]]", template_subpage=False):
"""
Return a dict of interlanguage links found in text.
@@ -4601,7 +4601,10 @@
result = {}
# Ignore interwiki links within nowiki tags, includeonly tags, pre tags,
# and HTML comments
- text = removeDisabledParts(text)
+ tags = ['comments', 'nowiki', 'pre', 'source']
+ if not template_subpage:
+ tags += ['includeonly']
+ text = removeDisabledParts(text, tags)
# This regular expression will find every link that is possibly an
# interwiki link.
@@ -4669,7 +4672,7 @@
else:
return removeLanguageLinks(text, site, marker)
-def replaceLanguageLinks(oldtext, new, site = None, addOnly = False, template = False):
+def replaceLanguageLinks(oldtext, new, site = None, addOnly = False, template = False, template_subpage = False):
"""Replace interlanguage links in the text with a new set of links.
'new' should be a dict with the Site objects as keys, and Page objects
@@ -4712,17 +4715,23 @@
s = separator + s + separator
newtext = s2[:firstafter].replace(marker,'') + s + s2[firstafter:]
else:
- if template:
+ if template or template_subpage:
+ if template_subpage:
+ includeOn = '<includeonly>'
+ includeOff = '</includeonly>'
+ else:
+ includeOn = '<noinclude>'
+ includeOff = '</noinclude>'
# Do we have a noinclude at the end of the template?
- parts = s2.split('</noinclude>')
+ parts = s2.split(includeOff)
lastpart = parts[-1]
if re.match('\s*%s' % marker, lastpart):
# Put the langlinks back into the noinclude's
- regexp = re.compile('</noinclude>\s*%s' % marker)
- newtext = regexp.sub(s + '</noinclude>', s2)
+ regexp = re.compile('%s\s*%s' % (includeOff, marker))
+ newtext = regexp.sub(s + includeOff, s2)
else:
# Put the langlinks at the end, inside noinclude's
- newtext = s2.replace(marker,'').strip() + separator + u'<noinclude>\n%s</noinclude>\n' % s
+ newtext = s2.replace(marker,'').strip() + separator + u'%s\n%s%s\n' % (includeOn, s, includeOff)
else:
newtext = s2.replace(marker,'').strip() + separator + s
else: