jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/362250 )
Change subject: [FEAT] Port ndashredir to core
......................................................................
[FEAT] Port ndashredir to core
- written from scratch, without compat2core
- added `-reversed` parameter to reverse script behavior
- removed deprecated compat parameters merged into pagegenerators
- `-save` and `-ignore` parameters not ported yet (possible TODO)
Bug: T66875
Change-Id: Ia5c9fd147c82514bdefa5545e7b61a50c9888317
---
A scripts/ndashredir.py
1 file changed, 176 insertions(+), 0 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/ndashredir.py b/scripts/ndashredir.py
new file mode 100644
index 0000000..70696e4
--- /dev/null
+++ b/scripts/ndashredir.py
@@ -0,0 +1,176 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+A script to create hyphenated redirects for n or m dash pages.
+
+This script collects pages with n or m dash in their title and creates
+a redirect from the corresponding hyphenated version. If the redirect
+already exists, it is skipped.
+
+Use -reversed option to create n dash redirects for hyphenated pages.
+Some communities can decide to use hyphenated titles for templates, modules
+or categories and in this case this option can be handy.
+
+
+The following parameters are supported:
+
+-always don't ask for confirmation when putting a page
+
+-reversed create n dash redirects for hyphenated pages
+
+-summary: set custom summary message for the edit
+
+
+The following generators and filters are supported:
+
+¶ms;
+"""
+#
+# (C) Bináris, 2012
+# (C) Pywikibot team, 2012-2017
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, unicode_literals
+
+__version__ = '$Id$'
+#
+
+import pywikibot
+from pywikibot import i18n, pagegenerators
+
+from pywikibot.bot import (MultipleSitesBot, ExistingPageBot,
+ NoRedirectPageBot)
+
+from pywikibot.tools.formatter import color_format
+
+# This is required for the text that is shown when you run this script
+# with the parameter -help.
+docuReplacements = {
+ '¶ms;': pagegenerators.parameterHelp
+}
+
+
+class DashRedirectBot(
+ MultipleSitesBot, # A bot working on multiple sites
+ ExistingPageBot, # CurrentPageBot which only treats existing pages
+ NoRedirectPageBot # CurrentPageBot which only treats non-redirects
+):
+
+ """Bot to create hyphenated or dash redirects."""
+
+ def __init__(self, generator, **kwargs):
+ """
+ Constructor.
+
+ @param generator: the page generator that determines which pages
+ to work on
+ @type generator: generator
+ """
+ # -always option is predefined by BaseBot class
+ self.availableOptions.update({
+ 'summary': None, # custom bot summary
+ 'reversed': False, # switch bot behavior
+ })
+
+ # call constructor of the super class
+ super(DashRedirectBot, self).__init__(site=True, **kwargs)
+
+ # assign the generator to the bot
+ self.generator = generator
+
+ def treat_page(self):
+ """Do the magic."""
+ # set origin
+ origin = self.current_page.title()
+ site = self.current_page.site
+
+ # create redirect title
+ if not self.getOption('reversed'):
+ redir = pywikibot.Page(site, origin.replace('–', '-')
+ .replace('—', '-'))
+ else:
+ redir = pywikibot.Page(site, origin.replace('-', '–'))
+
+ # skip unchanged
+ if redir.title() == origin:
+ pywikibot.output('No need to process %s, skipping…'
+ % redir.title())
+ # suggest -reversed parameter
+ if '-' in origin and not self.getOption('reversed'):
+ pywikibot.output('Consider using -reversed parameter '
+ 'for this particular page')
+ else:
+ # skip existing
+ if redir.exists():
+ pywikibot.output('%s already exists, skipping…'
+ % redir.title())
+ else:
+ # confirm and save redirect
+ if self.user_confirm(
+ color_format(
+ 'Redirect from {lightblue}{0}{default} doesn\'t exist
'
+ 'yet.\nDo you want to create it?',
+ redir.title())):
+ # If summary option is None, it takes the default
+ # i18n summary from i18n subdirectory with summary key.
+ if self.getOption('summary'):
+ summary = self.getOption('summary')
+ else:
+ summary = i18n.twtranslate(site,
+ 'ndashredir-create',
+ {'title': origin})
+ redir.set_redirect_target(self.current_page, create=True,
+ summary=summary)
+
+
+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
+ """
+ options = {}
+ # Process global arguments to determine desired site
+ local_args = pywikibot.handle_args(args)
+
+ # 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()
+
+ # Parse command line arguments
+ for arg in local_args:
+
+ # Catch the pagegenerators options
+ if genFactory.handleArg(arg):
+ continue # nothing to do here
+
+ # Now pick up custom options
+ arg, sep, value = arg.partition(':')
+ option = arg[1:]
+ if option == 'summary':
+ options[option] = value
+ # Take the remaining options as booleans.
+ # Output a hint if they aren't pre-defined in the bot class
+ else:
+ options[option] = True
+
+ # The preloading option is responsible for downloading multiple pages
+ # from the wiki simultaneously.
+ gen = genFactory.getCombinedGenerator(preload=True)
+ if gen:
+ # pass generator and private options to the bot
+ bot = DashRedirectBot(gen, **options)
+ bot.run() # guess what it does
+ return True
+ else:
+ pywikibot.bot.suggest_help(missing_generator=True)
+ return False
+
+
+if __name__ == '__main__':
+ main()
--
To view, visit
https://gerrit.wikimedia.org/r/362250
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ia5c9fd147c82514bdefa5545e7b61a50c9888317
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Hashar <hashar(a)free.fr>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <Ladsgroup(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Maven-release-user <maven-release-user(a)wikimedia.org>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Prianka <priyankajayaswal025(a)gmail.com>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>