Xqt has uploaded a new change for review.
https://gerrit.wikimedia.org/r/213193
Change subject: [FEAT] enable -always option for fixing_redirects.py
......................................................................
[FEAT] enable -always option for fixing_redirects.py
- use NoRedirectPageBot class
- global methods moved into class
- remove obsolete choice and replaceit variables because their content is
static. Remove obsolete code parts which depends on them.
bug: T100122
Change-Id: I9c52afe218e20f6ab2ecf41fdaf7934341cd2093
---
M scripts/fixing_redirects.py
1 file changed, 108 insertions(+), 120 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/93/213193/1
diff --git a/scripts/fixing_redirects.py b/scripts/fixing_redirects.py
index 9e13e28..4c9264f 100755
--- a/scripts/fixing_redirects.py
+++ b/scripts/fixing_redirects.py
@@ -15,7 +15,7 @@
#
# This script based on disambredir.py and solve_disambiguation.py
#
-# (C) Pywikibot team, 2004-2014
+# (C) Pywikibot team, 2004-2015
#
# Distributed under the terms of the MIT license.
#
@@ -29,6 +29,7 @@
from pywikibot import pagegenerators
from pywikibot import i18n
from pywikibot.tools import first_lower, first_upper as firstcap
+from pywikibot.bot import NoRedirectPageBot
# This is required for the text that is shown when you run this script
# with the parameter -help.
@@ -59,131 +60,113 @@
}
-def treat(text, linkedPage, targetPage):
- """Based on the method of the same name in solve_disambiguation.py."""
- mysite = pywikibot.Site()
- linktrail = mysite.linktrail()
+class FixingRedirectBot(NoRedirectPageBot):
- # make a backup of the original text so we can show the changes later
- linkR = re.compile(r'\[\[(?P<title>[^\]\|#]*)(?P<section>#[^\]\|]*)?'
- r'(\|(?P<label>[^\]]*))?\]\](?P<linktrail>' + linktrail
+ ')')
- curpos = 0
- # This loop will run until we have finished the current page
- while True:
- m = linkR.search(text, pos=curpos)
- if not m:
- break
- # Make sure that next time around we will not find this same hit.
- curpos = m.start() + 1
- # ignore interwiki links and links to sections of the same page
- if m.group('title').strip() == '' or \
- mysite.isInterwikiLink(m.group('title')):
- continue
- else:
- actualLinkPage = pywikibot.Page(targetPage.site, m.group('title'))
- # Check whether the link found is to page.
- if actualLinkPage != linkedPage:
+ """Run over pages and resolve redirect links."""
+
+ def __init__(self, **kwargs):
+ """Only accept options defined in availableOptions."""
+ self.availableOptions.update({
+ 'featured': False,
+ })
+ super(FixingRedirectBot, self).__init__(**kwargs)
+ self.summary = i18n.twtranslate(self.site, 'fixing_redirects-fixing')
+
+ def treat_link(self, text, linkedPage, targetPage):
+ """Based on the treat method in solve_disambiguation.py."""
+ linktrail = self.site.linktrail()
+
+ # make a backup of the original text so we can show the changes later
+ linkR = re.compile(r'\[\[(?P<title>[^\]\|#]*)(?P<section>#[^\]\|]*)?'
+ r'(\|(?P<label>[^\]]*))?\]\](?P<linktrail>%s)'
+ % linktrail)
+ curpos = 0
+ # This loop will run until we have finished the current page
+ while True:
+ m = linkR.search(text, pos=curpos)
+ if not m:
+ break
+ # Make sure that next time around we will not find this same hit.
+ curpos = m.start() + 1
+ # ignore interwiki links and links to sections of the same page
+ if m.group('title').strip() == '' or \
+ self.site.isInterwikiLink(m.group('title')):
continue
+ else:
+ actualLinkPage = pywikibot.Page(targetPage.site,
m.group('title'))
+ # Check whether the link found is to page.
+ if actualLinkPage != linkedPage:
+ continue
- choice = 'y'
+ # The link looks like this:
+ # [[page_title|link_text]]trailing_chars
+ page_title = m.group('title')
+ link_text = m.group('label')
- # The link looks like this:
- # [[page_title|link_text]]trailing_chars
- page_title = m.group('title')
- link_text = m.group('label')
+ if not link_text:
+ # or like this: [[page_title]]trailing_chars
+ link_text = page_title
+ if m.group('section') is None:
+ section = ''
+ else:
+ section = m.group('section')
+ trailing_chars = m.group('linktrail')
+ if trailing_chars:
+ link_text += trailing_chars
- if not link_text:
- # or like this: [[page_title]]trailing_chars
- link_text = page_title
- if m.group('section') is None:
- section = ''
- else:
- section = m.group('section')
- trailing_chars = m.group('linktrail')
- if trailing_chars:
- link_text += trailing_chars
+ # remove preleading ":"
+ if link_text[0] == ':':
+ link_text = link_text[1:]
+ if link_text[0].isupper():
+ new_page_title = targetPage.title()
+ else:
+ new_page_title = first_lower(targetPage.title())
- if choice in "uU":
- # unlink - we remove the section if there's any
- text = text[:m.start()] + link_text + text[m.end():]
+ # remove preleading ":"
+ if new_page_title[0] == ':':
+ new_page_title = new_page_title[1:]
+
+ elif new_page_title == link_text and not section:
+ newlink = "[[%s]]" % new_page_title
+ # check if we can create a link with trailing characters instead
of a
+ # pipelink
+ elif len(new_page_title) <= len(link_text) and \
+ firstcap(link_text[:len(new_page_title)]) == \
+ firstcap(new_page_title) and \
+ re.sub(re.compile(linktrail), '',
link_text[len(new_page_title):]) == '' and not section:
+ newlink = "[[%s]]%s" % (link_text[:len(new_page_title)],
+ link_text[len(new_page_title):])
+ else:
+ newlink = "[[%s%s|%s]]" % (new_page_title, section, link_text)
+ text = text[:m.start()] + newlink + text[m.end():]
continue
- replaceit = choice in "rR"
+ return text
- # remove preleading ":"
- if link_text[0] == ':':
- link_text = link_text[1:]
- if link_text[0].isupper():
- new_page_title = targetPage.title()
+ def treat_page(self):
+ """Change all redirects from the current page to actual links."""
+ links = self.current_page.linkedPages()
+ if links is not None:
+ links = pagegenerators.PreloadingGenerator(links)
else:
- new_page_title = first_lower(targetPage.title())
-
- # remove preleading ":"
- if new_page_title[0] == ':':
- new_page_title = new_page_title[1:]
-
- if replaceit and trailing_chars:
- newlink = "[[%s%s]]%s" % (new_page_title, section, trailing_chars)
- elif replaceit or (new_page_title == link_text and not section):
- newlink = "[[%s]]" % new_page_title
- # check if we can create a link with trailing characters instead of a
- # pipelink
- elif len(new_page_title) <= len(link_text) and \
- firstcap(link_text[:len(new_page_title)]) == \
- firstcap(new_page_title) and \
- re.sub(re.compile(linktrail), '',
link_text[len(new_page_title):]) == '' and not section:
- newlink = "[[%s]]%s" % (link_text[:len(new_page_title)],
- link_text[len(new_page_title):])
- else:
- newlink = "[[%s%s|%s]]" % (new_page_title, section, link_text)
- text = text[:m.start()] + newlink + text[m.end():]
- continue
- return text
-
-pageCache = []
-
-
-def workon(page):
- """Change all redirects from the given page to actual links."""
- mysite = pywikibot.Site()
- try:
- text = page.get()
- except pywikibot.IsRedirectPage:
- pywikibot.output(u'%s is a redirect page. Skipping' % page)
- return
- except pywikibot.NoPage:
- pywikibot.output(u'%s does not exist. Skipping' % page)
- return
- pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % page.title())
- links = page.linkedPages()
- if links is not None:
- links = pagegenerators.PreloadingGenerator(links)
-# pywikibot.getall(mysite, links)
- else:
- pywikibot.output('Nothing left to do.')
- return
-
- for page2 in links:
- try:
- target = page2.getRedirectTarget()
- except pywikibot.NoPage:
+ pywikibot.output('Nothing left to do.')
+ return
+ newtext = self.current_page.text
+ for page in links:
try:
- target = page2.getMovedTarget()
- except (pywikibot.NoPage, pywikibot.BadTitle):
+ target = page.getRedirectTarget()
+ except pywikibot.NoPage:
+ try:
+ target = page.getMovedTarget()
+ except (pywikibot.NoPage, pywikibot.BadTitle):
+ continue
+ except (pywikibot.Error, pywikibot.SectionError):
continue
- except (pywikibot.Error, pywikibot.SectionError):
- continue
- # no fix to user namespaces
- if target.namespace() in [0, 1] and not page2.namespace() in [0, 1]:
- continue
- text = treat(text, page2, target)
- if text != page.get():
- comment = i18n.twtranslate(mysite, 'fixing_redirects-fixing')
- pywikibot.showDiff(page.get(), text)
- try:
- page.put(text, comment)
- except (pywikibot.Error):
- pywikibot.error('unable to put %s' % page)
+ # no fix to user namespaces
+ if target.namespace() in [0, 1] and not page.namespace() in [0, 1]:
+ continue
+ newtext = self.treat_link(newtext, page, target)
+ self.userPut(self.current_page, self.current_page.text, newtext,
+ summary=self.summary)
def main(*args):
@@ -196,6 +179,7 @@
@type args: list of unicode
"""
featured = False
+ options = {}
gen = None
# Process global args and prepare generator args parser
@@ -205,14 +189,17 @@
for arg in local_args:
if arg == '-featured':
featured = True
+ elif genFactory.handleArg(arg):
+ pass
else:
- genFactory.handleArg(arg)
+ options[arg[1:]] = True
mysite = pywikibot.Site()
if mysite.sitename() == 'wikipedia:nl':
pywikibot.output(
- u'\03{lightred}There is consensus on the Dutch Wikipedia that bots
should not be used to fix redirects.\03{default}')
- sys.exit()
+ u'\03{lightred}There is consensus on the Dutch Wikipedia that bots
'
+ u'should not be used to fix redirects.\03{default}')
+ return
if featured:
featuredList = i18n.translate(mysite, featured_articles)
@@ -221,8 +208,9 @@
if not gen:
gen = genFactory.getCombinedGenerator()
if gen:
- for page in pagegenerators.PreloadingGenerator(gen):
- workon(page)
+ gen = pagegenerators.PreloadingGenerator(gen)
+ bot = FixingRedirectBot(generator=gen, **options)
+ bot.run()
else:
pywikibot.showHelp('fixing_redirects')
--
To view, visit https://gerrit.wikimedia.org/r/213193
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9c52afe218e20f6ab2ecf41fdaf7934341cd2093
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits