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)
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
0
def main(*args: str) -> None:
    """
    Process command line arguments and invoke bot.

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

    :param args: command line arguments
    """
    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.handle_arg(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

    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 = [pywikibot.Page(site, template_name, ns=10)
                     for template_name in templates]

    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 = (
            t.getReferences(only_template_inclusion=True,
                            follow_redirects=False)
            for t in old_templates
        )
        gen = roundrobin_generators(*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()
Пример #9
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
    """
    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)

    # 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()
    gen_factory = 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 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 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 = pagegenerators.DuplicateFilterPageGenerator(gen)
    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()