http://www.mediawiki.org/wiki/Special:Code/pywikipedia/8946
Revision: 8946 Author: xqt Date: 2011-02-09 22:13:09 +0000 (Wed, 09 Feb 2011) Log Message: ----------- enable clean_sandbox for user testpages. Patch submitted by DrTrigon. Thanks. (bug 3108403)
Modified Paths: -------------- trunk/pywikipedia/clean_sandbox.py
Modified: trunk/pywikipedia/clean_sandbox.py =================================================================== --- trunk/pywikipedia/clean_sandbox.py 2011-02-09 21:07:53 UTC (rev 8945) +++ trunk/pywikipedia/clean_sandbox.py 2011-02-09 22:13:09 UTC (rev 8946) @@ -14,6 +14,17 @@ hours and limits it between 5 and 15 minutes. The minimum delay time is 5 minutes.
+ -userlist Use this parameter to run the script in the user name- + space. + > ATTENTION: on most wiki THIS IS FORBIDEN FOR BOTS ! < + > (please talk with your admin first) < + Since it is considered bad style to edit user page with- + out permission, you have to pass a page containing a + list of user to process. Argument e.g. is given as + "-userlist:Benutzer:DrTrigonBot/Diene_Mir!". + Please be also aware that the rules when to clean the + user sandbox differ from those for project sandbox. + """ # # (C) Leonardo Gregianin, 2006 @@ -21,7 +32,12 @@ # (C) Andre Engels, 2007 # (C) Siebrand Mazeland, 2007 # (C) xqt, 2009 +# (C) Dr. Trigon, 2011 # +# DrTrigonBot: http://de.wikipedia.org/wiki/Benutzer:DrTrigonBot +# Clean User Sandbox Robot (clean_user_sandbox.py) +# https://fisheye.toolserver.org/browse/drtrigon/pywikipedia/clean_user_sandbo... +# # Distributed under the terms of the MIT license. # __version__ = '$Id$' @@ -120,13 +136,18 @@ }
class SandboxBot: - def __init__(self, hours, no_repeat, delay): + def __init__(self, hours, no_repeat, delay, userlist): self.hours = hours self.no_repeat = no_repeat if delay == None: self.delay = min(15, max(5, int(self.hours *60))) else: self.delay = max(5, delay) + self.site = pywikibot.getSite() + if userlist == None: + self.userlist = None + else: + self.userlist = [page.title().split(u'/')[0] for page in pywikibot.Page(self.site, userlist).linkedPages()]
def run(self):
@@ -143,29 +164,43 @@ int(time2[10:12]) return abs(t2-t1)
- mySite = pywikibot.getSite() + mySite = self.site while True: wait = False now = time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) localSandboxTitle = pywikibot.translate(mySite, sandboxTitle) + IsUserSandbox = (self.userlist is not None) # DrTrigonBot (Clean User Sandbox Robot) + if IsUserSandbox: + localSandboxTitle = u'%s/' + localSandboxTitle.split(u':')[-1] + localSandboxTitle = [localSandboxTitle % user for user in self.userlist] if type(localSandboxTitle) is list: titles = localSandboxTitle else: titles = [localSandboxTitle,] for title in titles: sandboxPage = pywikibot.Page(mySite, title) + pywikibot.output(u'Preparing to process sandbox page %s' % sandboxPage.title(asLink=True)) try: text = sandboxPage.get() translatedContent = pywikibot.translate(mySite, content) translatedMsg = pywikibot.translate(mySite, msg) subst = 'subst:' in translatedContent + pos = text.find(translatedContent.strip()) if text.strip() == translatedContent.strip(): pywikibot.output(u'The sandbox is still clean, no change necessary.') elif subst and sandboxPage.userName() == mySite.loggedInAs(): pywikibot.output(u'The sandbox might be clean, no change necessary.') - elif text.find(translatedContent.strip()) <> 0 and not subst: - sandboxPage.put(translatedContent, translatedMsg) - pywikibot.output(u'Standard content was changed, sandbox cleaned.') + elif pos <> 0 and not subst: + if IsUserSandbox: + endpos = pos + len(translatedContent.strip()) + if (pos < 0) or (endpos == len(text)): + pywikibot.output(u'The user sandbox is still clean or not set up, no change necessary.') + else: + sandboxPage.put(text[:endpos], translatedMsg) + pywikibot.output(u'Standard content was changed, user sandbox cleaned.') + else: + sandboxPage.put(translatedContent, translatedMsg) + pywikibot.output(u'Standard content was changed, sandbox cleaned.') else: diff = minutesDiff(sandboxPage.editTime(), time.strftime("%Y%m%d%H%M%S", time.gmtime())) if pywikibot.verbose: @@ -179,6 +214,9 @@ wait = True except pywikibot.EditConflict: pywikibot.output(u'*** Loading again because of edit conflict.\n') + except pywikibot.NoPage: + pywikibot.output(u'*** The sandbox is not existent, skipping.') + continue if self.no_repeat: pywikibot.output(u'\nDone.') return @@ -192,6 +230,7 @@ def main(): hours = 1 delay = None + userlist = None no_repeat = True for arg in pywikibot.handleArgs(): if arg.startswith('-hours:'): @@ -199,11 +238,13 @@ no_repeat = False elif arg.startswith('-delay:'): delay = int(arg[7:]) + elif arg.startswith('-userlist:'): + userlist = arg[10:] else: pywikibot.showHelp('clean_sandbox') return
- bot = SandboxBot(hours, no_repeat, delay) + bot = SandboxBot(hours, no_repeat, delay, userlist) try: bot.run() except KeyboardInterrupt: