Ricordisamoa has uploaded a new change for review.

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

Change subject: [WIP] script to fix "Pages using duplicate arguments in 
template calls"
......................................................................

[WIP] script to fix "Pages using duplicate arguments in template calls"

TODO:
* catch possible typos with TemplateData & Levenshtein distance
* documentation!

Change-Id: I2505afc34eebf32eb8a52bfbf47b3543d923d884
---
A scripts/duplicate_arguments.py
1 file changed, 127 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/43/184543/1

diff --git a/scripts/duplicate_arguments.py b/scripts/duplicate_arguments.py
new file mode 100644
index 0000000..92eef02
--- /dev/null
+++ b/scripts/duplicate_arguments.py
@@ -0,0 +1,127 @@
+# -*- coding: utf-8  -*-
+"""Script to fix template calls with duplicate arguments."""
+#
+# (C) Pywikibot team, 2015
+#
+# Distributed under the terms of the MIT license.
+#
+
+import pywikibot
+from pywikibot import i18n, pagegenerators, Bot
+from pywikibot.data import api
+import mwparserfromhell
+
+
+class DuplicateArgumentsBot(Bot):
+
+    """Bot to fix template calls with duplicate arguments."""
+
+    comment = {
+        'en': u'remove duplicate arguments in template calls'
+    }
+
+    def __init__(self, generator, **kwargs):
+        self.availableOptions.update({
+            'action_if_all_empty': None,
+        })
+        super(DuplicateArgumentsBot, self).__init__(**kwargs)
+        self.generator = generator
+
+    def treat(self, page):
+        self.current_page = page
+        code = mwparserfromhell.parse(page.text)
+        edited = False  # to prevent unwanted changes
+        for template in code.ifilter_templates():
+            params = {}
+            for param in template.params:
+                params.setdefault(param.name.strip(), []).append(param)
+            for name, items in params.items():
+                if len(items) == 1:
+                    # unique parameters
+                    continue
+                empty = [param for param in items if param.value.strip() == '']
+                diff = len(items) - len(empty)
+                if diff == 0:
+                    choice = self.getOption('action_if_all_empty')
+                    if not choice:
+                        choices = []
+                        for index, param in enumerate(items):
+                            choices.append((u'keep only occurrence 
#{index}'.format(index=index), str(index)))
+                        choices += [(u'keep only the last occurrence', 'l'),
+                                    (u'remove all occurrences', 'r'),
+                                    (u'skip these params', 's')]
+                        choice = pywikibot.input_choice(
+                            u'Found {count} params with name "{name}", all 
empty.\n'
+                            u'What to do?'.format(count=len(items), name=name),
+                            choices)
+                    if choice == 's':
+                        continue
+                    to_remove = items[:]
+                    if choice != 'r':
+                        to_remove.pop(-1 if choice == 'l' else int(choice))
+                    for param in to_remove:
+                        template.remove(param)
+                        edited = True
+                else:
+                    pywikibot.output(
+                        u'Found {count} params with name "{name}", '
+                        u'of which {empty} are empty.'.format(
+                            count=len(items), name=name, empty=len(empty)))
+        if edited:
+            text = unicode(code)
+            comment = i18n.translate(page.site.lang, self.comment, 
fallback=True)
+            self.userPut(page, page.text, text, comment=comment)
+
+
+def main(*args):
+    """
+    Process command line arguments and invoke bot.
+
+    If args is an empty list, sys.argv is used.
+
+    @param args: command line arguments
+    @type args: list of unicode
+    """
+    local_args = pywikibot.handle_args(args)
+    genFactory = pagegenerators.GeneratorFactory()
+    message_key = 'duplicate-args-category'
+    tracking = False
+
+    for arg in local_args:
+        if arg == '-tracking':
+            tracking = True
+        else:
+            genFactory.handleArg(arg)
+
+    tracking_gen = None
+    if tracking:
+        site = pywikibot.Site()
+        # TODO FIXME: make APISite.mediawiki_messages() support 
'amenableparser'
+        message_query = api.QueryGenerator(
+            site=site,
+            meta='allmessages',
+            ammessages=message_key,
+            amlang=site.lang,
+            amenableparser=True
+        )
+        message = None
+        for item in message_query:
+            if item['name'] == message_key and '*' in item:
+                message = item['*']
+                break
+        if message:
+            category = pywikibot.Category(site, message)
+            tracking_gen = pagegenerators.CategorizedPageGenerator(category)
+        else:
+            raise KeyError(u'Message "{key}" not found on site {site}'.format(
+                key=message_key, site=site))
+    gen = genFactory.getCombinedGenerator(gen=tracking_gen)
+    if gen:
+        bot = DuplicateArgumentsBot(gen)
+        bot.run()
+    else:
+        pywikibot.showHelp()
+
+
+if __name__ == '__main__':
+    main()

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2505afc34eebf32eb8a52bfbf47b3543d923d884
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