#!/usr/bin/python
# -*- coding: utf-8  -*-

"""
This bot goes over multiple pages of the home wiki, and if a page has a
lot of text but no images, inserts a warning about that.

"All words and no image makes uncyclopedia a dull site"

This script understands various command-line arguments:

    -start:        used as -start:page_name, specifies that the robot should
                   go alphabetically through all pages on the home wiki,
                   starting at the named page.

    -file:         used as -file:file_name, read a list of pages to treat
                   from the named textfile. Page titles should be enclosed
                   in [[double-squared brackets]].

    -ref:          used as -start:page_name, specifies that the robot should
                   touch all pages referring to the named page.

    -links:        used as -links:page_name, specifies that the robot should
                   touch all pages referred to from the named page.

    -cat:          used as -cat:category_name, specifies that the robot should
                   touch all pages in the named category.

    -redir         specifies that the robot should touch redirect pages;
                   otherwise, they will be skipped.

All other parameters will be regarded as a page title; in this case, the bot
will only touch a single page.
"""

__version__='$Id: imageless.py,v 1.13 2007/01/24 01:02:06 wendel Exp $'

import wikipedia, pagegenerators, catlib
import sys
import re


msg={
    'en':u'Imageless article bot',
    'pt':u'Bot de artigos sem imagens',
}

class ImageBot:
    def __init__(self, generator, touch_redirects, confirm_edits, minimum_words):
        self.generator = generator
        self.touch_redirects = touch_redirects
        self.confirm_edits = confirm_edits
	self.minimum_words = minimum_words

    def confirm(self):
	if self.confirm_edits:
	        answer = wikipedia.input('Perform edits ? [Y/n]');
        	if answer == 'n':
	            return False
        return True
        
    def run(self):
        mysite = wikipedia.getSite()
        wikipedia.setAction(wikipedia.translate(mysite,msg))
        for page in self.generator:
            try:
                print "Reading article: %s" % page.title()
                text = page.get(get_redirect=self.touch_redirects)
                # HACKHACKHACK: only works for pt: aka desciclopedia (call me lazy)
                # also, shoudn't touch private humour pages...
                if len(text.split(None)) >= self.minimum_words and not re.search(r"\{\{[pP]rivado",text) and not re.search(r"\[\[[cC]at(egoria)?:AAAAA\]\]",text):
                    if re.search(r"\[\[[iI]magem?:|<gallery>", text):
                        if re.search(r"\{\{imagens\}\}", text):
                            print "Article has images, but is wrongly tagged"
                            text = re.sub(r"\{\{imagens\}\}\n?",r"",text);
                            #print text
                            if self.confirm():
                                page.put(text)
                        else:
                            print "Article has images"
                    else:
                        if re.search(r"\{\{imagens\}\}", text):
                            print "Article has no images, and is correctly tagged"
                        else:
                            print "Article has no images, must tag"
                            text = "{{imagens}}\n" + text
                            if self.confirm():
                                page.put(text)
            except wikipedia.NoPage:
                print "Page %s does not exist?!" % page.aslink()
            except wikipedia.IsRedirectPage:
                print "Page %s is a redirect; skipping." % page.aslink()
            except wikipedia.LockedPage:
                print "Page %s is locked?!" % page.aslink()

def main():
    #page generator
    gen = None
    redirs = False
    confirm = False
    minwords = 500
    pageTitle = []
    for arg in wikipedia.handleArgs():
        if arg.startswith('-start:'):
            page = wikipedia.Page(wikipedia.getSite(),arg[7:])
            gen = pagegenerators.AllpagesPageGenerator(page.titleWithoutNamespace(),namespace=page.namespace())
        elif arg.startswith('-ref:'):
            referredPage = wikipedia.Page(wikipedia.getSite(), arg[5:])
            gen = pagegenerators.ReferringPageGenerator(referredPage)
        elif arg.startswith('-links:'):
            linkingPage = wikipedia.Page(wikipedia.getSite(), arg[7:])
            gen = pagegenerators.LinkedPageGenerator(linkingPage)
        elif arg.startswith('-file:'):
            gen = pagegenerators.TextfilePageGenerator(arg[6:])
        elif arg.startswith('-cat:'):
            cat = catlib.Category(wikipedia.getSite(), arg[5:])
            gen = pagegenerators.CategorizedPageGenerator(cat)
        elif arg.startswith('-redir'):
            redirs = True
        elif arg.startswith('-confirm'):
            confirm = True
	elif arg.startswith('-words:'):
	    minwords = int(arg[7:])
        else:
            pageTitle.append(arg)

    if pageTitle:
        page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle))
        gen = iter([page])
    if not gen:
        wikipedia.showHelp('imageless')
    else:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        bot = ImageBot(preloadingGen, redirs, confirm, minwords)
        bot.run()

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