Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- A bot creating redirects to anchors of a page.
- It looks for all usages of the template named 'A' in the page and will create a
- page with the content of the first parameter and redirecting to the current
- page handled.
- ¶ms;
- """
- #
- # (C) Pywikibot team, 2006-2015
- #
- # Distributed under the terms of the MIT license.
- #
- from __future__ import unicode_literals
- __version__ = '$Id: 7582c97b81fc2ef90dee82bfe9cfae8282fbd3db $'
- #
- import pywikibot
- from pywikibot import pagegenerators
- from pywikibot.bot import MultipleSitesBot, ExistingPageBot
- # This is required for the text that is shown when you run this script
- # with the parameter -help.
- docuReplacements = {
- '¶ms;': pagegenerators.parameterHelp
- }
- class RedirectBot(MultipleSitesBot, ExistingPageBot):
- """A bot creating a redirect to the sections of a page."""
- def treat_page(self):
- """Load the given page, does some changes, and saves it."""
- target_page = self.current_page
- # Iterate over every template of the current page
- for tmpl, params in self.current_page.templatesWithParams():
- # Check if the name of the template is 'A', and ignore otherwise
- # This does not care about the namespace
- if tmpl.title(withNamespace=False) != 'A':
- continue # == skip and go to next template
- # Check if there is only one parameter
- if len(params) != 1:
- pywikibot.output('Unable to handle template with '
- 'params: {0}'.format(params))
- continue
- # Create a page instance (this will not create a Page in the wiki!)
- new_page = pywikibot.Page(target_page.site, params[0])
- # Check if the target doesn't exist already
- if not new_page.exists():
- # Make the page a redirect. This will just set the page's text
- # and not save it immediately. It uses the current page title
- # and then the section (from the first parameter) as the target.
- new_page.set_redirect_target(target_page.title() + '#' +
- params[0], save=False, create=True)
- # Now actually save the page. This will query the user, show a
- # diff. The 'createonly' is passed through all layers and makes
- # sure that it only saves the page if it's created.
- self.userPut(new_page, '', new_page.text, createonly=True)
- else:
- pywikibot.output('Did not create "{0}" because it already '
- 'exists.'.format(new_page.title()))
- 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
- """
- # 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:
- genFactory.handleArg(arg)
- gen = genFactory.getCombinedGenerator()
- if gen:
- # The preloading generator is responsible for downloading multiple
- # pages from the wiki simultaneously.
- gen = pagegenerators.PreloadingGenerator(gen)
- bot = RedirectBot(generator=gen)
- bot.run()
- else:
- pywikibot.showHelp()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement