def test_match_any(self):
        """Test pages with one of many matches."""
        template1 = pywikibot.Page(self.site, 'Template:stack begin')
        template2 = pywikibot.Page(self.site, 'Template:foobar')
        builder = _MultiTemplateMatchBuilder(self.site)

        predicate = builder.search_any_predicate([template1, template2])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('article-pear-0.10.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(pages, ['Pear'],
                                  site=self.site)

        # reorder templates
        predicate = builder.search_any_predicate([template2, template1])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('article-pear-0.10.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(pages, ['Pear'],
                                  site=self.site)
    def test_match_any(self):
        """Test pages with one of many matches."""
        template1 = pywikibot.Page(self.site, 'Template:stack begin')
        template2 = pywikibot.Page(self.site, 'Template:foobar')
        builder = _MultiTemplateMatchBuilder(self.site)

        predicate = builder.search_any_predicate([template1, template2])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('article-pear-0.10.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(pages, ['Pear'],
                                  site=self.site)

        # reorder templates
        predicate = builder.search_any_predicate([template2, template1])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('article-pear-0.10.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(pages, ['Pear'],
                                  site=self.site)
 def generator(self, title, xml='article-pear-0.10.xml'):
     """Return XMLDumpPageGenerator list for a given template title."""
     template = pywikibot.Page(self.site, title, ns=10)
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(filename=join_xml_data_path(xml),
                                site=self.site,
                                text_predicate=predicate)
     return list(gen)
Exemplo n.º 4
0
 def test_no_match(self):
     """Test pages without any desired templates."""
     template = pywikibot.Page(self.site, 'Template:foobar')
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(
         filename=join_xml_data_path('article-pear-0.10.xml'),
         site=self.site,
         text_predicate=predicate)
     pages = list(gen)
     self.assertEqual(len(pages), 0)
 def test_no_match(self):
     """Test pages without any desired templates."""
     template = pywikibot.Page(self.site, 'Template:foobar')
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(
         filename=join_xml_data_path('article-pear-0.10.xml'),
         site=self.site,
         text_predicate=predicate)
     pages = list(gen)
     self.assertEqual(len(pages), 0)
Exemplo n.º 6
0
 def test_match_with_params(self):
     """Test pages with one match with parameters."""
     template = pywikibot.Page(self.site, 'Template:Taxobox')
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(
         filename=join_xml_data_path('article-pear-0.10.xml'),
         site=self.site,
         text_predicate=predicate)
     pages = list(gen)
     self.assertEqual(len(pages), 1)
     self.assertPagelistTitles(pages, ['Pear'], site=self.site)
 def test_match_with_params(self):
     """Test pages with one match with parameters."""
     template = pywikibot.Page(self.site, 'Template:Taxobox')
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(
         filename=join_xml_data_path('article-pear-0.10.xml'),
         site=self.site,
         text_predicate=predicate)
     pages = list(gen)
     self.assertEqual(len(pages), 1)
     self.assertPagelistTitles(pages, ['Pear'],
                               site=self.site)
Exemplo n.º 8
0
 def test_nested_match(self):
     """Test pages with one match inside another template."""
     template = pywikibot.Page(self.site, 'Template:boo')
     builder = _MultiTemplateMatchBuilder(self.site)
     predicate = builder.search_any_predicate([template])
     gen = XMLDumpPageGenerator(
         filename=join_xml_data_path('dummy-template.xml'),
         site=self.site,
         text_predicate=predicate)
     pages = list(gen)
     self.assertEqual(len(pages), 1)
     self.assertPagelistTitles(pages, ['Fake page with nested template'],
                               site=self.site)
    def test_match_msg(self):
        """Test pages with {{msg:..}}."""
        template = pywikibot.Page(self.site, 'Template:Foo')
        builder = _MultiTemplateMatchBuilder(self.site)

        predicate = builder.search_any_predicate([template])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('dummy-template.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(pages, ['Fake page with msg'],
                                  site=self.site)
Exemplo n.º 10
0
    def test_match_msg(self):
        """Test pages with {{msg:..}}."""
        template = pywikibot.Page(self.site, 'Template:Foo')
        builder = _MultiTemplateMatchBuilder(self.site)

        predicate = builder.search_any_predicate([template])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('dummy-template.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPageTitlesEqual(pages, ['Fake page with msg'],
                                   site=self.site)
