http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9678
Revision: 9678
Author: xqt
Date: 2011-10-28 16:44:01 +0000 (Fri, 28 Oct 2011)
Log Message:
-----------
fix identation to 4 spaces
Modified Paths:
--------------
trunk/pywikipedia/redirect.py
Modified: trunk/pywikipedia/redirect.py
===================================================================
--- trunk/pywikipedia/redirect.py 2011-10-28 04:45:50 UTC (rev 9677)
+++ trunk/pywikipedia/redirect.py 2011-10-28 16:44:01 UTC (rev 9678)
@@ -81,6 +81,7 @@
# Insert deletion template into page with a broken redirect
sd_template = 'redirect-broken-redirect-template'
+
class RedirectGenerator:
def __init__(self, xmlFilename=None, namespaces=[], offset=-1,
use_move_log=False, use_api=False, start=None, until=None,
@@ -456,6 +457,7 @@
break
offset_time = m.group(1)
+
class RedirectRobot:
def __init__(self, action, generator, always=False, number=None):
self.site = pywikibot.getSite()
@@ -542,151 +544,151 @@
break
def fix_1_double_redirect(self, redir_name):
- redir = pywikibot.Page(self.site, redir_name)
- # 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}
<<<"
- % redir.title())
- newRedir = redir
- redirList = [] # bookkeeping to detect loops
- while True:
- redirList.append(u'%s:%s' % (newRedir.site().lang,
- newRedir.sectionFreeTitle()))
- try:
- targetPage = newRedir.getRedirectTarget()
- except pywikibot.IsNotRedirectPage:
- if len(redirList) == 1:
- pywikibot.output(u'Skipping: Page %s is not a redirect.'
- % redir.title(asLink=True))
- break #do nothing
- elif len(redirList) == 2:
+ redir = pywikibot.Page(self.site, redir_name)
+ # 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}
<<<"
+ % redir.title())
+ newRedir = redir
+ redirList = [] # bookkeeping to detect loops
+ while True:
+ redirList.append(u'%s:%s' % (newRedir.site().lang,
+ newRedir.sectionFreeTitle()))
+ try:
+ targetPage = newRedir.getRedirectTarget()
+ except pywikibot.IsNotRedirectPage:
+ if len(redirList) == 1:
+ pywikibot.output(u'Skipping: Page %s is not a redirect.'
+ % redir.title(asLink=True))
+ break #do nothing
+ elif len(redirList) == 2:
+ pywikibot.output(
+ u'Skipping: Redirect target %s is not a redirect.'
+ % newRedir.title(asLink=True))
+ break # do nothing
+ else:
+ pass # target found
+ except pywikibot.SectionError:
+ pywikibot.output(
+ u'Warning: Redirect target section %s doesn\'t exist.'
+ % newRedir.title(asLink=True))
+ except pywikibot.BadTitle, e:
+ # str(e) is in the format 'BadTitle: [[Foo]]'
+ pywikibot.output(
+ u'Warning: Redirect target %s is not a valid page title.'
+ % str(e)[10:])
+ #sometimes this error occures. Invalid Title starting with a '#'
+ except pywikibot.InvalidTitle, err:
+ pywikibot.output(u'Warning: %s' % err)
+ break
+ except pywikibot.NoPage:
+ if len(redirList) == 1:
+ pywikibot.output(u'Skipping: Page %s does not exist.'
+ % redir.title(asLink=True))
+ break
+ else:
+ if self.always:
pywikibot.output(
- u'Skipping: Redirect target %s is not a redirect.'
+ u"Skipping: Redirect target %s doesn't exist."
% newRedir.title(asLink=True))
- break # do nothing
+ break # skip if automatic
else:
- pass # target found
- except pywikibot.SectionError:
- pywikibot.output(
- u'Warning: Redirect target section %s doesn\'t
exist.'
- % newRedir.title(asLink=True))
- except pywikibot.BadTitle, e:
- # str(e) is in the format 'BadTitle: [[Foo]]'
- pywikibot.output(
- u'Warning: Redirect target %s is not a valid page
title.'
- % str(e)[10:])
- #sometimes this error occures. Invalid Title starting with a '#'
- except pywikibot.InvalidTitle, err:
- pywikibot.output(u'Warning: %s' % err)
- break
- except pywikibot.NoPage:
- if len(redirList) == 1:
- pywikibot.output(u'Skipping: Page %s does not exist.'
- % redir.title(asLink=True))
+ pywikibot.output(
+ u"Warning: Redirect target %s doesn't exist."
+ % newRedir.title(asLink=True))
+ except pywikibot.ServerError:
+ pywikibot.output(u'Skipping: Server Error')
+ break
+ else:
+ pywikibot.output(
+ u' Links to: %s.'
+ % targetPage.title(asLink=True))
+ if targetPage.site().sitename() == 'wikipedia:en':
+ mw_msg = targetPage.site().mediawiki_message(
+ 'wikieditor-toolbar-tool-redirect-example')
+ if targetPage.title() == mw_msg:
+ pywikibot.output(
+ u"Skipping toolbar example: Redirect source is
potentially vandalized.")
break
- else:
- if self.always:
- pywikibot.output(
- u"Skipping: Redirect target %s doesn't
exist."
- % newRedir.title(asLink=True))
- break # skip if automatic
- else:
- pywikibot.output(
- u"Warning: Redirect target %s doesn't
exist."
- % newRedir.title(asLink=True))
- except pywikibot.ServerError:
- pywikibot.output(u'Skipping: Server Error')
- break
- else:
+ if targetPage.site() != self.site:
pywikibot.output(
- u' Links to: %s.'
+ u'Warning: redirect target (%s) is on a different site.'
% targetPage.title(asLink=True))
- if targetPage.site().sitename() == 'wikipedia:en':
- mw_msg = targetPage.site().mediawiki_message(
- 'wikieditor-toolbar-tool-redirect-example')
- if targetPage.title() == mw_msg:
- pywikibot.output(
- u"Skipping toolbar example: Redirect source is
potentially vandalized.")
- break
- if targetPage.site() != self.site:
+ if self.always:
+ break # skip if automatic
+ # watch out for redirect loops
+ if redirList.count(u'%s:%s'
+ % (targetPage.site().lang,
+ targetPage.sectionFreeTitle())
+ ) > 0:
+ pywikibot.output(
+ u'Warning: Redirect target %s forms a redirect loop.'
+ % targetPage.title(asLink=True))
+ break ### doesn't work. edits twice!
+## try:
+## content = targetPage.get(get_redirect=True)
+## except pywikibot.SectionError:
+## content = pywikibot.Page(
+## targetPage.site(),
+## targetPage.sectionFreeTitle()
+## ).get(get_redirect=True)
+## if targetPage.site().lang in sd_template and \
+## targetPage.site().lang in sd_tagging_sum:
+## pywikibot.output(u"Tagging redirect for deletion")
+## # Delete the two redirects
+## content = pywikibot.translate(
+## targetPage.site().lang,
+## sd_template)+"\n"+content
+## summ = pywikibot.translate(targetPage.site().lang,
+## reason_loop)
+## targetPage.put(content, summ)
+## redir.put(content, summ)
+## break # TODO Better implement loop redirect
+ else: # redirect target found
+ if targetPage.isStaticRedirect():
pywikibot.output(
- u'Warning: redirect target (%s) is on a different
site.'
- % targetPage.title(asLink=True))
- if self.always:
- break # skip if automatic
- # watch out for redirect loops
- if redirList.count(u'%s:%s'
- % (targetPage.site().lang,
- targetPage.sectionFreeTitle())
- ) > 0:
- pywikibot.output(
- u'Warning: Redirect target %s forms a redirect
loop.'
- % targetPage.title(asLink=True))
- break ### doesn't work. edits twice!
-## try:
-## content = targetPage.get(get_redirect=True)
-## except pywikibot.SectionError:
-## content = pywikibot.Page(
-## targetPage.site(),
-## targetPage.sectionFreeTitle()
-## ).get(get_redirect=True)
-## if targetPage.site().lang in sd_template and \
-## targetPage.site().lang in sd_tagging_sum:
-## pywikibot.output(u"Tagging redirect for
deletion")
-## # Delete the two redirects
-## content = pywikibot.translate(
-## targetPage.site().lang,
-## sd_template)+"\n"+content
-## summ = pywikibot.translate(targetPage.site().lang,
-## reason_loop)
-## targetPage.put(content, summ)
-## redir.put(content, summ)
-## break # TODO Better implement loop redirect
- else: # redirect target found
- if targetPage.isStaticRedirect():
- pywikibot.output(
- u" Redirect target is STATICREDIRECT.")
- pass
- else:
- newRedir = targetPage
- continue
+ u" Redirect target is STATICREDIRECT.")
+ pass
+ else:
+ newRedir = targetPage
+ continue
+ try:
+ oldText = redir.get(get_redirect=True)
+ except pywikibot.BadTitle:
+ pywikibot.output(u"Bad Title Error")
+ break
+ text = self.site.redirectRegex().sub(
+ '#%s %s' % (self.site.redirect(),
+ targetPage.title(asLink=True, textlink=True)),
+ oldText)
+ if redir.title() == targetPage.title() or text == oldText:
+ pywikibot.output(u"Note: Nothing left to do on %s"
+ % redir.title(asLink=True))
+ break
+ summary = i18n.twtranslate(self.site, msg_double) \
+ % {'to': targetPage.title(asLink=True)}
+ pywikibot.showDiff(oldText, text)
+ if self.prompt(u'Do you want to accept the changes?'):
try:
- oldText = redir.get(get_redirect=True)
- except pywikibot.BadTitle:
- pywikibot.output(u"Bad Title Error")
- break
- text = self.site.redirectRegex().sub(
- '#%s %s' % (self.site.redirect(),
- targetPage.title(asLink=True, textlink=True)),
- oldText)
- if redir.title() == targetPage.title() or text == oldText:
- pywikibot.output(u"Note: Nothing left to do on %s"
- % redir.title(asLink=True))
- break
- summary = i18n.twtranslate(self.site, msg_double) \
- % {'to': targetPage.title(asLink=True)}
- pywikibot.showDiff(oldText, text)
- if self.prompt(u'Do you want to accept the changes?'):
- try:
- redir.put(text, summary)
- except pywikibot.LockedPage:
- pywikibot.output(u'%s is locked.' % redir.title())
- except pywikibot.SpamfilterError, error:
- pywikibot.output(
- u"Saving page [[%s]] prevented by spam filter: %s"
- % (redir.title(), error.url))
- except pywikibot.PageNotSaved, error:
- pywikibot.output(u"Saving page [[%s]] failed: %s"
- % (redir.title(), error))
- except pywikibot.NoUsername:
- pywikibot.output(
- u"Page [[%s]] not saved; sysop privileges
required."
- % redir.title())
- except pywikibot.Error, error:
- pywikibot.output(
- u"Unexpected error occurred trying to save [[%s]]:
%s"
- % (redir.title(), error))
- break
+ redir.put(text, summary)
+ except pywikibot.LockedPage:
+ pywikibot.output(u'%s is locked.' % redir.title())
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(
+ u"Saving page [[%s]] prevented by spam filter: %s"
+ % (redir.title(), error.url))
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u"Saving page [[%s]] failed: %s"
+ % (redir.title(), error))
+ except pywikibot.NoUsername:
+ pywikibot.output(
+ u"Page [[%s]] not saved; sysop privileges required."
+ % redir.title())
+ except pywikibot.Error, error:
+ pywikibot.output(
+ u"Unexpected error occurred trying to save [[%s]]: %s"
+ % (redir.title(), error))
+ break
def fix_double_or_delete_broken_redirects(self):
# TODO: part of this should be moved to generator, the rest merged into
self.run()
@@ -716,6 +718,7 @@
elif self.action == 'both':
self.fix_double_or_delete_broken_redirects()
+
def main(*args):
# read command line parameters
# what the bot should do (either resolve double redirs, or delete broken