http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11141
Revision: 11141
Author: xqt
Date: 2013-03-01 12:19:30 +0000 (Fri, 01 Mar 2013)
Log Message:
-----------
remove unused -dry option, use site.image_repository() as commons site,
skip third commonscat parameter at de-wiki (which should be handled further);
some doc cleanup
Modified Paths:
--------------
trunk/pywikipedia/commonscat.py
Modified: trunk/pywikipedia/commonscat.py
===================================================================
--- trunk/pywikipedia/commonscat.py 2013-02-28 17:18:29 UTC (rev 11140)
+++ trunk/pywikipedia/commonscat.py 2013-03-01 12:19:30 UTC (rev 11141)
@@ -205,13 +205,14 @@
'zh': u'機器人:更改 commonscat 連結,從 %(oldcat)s 至 %(newcat)s',
}
+
class CommonscatBot:
def __init__(self, generator, always, summary=None):
self.generator = generator
self.always = always
- self.dry = False
self.summary = summary
+ self.site = pywikibot.getSite()
def run(self):
for page in self.generator:
@@ -264,35 +265,34 @@
# show what was changed
pywikibot.showDiff(page.get(), text)
pywikibot.output(u'Comment: %s' %comment)
- if not self.dry:
- if not self.always:
- choice = pywikibot.inputChoice(
- u'Do you want to accept these changes?',
- ['Yes', 'No', 'Always', 'Quit'],
- ['y', 'N', 'a', 'q'], 'N')
- if choice == 'a':
- self.always = True
- elif choice == 'q':
- import sys
- sys.exit()
- if self.always or choice == 'y':
- try:
- # Save the page
- page.put(text, comment=comment,
- minorEdit=minorEdit, botflag=botflag)
- except pywikibot.LockedPage:
- pywikibot.output(u"Page %s is locked; skipping."
- % page.title(asLink=True))
- except pywikibot.EditConflict:
- pywikibot.output(
- u'Skipping %s because of edit conflict'
- % (page.title()))
- except pywikibot.SpamfilterError, error:
- pywikibot.output(
+ if not self.always:
+ choice = pywikibot.inputChoice(
+ u'Do you want to accept these changes?',
+ ['Yes', 'No', 'Always', 'Quit'],
+ ['y', 'N', 'a', 'q'], 'N')
+ if choice == 'a':
+ self.always = True
+ elif choice == 'q':
+ import sys
+ sys.exit()
+ if self.always or choice == 'y':
+ try:
+ # Save the page
+ page.put(text, comment=comment,
+ minorEdit=minorEdit, botflag=botflag)
+ except pywikibot.LockedPage:
+ pywikibot.output(u"Page %s is locked; skipping."
+ % page.title(asLink=True))
+ except pywikibot.EditConflict:
+ pywikibot.output(
+ u'Skipping %s because of edit conflict'
+ % (page.title()))
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(
u'Cannot change %s because of spam blacklist entry %s'
- % (page.title(), error.url))
- else:
- return True
+ % (page.title(), error.url))
+ else:
+ return True
return False
@classmethod
@@ -325,15 +325,15 @@
return False
def updateInterwiki (self, wikipediaPage = None, commonsPage = None):
- '''
- Update the interwiki's at commons from a wikipedia page. The bot just
- replaces the interwiki links at the commons page with the interwiki's from
- the wikipedia page. This should probably be more intelligent. We could use
- add all the interwiki's and remove duplicates. Or only remove language links
- if multiple language links to the same language exist.
+ '''Update the interwiki's at commons from a wikipedia page. The bot just
+ replaces the interwiki links at the commons page with the interwiki's
+ from the wikipedia page. This should probably be more intelligent. We
+ could use add all the interwiki's and remove duplicates. Or only remove
+ language links if multiple language links to the same language exist.
- This function is disabled for the moment untill i figure out what the best
- way is to update the interwiki's.
+ This function is disabled for the moment until i figure out what the
+ best way is to update the interwiki's.
+
'''
interwikis = {}
comment= u''
@@ -356,11 +356,11 @@
commonsPage.put(newtext=newtext, comment=comment)
def addCommonscat (self, page):
+ '''Take a page. Go to all the interwiki page looking for a commonscat
+ template. When all the interwiki's links are checked and a proper
+ category is found add it to the page.
+
'''
- Take a page. Go to all the interwiki page looking for a commonscat template.
- When all the interwiki's links are checked and a proper category is found
- add it to the page.
- '''
pywikibot.output(u'Working on ' + page.title());
#Get the right templates for this page
primaryCommonscat, commonscatAlternatives = self.getCommonscatTemplate(
@@ -416,6 +416,8 @@
newtemplate=u'', newcat=u'', linktitle=u'',
description=u''):
''' Change the current commonscat template and target. '''
+ if oldcat == '3=S' or linktitle == '3=S':
+ return #additional param on de-wiki, TODO: to be handled
if not linktitle and (page.title().lower() in oldcat.lower() or
oldcat.lower() in page.title().lower()):
linktitle = oldcat
@@ -501,12 +503,13 @@
if pywikibot.verbose:
pywikibot.output("getCommonscat: " + name )
try:
- commonsSite = pywikibot.getSite("commons", "commons")
+ commonsSite = self.site.image_repository()
#This can throw a pywikibot.BadTitle
commonsPage = pywikibot.Page(commonsSite, "Category:" + name)
if not commonsPage.exists():
- logpages = commonsSite.logpages(mode='delete', title=commonsPage.title())
+ logpages = commonsSite.logpages(mode='delete',
+ title=commonsPage.title())
try:
logitem = logpages.next()
(logpage, loguser, logtimestamp, logcomment) = logitem
@@ -519,15 +522,19 @@
elif m.group('newcat2'):
return self.checkCommonscatLink(m.group('newcat2'))
else:
- pywikibot.output(u'getCommonscat: Deleted by %s. Couldn\'t find move target in \" %s \"' % (loguser, logcomment))
+ pywikibot.output(
+ u'getCommonscat: Deleted by %s. Couldn\'t find move target in \" %s \"'
+ % (loguser, logcomment))
return u''
except StopIteration:
if pywikibot.verbose:
- pywikibot.output(u"getCommonscat: The category doesnt exist and nothing found in the deletion log.")
+ pywikibot.output(
+ u"getCommonscat: The category doesnt exist and nothing found in the deletion log.")
return u''
elif commonsPage.isRedirectPage():
if pywikibot.verbose:
- pywikibot.output(u"getCommonscat: The category is a redirect")
+ pywikibot.output(
+ u"getCommonscat: The category is a redirect")
return self.checkCommonscatLink(
commonsPage.getRedirectTarget().title(withNamespace=False))
elif "Category redirect" in commonsPage.templates():