Xqt has uploaded a new change for review.

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


Change subject: use all pagegenerators options for protect.py
......................................................................

use all pagegenerators options for protect.py

The edit summary depends on the genFactory option provided by
pagegenerators

Change-Id: I6e2b6b85670a28e24c50f6da9cb6b3db236f78b5
---
M scripts/protect.py
1 file changed, 49 insertions(+), 103 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/98/104198/1

diff --git a/scripts/protect.py b/scripts/protect.py
index deaae24..32ecb5a 100644
--- a/scripts/protect.py
+++ b/scripts/protect.py
@@ -3,21 +3,21 @@
 This script can be used to protect and unprotect pages en masse.
 Of course, you will need an admin account on the relevant wiki.
 
-Syntax: python protect.py OPTION...
 
-Command line options:
+These command line parameters can be used to specify which pages to work on:
 
--page:       Protect specified page
--cat:        Protect all pages in the given category.
--nosubcats:  Don't protect pages in the subcategories.
--links:      Protect all pages linked from a given page.
--file:       Protect all pages listed in a text file.
--ref:        Protect all pages referring from a given page.
--images:     Protect all images used on a given page.
--always:     Don't prompt to protect pages, just do it.
--summary:    Supply a custom edit summary.
--unprotect:   Actually unprotect pages instead of protecting
+&params;
+
+Furthermore, the following command line parameters are supported:
+
+-always:          Don't prompt to protect pages, just do it.
+
+-summary:         Supply a custom edit summary.
+
+-unprotect:       Actually unprotect pages instead of protecting
+
 -edit:PROTECTION_LEVEL Set edit protection level to PROTECTION_LEVEL
+
 -move:PROTECTION_LEVEL Set move protection level to PROTECTION_LEVEL
 
 ## Without support ##
@@ -26,6 +26,8 @@
 Values for PROTECTION_LEVEL are: sysop, autoconfirmed, none.
 If an operation parameter (edit, move or create) is not specified, default
 protection level is 'sysop' (or 'none' if -unprotect).
+
+Usage: python protect.py <OPTIONS>
 
 Examples:
 
@@ -40,7 +42,7 @@
 # Written by http://it.wikisource.org/wiki/Utente:Qualc1
 # Created by modifying delete.py
 #
-# (C) Pywikibot team, 2008-2013
+# (c) Pywikibot team, 2008-2013
 #
 # Distributed under the terms of the MIT license.
 #
@@ -51,11 +53,15 @@
 from pywikibot import i18n
 from pywikibot import pagegenerators
 
+# This is required for the text that is shown when you run this script
+# with the parameter -help.
+docuReplacements = {
+    '&params;':     pagegenerators.parameterHelp,
+}
+
 
 class ProtectionRobot:
-    """
-    This robot allows protection of pages en masse.
-    """
+    """ This robot allows protection of pages en masse. """
 
     def __init__(self, generator, summary, always=False, unprotect=False,
                  edit='sysop', move='sysop', create='sysop'):
@@ -75,9 +81,7 @@
         self.move = move
 
     def run(self):
-        """
-        Starts the robot's action.
-        """
+        """ Starts the robot's action. """
         #Loop through everything in the page generator and (un)protect it.
         for page in self.generator:
             pywikibot.output(u'Processing page %s' % page.title())
@@ -100,74 +104,38 @@
             return level
 
 
-def main():
+def main(*args):
     global protectionLevels
     protectionLevels = ['sysop', 'autoconfirmed', 'none']
 
-    pageName = ''
-    summary = ''
+    summary = None
     always = False
-    doSinglePage = False
-    doCategory = False
-    protectSubcategories = True
-    doRef = False
-    doLinks = False
-    doImages = False
-    fileName = ''
     gen = None
     edit = ''
     move = ''
     defaultProtection = 'sysop'
+    # This factory is responsible for processing command line arguments
+    # that are also used by other scripts and that determine on which pages
+    # to work on.
+    genFactory = pagegenerators.GeneratorFactory()
 
     # read command line parameters
-    for arg in pywikibot.handleArgs():
+    for arg in pywikibot.handleArgs(*args):
+        if genFactory.handleArg(arg):
+            continue
         if arg == '-always':
             always = True
-        elif arg.startswith('-file'):
-            if len(arg) == len('-file'):
-                fileName = pywikibot.input(
-                    u'Enter name of file to protect pages from:')
-            else:
-                fileName = arg[len('-file:'):]
         elif arg.startswith('-summary'):
             if len(arg) == len('-summary'):
                 summary = pywikibot.input(
                     u'Enter a reason for the protection:')
             else:
                 summary = arg[len('-summary:'):]
