从x1.5或x2RC升级到x2正式版的过程是覆盖原文件,然后将update.php文件上传到install目录,那么我们来分析下升级的执行过程
由于我们采用的是覆盖原来的文件,那么在执行的时候 调用的配置文件还是原先的配置文件(config目录下)
首先初始化环境变量
include_once('../source/class/class_core.php');
include_once('../source/function/function_core.php');
@set_time_limit(0);
$cachelist=array();
$discuz= & discuz_core::instance();
$discuz->cachelist= $cachelist;
$discuz->init_cron=false;
$discuz->init_setting=false;
$discuz->init_user=false;
$discuz->init_session=false;
$discuz->init_misc=false;
$discuz->init();
$config=array(
'dbcharset' =>$_G['config']['db']['1']['dbcharset'],
'charset' =>$_G['config']['output']['charset'],
'tablepre' =>$_G['config']['db']['1']['tablepre']
);
其中在升级过程中,调用的还是原来的表前缀。检查update.lock文件是否存在
$devmode=file_exists(DISCUZ_ROOT.'./install/data/install_dev.sql');
$sqlfile=DISCUZ_ROOT.($devmode ? './install/data/install_dev.sql' : './install/data/install.sql')
调用x2的标准数据库结果文件,用于在x1.5或x2RC版中进行数据表结果的创建和必要的初始化数据
当进行升级操作的时候,点击“准备完毕,开始升级”访问路径“update.php?step=prepare”
准备完毕之后,show_msg('准备完毕,进入下一步数据库结构升级', $theurl.'?step=sql'); show_msg函数用于提示执行的过程和进入下一升级过程
$_GET['step'] == 'sql' 时 ,进入数据库结构升级,在原有的基础上对数据库结构进行升级
当数据结构升级完毕
if($i>=$count_i) {
show_msg('数据库结构升级完毕,进入下一步数据升级操作', $theurl.'?step=data');
}
进入数据转换过程
$_GET['step'] == 'data'
在数据转换过程中通过 类似这种方式show_msg("实名功能升级完毕", "$theurl?step=data&op=$nextop"),循环的进行数据转换,其中容易出问题的地方是附件的转换$_GET['op'] == 'forumattach'
$limit=10000;
$start= !empty($_GET['start']) ? $_GET['start'] : 0;
$needupgrade=DB::query("SELECT COUNT(*) FROM ".DB::table('forum_attachmentfield'), 'SILENT');
$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('forum_attachment'));
if($needupgrade && $count) {
if(!$start) {
for($i=0;$i<10;$i++) {
DB::query("TRUNCATE ".DB::table('forum_attachment_'.$i));
}
}
$query=DB::query("SELECT a.*,af.description FROM ".DB::table('forum_attachment')." a
LEFT JOIN ".DB::table('forum_attachmentfield')." af USING(aid)
ORDER BY aid LIMIT $start, $limit");
if(DB::num_rows($query)) {
while($row=DB::fetch($query)) {
$row=daddslashes($row);
$tid= (string)$row['tid'];
$tableid= $tid{strlen($tid)-1};
DB::update('forum_attachment', array('tableid' =>$tableid), array('aid' =>$row['aid']));
DB::insert('forum_attachment_'.$tableid, array(
'aid' =>$row['aid'],
'tid' =>$row['tid'],
'pid' =>$row['pid'],
'uid' =>$row['uid'],
'dateline' =>$row['dateline'],
'filename' =>$row['filename'],
'filesize' =>$row['filesize'],
'attachment' =>$row['attachment'],
'remote' =>$row['remote'],
'description' =>$row['description'],
'readperm' =>$row['readperm'],
'price' =>$row['price'],
'isimage' =>$row['isimage'],
'width' =>$row['width'],
'thumb' =>$row['thumb'],
'picid' =>$row['picid'],
));
}
$start += $limit;
show_msg("论坛附件表升级中 ... $start/$count", "$theurl?step=data&op=forumattach&start=$start");
}
DB::query("DROP TABLE `".DB::table('forum_attachmentfield')."`");
DB::query("ALTER TABLE ".DB::table('forum_attachment')."
DROP `width`,
DROP `dateline`,
DROP `readperm`,
DROP `price`,
DROP `filename`,
DROP `filetype`,
DROP `filesize`,
DROP `attachment`,
DROP `isimage`,
DROP `thumb`,
DROP `remote`,
DROP `picid`
");
}
show_msg("论坛附件表升级完毕", "$theurl?step=data&op=$nextop");
程序先是对forum_attachmentfield、forum_attachment表进行查询统计 这里主要是针对x1.5的处理,如果$needupgrade && $count都成立,对附件表 attachment0_9(这在数据结构升级中以完成处理)进行滞空。然后,对两表forum_attachmentfield、forum_attachment进行关联查询将数据插入到x2的数据表中,由于x2的附件表进行了分表 所以在插入附件表的时候要进行处理
$tid = (string)$row['tid'];$tableid = $tid{strlen($tid)-1}; 取得每个主题最后一位,从而插入到对应的附件表中
附件转换完成之后。 用户检验附件升级是否成功可以查看 attachment0_9中的数据是否存在,x1.5的附件表是否还存在。如果从x2RC版升级到X2正式版,在升级的时候x1.5的forum_attachmentfield表还存在的话,会将 attachment0_9表中的数据给清空这样会导致附件转换失败
$_GET['step'] == 'delete' 将进入数据库冗余数据进行分析并显示,当用户提交删除之后
进入$_POST['delsubmit']对表进行删除,如果没有多余数据或者用户忽略
将进入$_GET['step'] == 'style' 对样式进行更新
最后进行缓存更新$_GET['step'] == 'cache' 到此升级以完成