Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/149219

Change subject: standardize spamremove.py
......................................................................

standardize spamremove.py

- SpamRemoveBot extends pywikibot.Bot (with built-in 'always' option)

- use GeneratorFactory to provide -namespace: filtering
  and other standard arguments

Change-Id: If8e5f694721fcf28256c868f2b1a8eb230227208
---
M scripts/spamremove.py
1 file changed, 84 insertions(+), 71 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/19/149219/1

diff --git a/scripts/spamremove.py b/scripts/spamremove.py
index 60906f1..f0d8c09 100755
--- a/scripts/spamremove.py
+++ b/scripts/spamremove.py
@@ -18,100 +18,113 @@
 -always           Do not ask, but remove the lines automatically. Be very
                   careful in using this option!
 
--namespace:       Filters the search to a given namespace. If this is specified
-                  multiple times it will search all given namespaces
+In addition, these arguments can be used to restrict changes to some pages:
+
+&params;
 
 """
 
 #
-# (C) Pywikipedia bot team, 2007-2014
+# (C) Pywikibot team, 2007-2014
 #
 # Distributed under the terms of the MIT license.
 #
 __version__ = '$Id$'
 
-#
 
 import pywikibot
-from pywikibot import pagegenerators, i18n
+from pywikibot import i18n, pagegenerators, Bot
 from pywikibot.editor import TextEditor
+
+docuReplacements = {
+    '&params;': pagegenerators.parameterHelp
+}
+
+
+class SpamRemoveBot(Bot):
+    def __init__(self, generator, spamSite, **kwargs):
+        super(SpamRemoveBot, self).__init__(**kwargs)
+        self.generator = generator
+        self.spamSite = spamSite
+        self.summary = i18n.twtranslate(pywikibot.Site(), 'spamremove-remove',
+                                        {'url': self.spamSite})
+
+    def run(self):
+        for i, p in enumerate(self.generator, 1):
+            text = p.text
+            if self.spamSite not in text:
+                continue
+            # 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} <<<'
+                             % p.title())
+            lines = text.split('\n')
+            newpage = []
+            lastok = ''
+            for line in lines:
+                if self.spamSite in line:
+                    if lastok:
+                        pywikibot.output(lastok)
+                    pywikibot.output('\03{lightred}%s\03{default}' % line)
+                    lastok = None
+                else:
+                    newpage.append(line)
+                    if line.strip():
+                        if lastok is None:
+                            pywikibot.output(line)
+                        lastok = line
+            if self.getOption('always'):
+                answer = 'y'
+            else:
+                answer = pywikibot.inputChoice(u'\nDelete the red lines?',
+                                               ['yes', 'no', 'edit'],
+                                               ['y', 'N', 'e'], 'n')
+            if answer == 'n':
+                continue
+            elif answer == 'e':
+                editor = TextEditor()
+                newtext = editor.edit(text, highlight=self.spamSite,
+                                      jumpIndex=text.find(self.spamSite))
+            else:
+                newtext = '\n'.join(newpage)
+            if newtext != text:
+                p.text = newtext
+                p.save(self.summary)
+        else:
+            if 'i' not in locals():
+                pywikibot.output('No page found.')
+            elif i == 1:
+                pywikibot.output('1 page done.')
+            else:
+                pywikibot.output('%d pages done.' % i)
 
 
 def main():
-    always = False
-    namespaces = []
-    spamSite = ''
-    for arg in pywikibot.handleArgs():
-        if arg == "-always":
-            always = True
-        elif arg.startswith('-namespace:'):
-            try:
-                namespaces.append(int(arg[len('-namespace:'):]))
-            except ValueError:
-                namespaces.append(arg[len('-namespace:'):])
+    spamSite = None
+    options = {}
+
+    local_args = pywikibot.handleArgs()
+    genFactory = pagegenerators.GeneratorFactory()
+
+    for arg in local_args:
+        if arg == '-always':
+            options['always'] = True
+        elif genFactory.handleArg(arg):
+            continue
         else:
             spamSite = arg
 
     if not spamSite:
         pywikibot.showHelp()
-        pywikibot.output(u"No spam site specified.")
+        pywikibot.output(u'No spam site specified.')
         return
 
-    mysite = pywikibot.Site()
-    pages = mysite.exturlusage(spamSite)
-    if namespaces:
-        pages = pagegenerators.NamespaceFilterPageGenerator(pages, namespaces)
-    pages = pagegenerators.PreloadingGenerator(pages)
+    genFactory.gens.append(pagegenerators.LinksearchPageGenerator(spamSite))
+    generator = genFactory.getCombinedGenerator()
+    generator = pagegenerators.PreloadingGenerator(generator)
 
-    summary = i18n.twtranslate(mysite, 'spamremove-remove',
-                               {'url': spamSite})
-    for i, p in enumerate(pages, 1):
-        text = p.text
-        if spamSite not in text:
-            continue
-        # 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} <<<"
-                         % p.title())
-        lines = text.split('\n')
-        newpage = []
-        lastok = ""
-        for line in lines:
-            if spamSite in line:
-                if lastok:
-                    pywikibot.output(lastok)
-                pywikibot.output('\03{lightred}%s\03{default}' % line)
-                lastok = None
-            else:
-                newpage.append(line)
-                if line.strip():
-                    if lastok is None:
-                        pywikibot.output(line)
-                    lastok = line
-        if always:
-            answer = "y"
-        else:
-            answer = pywikibot.inputChoice(u'\nDelete the red lines?',
-                                           ['yes', 'no', 'edit'],
-                                           ['y', 'N', 'e'], 'n')
-        if answer == "n":
-            continue
-        elif answer == "e":
-            editor = TextEditor()
-            newtext = editor.edit(text, highlight=spamSite,
-                                  jumpIndex=text.find(spamSite))
-        else:
-            newtext = "\n".join(newpage)
-        if newtext != text:
-            p.text = newtext
-            p.save(summary)
-    else:
-        if "i" not in locals():
-            pywikibot.output('No page found.')
-        elif i == 1:
-            pywikibot.output('1 pages done.')
-        else:
-            pywikibot.output('%d pages done.' % i)
+    bot = SpamRemoveBot(generator, spamSite, **options)
+    bot.run()
 
 
 if __name__ == '__main__':

-- 
To view, visit https://gerrit.wikimedia.org/r/149219
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If8e5f694721fcf28256c868f2b1a8eb230227208
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to