Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:Redirect creation (en)

From Botwiki
Jump to: navigation, search

This script creates redirects to pages.

I made it for my site, so it needs changes to be more general:

  • it looks for pages with titles like "foo - bar"
  • and then makes a redirect at "foo" and "bar" to "foo - bar"

Change these tests so it does something else :)

Hope it's useful.

#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
Based on the basic.py script.
 
"""
import wikipedia
import pagegenerators
import sys
import re
 
summary = "Bot: Automatic redirect creation to "
 
try:
    start = list()
    test = False
    for arg in wikipedia.handleArgs():
        if arg.startswith("-test"):
            test = True
        else:
            start.append(arg)
    if start:
        start = " ".join(start)
    else:
        start = "!"
    mysite = wikipedia.getSite()
    # If anything needs to be prepared, you can do it here
    basicgenerator = pagegenerators.AllpagesPageGenerator(start=start)
    generator = pagegenerators.PreloadingGenerator(basicgenerator)
 
    for page in generator:
        # skip redirects
        if page.isRedirectPage():
            continue
 
        pagetitle = page.title()
        # condition for pages
        if pagetitle.find("-")  > 0:            
            # new titles
            newtitles     = pagetitle.split(' - ')
            suggestions   = [">>> %s <<<" % x for x in newtitles]
            newredirects  = [wikipedia.Page(mysite, title) for title in newtitles ]         
            allexist = 1           
            for x in newredirects:
                allexist = allexist and x.exists()   
 
            if allexist:
                # all needed redirects exist; print a message and skip to the next page
                wikipedia.output(u'>>>> %s <<<< -- redirects already exist, skipping\n' % pagetitle)
                continue
 
            wikipedia.output(u'\n>>>> %s <<<<' % pagetitle)
            wikipedia.output(u'Suggested redirects: %s ' % ', '.join(suggestions) )     
            ask = wikipedia.input('What do you do: (c)reate redirects, (n)ext page or (q)uit?')                
            if ask in ['c', 'C']:
                for x in newredirects:
                    # for each new title split from the current page, create a redirect
                    if x.exists():
                      print "page [[%s]] already exists" % x.title()
                    else:
                      content  = '#REDIRECT [[%s]]' % pagetitle
                      print "content is: " + content                  
                      x.put(content, comment = summary + u'[[' + pagetitle + u']]', minorEdit = False)
 
 
            elif ask in ['n', 'N']:
                pass
            elif ask in ['q', 'Q']:
                sys.exit()
            else:
                wikipedia.output(u'Input certain code.')
                self.run()                                
finally:
    wikipedia.stopme()
Personal tools
Share