diff --git a/application/index/controller/Admin.php b/application/index/controller/Admin.php index bebe45de..f7380f13 100644 --- a/application/index/controller/Admin.php +++ b/application/index/controller/Admin.php @@ -29,12 +29,44 @@ class Admin extends Controller{ } public function index(){ + if($this->adminObj->checkDbVersion()){ + $this->redirect(url('/Admin/UpdateDb','','')); + exit(); + } return view('admin_index', [ 'options' => $this->siteOptions, 'statics' => $this->adminObj->getStatics(), ]); } + public function UpdateDb(){ + if(!$this->adminObj->checkDbVersion()){ + $this->redirect(url('/Admin','','')); + exit(); + } + echo ""; + echo "准备升级数据库,当前数据库版本:".$this->adminObj->dbVerInfo["now"]; + echo ", 目标数据库版本:".$this->adminObj->dbVerInfo["require"].";
"; + $updatePath = ROOT_PATH . "update_".$this->adminObj->dbVerInfo["now"]."to".$this->adminObj->dbVerInfo["require"].".sql"; + if(!file_exists($updatePath)){ + die("数据库更新文件(".$updatePath.")不存在,升级中止."); + } + echo "获取升级SQL文件(".$updatePath.")
"; + $updateContent = file_get_contents($updatePath); + echo "将执行以下指令:
"; + echo "".htmlspecialchars($updateContent)."
"; + $sqlSingle = explode(";", $updateContent); + foreach ($sqlSingle as $key => $value){ + if(empty($value)){ + continue; + } + if(!Db::execute($value)){ + echo "执行$value 时出现错误
"; + } + } + echo "升级完成,返回管理面板"; + } + public function Setting(){ return view('basic_setting', [ 'options' => $this->siteOptions, diff --git a/application/index/model/AdminHandler.php b/application/index/model/AdminHandler.php index bb3ebe19..0fa28a24 100644 --- a/application/index/model/AdminHandler.php +++ b/application/index/model/AdminHandler.php @@ -14,11 +14,22 @@ class AdminHandler extends Model{ public $pageNow; public $pageTotal; public $dataTotal; + public $dbVerInfo; public function __construct($options){ $this->siteOptions = $options; } + public function checkDbVersion(){ + $versionInfo = json_decode(@file_get_contents(ROOT_PATH. "application/version.json"),true); + $dbVerNow = Option::getValue("database_version"); + if(!isset($versionInfo["db_version"]) || $dbVerNow < (int)$versionInfo["db_version"]){ + $this->dbVerInfo = array('now' => $dbVerNow, 'require' => $versionInfo["db_version"]); + return true; + } + return false; + } + public function getStatics(){ $statics["fileNum"] = Db::name('files')->count(); $statics["privateShareNum"] = Db::name('shares')->where("type","private")->count(); diff --git a/application/version.json b/application/version.json index 3c509e4d..fcc78cb2 100644 --- a/application/version.json +++ b/application/version.json @@ -1 +1 @@ -{"type":"","version":"1.0.2","version_id":3} \ No newline at end of file +{"type":"","version":"1.0.2","version_id":3,"db_version":2} \ No newline at end of file