User:SoxBot/source

From Wikipedia, the free encyclopedia

This code was taken from Botwiki, modified to allow for emergency shutoff. Thanks to valhallasw for fixing the disambig page. This is current as of 4/13/2008

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
#
# (C) Pietrodn, it.wiki 2006-2007
# (C) Filnik, it.wiki 2007
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id:$'
#
 
import wikipedia
import re
import time
 
from pagegenerators import AllpagesPageGenerator
 
commento = {
           'en':u'Bot: Orphan page, add template',
           'it':u'Bot: Voce orfana, aggiungo template {{O}}',
           }
 
commenttodisambig = {
           'en':u'Bot: This page is a disambig',
           'it':u'Bot: Voce orfana, aggiungo template {{O}}',
           }
 
Template = {
            'en':u'{{Orphan|date={{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}<!-- Automatically added by User:SoxBot. If this is an error, please contact User:Soxred93 -->}}',
            'it':u'{{O||mese={{subst:CURRENTMONTHNAME}} {{subst:CURRENTYEAR}}}}',
            }
 
def main():
    while 1 == 1:
        global commento; global Template
 
        args = wikipedia.handleArgs()
        for arg in wikipedia.handleArgs():
            if arg == '-all':
                allfunction = True
        wikiSite = wikipedia.getSite()  
        comment = wikipedia.translate(wikiSite, commento)
        commentdisambig = wikipedia.translate(wikiSite, commenttodisambig)
        template = wikipedia.translate(wikiSite, Template)
        allfunction = False
        wikipediasite = wikipedia.getSite('en', 'wikipedia')
        enable = wikipedia.Page(wikipediasite, 'User:SoxBot/Run')
        getenable = enable.get()
        disambigpage = wikipedia.Page(wikipediasite, 'User:SoxBot/disambig')
        disambigtext = disambigpage.get()
        if getenable != 'enable':
            wikipedia.output('The bot is disabled')
            wikipedia.stopme()
        else:
            for i in wikiSite.lonelypages(number = 1000, repeat = False):
                #if i.title()[0] in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']:
                    #continue
                if i.isRedirectPage():
                    continue
                refs = i.getReferences()
                refsList = list()
                for j in refs:
                    if j == None:
                        continue
                    refsList.append(j)
                if getenable != 'enable':
                    wikipedia.output('The bot is disabled')
                    wikipedia.stopme()
                elif refsList != []:
                    continue
                elif refsList == None:
                    continue # FIXME: ????
                else:
                    try:
                        oldtxt = i.get()
                    except wikipedia.NoPage:
                        wikipedia.output("%s doesn't exist! Skip.." % i.title())
                        continue
                    except wikipedia.IsRedirectPage:
                        wikipedia.output("%s is a redirect! Skip..." % i.title())
                    if getenable != 'enable':
                        wikipedia.output('The bot is disabled')
                        wikipedia.stopme()
                    elif '{{orphan}}' in oldtxt.lower() or '{{orphan|' in oldtxt.lower() or '{{wi|' in oldtxt.lower() or '{{soft redirect|' in oldtxt.lower() or '{{wi}}' in oldtxt.lower():
                        wikipedia.output('This page already has the template or a wiktionary redirect, skipping')
                        continue
                    elif i.isDisambig():
                        wikipedia.output('Disambig page...')
                        disambigtext = disambigtext + ' [[' + i.title() + ']]'
                        disambigpage.put(disambigtext, commentdisambig)
                        continue
                    else:
                        newtxt = template + '\n' + oldtxt
                        wikipedia.output("\t\t>>> %s <<<" % i.title())
                        wikipedia.showDiff(oldtxt, newtxt)
                        choice = 'y'
                        try:
                            i.put(newtxt, comment)      
                        except wikipedia.EditConflict:
                            wikipedia.output('Edit Conflict! Skip...')
                        except wikipedia.ServerError:
                            wikipedia.output('Server error')
 
 
if __name__ == '__main__':
    try:
        main()
    finally:
        wikipedia.stopme()