MarcAbonce has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/340063 )
Change subject: Add surnames_redirects script ...................................................................... Add surnames_redirects script Creates redirects based on name order. For example Einstein, Albert to Albert Einstein or vice versa. Addresses T57010 Change-Id: If23b11e5e1a93bc5c1d1fc60dd5038d27eb27e84 --- A scripts/surnames_redirects.py 1 file changed, 125 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/63/340063/1 diff --git a/scripts/surnames_redirects.py b/scripts/surnames_redirects.py new file mode 100755 index 0000000..24215f0 --- /dev/null +++ b/scripts/surnames_redirects.py @@ -0,0 +1,125 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +Bot to create redirects based on name order. + +By default it creates a "Surnames, Given Names" redirect +version of a given page where title is 2 or 3 titlecased words. + +Command-line arguments: + +¶ms; + +-surnames_last Creates a "Given Names Surnames" redirect version of a + given page where title is "Surnames, Given Names". + +Example: "python surnames_redirects.py -start:B" +""" +# +# (C) Pywikibot team, 2017 +# +# Distributed under the terms of the MIT license. +# +from __future__ import unicode_literals + +from difflib import SequenceMatcher + +import pywikibot +from pywikibot import i18n, pagegenerators +from pywikibot.bot import FollowRedirectPageBot, ExistingPageBot + +docuReplacements = { + '¶ms;': pagegenerators.parameterHelp +} + + +class SurnamesBot(FollowRedirectPageBot, ExistingPageBot): + + """Surnames Bot.""" + + def __init__(self, generator, **kwargs): + """Constructor. + + Parameters: + @param generator: The page generator that determines on which pages to work. + @kwarg surnames-last: Redirect "Surnames, Given Names" to "Given Names Surnames". + """ + self.availableOptions.update({ + 'surnames_last': False, + }) + + super(SurnamesBot, self).__init__(generator=generator, **kwargs) + + def treat_page(self): + """Surnames redirects of the current page.""" + if self.current_page.isRedirectPage(): + return + + page_t = self.current_page.title() + site = self.current_page.site + + possible_redirects = [] + if self.getOption('surnames_last'): + name_parts = page_t.split(', ') + if len(name_parts) == 2 and len(page_t.split(' ')) <= 3: + possible_redirects.append(name_parts[1] + ' ' + name_parts[0]) + else: + name_parts = page_t.split() + if len(name_parts) == 2 and page_t == page_t.title(): + possible_redirects.append(name_parts[1] + ', ' + name_parts[0]) + elif len(name_parts) == 3: + """page title should be titlecased or at least close to titlecased""" + if len(SequenceMatcher(None, page_t, page_t.title()).get_matching_blocks()) <= 3: + possible_redirects.append(name_parts[1] + ' ' + name_parts[2] + ', ' + name_parts[0]) + possible_redirects.append(name_parts[2] + ', ' + name_parts[0] + ' ' + name_parts[1]) + + for possible_name in possible_redirects: + page_cap = pywikibot.Page(site, possible_name) + if page_cap.exists(): + pywikibot.output(u'%s already exists, skipping...\n' + % page_cap.title(asLink=True)) + else: + pywikibot.output(u'%s doesn\'t exist' + % page_cap.title(asLink=True)) + choice = pywikibot.input_choice( + u'Do you want to create a redirect?', + [('Yes', 'y'), ('No', 'n')], 'n') + if choice == 'y': + comment = i18n.twtranslate( + site, + 'capitalize_redirects-create-redirect', + {'to': page_t}) + page_cap.set_redirect_target(self.current_page, create=True, + summary=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 + """ + options = {} + + local_args = pywikibot.handle_args(args) + genFactory = pagegenerators.GeneratorFactory() + + for arg in local_args: + if arg == '-surnames_last': + options['surnames_last'] = True + else: + genFactory.handleArg(arg) + + gen = genFactory.getCombinedGenerator() + if gen: + preloadingGen = pagegenerators.PreloadingGenerator(gen) + bot = SurnamesBot(preloadingGen, **options) + bot.run() + else: + pywikibot.showHelp() + +if __name__ == "__main__": + main() -- To view, visit https://gerrit.wikimedia.org/r/340063 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If23b11e5e1a93bc5c1d1fc60dd5038d27eb27e84 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: MarcAbonce <a01200...@itesm.mx> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits