User:MordantMaenad/scripts/welcomeusers.py

From RationalWiki
Jump to navigation Jump to search

This is a script to help welcome users (including IPs) who may have fallen through the cracks on recentchanges. This version works fully, suggestions for additions welcome.

"""
Uses the wikipedia library from pywikipediabot.

A short but sweet process. After you start it, it will ask for the number of
revisions in Recent Changes that it will review. If you don\'t type anything,
it will examine the last 500.

After that, type the text you want signed to every talk page you create. The
script obviously can't access your signature unless you have it separate from
your wiki preferences, so if you do have it separate, type that, i.e.
{{User:RWuser/sig}}.

Wait for a while. When it reaches an unwelcomed user, it will tell you. Enter
the text you want to welcome the user with, i.e. something like:

{{welcome/IP}} Hello!

Your signature will be automatically appended to this text. Press Enter and
wait for the next user. The script will terminate when all of the revisions
specified are checked for unwelcomed users.

At any point, type "exit" into any field to stop the process.

by Miranda van Etten (User:Blue) on RationalWiki.
"""
import wikipedia, userlib

def getUserContrib(userName, wikiPage):
    """
    Returns [None] if user has not made any revisions, returns set of sets
    containing user's revision ID and previous revision ID for diff
    purposes.
    """
    a = False
    result = []
    marker = 0
    site = wikipedia.getSite('en', 'rationalwiki')
    pageHist = wikiPage.fullVersionHistory()
    for l in pageHist:
        if l[2] == userName:
            prevRev = pageHist[marker - 1]
            result.append([prevRev[0], l[0]])
            a = True
        marker += 1
    if not a:
        return None
    else:
        return result

def getContribList(userName):
    """
    Returns a list of pages a user has edited (the Page object).
    """
    site = wikipedia.getSite('en', 'rationalwiki')
    user = userlib.User(site, userName)
    result = []
    for t in user.contributions():
        result.append(t[0])
    return result

def __main__():
    site = wikipedia.getSite('en', 'rationalwiki')
    userList = ['Blue']
    alreadyCheckedList = ['Blue']
    try:
        nUsers = wikipedia.input(u'Revs to check (defaults to 500): ')
        if nUsers == u'':
            nUsers = u'500'
        try:
            nUsers = int(nUsers)
        except ValueError:
            wikipedia.output(u'Please enter a whole number.')
            nUsers = wikipedia.input(u'Revs to check: ')
            if nUsers == u'':
                nUsers = u'500'
            try:
                nUsers = int(nUsers)
            except ValueError:
                wikipedia.output(u'You are an imbecile. Defaulting.')
                nUsers = 500
        cLog = site.recentchanges(nUsers)
        for page in cLog:
            try:
                userList.append(str(page[2]))                                        
            except UnicodeEncodeError:
                pass
        try:
            signature = unicode(wikipedia.input(u'Signature (defaults to none, type "exit" to leave): '))
        except UnicodeEncodeError:
            signature = unicode(wikipedia.input(u'One more time, with feeling: '))
        finally:
            pass
        if signature == u'exit':
            wikipedia.stopme()
            exit()
        site.forceLogin()
        wikipedia.output(u'Searching...')
        for user in userList:
            if user in alreadyCheckedList:
                pass
            else:
                try:
                    talkTitle = u'User talk:' + unicode(user)
                    talkPage = wikipedia.Page(site, talkTitle)
                    if not talkPage.exists():
                        wikipedia.output(u'----------------')
                        wikipedia.output(u'User:' + user + u':')
                        last = getContribList(user)
                        wikipedia.output(u'Last edit was to ' + str(last[0]))
                        if wikipedia.input(u'Show user contribs? (type "more" to show, enter = skip) ') == u'more':
                            wikipedia.output(u'Contributions:')
                            for item in last:
                                cl = getUserContrib(user, item)
                                if cl == None:
                                    wikipedia.output(u"User's contribs deleted.")
                                else:
                                    for edit in cl:
                                        wikipedia.output(u'In ' + str(item) + u':')
                                        wikipedia.showDiff(item.getOldVersion(edit[0]), item.getOldVersion(edit[1]))
                        wikipedia.output(u'User is not welcomed. (enter = skip, type "exit" to leave)')
                        action = wikipedia.input(u'Create page with: ')
                        if action != u'' and action != u'exit':
                            comment = wikipedia.input(u'Edit comment (defaults to "Welcome!"): ')
                            if comment == u'':
                                comment = u'Welcome!'
                            try:
                                talkPage.put(unicode(action) + u' ' + signature + u' ~~~~~', unicode(comment), False, False)
                            except UnicodeEncodeError:
                                wikipedia.output(u'Please don\'t hurt the encoding.')
                                pass
                            except wikipedia.PageNotSaved:
                                wikipedia.output(u'Error: page not saved.')
                                pass
                            finally:
                                pass
                            wikipedia.output(u'----------------')
                            wikipedia.output(u'Searching...')
                        elif action == u'exit':
                            wikipedia.stopme()
                            exit()
                        else:
                            pass
                    else:
                        alreadyCheckedList.append(user)
                        pass
                except UnicodeEncodeError:
                    wikipedia.output(u'User name unencodable, skipping to next.')
                    pass
        wikipedia.output(u'Finished searching.')
    finally:
        pass

if __name__ == "__main__":
    try:
        __main__()
    finally:
        wikipedia.stopme()
        exit()