<?php
//--------------------- 更新信息表 ---------------------

//评分字段增加
function upChangeAddPfenField($tid,$tbname){
	global $empire,$dbtbpre,$ecms_config;
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$tbname." where infopfennum>0");
	if(!$num)
	{
		return 0;
	}
	//采集临时表
	$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." 
		add `infopfen` int(11) NOT NULL default '0',
		add `infopfennum` int(11) NOT NULL default '0';");
	//采集节点副表
	$empire->query("alter table {$dbtbpre}ecms_infoclass_".$tbname." 
		add `zz_infopfen` text NOT NULL,
		add `z_infopfen` varchar(255) NOT NULL default '',
		add `qz_infopfen` varchar(255) NOT NULL default '',
		add `save_infopfen` varchar(10) NOT NULL default '',
		add `zz_infopfennum` text NOT NULL,
		add `z_infopfennum` varchar(255) NOT NULL default '',
		add `qz_infopfennum` varchar(255) NOT NULL default '',
		add `save_infopfennum` varchar(10) NOT NULL default '';");

	$empire->query("replace into `{$dbtbpre}enewsf` values(NULL,'infopfen','评分数','text','\r\n<input name=\\\\\"infopfen\\\\\" type=\\\\\"text\\\\\" id=\\\\\"infopfen\\\\\" value=\\\\\"<?=\$ecmsfirstpost==1?\\\\\"\\\\\":ehtmlspecialchars(stripSlashes(\$r[infopfen]))?>\\\\\" size=\\\\\"\\\\\">\r\n','','1','1','1','\r\n  <tr bgcolor=\\\\\"#FFFFFF\\\\\"> \r\n    <td height=\\\\\"22\\\\\" valign=\\\\\"top\\\\\"><strong>[!--enews.name--]正则：</strong><br>\r\n      (<input name=\\\\\"textfield\\\\\" type=\\\\\"text\\\\\" id=\\\\\"textfield\\\\\" value=\\\\\"[!--infopfen--]\\\\\" size=\\\\\"20\\\\\">)</td>\r\n    <td><table width=\\\\\"100%\\\\\" border=\\\\\"0\\\\\" cellspacing=\\\\\"1\\\\\" cellpadding=\\\\\"3\\\\\">\r\n        <tr> \r\n          <td><textarea name=\\\\\"add[zz_infopfen]\\\\\" cols=\\\\\"60\\\\\" rows=\\\\\"10\\\\\" id=\\\\\"textarea\\\\\"><?=ehtmlspecialchars(stripSlashes(\$r[zz_infopfen]))?></textarea></td>\r\n        </tr>\r\n        <tr> \r\n          <td><input name=\\\\\"add[z_infopfen]\\\\\" type=\\\\\"text\\\\\" id=\\\\\"add[z_infopfen]\\\\\" value=\\\\\"<?=stripSlashes(\$r[z_infopfen])?>\\\\\">\r\n            (如填写这里，将为字段的值)</td>\r\n        </tr>\r\n      </table></td>\r\n  </tr>\r\n','0','INT','','1','$tid','$tbname','0','','0','0','1','\r\n<input name=\\\\\"infopfen\\\\\" type=\\\\\"text\\\\\" id=\\\\\"infopfen\\\\\" value=\\\\\"<?=\$ecmsfirstpost==1?\\\\\"\\\\\":DoReqValue(\$mid,\\\\''infopfen\\\\'',stripSlashes(\$r[infopfen]))?>\\\\\" size=\\\\\"\\\\\">\r\n','0','','0','','0','0','','','','','','','0','0');");
	$empire->query("replace into `{$dbtbpre}enewsf` values(NULL,'infopfennum','评分人数','text','\r\n<input name=\\\\\"infopfennum\\\\\" type=\\\\\"text\\\\\" id=\\\\\"infopfennum\\\\\" value=\\\\\"<?=\$ecmsfirstpost==1?\\\\\"\\\\\":ehtmlspecialchars(stripSlashes(\$r[infopfennum]))?>\\\\\" size=\\\\\"\\\\\">\r\n','','1','1','1','\r\n  <tr bgcolor=\\\\\"#FFFFFF\\\\\"> \r\n    <td height=\\\\\"22\\\\\" valign=\\\\\"top\\\\\"><strong>[!--enews.name--]正则：</strong><br>\r\n      (<input name=\\\\\"textfield\\\\\" type=\\\\\"text\\\\\" id=\\\\\"textfield\\\\\" value=\\\\\"[!--infopfennum--]\\\\\" size=\\\\\"20\\\\\">)</td>\r\n    <td><table width=\\\\\"100%\\\\\" border=\\\\\"0\\\\\" cellspacing=\\\\\"1\\\\\" cellpadding=\\\\\"3\\\\\">\r\n        <tr> \r\n          <td><textarea name=\\\\\"add[zz_infopfennum]\\\\\" cols=\\\\\"60\\\\\" rows=\\\\\"10\\\\\" id=\\\\\"textarea\\\\\"><?=ehtmlspecialchars(stripSlashes(\$r[zz_infopfennum]))?></textarea></td>\r\n        </tr>\r\n        <tr> \r\n          <td><input name=\\\\\"add[z_infopfennum]\\\\\" type=\\\\\"text\\\\\" id=\\\\\"add[z_infopfennum]\\\\\" value=\\\\\"<?=stripSlashes(\$r[z_infopfennum])?>\\\\\">\r\n            (如填写这里，将为字段的值)</td>\r\n        </tr>\r\n      </table></td>\r\n  </tr>\r\n','0','INT','','1','$tid','$tbname','0','','0','0','1','\r\n<input name=\\\\\"infopfennum\\\\\" type=\\\\\"text\\\\\" id=\\\\\"infopfennum\\\\\" value=\\\\\"<?=\$ecmsfirstpost==1?\\\\\"\\\\\":DoReqValue(\$mid,\\\\''infopfennum\\\\'',stripSlashes(\$r[infopfennum]))?>\\\\\" size=\\\\\"\\\\\">\r\n','0','','0','','0','0','','','','','','','0','0');");

	return $num;
}

//单表开始处理
function upStartChangeSingleInfoTbStru($tid,$tbname){
	global $empire,$dbtbpre,$ecms_config;
	//建主表索引表
	upCreateInfoIndexTb($tbname,0);
	//建归档索引表
	upCreateInfoIndexTb($tbname,1);
	//开始更新主表
	upStartFieldInfoTb($tbname,0);
	//开始更新归档主表
	upStartFieldInfoTb($tbname,1);
	//开始信息副表
	$tbr=$empire->fetch1("select datatbs from {$dbtbpre}enewstable where tid='$tid' limit 1");
	if($tbr['datatbs'])
	{
		$dtbr=explode(',',$tbr['datatbs']);
		$count=count($dtbr);
		for($i=1;$i<$count-1;$i++)
		{
			upStartFieldInfoDataTb($tbname,$dtbr[$i],0);
		}
	}
	//开始归档副表
	upStartFieldInfoDataTb($tbname,0,1);
	//建未审核表
	upCreateInfoCheckTb($tbname);
	//公共表开始处理
	upStartFieldInfoPubTb();
	//采集表开始处理
	upStartFieldInfoCjTb($tbname);
	//评分字段处理
	$pfennum=upChangeAddPfenField($tid,$tbname);
	return $pfennum;
}

//单表结束处理
function upEndChangeSingleInfoTbStru($tid,$tbname){
	global $empire,$dbtbpre,$ecms_config;
	//信息主表
	upEndFieldInfoTb($tbname,0);
	//信息归档表
	upEndFieldInfoTb($tbname,1);
	//信息待审核表
	upEndFieldInfoTb($tbname,2);
}

//建索引表
function upCreateInfoIndexTb($tb,$ecms=0){
	global $empire,$dbtbpre,$ecms_config;
	if($ecms==1)//归档
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_doc_index';
	}
	else
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_index';
	}
	$empire->query(SetCreateTable("CREATE TABLE `".$tbname."` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `classid` smallint(5) unsigned NOT NULL default '0',
  `checked` tinyint(1) NOT NULL default '0',
  `newstime` int(10) unsigned NOT NULL default '0',
  `truetime` int(10) unsigned NOT NULL default '0',
  `lastdotime` int(10) unsigned NOT NULL default '0',
  `havehtml` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `classid` (`classid`),
  KEY `checked` (`checked`),
  KEY `newstime` (`newstime`),
  KEY `truetime` (`truetime`,`id`),
  KEY `havehtml` (`classid`,`truetime`,`havehtml`,`checked`,`id`)
) TYPE=MyISAM;",$ecms_config['db']['dbchar']));
}

//建未审核表
function upCreateInfoCheckTb($tb){
	global $empire,$dbtbpre,$ecms_config;
	$otbname=$dbtbpre."ecms_".$tb;
	$tbname=$otbname."_check";
	CopyEcmsTb($otbname,$tbname);
	$odtbname=$dbtbpre."ecms_".$tb."_data_1";
	$dtbname=$tbname."_data";
	CopyEcmsTb($odtbname,$dtbname);
}

