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:Protectioncheck.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/python
# -*- coding: utf-8  -*-
"""
This bot checks if a page is protected, semi-protected, locked, available, etc.
 
All parameters will be regarded as part of the title of a single page,
and the bot will only work on that single page.
"""
import wikipedia
import pagegenerators
import sys
 
# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {
    '&params;': pagegenerators.parameterHelp
}
 
class ProtectionBot:
 
    def __init__(self, generator):
        """
        Constructor. Parameters:
            * generator - The page generator that determines on which pages
                          to work on.
        """
        self.generator = generator
 
    def run(self):
        # Set the edit summary message
        wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), u'Adding "test" to the beginning of the page.'))
        for page in self.generator:
            self.treat(page)
 
    def treat(self, page):
        """
        Loads the given page, does some changes, and saves it.
        """
        try:
            # Load the page
            (text, useless, editRestriction) = page.getEditPage()
        except wikipedia.NoPage:
            wikipedia.output(u"Page %s does not exist." % page.aslink())
            return
        except wikipedia.IsRedirectPage:
            wikipedia.output(u"Page %s is a redirect." % page.aslink())
            return
        except wikipedia.LockedPage:
            wikipedia.output(u"Page %s is locked." % page.aslink())
            return
        if editRestriction == 'sysop':
            wikipedia.output(u'The page is protected to the sysop.')
            return
        elif editRestriction == 'autoconfirmed':
            wikipedia.output(u'The page is editable only for the autoconfirmed users.')
            return
        else:
            wikipedia.output(u'The page is editable for all users.')
            return
 
def main():
    # 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()
    # The generator gives the pages that should be worked upon.
    gen = None
    # This temporary array is used to read the page title if one single
    # page to work on is specified by the arguments.
    pageTitleParts = []
 
    # Parse command line arguments
    for arg in wikipedia.handleArgs():
        # check if a standard argument like
        # -start:XYZ or -ref:Asdf was given.
        generator = genFactory.handleArg(arg)
        if generator:
            gen = generator
        else:
            pageTitleParts.append(arg)
 
    if pageTitleParts != []:
        # We will only work on a single page.
        pageTitle = ' '.join(pageTitleParts)
        page = wikipedia.Page(wikipedia.getSite(), pageTitle)
        gen = iter([page])
 
    if gen:
        # The preloading generator is responsible for downloading multiple
        # pages from the wiki simultaneously.
        gen = pagegenerators.PreloadingGenerator(gen)
        bot = ProtectionBot(gen)
        bot.run()
    else:
        wikipedia.showHelp()
 
if __name__ == "__main__":
    try:
        main()
    finally:
        wikipedia.stopme()
Personal tools
Share