# -*- coding: utf-8 -*-
__author__ = '[[User:TyA]]'
__version__ = '2.0'
print """ Welcome to the Semi-automatic Category Adder! 
This program allows you to specify a wiki, namespace, and category and then \nquickly list pages to add to that category. 
This is particulary useful when you're on [[Special:Uncategorizedfiles]] and \nwant to add a lot of pages to a category quickly. 
To report any bugs, please to go 
from simplemediawiki import MediaWiki
import getpass
import sys
def getMode():
    mode = raw_input("What mode would you like to use? (c for categories; t for templates): ")
    #mode = mode.lower()
    return mode
def getUserName():
    username = raw_input("Username: ") # Gets user's username
    return username
def getUserPass():    
    password = getpass.getpass() #Gets user's password
    return password
def getContent(page): # Makes the function to get page content
    content ={'action':'query','format':'json','prop':'revisions','rvprop':'content','titles':page})
        content = content["query"]["pages"]
        return content[content.keys()[0]]["revisions"][0]['*']
        print "Page does not exist"
        content = ""
        return content
def saveEdit(page, content):
    query = {"action":"edit", "title":page, "summary":summary, "text":content, "token":edittoken} # Creates the API query
    try: # Tries to make the query
        print "Error" 
def edit(page, edittoken):  # Makes the edit function
    page = namespace + page # Makes the Full page name
    content = getContent(page)
    if mode is "c":
        if ("[[Category:" + dataName + "]]" in content):
            print "Category is already on the page - Skipping" 
        elif (content is ""):
            content = "[[Category:" + dataName + "]]"
            print content
            saveEdit(page, content)
            content = content + "\n[[Category:" + dataName + "]]"
            print content
            saveEdit(page, content)
    elif mode is "t":
        #if ("{{" + template in content):
        #    print "Template is already on page"
        if (content is ""):
            content = "{{" + dataName + "}}"
            print content
            saveEdit(page, content)
            content = content + "\n{{" + dataName + "}}"
            print content
            saveEdit(page, content)
def getEditToken():
    query = {"action":"query", "prop":"info", "intoken":"edit", "titles":"Main Page"} #The query to request an edit token
    q =["query"]["pages"] # Making the call
    for i in q: 
        try: # Trying
            wiki.edittoken = q[i]["edittoken"] 
            return wiki.edittoken
        except: # no edit privilege for us
            wiki.edittoken = None;
            print "Failed to get edit token!"
            return False
def start():
    counter = 1
    while counter is 1:
        page = raw_input("Page: ")
	## TODO: Make this work, currently it only does the else: statement
        if page.lower() is "change category":
            category = raw_input("New Category: ")
            print "Category changed!"
        elif page.lower() is "change namespace":
            namespace = raw_input("New Namespace: ")
            print "Namespace changed!"
        elif page.lower() is "change summary":
            summary = raw_input("New Summary: ")
            print "Summary changed!"
        elif page.lower() is "change mode":
            mode = raw_input("New Mode: ")
            print "Mode changed to " + mode 
        elif page.lower() is "quit":
            print "Goodbye."
            edit(page, edittoken)
url = raw_input("Sitename: ") # Gets the API.php url
url = "http://" + url + ""
wiki = MediaWiki(url)
username = getUserName()
password = getUserPass()
print "Attempting to Login to " + url # Prints where it's trying to login to
if (wiki.login(username, password)  is True): #Attempts to login to the wiki
    print "Login successful!" # If successful
        print "Login failed." # If fails
        username = getUserName()
        password = getUserPass()
        if (wiki.login(username, password) is True):
            print "Login successful!"
            print "Login failed. Terminating... "
summary = raw_input("Summary: ") # Gets the summary
namespace = raw_input("Namespace: ") # Gets the starting namespace
edittoken = getEditToken()
mode = getMode()
if mode is "c":
    dataName = raw_input("Category: ")
elif mode is "t":
    dataName = raw_input("Template: ")