從 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' 到此升級以完成