#!/usr/bin/python


# Copyright (C) 2001 John Leach <john@johnleach.co.uk>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import string, os, sys, re , types, crypt, random
import MySQLdb

slash = {}
slash["dbhost"] = "127.0.0.1"
slash["dbport"] = "3306"
slash["dbuser"] = ""
slash["dbpass"] = ""
slash["dbname"] = "slash"

nuke = {}
nuke["dbhost"] = "127.0.0.1"
nuke["dbport"] = "3306"
nuke["dbuser"] = ""
nuke["dbpass"] = ""
nuke["dbname"] = "phpnuke"
nuke["prefix"] = "nuke_"

def writedebug(msg):
	sys.stderr.write("%s\n" % msg)
	
def connectdb(dict):
	"connection to database, handling exceptions, and return the db object"
	try:
		db = MySQLdb.connect(	db=dict["dbname"],
																	host=dict["dbhost"],
																	user=dict["dbuser"],
																	passwd=dict["dbpass"])	
	except:
		writedebug("Error connecting to database %s" % dict["dbname"])
		writedebug(sys.exc_value[1])
		sys.exit()
	return db

def cleanstrings(dict):
	"change a's in all strings in a dictionary to ''s to please mysql"
	for key in dict.keys():
		value = dict[key]
		if type(value) == types.StringType:
			value = string.replace(value,"'","''")																		
			dict[key] = value
		if value==None:
			value = ""
			dict[key] = value
	return dict


def convert_topics():
	# Convert topics ==============================================================
	slashcur.execute("select * from topics")
	slash["topics"] = slashcur.fetchallDict()
	writedebug("Loaded %s topics from slash database" % len(slash["topics"]))
	for row in slash["topics"]:
		writedebug("-Converting topic %(tid)s, \"%(alttext)s\"" % row)
		row = cleanstrings(row)
		nukecur.execute("insert into " + nprefix + "topics "
											"(topicname,topicimage,topictext) values ('%(tid)s','%(image)s','%(alttext)s')" % row)	
	nukecur.execute("select * from " + nprefix + "topics")
	nuke["topics"] = nukecur.fetchallDict()
	writedebug("Loaded %s topics from nuke database" % len(nuke["topics"]))

def convert_users():
	# Convert Users ===============================================================
	slashcur.execute("select * from users")
	slash["users"] = slashcur.fetchallDict()
	writedebug("Loaded %s users from slash database" % len(slash["users"]))
	for user in slash["users"]:
		user = cleanstrings(user)
		salt = str(random.randint(10,99))
		user["passwd"] = crypt.crypt(user["passwd"], salt)
		if user["fakeemail"]==None: user["fakeemail"] = user["realemail"]
		nukecur.execute("insert into " + nprefix + "users (name,uname,email,femail,url,pass,user_sig) values "
											"('%(nickname)s','%(nickname)s','%(realemail)s','%(fakeemail)s','%(homepage)s','%(passwd)s','%(sig)s')" % user)
		writedebug("-Converting user %(nickname)s, \"%(realemail)s\"" % user)
	nukecur.execute("select * from " + nprefix + "users")
	nuke["users"] = nukecur.fetchallDict()
	writedebug("Loaded %s users from nuke database" % len(nuke["users"]))

def convert_authors():
	# Convert Authors =============================================================
	slashcur.execute("select * from authors")
	slash["authors"] = slashcur.fetchallDict()
	writedebug("Loaded %s authors from slash database" % len(slash["authors"]))
	for author in slash["authors"]:
		writedebug("-Converting author %(aid)s, %(name)s" % author)
		try:
			nukecur.execute("insert into " + nprefix + "authors (aid,name,url,email,pwd) values "
											"('%(aid)s','%(name)s','%(url)s','%(email)s','%(pwd)s')" % author)
		except:
			writedebug("-error, author %(aid)s already exists!" % author)
	nukecur.execute("select * from " + nprefix + "authors")
	nuke["authors"] = nukecur.fetchallDict()
	writedebug("Loaded %s authors from nuke database" % len(nuke["authors"]))					
	
def convert_sections():
	# Convert Sections ============================================================
	slashcur.execute("select * from sections")
	slash["sections"] = slashcur.fetchallDict()
	writedebug("Loaded %s sections from slash database" % len(slash["sections"]))
	nuke["cat_tran"] = {}
	for section in slash["sections"]:
		cleanstrings(section)
		writedebug("-Converting section %(section)s" % section)
		nukecur.execute("insert into "+nprefix+"stories_cat (title) values ('%(title)s')" % section)
	nukecur.execute("select * from "+nprefix+"stories_cat")
	nuke["sections"] = nukecur.fetchallDict()
	# clean the cat strings
	for cat in nuke["sections"]:
		cat = cleanstrings(cat)
	writedebug("Loaded %s section from nuke database" % len(nuke["sections"]))					
	# set up the category/section translation dictionary
	for section in slash["sections"]:
		for cat in nuke["sections"]:
			#writedebug("cat: \"%s\"   title: \"%s\"" % (cat["title"],section["title"]))
			if cat["title"]==section["title"]:
				#writedebug("found: \"%s\" = \"%s\"" % (section["section"],cat["catid"]))
				nuke["cat_tran"][section["section"]] = cat["catid"]
	