Exemplo n.º 11
0
    def test_match_unnecessary_template_prefix(self):
        """Test pages with {{template:..}}."""
        template = pywikibot.Page(self.site, 'Template:Bar')
        builder = _MultiTemplateMatchBuilder(self.site)

        predicate = builder.search_any_predicate([template])
        gen = XMLDumpPageGenerator(
            filename=join_xml_data_path('dummy-template.xml'),
            site=self.site,
            text_predicate=predicate)
        pages = list(gen)
        self.assertEqual(len(pages), 1)
        self.assertPagelistTitles(
            pages, ['Fake page with unnecessary template prefix'],
            site=self.site)
Exemplo n.º 12
0
    def replaceDeprecatedTemplates(self, text):
        """Replace deprecated templates."""
        exceptions = ['comment', 'math', 'nowiki', 'pre']
        builder = _MultiTemplateMatchBuilder(self.site)

        if self.site.family.name in deprecatedTemplates and \
           self.site.code in deprecatedTemplates[self.site.family.name]:
            for template in deprecatedTemplates[self.site.family.name][
                    self.site.code]:
                old, new = template
                if new is None:
                    new = ''
                else:
                    new = '{{%s}}' % new

                text = textlib.replaceExcept(text, builder.pattern(old), new,
                                             exceptions)

        return text
Exemplo n.º 13
0
    def replaceDeprecatedTemplates(self, text):
        """Replace deprecated templates."""
        exceptions = ["comment", "math", "nowiki", "pre"]
        builder = _MultiTemplateMatchBuilder(self.site)

        if (
            self.site.family.name in deprecatedTemplates
            and self.site.code in deprecatedTemplates[self.site.family.name]
        ):
            for template in deprecatedTemplates[self.site.family.name][self.site.code]:
                old = template[0]
                new = template[1]
                if new is None:
                    new = ""
                else:
                    new = "{{%s}}" % new

                text = textlib.replaceExcept(text, builder.pattern(old), new, exceptions)

        return text
Exemplo n.º 14
0
    def replaceDeprecatedTemplates(self, text):
        """Replace deprecated templates."""
        exceptions = ['comment', 'math', 'nowiki', 'pre']
        builder = _MultiTemplateMatchBuilder(self.site)

        if self.site.family.name in deprecatedTemplates and \
           self.site.code in deprecatedTemplates[self.site.family.name]:
            for template in deprecatedTemplates[
                    self.site.family.name][self.site.code]:
                old, new = template
                if new is None:
                    new = ''
                else:
                    new = '{{%s}}' % new

                text = textlib.replaceExcept(
                    text,
                    builder.pattern(old),
                    new, exceptions)

        return text