-        elif arg.startswith('-cat'):
-            doCategory = True
-            if len(arg) == len('-cat'):
-                pageName = pywikibot.input(
-                    u'Enter the category to protect from:')
-            else:
-                pageName = arg[len('-cat:'):]
-        elif arg.startswith('-nosubcats'):
-            protectSubcategories = False
-        elif arg.startswith('-links'):
-            doLinks = True
-            if len(arg) == len('-links'):
-                pageName = pywikibot.input(u'Enter the page to protect from:')
-            else:
-                pageName = arg[len('-links:'):]
-        elif arg.startswith('-ref'):
-            doRef = True
-            if len(arg) == len('-ref'):
-                pageName = pywikibot.input(u'Enter the page to protect from:')
-            else:
-                pageName = arg[len('-ref:'):]
-        elif arg.startswith('-page'):
-            doSinglePage = True
-            if len(arg) == len('-page'):
-                pageName = pywikibot.input(u'Enter the page to protect:')
-            else:
-                pageName = arg[len('-page:'):]
-        elif arg.startswith('-images'):
-            doImages = True
-            if len(arg) == len('-images'):
-                pageName = pywikibot.input(u'Enter the page with the images to 
protect:')
-            else:
-                pageName = arg[len('-images:'):]
+        elif arg.startswith('-nosubcats') or arg.startswith('-images'):
+            pywikibot.output(u'Option %s is no longer valid. Please '
+                             u'read the documetation using -help option'
+                             % (arg if arg.find(':') < 0
+                                else arg[0:arg.find(':')]))
         elif arg.startswith('-unprotect'):
             defaultProtection = 'none'
         elif arg.startswith('-edit'):
@@ -184,48 +152,26 @@
                 create = choiceProtectionLevel('create', defaultProtection)
 
     mysite = pywikibot.getSite()
-
-    if doSinglePage:
-        if not summary:
-            summary = pywikibot.input(u'Enter a reason for the protection:')
-        page = pywikibot.Page(mysite, pageName)
-        gen = iter([page])
-    elif doCategory:
-        if not summary:
+    gen = genFactory.getCombinedGenerator()
+    option = genFactory.option
+    if summary is None:
+        if option in ('catr', 'cat', 'category', 'subcatsr', 'subcats'):
             summary = i18n.twtranslate(mysite, 'protect-category',
                                        {'cat': pageName})
-        ns = mysite.category_namespace()
-        categoryPage = pywikibot.Category(mysite, ns + ':' + pageName)
-        gen = pagegenerators.CategorizedPageGenerator(
-            categoryPage, recurse=protectSubcategories)
-    elif doLinks:
-        if not summary:
-            summary = i18n.twtranslate(mysite, 'protect-links',
+        elif option in ('links', 'ref'):
+            summary = i18n.twtranslate(mysite, 'protect-%s' % option,
                                        {'page': pageName})
-        linksPage = pywikibot.Page(mysite, pageName)
-        gen = pagegenerators.LinkedPageGenerator(linksPage)
-    elif doRef:
-        if not summary:
-            summary = i18n.twtranslate(mysite, 'protect-ref',
-                                       {'page': pageName})
-        refPage = pywikibot.Page(mysite, pageName)
-        gen = pagegenerators.ReferringPageGenerator(refPage)
-    elif fileName:
-        if not summary:
+        elif option == 'file':
             summary = i18n.twtranslate(mysite, 'protect-simple')
-        gen = pagegenerators.TextfilePageGenerator(fileName)
-    elif doImages:
-        if not summary:
+        elif option == 'imageused':
             summary = i18n.twtranslate(mysite, 'protect-images',
                                        {'page': pageName})
-        gen = pagegenerators.ImagesPageGenerator(pywikibot.Page(mysite,
-                                                                pageName))
+        else:
+            summary = pywikibot.input(u'Enter a reason for the protection:')
 
     if gen:
-        pywikibot.setAction(summary)
         # We are just protecting pages, so we have no need of using a 
preloading
-        # page generator
-        # to actually get the text of those pages.
+        # page generator to actually get the text of those pages.
         if not edit:
             edit = defaultProtection
         if not move:
@@ -233,7 +179,7 @@
         bot = ProtectionRobot(gen, summary, always, edit=edit, move=move)
         bot.run()
     else:
-        pywikibot.showHelp(u'protect')
+        pywikibot.showHelp()
 
 
 if __name__ == "__main__":

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

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

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

Reply via email to