Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

x2升级程序(update.php)解析

[复制链接]
evenzhou 发表于 2011-6-7 21:54:38 | 显示全部楼层 |阅读模式
从x1.5或x2RC升级到x2正式版的过程是覆盖原文件,然后将update.php文件上传到install目录,那么我们来分析下升级的执行过程
由于我们采用的是覆盖原来的文件,那么在执行的时候 调用的配置文件还是原先的配置文件(config目录下)
首先初始化环境变量
  1. include_once('../source/class/class_core.php');
  2. include_once('../source/function/function_core.php');
  3. @set_time_limit(0);
  4. $cachelist = array();
  5. $discuz = & discuz_core::instance();
  6. $discuz->cachelist = $cachelist;
  7. $discuz->init_cron = false;
  8. $discuz->init_setting = false;
  9. $discuz->init_user = false;
  10. $discuz->init_session = false;
  11. $discuz->init_misc = false;
  12. $discuz->init();
  13. $config = array(
  14. 'dbcharset' => $_G['config']['db']['1']['dbcharset'],
  15. 'charset' => $_G['config']['output']['charset'],
  16. 'tablepre' => $_G['config']['db']['1']['tablepre']
  17. );
复制代码

其中在升级过程中,调用的还是原来的表前缀。检查update.lock文件是否存在
  1. $devmode = file_exists(DISCUZ_ROOT.'./install/data/install_dev.sql');
  2. $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' 时 ,进入数据库结构升级,在原有的基础上对数据库结构进行升级
当数据结构升级完毕
  1. if($i>=$count_i) {
  2.   show_msg('数据库结构升级完毕,进入下一步数据升级操作', $theurl.'?step=data');
  3. }
复制代码

进入数据转换过程
$_GET['step'] == 'data'
在数据转换过程中通过 类似这种方式show_msg("实名功能升级完毕", "$theurl?step=data&op=$nextop"),循环的进行数据转换,其中容易出问题的地方是附件的转换$_GET['op'] == 'forumattach'
  1. $limit = 10000;
  2.   $start = !empty($_GET['start']) ? $_GET['start'] : 0;
  3.   $needupgrade = DB::query("SELECT COUNT(*) FROM ".DB::table('forum_attachmentfield'), 'SILENT');
  4.   $count = DB::result_first("SELECT COUNT(*) FROM ".DB::table('forum_attachment'));
  5.   if($needupgrade && $count) {
  6.    if(!$start) {
  7.     for($i = 0;$i < 10;$i++) {
  8.      DB::query("TRUNCATE ".DB::table('forum_attachment_'.$i));
  9.     }
  10.    }
  11.    $query = DB::query("SELECT a.*,af.description FROM ".DB::table('forum_attachment')." a
  12.     LEFT JOIN ".DB::table('forum_attachmentfield')." af USING(aid)
  13.     ORDER BY aid LIMIT $start, $limit");
  14.    if(DB::num_rows($query)) {
  15.     while($row = DB::fetch($query)) {
  16.      $row = daddslashes($row);
  17.      $tid = (string)$row['tid'];
  18.      $tableid = $tid{strlen($tid)-1};
  19.      DB::update('forum_attachment', array('tableid' => $tableid), array('aid' => $row['aid']));
  20.      DB::insert('forum_attachment_'.$tableid, array(
  21.       'aid' => $row['aid'],
  22.       'tid' => $row['tid'],
  23.       'pid' => $row['pid'],
  24.       'uid' => $row['uid'],
  25.       'dateline' => $row['dateline'],
  26.       'filename' => $row['filename'],
  27.       'filesize' => $row['filesize'],
  28.       'attachment' => $row['attachment'],
  29.       'remote' => $row['remote'],
  30.       'description' => $row['description'],
  31.       'readperm' => $row['readperm'],
  32.       'price' => $row['price'],
  33.       'isimage' => $row['isimage'],
  34.       'width' => $row['width'],
  35.       'thumb' => $row['thumb'],
  36.       'picid' => $row['picid'],
  37.      ));
  38.     }
  39.     $start += $limit;
  40.     show_msg("论坛附件表升级中 ... $start/$count", "$theurl?step=data&op=forumattach&start=$start");
  41.    }
  42.    DB::query("DROP TABLE `".DB::table('forum_attachmentfield')."`");
  43.    DB::query("ALTER TABLE ".DB::table('forum_attachment')."
  44.     DROP `width`,
  45.     DROP `dateline`,
  46.     DROP `readperm`,
  47.     DROP `price`,
  48.     DROP `filename`,
  49.     DROP `filetype`,
  50.     DROP `filesize`,
  51.     DROP `attachment`,
  52.     DROP `isimage`,
  53.     DROP `thumb`,
  54.     DROP `remote`,
  55.     DROP `picid`
  56.    ");
  57.   }
  58.   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' 到此升级以完成

评分

1

查看全部评分

otherbank 发表于 2011-8-24 11:47:23 | 显示全部楼层
顶,学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-4-19 17:58 , Processed in 0.111097 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表