//信息表开始字段处理
function upStartFieldInfoTb($tb,$ecms=0){
	global $empire,$dbtbpre,$ecms_config;
	if($ecms==1)//归档
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_doc';
	}
	else
	{
		$tbname=$dbtbpre.'ecms_'.$tb;
	}
	$empire->query("alter table ".$tbname." 
		change id id int(10) unsigned NOT NULL auto_increment,
		change classid classid smallint(5) unsigned NOT NULL default '0',
		change ttid ttid smallint(5) unsigned NOT NULL default '0',
		change onclick onclick int(10) unsigned NOT NULL default '0',
		change plnum plnum mediumint(8) unsigned NOT NULL default '0',
		change totaldown totaldown mediumint(8) unsigned NOT NULL default '0',
		change filename filename char(36) NOT NULL default '',
		change userid userid mediumint(8) unsigned NOT NULL default '0',
		change username username char(20) NOT NULL default '',
		add isurl tinyint(1) NOT NULL default '0',
		change truetime truetime int(10) unsigned NOT NULL default '0',
		change lastdotime lastdotime int(10) unsigned NOT NULL default '0',
		change groupid groupid smallint(5) unsigned NOT NULL default '0',
		change userfen userfen smallint(5) unsigned NOT NULL default '0',
		change titlefont titlefont char(14) NOT NULL default '',
		change stb stb tinyint(3) unsigned NOT NULL default '1',
		add fstb tinyint(3) unsigned NOT NULL default '1',
		add restb tinyint(3) unsigned NOT NULL default '1',
		change keyboard keyboard char(80) NOT NULL default '',
		change title title char(100) NOT NULL default '',
		change newstime newstime int(10) unsigned NOT NULL default '0',
		change titlepic titlepic char(120) NOT NULL default '',
		add INDEX(firsttitle),
		add INDEX(isgood),
		add INDEX(ispic),
		DROP INDEX truetime;");
}

//信息副表开始字段处理
function upStartFieldInfoDataTb($tb,$stb,$ecms=0){
	global $empire,$dbtbpre,$ecms_config;
	if($ecms==1)//归档
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_doc_data';
	}
	else
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_data_'.$stb;
	}
	$empire->query("alter table ".$tbname." 
		change id id int(10) unsigned NOT NULL,
		change classid classid smallint(5) unsigned NOT NULL default '0',
		add `keyid` char(255) NOT NULL default '',
		add `dokey` tinyint(1) NOT NULL default '0',
		add `newstempid` smallint(5) unsigned NOT NULL default '0',
		add `closepl` tinyint(1) NOT NULL default '0',
		add `haveaddfen` tinyint(1) NOT NULL default '0',
		add `infotags` char(80) NOT NULL default '';");
}

//信息公共表开始字段处理
function upStartFieldInfoPubTb(){
	global $empire,$dbtbpre,$ecms_config;
	if($_GET['tid'])
	{
		return '';
	}

	$empire->query("alter table {$dbtbpre}enewsinfovote 
		change id id int(10) unsigned NOT NULL,
		change classid classid smallint(5) unsigned NOT NULL default '0',
		add `pubid` bigint(16) unsigned NOT NULL default '0',
		add `votenum` int(10) unsigned NOT NULL default '0',
		change tempid tempid smallint(5) unsigned NOT NULL default '0',
		change width width int(10) unsigned NOT NULL default '0',
		change height height int(10) unsigned NOT NULL default '0',
		change infouptime infouptime int(10) unsigned NOT NULL default '0',
		change infodowntime infodowntime int(10) unsigned NOT NULL default '0',
		add `copyids` varchar(255) NOT NULL default '',
		add PRIMARY KEY(pubid);");
	
	//专题信息表
	$empire->query(SetCreateTable("CREATE TABLE `{$dbtbpre}enewsztinfo` (
  `zid` int(10) unsigned NOT NULL auto_increment,
  `ztid` smallint(5) unsigned NOT NULL default '0',
  `cid` smallint(5) unsigned NOT NULL default '0',
  `classid` smallint(5) unsigned NOT NULL default '0',
  `id` int(10) unsigned NOT NULL default '0',
  `newstime` int(10) unsigned NOT NULL default '0',
  `mid` smallint(5) unsigned NOT NULL default '0',
  `isgood` tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (`zid`),
  KEY `ztid` (`ztid`),
  KEY `cid` (`cid`),
  KEY `classid` (`classid`),
  KEY `id` (`id`),
  KEY `newstime` (`newstime`),
  KEY `mid` (`mid`),
  KEY `isgood` (`isgood`)
) TYPE=MyISAM;",$ecms_config['db']['dbchar']));
}

//信息采集表开始字段处理
function upStartFieldInfoCjTb($tb){
	global $empire,$dbtbpre,$ecms_config;
	$empire->query("alter table {$dbtbpre}ecms_infoclass_".$tb." 
		change classid classid int(10) unsigned NOT NULL default '0',
		DROP INDEX classid,
		add PRIMARY KEY(classid);");

	$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tb." 
		change id id bigint(20) unsigned NOT NULL auto_increment,	
		change classid classid int(10) unsigned NOT NULL default '0',
		change oldurl oldurl varchar(200) NOT NULL default '',
		change userid userid mediumint(8) unsigned NOT NULL default '0',
		change username username varchar(20) NOT NULL default '',
		change truetime truetime int(10) unsigned NOT NULL default '0',
		change keyboard keyboard varchar(100) NOT NULL default '',
		change title title varchar(100) NOT NULL default '',
		change titlepic titlepic varchar(120) NOT NULL default '',
		add INDEX(checked);");
}

//信息表结束字段处理
function upEndFieldInfoTb($tb,$ecms=0){
	global $empire,$dbtbpre,$ecms_config;
	if($ecms==1)//归档表
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_doc';
	}
	elseif($ecms==2)//待审核表
	{
		$tbname=$dbtbpre.'ecms_'.$tb.'_check';
	}
	else
	{
		$tbname=$dbtbpre.'ecms_'.$tb;
	}
	$empire->query("alter table ".$tbname." 
		change id id int(10) unsigned NOT NULL auto_increment,
		DROP INDEX checked,
		drop COLUMN keyid,
		drop COLUMN ztid,
		drop COLUMN checked,
		drop COLUMN dokey,
		drop COLUMN newstempid,
		drop COLUMN closepl,
		drop COLUMN haveaddfen,
		drop COLUMN votenum,
		drop COLUMN copyids,
		drop COLUMN infotags;");

	if(empty($_GET['pfennum']))
	{
		$empire->query("alter table ".$tbname." 
		drop COLUMN infopfen,
		drop COLUMN infopfennum;");
	}
}

//信息转换
function upChangeInfoTbData($add){
	global $empire,$public_r,$class_r,$fun_r,$dbtbpre,$etable_r,$ecms_config,$doline,$doretime;
	$doline=(int)$doline;
	$doretime=(int)$doretime;
	$isdoc=(int)$add['isdoc'];
	$tid=(int)$add['tid'];
	$pfennum=(int)$add['pfennum'];
	if(!$tid)
	{
		$firsttbr=$empire->fetch1("select tid from {$dbtbpre}enewstable order by tid limit 1");
		$tid=$firsttbr['tid'];
    }
	$tbr=$empire->fetch1("select tbname from {$dbtbpre}enewstable where tid='$tid'");
	if(!$tbr['tbname'])
	{
		printerror("ErrorUrl","history.go(-1)");
    }
	$start=(int)$add['start'];
	//表结构开始处理
	if($start==0&&$isdoc==0)
	{
		$pfennum=upStartChangeSingleInfoTbStru($tid,$tbr['tbname']);
	}
	//归档表
	if($isdoc)
	{
		$maintbname=$dbtbpre."ecms_".$tbr['tbname']."_doc";
	}
	else
	{
		$maintbname=$dbtbpre."ecms_".$tbr['tbname'];
	}
	$b=0;
	$sql=$empire->query("select * from ".$maintbname." where id>$start order by id limit ".$doline);
	while($r=$empire->fetch($sql))
	{
		$b=1;
		$new_start=$r[id];
		//主表处理
		$isurl=$r['titleurl']?1:0;
		$titleurl=GotoGetTitleUrl($r['classid'],$r['id'],$r['newspath'],$r['filename'],$r['groupid'],$isurl,$r['titleurl']);
		$uppfen='';
		if($pfennum)
		{
			$uppfen=",infopfen='$r[infopfen]',infopfennum='$r[infopfennum]'";
		}
		$empire->query("update ".$maintbname." set isurl='$isurl',titleurl='".addslashes($titleurl)."'".$uppfen." where id='$r[id]'");
		//副表处理
		if($isdoc)
		{
			$empire->query("replace into {$dbtbpre}ecms_".$tbr['tbname']."_doc_index(id,classid,checked,newstime,truetime,lastdotime,havehtml) values('$r[id]','$r[classid]','$r[checked]','$r[newstime]','$r[truetime]','$r[lastdotime]','$r[havehtml]');");
			$datatb=$dbtbpre."ecms_".$tbr['tbname']."_doc_data";
			$empire->query("update ".$datatb." set keyid='".addslashes($r[keyid])."',dokey='$r[dokey]',newstempid='$r[newstempid]',closepl='$r[closepl]',haveaddfen='$r[haveaddfen]',infotags='".addslashes($r[infotags])."' where id='$r[id]'");
		}
		else
		{
			$empire->query("replace into {$dbtbpre}ecms_".$tbr['tbname']."_index(id,classid,checked,newstime,truetime,lastdotime,havehtml) values('$r[id]','$r[classid]','$r[checked]','$r[newstime]','$r[truetime]','$r[lastdotime]','$r[havehtml]');");
			$datatb=$dbtbpre."ecms_".$tbr['tbname']."_data_".$r['stb'];
			$empire->query("update ".$datatb." set keyid='".addslashes($r[keyid])."',dokey='$r[dokey]',newstempid='$r[newstempid]',closepl='$r[closepl]',haveaddfen='$r[haveaddfen]',infotags='".addslashes($r[infotags])."' where id='$r[id]'");
			//未审核
			if(!$r['checked'])
			{
				MoveCheckInfoData($tbr['tbname'],1,$r['stb'],"id='$r[id]'");
			}
		}
		$pubid=ReturnInfoPubid($r['classid'],$r['id'],$tid);
		//公共表处理
		$pubnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsinfovote where id='$r[id]' and classid='$r[classid]' limit 1");
		if($pubnum)
		{
			$empire->query("update {$dbtbpre}enewsinfovote set pubid='$pubid',copyids='".addslashes($r[copyids])."',votenum='$r[votenum]' where id='$r[id]' and classid='$r[classid]' limit 1;");
		}
		else
		{
			if($r['copyids'])
			{
				$empire->query("insert into {$dbtbpre}enewsinfovote(pubid,classid,id,copyids) values('$pubid','$r[classid]','$r[id]','".addslashes($r[copyids])."');");
			}
		}
		//专题信息表处理
		if($r['ztid']&&$r['ztid']!='|')
		{
			$ztr=explode('|',$r['ztid']);
			$ztcount=count($ztr);
			for($zi=1;$zi<$ztcount-1;$zi++)
			{
				$trueztid=(int)$ztr[$zi];
				if($trueztid)
				{
					$ztmid=$class_r[$r['classid']]['modid'];
					$empire->query("insert into {$dbtbpre}enewsztinfo(ztid,cid,classid,id,newstime,mid,isgood) values('$trueztid',0,'$r[classid]','$r[id]','$r[newstime]','$ztmid',0);");
				}
			}
		}
	}
	if(empty($b))
	{
		if(!$isdoc)
		{
			echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doretime.";url=index.php?ecms=ChangeInfoTbData&pfennum=$pfennum&tid=$tid&isdoc=1\">数据表 <b>".$tbr['tbname']."</b> 主表数据转换完毕，正在进入下一个归档表数据转换......";
			exit();
		}
		//表结构结束处理
		upEndChangeSingleInfoTbStru($tid,$tbr['tbname']);
		//最后一个表
		$nexttbr=$empire->fetch1("select tid from {$dbtbpre}enewstable where tid>$tid order by tid limit 1");
		if(!$nexttbr['tid'])
		{
			echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doretime.";url=index.php?ecms=ChangePlTbData\">所有信息数据表全部数据转换完毕! 正进入评论数据转换......";
			db_close();
			$empire=null;
			exit();
		}
		echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doretime.";url=index.php?ecms=ChangeInfoTbData&tid=".$nexttbr['tid']."\">数据表 <b>".$tbr['tbname']."</b> 全部数据转换完毕，正在进入下一个表转换......";
		exit();
	}
	echo"<link rel=\"stylesheet\" href=\"../data/images/css.css\" type=\"text/css\"><meta http-equiv=\"refresh\" content=\"".$doretime.";url=index.php?ecms=ChangeInfoTbData&pfennum=$pfennum&isdoc=$isdoc&tid=$tid&start=$new_start\">数据表 <b>".$tbr['tbname']."</b>".($isdoc?"(归档表)":"").": 转换一组数据完毕，正在进入下一组......(ID:<font color=red><b>".$new_start."</b></font>)";
	exit();
}

?>