# -*- coding: utf-8  -*-

import re
import wikipedia
import catlib
import pagegenerators
import config

debug = True
page_sauvegarde = u'Test01'
catlist = (u'Catégorie:Cacographie (casse)',)
	
def addCat(txt):
	if not txt.startswith(u'Catégorie:'):
		return u'Catégorie:'+txt
	return txt

class cacographieListBot:
	def __init__(self, ask=True, categories=[]):
		self.ask = ask
		self.cmd_categories = categories
		self.site = wikipedia.getSite()
		self.result = []
		self.done = []
		self.username = config.usernames[self.site.family.name][self.site.lang]
	
	def catGenerator(self, only_selected=True):
		for cat in catlist:
			if only_selected and len(self.cmd_categories)>0:
				if cat in self.cmd_categories:
					yield addCat(cat)
			else:
				yield addCat(cat)
				
	def getData(self):
		for cat_title in self.catGenerator(only_selected=True):
			catpage = catlib.Category(self.site, cat_title)
			i=1
			for page in catpage.articles():
				if page.title() in self.done:
					continue
				self.done.append(page.title())
				print u'%-4s' % i,
				gen = pagegenerators.ReferringPageGenerator(page)
				gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0,])
				count = 0
				for article in gen:
					count+=1
				self.result.append((count, page.title()))
				i+=1
				if debug and i>5:
					break
				
	def genOutput(self):
		self.result = reversed(sorted(self.result))
		output = u''
		for res in self.result:
			if res[0]>0:
				output += u'# [[%s]] [http://fr.wikipedia.org/wiki/Special:Whatlinkshere/%s %s]\r\n' % (res[1], res[1].replace(' ', '_'), res[0])
		return output
			
	def askPut(self, old, new, comment=u'Bot: Mise à jour.'):
		if self.ask:
			if old != u'':
				wikipedia.showDiff(old, new)
			else:
				wikipedia.output(new)
			rep = wikipedia.inputChoice(u'Valider les changements ?', ['Oui', 'Non'], ['O', 'N'], 'N')
			if (rep in ['O', 'o']):
				page.put(new, comment=comment)
		else:
			page.put(new, comment=comment)
		
				
	def run(self):
		self.getData()
		new_content = self.genOutput()

		title = u'Utilisateur:%s/%s' % (self.username, page_sauvegarde)
		page = wikipedia.Page(self.site, title)
		content = u''
		if page.exists():
			content = page.get()
		self.askPut(content, new_content)

def main():
	ask = False
	cmd_line_cats = []
	
	for arg in wikipedia.handleArgs():
		if arg.startswith('-ask'):
			ask = True
		else:
			cmd_line_cats.append(addCat(arg))

	bot = cacographieListBot(ask, cmd_line_cats)
	bot.run()

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