Exemplo n.º 15
0
    def __init__(self, generator, templates, **kwargs):
        """
        Constructor.

        @param generator: the pages to work on
        @type  generator: iterable
        @param templates: a dictionary which maps old template names to
            their replacements. If remove or subst is True, it maps the
            names of the templates that should be removed/resolved to None.
        @type  templates: dict
        """
        self.availableOptions.update({
            'subst': False,
            'remove': False,
            'summary': None,
            'addedCat': None,
        })

        Bot.__init__(self, generator=generator, **kwargs)

        self.templates = templates

        # get edit summary message if it's empty
        if not self.getOption('summary'):
            comma = self.site.mediawiki_message('comma-separator')
            params = {
                'list': comma.join(self.templates.keys()),
                'num': len(self.templates)
            }

            site = self.site

            if self.getOption('remove'):
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-removing', params)
            elif self.getOption('subst'):
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-substituting', params)
            else:
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-changing', params)

        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []
        exceptions = {}
        builder = textlib._MultiTemplateMatchBuilder(site)
        for old, new in self.templates.items():
            templateRegex = builder.pattern(old)

            if self.getOption('subst') and self.getOption('remove'):
                replacements.append(
                    (templateRegex, r'{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.getOption('subst'):
                replacements.append(
                    (templateRegex, r'{{subst:%s\g<parameters>}}' % old))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.getOption('remove'):
                replacements.append((templateRegex, ''))
            else:
                template = pywikibot.Page(self.site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(u'Template "%s" does not exist.' % new)
                    if not pywikibot.input_yn('Do you want to proceed anyway?',
                                              default=False,
                                              automatic_quit=False):
                        continue
                replacements.append(
                    (templateRegex, r'{{%s\g<parameters>}}' % new))

        super(TemplateRobot,
              self).__init__(generator,
                             replacements,
                             exceptions,
                             always=self.getOption('always'),
                             addedCat=self.getOption('addedCat'),
                             summary=self.getOption('summary'))
Exemplo n.º 16
0
    def __init__(self, generator, templates, **kwargs):
        """
        Constructor.

        @param generator: the pages to work on
        @type generator: iterable
        @param templates: a dictionary which maps old template names to
            their replacements. If remove or subst is True, it maps the
            names of the templates that should be removed/resolved to None.
        @type templates: dict
        """
        self.availableOptions.update({
            'subst': False,
            'remove': False,
            'summary': None,
            'addedCat': None,
        })

        Bot.__init__(self, generator=generator, **kwargs)

        self.templates = templates

        # get edit summary message if it's empty
        if not self.getOption('summary'):
            comma = self.site.mediawiki_message('comma-separator')
            params = {'list': comma.join(self.templates.keys()),
                      'num': len(self.templates)}

            if self.getOption('remove'):
                self.options['summary'] = i18n.twtranslate(
                    self.site, 'template-removing', params)
            elif self.getOption('subst'):
                self.options['summary'] = i18n.twtranslate(
                    self.site, 'template-substituting', params)
            else:
                self.options['summary'] = i18n.twtranslate(
                    self.site, 'template-changing', params)

        replacements = []
        exceptions = {}
        builder = textlib._MultiTemplateMatchBuilder(self.site)
        for old, new in self.templates.items():
            templateRegex = builder.pattern(old)

            if self.getOption('subst') and self.getOption('remove'):
                replacements.append((templateRegex,
                                     r'{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
                                             'pagelist', ]
            elif self.getOption('subst'):
                replacements.append((templateRegex,
                                     r'{{subst:%s\g<parameters>}}' % old))
                exceptions['inside-tags'] = ['ref', 'gallery', 'poem',
                                             'pagelist', ]
            elif self.getOption('remove'):
                separate_line_regex = re.compile(
                    r'^[*#:]* *{0} *\n'.format(templateRegex.pattern),
                    re.DOTALL | re.MULTILINE)
                replacements.append((separate_line_regex, ''))

                spaced_regex = re.compile(
                    r' +{0} +'.format(templateRegex.pattern),
                    re.DOTALL)
                replacements.append((spaced_regex, ' '))

                replacements.append((templateRegex, ''))
            else:
                template = pywikibot.Page(self.site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(u'Template "%s" does not exist.' % new)
                    if not pywikibot.input_yn('Do you want to proceed anyway?',
                                              default=False,
                                              automatic_quit=False):
                        continue
                replacements.append((templateRegex,
                                     r'{{%s\g<parameters>}}' % new))

        super(TemplateRobot, self).__init__(
            generator, replacements, exceptions,
            always=self.getOption('always'),
            addedCat=self.getOption('addedCat'),
            summary=self.getOption('summary'))
Exemplo n.º 17
0
    def __init__(self, generator, templates: dict, **kwargs) -> None:
        """
        Initializer.

        @param generator: the pages to work on
        @type generator: iterable
        @param templates: a dictionary which maps old template names to
            their replacements. If remove or subst is True, it maps the
            names of the templates that should be removed/resolved to None.
        """
        self.available_options.update({
            'addcat': None,
            'remove': False,
            'subst': False,
            'summary': None,
        })

        SingleSiteBot.__init__(self, generator=generator, **kwargs)

        self.templates = templates

        # get edit summary message if it's empty
        if not self.opt.summary:
            comma = self.site.mediawiki_message('comma-separator')
            params = {
                'list': comma.join(self.templates.keys()),
                'num': len(self.templates)
            }

            if self.opt.remove:
                tw_key = 'template-removing'
            elif self.opt.subst:
                tw_key = 'template-substituting'
            else:
                tw_key = 'template-changing'
            self.opt.summary = i18n.twtranslate(self.site, tw_key, params)

        replacements = []
        exceptions = {}
        builder = textlib._MultiTemplateMatchBuilder(self.site)
        for old, new in self.templates.items():
            template_regex = builder.pattern(old)

            if self.opt.subst and self.opt.remove:
                replacements.append(
                    (template_regex, r'{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = [
                    'ref',
                    'gallery',
                    'poem',
                    'pagelist',
                ]
            elif self.opt.subst:
                replacements.append(
                    (template_regex,
                     r'{{%s:%s\g<parameters>}}' % (self.opt.subst, old)))
                exceptions['inside-tags'] = [
                    'ref',
                    'gallery',
                    'poem',
                    'pagelist',
                ]
            elif self.opt.remove:
                separate_line_regex = re.compile(
                    r'^[*#:]* *{0} *\n'.format(template_regex.pattern),
                    re.DOTALL | re.MULTILINE)
                replacements.append((separate_line_regex, ''))

                spaced_regex = re.compile(
                    r' +{0} +'.format(template_regex.pattern), re.DOTALL)
                replacements.append((spaced_regex, ' '))

                replacements.append((template_regex, ''))
            else:
                template = pywikibot.Page(self.site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(
                        'Template "{0}" does not exist.'.format(new))
                    if not pywikibot.input_yn('Do you want to proceed anyway?',
                                              default=False,
                                              automatic_quit=False):
                        continue
                replacements.append(
                    (template_regex, r'{{%s\g<parameters>}}' % new))

        super().__init__(generator,
                         replacements,
                         exceptions,
                         always=self.opt.always,
                         addcat=self.opt.addcat,
                         summary=self.opt.summary)
Exemplo n.º 18
0
    def __init__(self, generator, templates, **kwargs):
        """
        Constructor.

        @param generator: the pages to work on
        @type  generator: iterable
        @param templates: a dictionary which maps old template names to
            their replacements. If remove or subst is True, it maps the
            names of the templates that should be removed/resolved to None.
        @type  templates: dict
        """
        self.availableOptions.update({
            'subst': False,
            'remove': False,
            'summary': None,
            'addedCat': None,
        })

        Bot.__init__(self, generator=generator, **kwargs)

        self.templates = templates

        # get edit summary message if it's empty
        if not self.getOption('summary'):
            comma = self.site.mediawiki_message('comma-separator')
            params = {'list': comma.join(self.templates.keys()),
                      'num': len(self.templates)}

            site = self.site

            if self.getOption('remove'):
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-removing', params)
            elif self.getOption('subst'):
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-substituting', params)
            else:
                self.options['summary'] = i18n.twtranslate(
                    site, 'template-changing', params)

        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []
        exceptions = {}
        builder = textlib._MultiTemplateMatchBuilder(site)
        for old, new in self.templates.items():
            templateRegex = builder.pattern(old)

            if self.getOption('subst') and self.getOption('remove'):
                replacements.append((templateRegex,
                                     r'{{{{subst:{0!s}\g<parameters>}}}}'.format(new)))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.getOption('subst'):
                replacements.append((templateRegex,
                                     r'{{{{subst:{0!s}\g<parameters>}}}}'.format(old)))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.getOption('remove'):
                replacements.append((templateRegex, ''))
            else:
                template = pywikibot.Page(self.site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(u'Template "{0!s}" does not exist.'.format(new))
                    if not pywikibot.input_yn('Do you want to proceed anyway?',
                                              default=False, automatic_quit=False):
                        continue
                replacements.append((templateRegex,
                                     r'{{{{{0!s}\g<parameters>}}}}'.format(new)))

        super(TemplateRobot, self).__init__(
            generator, replacements, exceptions,
            always=self.getOption('always'),
            addedCat=self.getOption('addedCat'),
            summary=self.getOption('summary'))
Exemplo n.º 19
0
def main(*args) -> None:
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    template_names = []
    templates = {}
    options = {}
    # If xmlfilename is None, references will be loaded from the live wiki.
    xmlfilename = None
    user = None
    skip = False
    timestamp = None

    # read command line parameters
    local_args = pywikibot.handle_args(args)

    site = pywikibot.Site()
    gen_factory = pagegenerators.GeneratorFactory()
    for arg in local_args:
        if arg == '-remove':
            options['remove'] = True
        elif arg.startswith('-subst'):
            options['subst'] = arg[len('-subst:'):] + 'subst'
            assert options['subst'] in ('subst', 'safesubst')
        elif arg == '-assubst':
            options['subst'] = 'subst'
            options['remove'] = True
        elif arg == '-always':
            options['always'] = True
        elif arg.startswith('-xml'):
            if len(arg) == 4:
                xmlfilename = pywikibot.input(
                    "Please enter the XML dump's filename: ")
            else:
                xmlfilename = arg[5:]
        elif arg.startswith('-addcat:'):
            options['addcat'] = arg[len('-addcat:'):]
        elif arg.startswith('-summary:'):
            options['summary'] = arg[len('-summary:'):]
        elif arg.startswith('-onlyuser:'******'-onlyuser:'******'-skipuser:'******'-skipuser:'******'-timestamp:'):
            timestamp = arg[len('-timestamp:'):]
        else:
            if not gen_factory.handleArg(arg):
                template_name = pywikibot.Page(site, arg, ns=10)
                template_names.append(template_name.title(with_ns=False))

    if not template_names:
        pywikibot.bot.suggest_help(missing_parameters=['templates'])
        return False

    if bool(options.get('subst', False)) ^ options.get('remove', False):
        for template_name in template_names:
            templates[template_name] = None
    else:
        try:
            for i in range(0, len(template_names), 2):
                templates[template_names[i]] = template_names[i + 1]
        except IndexError:
            pywikibot.output('Unless using solely -subst or -remove, '
                             'you must give an even number of template names.')
            return

    old_templates = []
    for template_name in templates.keys():
        old_template = pywikibot.Page(site, template_name, ns=10)
        old_templates.append(old_template)

    if xmlfilename:
        builder = textlib._MultiTemplateMatchBuilder(site)
        predicate = builder.search_any_predicate(old_templates)

        gen = XMLDumpPageGenerator(xmlfilename,
                                   site=site,
                                   text_predicate=predicate)
    else:
        gen = gen_factory.getCombinedGenerator()

    if not gen:
        gens = (pagegenerators.ReferringPageGenerator(
            t, onlyTemplateInclusion=True) for t in old_templates)
        gen = chain(*gens)
        gen = filter_unique(gen, key=lambda p: '{}:{}:{}'.format(*p._cmpkey()))
    if user:
        gen = pagegenerators.UserEditFilterGenerator(gen,
                                                     user,
                                                     timestamp,
                                                     skip,
                                                     max_revision_depth=100,
                                                     show_filtered=True)

    if not gen_factory.gens:
        # make sure that proper namespace filtering etc. is handled
        gen = gen_factory.getCombinedGenerator(gen)

    if not gen_factory.nopreload:
        gen = pagegenerators.PreloadingGenerator(gen)

    bot = TemplateRobot(gen, templates, site=site, **options)
    bot.run()
Exemplo n.º 20
0
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
    """
    templateNames = []
    templates = {}
    options = {}
    # If xmlfilename is None, references will be loaded from the live wiki.
    xmlfilename = None
    user = None
    skip = False
    timestamp = None

    # read command line parameters
    local_args = pywikibot.handle_args(args)

    # Avoid conflicts with pagegenerators.py parameters.
    if any(arg.startswith('-category:') for arg in local_args):
        warn('-category (to append a category to each edited page) has been'
             ' renamed to -addcat; make sure you are using the correct param.',
             ArgumentDeprecationWarning)

    site = pywikibot.Site()
    genFactory = pagegenerators.GeneratorFactory()
    for arg in local_args:
        if arg == '-remove':
            options['remove'] = True
        elif arg == '-subst':
            options['subst'] = True
        elif arg == '-assubst':
            options['subst'] = options['remove'] = True
        elif arg == '-always':
            options['always'] = True
        elif arg.startswith('-xml'):
            if len(arg) == 4:
                xmlfilename = pywikibot.input(
                    u'Please enter the XML dump\'s filename: ')
            else:
                xmlfilename = arg[5:]
        elif arg.startswith('-addcat:'):
            options['addedCat'] = arg[len('-addcat:'):]
        elif arg.startswith('-summary:'):
            options['summary'] = arg[len('-summary:'):]
        elif arg.startswith('-onlyuser:'******'-onlyuser:'******'-skipuser:'******'-skipuser:'******'-timestamp:'):
            timestamp = arg[len('-timestamp:'):]
        else:
            if not genFactory.handleArg(arg):
                templateName = pywikibot.Page(site, arg, ns=10)
                templateNames.append(templateName.title(withNamespace=False))

    if not templateNames:
        pywikibot.bot.suggest_help(missing_parameters=['templates'])
        return False

    if options.get('subst', False) ^ options.get('remove', False):
        for templateName in templateNames:
            templates[templateName] = None
    else:
        try:
            for i in range(0, len(templateNames), 2):
                templates[templateNames[i]] = templateNames[i + 1]
        except IndexError:
            pywikibot.output('Unless using solely -subst or -remove, '
                             'you must give an even number of template names.')
            return

    oldTemplates = []
    for templateName in templates.keys():
        oldTemplate = pywikibot.Page(site, templateName, ns=10)
        oldTemplates.append(oldTemplate)

    if xmlfilename:
        builder = textlib._MultiTemplateMatchBuilder(site)
        predicate = builder.search_any_predicate(oldTemplates)

        gen = XMLDumpPageGenerator(
            xmlfilename, site=site, text_predicate=predicate)
    else:
        gen = genFactory.getCombinedGenerator()

    if not gen:
        gens = [
            pagegenerators.ReferringPageGenerator(t,
                                                  onlyTemplateInclusion=True)
            for t in oldTemplates
        ]
        gen = pagegenerators.CombinedPageGenerator(gens)
        gen = pagegenerators.DuplicateFilterPageGenerator(gen)
    if user:
        gen = pagegenerators.UserEditFilterGenerator(gen, user, timestamp,
                                                     skip,
                                                     max_revision_depth=100,
                                                     show_filtered=True)

    if not genFactory.gens:
        # make sure that proper namespace filtering etc. is handled
        gen = genFactory.getCombinedGenerator(gen)

    if not genFactory.nopreload:
        gen = pagegenerators.PreloadingGenerator(gen)

    bot = TemplateRobot(gen, templates, site=site, **options)
    bot.run()
Exemplo n.º 21
0
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
    """
    templateNames = []
    templates = {}
    options = {}
    # If xmlfilename is None, references will be loaded from the live wiki.
    xmlfilename = None
    user = None
    skip = False
    timestamp = None

    # read command line parameters
    local_args = pywikibot.handle_args(args)

    # Avoid conflicts with pagegenerators.py parameters.
    if any(arg.startswith('-category:') for arg in local_args):
        warn(
            '-category (to append a category to each edited page) has been'
            ' renamed to -addcat; make sure you are using the correct param.',
            ArgumentDeprecationWarning)

    site = pywikibot.Site()
    genFactory = pagegenerators.GeneratorFactory()
    for arg in local_args:
        if arg == '-remove':
            options['remove'] = True
        elif arg == '-subst':
            options['subst'] = True
        elif arg == '-assubst':
            options['subst'] = options['remove'] = True
        elif arg == '-always':
            options['always'] = True
        elif arg.startswith('-xml'):
            if len(arg) == 4:
                xmlfilename = pywikibot.input(
                    u'Please enter the XML dump\'s filename: ')
            else:
                xmlfilename = arg[5:]
        elif arg.startswith('-addcat:'):
            options['addedCat'] = arg[len('-addcat:'):]
        elif arg.startswith('-summary:'):
            options['summary'] = arg[len('-summary:'):]
        elif arg.startswith('-onlyuser:'******'-onlyuser:'******'-skipuser:'******'-skipuser:'******'-timestamp:'):
            timestamp = arg[len('-timestamp:'):]
        else:
            if not genFactory.handleArg(arg):
                templateName = pywikibot.Page(site, arg, ns=10)
                templateNames.append(templateName.title(withNamespace=False))

    if not templateNames:
        pywikibot.bot.suggest_help(missing_parameters=['templates'])
        return False

    if options.get('subst', False) ^ options.get('remove', False):
        for templateName in templateNames:
            templates[templateName] = None
    else:
        try:
            for i in range(0, len(templateNames), 2):
                templates[templateNames[i]] = templateNames[i + 1]
        except IndexError:
            pywikibot.output('Unless using solely -subst or -remove, '
                             'you must give an even number of template names.')
            return

    oldTemplates = []
    for templateName in templates.keys():
        oldTemplate = pywikibot.Page(site, templateName, ns=10)
        oldTemplates.append(oldTemplate)

    if xmlfilename:
        builder = textlib._MultiTemplateMatchBuilder(site)
        predicate = builder.search_any_predicate(oldTemplates)

        gen = XmlDumpTemplatePageGenerator(xmlfilename,
                                           site=site,
                                           text_predicate=predicate)
    else:
        gen = genFactory.getCombinedGenerator()

    if not gen:
        gens = [
            pagegenerators.ReferringPageGenerator(t,
                                                  onlyTemplateInclusion=True)
            for t in oldTemplates
        ]
        gen = pagegenerators.CombinedPageGenerator(gens)
        gen = pagegenerators.DuplicateFilterPageGenerator(gen)
    if user:
        gen = pagegenerators.UserEditFilterGenerator(gen,
                                                     user,
                                                     timestamp,
                                                     skip,
                                                     max_revision_depth=100,
                                                     show_filtered=True)

    if not genFactory.gens:
        # make sure that proper namespace filtering etc. is handled
        gen = genFactory.getCombinedGenerator(gen)

    preloadingGen = pagegenerators.PreloadingGenerator(gen)

    bot = TemplateRobot(preloadingGen, templates, **options)
    bot.run()