Advertisement
Technobliterator

redirectmaker.py

Nov 27th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8  -*-
  3. """
  4. A bot creating redirects to anchors of a page.
  5.  
  6. It looks for all usages of the template named 'A' in the page and will create a
  7. page with the content of the first parameter and redirecting to the current
  8. page handled.
  9.  
  10. &params;
  11.  
  12. """
  13. #
  14. # (C) Pywikibot team, 2006-2015
  15. #
  16. # Distributed under the terms of the MIT license.
  17. #
  18. from __future__ import unicode_literals
  19.  
  20. __version__ = '$Id: 7582c97b81fc2ef90dee82bfe9cfae8282fbd3db $'
  21. #
  22.  
  23. import pywikibot
  24. from pywikibot import pagegenerators
  25.  
  26. from pywikibot.bot import MultipleSitesBot, ExistingPageBot
  27.  
  28. # This is required for the text that is shown when you run this script
  29. # with the parameter -help.
  30. docuReplacements = {
  31.     '&params;': pagegenerators.parameterHelp
  32. }
  33.  
  34.  
  35. class RedirectBot(MultipleSitesBot, ExistingPageBot):
  36.  
  37.     """A bot creating a redirect to the sections of a page."""
  38.  
  39.     def treat_page(self):
  40.         """Load the given page, does some changes, and saves it."""
  41.         target_page = self.current_page
  42.         # Iterate over every template of the current page
  43.         for tmpl, params in self.current_page.templatesWithParams():
  44.             # Check if the name of the template is 'A', and ignore otherwise
  45.             # This does not care about the namespace
  46.             if tmpl.title(withNamespace=False) != 'A':
  47.                 continue  # == skip and go to next template
  48.             # Check if there is only one parameter
  49.             if len(params) != 1:
  50.                 pywikibot.output('Unable to handle template with '
  51.                                  'params: {0}'.format(params))
  52.                 continue
  53.             # Create a page instance (this will not create a Page in the wiki!)
  54.             new_page = pywikibot.Page(target_page.site, params[0])
  55.             # Check if the target doesn't exist already
  56.             if not new_page.exists():
  57.                 # Make the page a redirect. This will just set the page's text
  58.                 # and not save it immediately. It uses the current page title
  59.                 # and then the section (from the first parameter) as the target.
  60.                 new_page.set_redirect_target(target_page.title() + '#' +
  61.                                              params[0], save=False, create=True)
  62.                 # Now actually save the page. This will query the user, show a
  63.                 # diff. The 'createonly' is passed through all layers and makes
  64.                 # sure that it only saves the page if it's created.
  65.                 self.userPut(new_page, '', new_page.text, createonly=True)
  66.             else:
  67.                 pywikibot.output('Did not create "{0}" because it already '
  68.                                  'exists.'.format(new_page.title()))
  69.  
  70.  
  71. def main(*args):
  72.     """
  73.    Process command line arguments and invoke bot.
  74.  
  75.    If args is an empty list, sys.argv is used.
  76.  
  77.    @param args: command line arguments
  78.    @type args: list of unicode
  79.    """
  80.     # Process global arguments to determine desired site
  81.     local_args = pywikibot.handle_args(args)
  82.  
  83.     # This factory is responsible for processing command line arguments
  84.     # that are also used by other scripts and that determine on which pages
  85.     # to work on.
  86.     genFactory = pagegenerators.GeneratorFactory()
  87.  
  88.     # Parse command line arguments
  89.     for arg in local_args:
  90.         genFactory.handleArg(arg)
  91.  
  92.     gen = genFactory.getCombinedGenerator()
  93.     if gen:
  94.         # The preloading generator is responsible for downloading multiple
  95.         # pages from the wiki simultaneously.
  96.         gen = pagegenerators.PreloadingGenerator(gen)
  97.         bot = RedirectBot(generator=gen)
  98.         bot.run()
  99.     else:
  100.         pywikibot.showHelp()
  101.  
  102. if __name__ == "__main__":
  103.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement