<?php

//--------------------- 更新评论表 ---------------------

//返回字段类型
function upChangeReturnPlFtype($add){
	//字段类型
	if($add[ftype]=="TINYINT"||$add[ftype]=="SMALLINT"||$add[ftype]=="INT"||$add[ftype]=="BIGINT"||$add[ftype]=="FLOAT"||$add[ftype]=="DOUBLE")
	{
		$def=" default '0'";
	}
	elseif($add[ftype]=="VARCHAR")
	{
		$def=" default ''";
	}
	else
	{
		$def="";
	}
	$type=$add[ftype];
	//VARCHAR
	if($add[ftype]=='VARCHAR'&&empty($add[flen]))
	{
		$add[flen]='255';
	}
	//字段长度
	if($add[flen])
	{
		if($add[ftype]!="TEXT"&&$add[ftype]!="MEDIUMTEXT"&&$add[ftype]!="LONGTEXT")
		{
			$type.="(".$add[flen].")";
		}
	}
	$field="`".$add[f]."` ".$type." NOT NULL".$def;
	return $field;
}

//返回评论自定义字段列表
function upReturnPlDiyFieldList(){
	global $empire,$dbtbpre,$ecms_config;
	$fields='';
	$plfsql=$empire->query("select f from {$dbtbpre}enewsplf order by fid");
	while($plfr=$empire->fetch($plfsql))
	{
		$fields.=$dh.$plfr['f'];
		$dh=',';
	}
	return $fields;
}

//返回评论自定义字段更新
function upChangePlDiyFieldUpdate($r,$fields){
	$upstr='';
	if(empty($fields))
	{
		return '';
	}
	$fr=explode(',',$fields);
	$fcount=count($fr);
	for($i=0;$i<$fcount;$i++)
	{
		$f=$fr[$i];
		$upstr.=",".$f."='".addslashes($r[$f])."'";
	}
	return $upstr;
}

//评论表结构开始处理
function upStartChangePlTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewspl 
		change plid plid int(10) unsigned NOT NULL auto_increment,
		add `pubid` bigint(16) NOT NULL default '0',
		change username username varchar(20) NOT NULL default '',
		add `oldsaytime` datetime NOT NULL default '0000-00-00 00:00:00',
		change id id int(10) unsigned NOT NULL default '0',
		change classid classid smallint(5) unsigned NOT NULL default '0',
		change zcnum zcnum smallint(5) unsigned NOT NULL default '0',
		change fdnum fdnum smallint(5) unsigned NOT NULL default '0',
		change userid userid mediumint(8) unsigned NOT NULL default '0',
		add `saytext` text NOT NULL,
		add INDEX `pubid`(`pubid`,`checked`,`plid`);");

	$empire->query("update {$dbtbpre}enewspl set oldsaytime=saytime");

	$empire->query("alter table {$dbtbpre}enewspl 
		change saytime saytime int(10) unsigned NOT NULL default '0';");
	//自定义字段
	$plfsql=$empire->query("select * from {$dbtbpre}enewsplf order by fid");
	while($plfr=$empire->fetch($plfsql))
	{
		$field=upChangeReturnPlFtype($plfr);//字段类型
		$empire->query("alter table {$dbtbpre}enewspl add ".$field);
	}
}

//评论表结构结束处理
function upEndChangePlTbStru(){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}enewspl 
		drop COLUMN oldsaytime,
		drop COLUMN stb;");
	//重命名表
	$empire->query("ALTER TABLE `{$dbtbpre}enewspl` RENAME `{$dbtbpre}enewspl_1`");
	//删除副表
	$tbr=$empire->fetch1("select pldatatbs from {$dbtbpre}enewspublic limit 1");
	if($tbr['pldatatbs'])
	{
		$dtbr=explode(',',$tbr['pldatatbs']);
		$count=count($dtbr);
		for($i=1;$i<$count-1;$i++)
		{
			$empire->query("DROP TABLE IF EXISTS `{$dbtbpre}enewspl_data_".$dtbr[$i]."`;");
		}
	}
}

//评论转换
function upChangePlTbData($add){
	global $empire,$public_r,$class_r,$fun_r,$dbtbpre,$etable_r,$ecms_config,$doplline,$doplretime;
	$doplline=(int)$doplline;
	$doplretime=(int)$doplretime;
	$start=(int)$add['start'];
	//表结构开始处理
	if($start==0)
	{
		upStartChangePlTbStru();
	}
	//返回自定义字段
	$fields=upReturnPlDiyFieldList();
	$b=0;
	$sql=$empire->query("select * from {$dbtbpre}enewspl where plid>$start order by plid limit ".$doplline);
	while($r=$empire->fetch($sql))
	{
		$b=1;
		$new_start=$r[plid];
		$pubid=ReturnInfoPubid($r['classid'],$r['id'],upChangeClassReturnTbid($r['classid']));
		//副表
		$fr=$empire->fetch1("select * from {$dbtbpre}enewspl_data_".$r[stb]." where plid='$r[plid]'");
		$addupdate=upChangePlDiyFieldUpdate($fr,$fields);
		//更新主表
		$saytime=to_time($r['oldsaytime']);
		$empire->query("update {$dbtbpre}enewspl set pubid='$pubid',saytime='$saytime',saytext='".addslashes($fr[saytext])."'".$addupdate." where plid='$r[plid]'");
	}
	if(empty($b))
	{
		//表结构结束处理
		upEndChangePlTbStru();
		echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doplretime.";url=index.php?ecms=ChangeFileTbData\">评论数据转换完毕!正进入附件数据转换......";
		db_close();
		$empire=null;
		exit();
	}
	echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doplretime.";url=index.php?ecms=ChangePlTbData&start=$new_start\">转换一组评论数据完毕，正在进入下一组......(ID:<font color=red><b>".$new_start."</b></font>)";
	exit();
}

?>