<?php
/*
ucenter短消息表：pre_ucenter_pms
*/
if($start <= 1) {
	truncatetable('ucenter_pm_lists');
	truncatetable('ucenter_pm_indexes');
	truncatetable('ucenter_pm_members');
	for($i = 0; $i < 10; $i ++) {
		truncatetable('ucenter_pm_messages_'.$i);
	}
}
$maxmid = $xc->db['source']->result($xc->db['source']->query("SELECT max(mid) FROM {$source_tablepre}ms_messages"), 0);
if($start < $maxmid) $converted = 1;

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}ms_messages WHERE mid >= $start AND mid < $start + $rpp") or dexit();
while ($pms = $xc->db['source']->fetch_array($query)) {
	$pms		=	array_change_key_case(daddslashes($pms));

	$fromusername	=	$pms['create_username'];
	$fromuid	=	$pms['create_uid'];
	$toid	=	$xc->db['source']->result($xc->db['source']->query("SELECT uid FROM {$source_tablepre}ms_searchs WHERE mid={$pms[mid]}"),0);
	$folder		=	 'inbox' ;
	$message	=	formatstr(@strip_tags(trim($pms['content'])));
	$lastsummary = cutstr(trim($message), 150);

	if(!$fromuid || !$toid) {
		continue;
	}
	
	//ucenter_pm_lists
	$authorid	=	$pms['create_uid'];
	$pmtype		=	1;
	$subject	=	formatstr(cutstr(htmlspecialchars(trim(@strip_tags($pms['title']))), 78));
	$members	=	2;
	$min_max	=	relationship($authorid, $toid);
	$dateline	=	$pms['modified_time'];
	$lastmessage = array('lastauthorid' => $fromuid, 'lastauthor' => $fromusername, 'lastsummary' => $lastsummary);
	$lastmessage = addslashes(serialize($lastmessage));

	$sessionexists = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT plid FROM {$discuz_tablepre}ucenter_pm_lists WHERE min_max='".$min_max."' LIMIT 1"),0);
	if($sessionexists) {
		$plid = $sessionexists;
	} else {
		$field1 = array('authorid','pmtype','subject','members','min_max','dateline','lastmessage');
		$query1 = getinsertsql("{$discuz_tablepre}ucenter_pm_lists", $field1);

		if($xc->db['discuz']->query($query1)){
			$plid = $xc->db['discuz']->insert_id();
		} else {
			continue;
		}
	}

	//ucenter_pm_indexes
	$field2 = array('plid');
	$query2 = getinsertsql("{$discuz_tablepre}ucenter_pm_indexes", $field2);

	if($xc->db['discuz']->query($query2)){
		$pmid = $xc->db['discuz']->insert_id();
	} else {
		continue;
	}

	//ucenter_pm_message_X
	$tmpplid = (string)$plid;
	$tableid = $tmpplid{strlen($tmpplid)-1};
	$delstatus = 0;

	$field3 = array('pmid','plid','authorid','message','dateline','delstatus');
	$query3 = getinsertsql("{$discuz_tablepre}ucenter_pm_messages_{$tableid}", $field3);

	//ucenter_pm_members
	$isnew	=	0;
	$pmnum	=	1;
	$lastupdate	=	$dateline;
	$lastdateline	=	$dateline;

	if(!$sessionexists){
		$uid	=	$fromuid;
		$field4 = array('plid','uid','isnew','pmnum','lastupdate','lastdateline');
		$query4 = getinsertsql("{$discuz_tablepre}ucenter_pm_members", $field4);

		$uid	=	$toid;
		$field5 = array('plid','uid','isnew','pmnum','lastupdate','lastdateline');
		$query5 = getinsertsql("{$discuz_tablepre}ucenter_pm_members", $field5);
	}
	
	
	if ($xc->db['discuz']->query($query3)) {
		if(!$sessionexists){
			$xc->db['discuz']->query($query4);
			$xc->db['discuz']->query($query5);
		}
		$convertedrows ++;
	} else {
		reportlog("无法转换短消息 $subject");
	}

	$converted = 1;
	$totalrows ++;
}
?>
