Hello xqt!

Here is the new (may be final? ;) patch for clean_sandbox.py.

By running e.g.:

python clean_sandbox.py -userlist:Benutzer:DrTrigonBot/Diene_Mir\!

You can clean all sandboxes for users linked on
'Benutzer:DrTrigonBot/Diene_Mir!'. They are checked for the same
template as the global sandbox, but this can easily be changed
by sub-classing as in (new) clear_user_sandbox.py showed.

(https://fisheye.toolserver.org/browse/drtrigon/pywikipedia/clean_user_sandbox.py?hb=true)

Greetings


Am 04.02.2011 15:47, schrieb Dr. Trigon:
> Hello xqt!
> Hello all!
> 
> Sorry I was not able to attach a file to [1] or change 'Assigned' or
> something else. This is the reason why I post this patch here.
> Could you please commit it? This would be nice... :)
> 
> Thanks a lot and Greetings
> Dr. Trigon
> 
> [1]
> https://sourceforge.net/tracker/?func=detail&aid=3108403&group_id=93107&atid=603141
> 
> 
> 
> _______________________________________________
> Pywikipedia-l mailing list
> [email protected]
> https://lists.wikimedia.org/mailman/listinfo/pywikipedia-l

Index: clean_sandbox.py
===================================================================
--- clean_sandbox.py    (Revision 8916)
+++ clean_sandbox.py    (Arbeitskopie)
@@ -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_sandbox.py?hb=true
+#
 # 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:
_______________________________________________
Pywikipedia-l mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-l

Reply via email to