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.
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:Create redirect.py
# -*- coding: utf-8 -*- """ Bot to create redirects. Command-line arguments: -file Work on all pages listed in a text file. Argument can also be given as "-file:filename". -cat Work on all pages which are in a specific category. Argument can also be given as "-cat:categoryname". -ref Work on all pages that link to a certain page. Argument can also be given as "-ref:referredpagetitle". -links Work on all pages that are linked from a certain page. Argument can also be given as "-link:linkingpagetitle". -start Work on all pages on the home wiki, starting at the named page. -page Work on a single page. -namespace Run over especific namespace. Argument can also be given as "-namespace:100" or "-namespace:Image". -always Don't prompt to make changes, just do them. Example: python capitalize_redirects.py -start:B -always """ # # (C) Yrithinnd, 2007 # (C) Filnik, 2007 # Class licensed under terms of the MIT license # __version__ = '$Id: capitalize_redirects.py 4251 2007-09-12 10:36:03Z wikipedian $' import time, sys, string, re import wikipedia, pagegenerators, catlib msg = { 'en': u'Robot: Create redirect to [[%s]]', 'es': u'Bot: Creando redirección a [[%s]]', 'it': u'Bot: Creo redirect a [[%s]]', 'fr': u'Robot : Creation redirect vers [[%s]]', 'pt': u'Bot: Criando redirect para [[%s]]', } def minicap(word): if word != '': wordnw = word[0].upper() for letter in word[1:]: wordnw += letter return wordnw else: return '' def cap(word): split = word.split(' ') stringa = '' nm = 0 while 1: word = split[nm] if '(' in word: wordnw = '(' + minicap(word.split('(')[1]) elif '-' in word: wordnw = minicap(word.split('-')[0]) + '-' + minicap(word.split('-')[1]) if len(word.split('-')) > 2: for a in word.split('-')[2:]: wordnw += '-' + minicap(a) else: wordnw = minicap(word) if len(split) - 1 == nm: stringa += wordnw break else: stringa += wordnw + ' ' nm += 1 continue return stringa def checkRedirect(page): try: text = page.get() # Double redirect! except wikipedia.IsRedirectPage: return True # Break Redirect! except wikipedia.NoPage: return True # Something-else! :-) except wikipedia.Error: return True text = text.lower() # Checking the text in the page. res = re.findall(r'([^>]#redirect\s*?\[\[)', text) if res == []: return False else: return True class CapitalizeBot: def __init__(self, generator, acceptall, msg): self.generator = generator self.acceptall = acceptall self.msg = msg def run(self): for page in self.generator: page_t = page.title() if checkRedirect(page): wikipedia.output(u'\03{lightblue}%s\03{default} is a redirect! Skip...' % page_t) continue ok = False if ' ' in page_t: for word in page_t.split(' '): if word == word.lower(): ok = True if ok == False and '-' not in page_t: wikipedia.output(u'\03{lightyellow}%s\03{default} is ok, skip!' % page.title()) continue site = wikipedia.getSite() lowpage = wikipedia.Page(site, page_t.lower()) upperpage = wikipedia.Page(site, cap(page_t)) low = False upper = False # Show the title of the page we're working on. # Highlight the title in purple. if not lowpage.exists() and page_t.lower() != page_t and page_t != lowpage.title(): wikipedia.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" % page_t) wikipedia.output(u'Creating \03{lightyellow}%s\03{default}...' % lowpage.title()) low = True if not upperpage.exists() and cap(page_t) != page_t and page_t != upperpage.title(): if low == False: wikipedia.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" % page_t) wikipedia.output(u'Creating \03{lightyellow}%s\03{default}...' % upperpage.title()) upper = True if low == False and upper == False: continue if not self.acceptall: choice = wikipedia.inputChoice( u'Do you want to create the redirects?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice in ['a', 'A']: self.acceptall = True if self.acceptall or choice in ['y', 'Y']: try: comment = wikipedia.translate(wikipedia.getSite(), self.msg) % page_t #wikipedia.setAction(comment) if low == True: lowpage._putPage(u"#REDIRECT [[%s]]" % page_t, comment, newPage = True) if upper == True and lowpage.title() != upperpage.title(): upperpage._putPage(u"#REDIRECT [[%s]]" % page_t, comment, newPage = True) except wikipedia.EditConflict: wikipedia.output(u"An edit conflict has occurred. Skip!...") continue except wikipedia.LockedPage: wikipedia.output(u"page was protected against creation. Skip!...") continue def main(): gen = None source = None textfilename = None categoryname = None pageNames = [] referredPageName = None acceptall = False namespaces = [] startpage = None for arg in wikipedia.handleArgs(): if arg.startswith('-file'): if len(arg) == 5: textfilename = wikipedia.input(u'Please enter the filename:') else: textfilename = arg[6:] source = 'textfile' elif arg.startswith('-cat'): if len(arg) == 4: categoryname = wikipedia.input( u'Please enter the category name:') else: categoryname = arg[5:] source = 'category' elif arg.startswith('-page'): if len(arg) == 5: pageNames.append(wikipedia.input( u'Which page do you want to change?')) else: pageNames.append(arg[6:]) source = 'singlepage' elif arg.startswith('-ref'): if len(arg) == 4: referredPageName = wikipedia.input( u'Links to which page should be processed?') else: referredPageName = arg[5:] source = 'ref' elif arg.startswith('-start'): if len(arg) == 6: firstPageTitle = wikipedia.input( u'Which page do you want to change?') else: firstPageTitle = arg[7:] source = 'allpages' elif arg == '-always': acceptall = True elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[11:])) except ValueError: namespaces.append(arg[11:]) else: commandline_replacements.append(arg) if source == 'textfile': gen = pagegenerators.TextfilePageGenerator(textfilename) elif source == 'category': cat = catlib.Category(wikipedia.getSite(), categoryname) gen = pagegenerators.CategorizedPageGenerator(cat) elif source == 'singlepage': pages = [wikipedia.Page(wikipedia.getSite(), pageName) for pageName in pageNames] gen = iter(pages) elif source == 'allpages': namespace = wikipedia.Page(wikipedia.getSite(), firstPageTitle).namespace() gen = pagegenerators.AllpagesPageGenerator(firstPageTitle, namespace) elif source == 'ref': referredPage = wikipedia.Page(wikipedia.getSite(), referredPageName) gen = pagegenerators.ReferringPageGenerator(referredPage) elif source == None or len(commandline_replacements) not in [0, 2]: wikipedia.stopme() wikipedia.showHelp(u'capitalize_redirects') sys.exit() if namespaces != []: gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces) preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber = 20) bot = CapitalizeBot(preloadingGen, acceptall, msg) bot.run() if __name__ == "__main__": try: main() finally: wikipedia.stopme()