コード例 #1
0
 /**
  * 执行模块升级脚本
  * @param type $module 模块名称
  * @return boolean
  */
 public function upgrade($module)
 {
     //检查模块是否安装
     if ($this->isInstall($module) == false) {
         $this->error = '模块没进行安装,无法进行模块升级!';
         return -10022;
     }
     //取得模块信息
     $info = $this->where(array('module' => $module))->find();
     if (empty($info)) {
         $this->error = '获取模块信息错误!';
         return -10023;
     }
     //模块路径
     $base = $this->appPath . $module . '/';
     //SQL脚本文件
     $exec = $base . 'Upgrade/upgrade.sql';
     //phpScript
     $phpScript = $base . 'Upgrade/Upgrade.class.php';
     //判断是否有数据库升级脚本
     if (file_exists($exec)) {
         //获取全部参数
         preg_match_all("/#\\[version=(.*?)\\](.+?)#\\[\\/version\\]/ism", file_get_contents($exec), $match);
         //遍历
         foreach ($match[1] as $index => $version) {
             //比较模块版本,仅处理小于或等于当前版本
             if ($version && version_compare($version, $info['version'], '>=')) {
                 //记录最后一个更新的版本号
                 $upgradeVersion = $version;
                 $sql = $this->sqlSplit($sql, C("DB_PREFIX"));
                 if (!empty($sql) && is_array($sql)) {
                     foreach ($sql as $sql_split) {
                         $this->execute($sql_split);
                     }
                 }
             }
         }
     }
     //判断是否有升级程序脚本
     if (file_exists($phpScript)) {
         require_cache($phpScript);
         if (class_exists('Upgrade')) {
             $Upgrade = new Upgrade();
             if ($Upgrade->run() == false) {
                 $this->error = $Upgrade->getError() ? $Upgrade->getError() : "执行模块升级脚本错误,升级未完成!";
                 return -10024;
             }
         }
     }
     //加载配置
     $config = $this->getModuleInstallConfig($module);
     if (!empty($config)) {
         //更新版本号
         $this->where(array('module' => $module))->save(array('version' => $config['version'], 'updatedate' => date('Y-m-d')));
     }
     return true;
 }