def convert_stories():
	# Convert Stories =============================================================
	slashcur.execute("select * from stories")
	slash["stories"] = slashcur.fetchallDict()
	slashcur.execute("select * from storiestuff")
	slash["hits"] = slashcur.fetchallDict()
	writedebug("Loaded %s stories from slash database" % len(slash["stories"]))
	for story in slash["stories"]:
		story = cleanstrings(story)
		writedebug("-Converting story %(sid)s, \"%(title)s\"" % story)
		for topic in nuke["topics"]:
			if topic["topicname"]==story["tid"]:
				story["nuketopic"] = topic["topicid"]
				break
		for hit in slash["hits"]:
			if hit["sid"]==story["sid"]:
				story["hits"] = hit["hits"]
				break
		story["section"] = nuke["cat_tran"][story["section"]]
		nukecur.execute("insert into " +nprefix+"stories (aid,title,time,hometext,bodytext,comments,counter,topic,informant,catid) values "
										"('%(aid)s','%(title)s','%(time)s','%(introtext)s','%(bodytext)s','%(commentcount)s','%(hits)s','%(nuketopic)s','%(aid)s','%(section)s')" % story)
	
def convert_polls():
	# Convert Polls ===============================================================
	slashcur.execute("select * from pollquestions")
	slash["pollquestions"] = slashcur.fetchallDict()
	writedebug("Loaded %s poll questions from slash database" % len(slash["pollquestions"]))																		
	
	for question in slash["pollquestions"]:
		cleanstrings(question)
		writedebug("Converting question \"%(question)s\"..." % question)
		slashcur.execute("select * from pollanswers where qid='%s'" % question["qid"])
		answers = slashcur.fetchallDict()
		writedebug("-Loaded %s poll answers from slash database" % len(answers))
		nukecur.execute("insert into "+nprefix+"poll_desc (pollTitle,timeStamp,voters) values ('%(question)s',unix_timestamp('%(date)s'),%(voters)i)" % question)
		nukecur.execute("select pollID from "+nprefix+"poll_desc where pollTitle='%(question)s'" % question)
		pollidD = nukecur.fetchoneDict()
		if pollidD:
			pollid = pollidD["pollID"]
		for answer in answers:
			cleanstrings(answer)
			nukecur.execute("insert into "+nprefix+"poll_data (pollID,optionText,optionCount,voteID) values ('"+str(pollid)+"','%(answer)s','%(votes)s','%(aid)s	')" % answer)


	def getnukeuser(uid):
		for suser in slash["users"]:
			if suser["uid"]==uid:
				for nuser in nuke["users"]:
					if nuser["uname"]==suser["nickname"]:
						return nuser
						break
						
def convert_comments():
	#Convert Comments ============================================================
	

	
	def scomments(comment,table,id,idval):
		nukecur.execute("select tid from "+nprefix+table+" where date='%(date)s'" % comment)
		tid = nukecur.fetchoneDict()["tid"]
		slashcur.execute("select * from comments where sid='%(sid)s' and pid=%(cid)i" % comment)
		subcomments = slashcur.fetchallDict()
		writedebug("--Loaded %s sub comments from slash database" % len(subcomments))
		for comment in subcomments:
			cleanstrings(comment)
			user = getnukeuser(comment["uid"])
			name = user["uname"]
			email = user["femail"]
			url = user["url"]
			nukecur.execute("insert into "+nprefix+table+" (pid,"+id+",date,name,email,url,host_name,subject,comment,score,reason) values('"+tid+"','"+idval+"','%(date)s','"+name+"','"+email+"','"+url+"','%(host_name)s','%(subject)s','%(comment)s','%(points)s','%(reason)')")
			scomments(comment,table,id,idval)
			
	writedebug("Converting comments...")
	slashcur.execute("select * from comments where pid=0")
	slash["comments"] = slashcur.fetchallDict()
	writedebug("-Loaded %s root comments from slash database" % len(slash["comments"]))
	for rootcomment in slash["comments"]:
		cleanstrings(rootcomment)
		slashcur.execute("select time from stories where sid='%(sid)s'" % rootcomment);
		sidarray = slashcur.fetchallDict()
		if (len(sidarray)==0):
			writedebug("-Converting poll comments...")
			slashcur.execute("select question from pollquestions where qid='%(sid)s'" % rootcomment)
			pq = slashcur.fetchoneDict()["question"]
			nukecur.execute("select pollID from "+nprefix+"poll_desc where pollTitle='%s'" % pq)
			idval = nukecur.fetchoneDict()["pollID"]
			
			table = "pollcomments"
			id = "pollID"			
		else:
			writedebug("-Converting story comments...")
			slashcur.execute("select time from stories where sid='%(sid)s'" % rootcomment)
			date = slashcur.fetchoneDict()["time"]
			nukecur.execute("select sid from "+nprefix+"stories where time='%s'" % date)
			idval = nukecur.fetchoneDict()["sid"]
			id = "sid"
			table = "comments"
	
		#nukecur.execute("select uname,femail,url from "+nprefix+"users where uid=%(uid)i" % rootcomment)
		#user = nukecur.fetchoneDict()
		user = getnukeuser(rootcomment["uid"])
		#print user
		name = user["uname"]
		email = user["femail"]
		url = user["url"]
		query = "insert into "+nprefix+table+" (pid,"+id+",date,name,email,url,host_name,subject,comment,score,reason) values('%(pid)s','"+str(idval)+"','%(date)s','"+name+"','"+email+"','"+url+"','%(host_name)s','%(subject)s','%(comment)s','%(points)s','%(reason)s')"
		nukecur.execute(query % rootcomment)
		scomments(rootcomment,table,id,idval)
	

nprefix = nuke["prefix"]

# Connect to databases and set up db cursors ==================================
slashdb = connectdb(slash)
nukedb = connectdb(nuke)
slashcur = slashdb.cursor(MySQLdb.cursors.DictCursor)
nukecur = nukedb.cursor(MySQLdb.cursors.DictCursor)


convert_topics()
convert_users()
convert_authors()
convert_sections()
convert_stories()
convert_polls()
convert_comments()
