diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php index 0f6b23f2..f982680c 100644 --- a/nexus/Install/Install.php +++ b/nexus/Install/Install.php @@ -6,11 +6,11 @@ use Nexus\Database\DB; class Install { - private $currentStep; + protected $currentStep; - private $minimumPhpVersion = '7.2.0'; + protected $minimumPhpVersion = '7.2.0'; - private $progressKeyPrefix = '__step'; + protected $progressKeyPrefix = '__step'; protected $steps = ['环境检测', '添加 .env 文件', '创建数据表', '导入数据', '创建管理员账号']; @@ -97,7 +97,6 @@ class Install public function listExistsTable() { - dbconn(false, false); $sql = 'show tables'; $res = sql_query($sql); $data = []; diff --git a/nexus/Install/Update.php b/nexus/Install/Update.php new file mode 100644 index 00000000..e1b940e4 --- /dev/null +++ b/nexus/Install/Update.php @@ -0,0 +1,77 @@ +listExistsTable(); + $data = []; + foreach ($tables as $table) { + $sql = "desc $table"; + $res = sql_query($sql); + while ($row = mysql_fetch_assoc($res)) { + if ($row['Type'] == 'datetime' && $row['Default'] == '0000-00-00 00:00:00') { + $data[$table][] = $row['Field']; + $data[] = [ + 'label' => "$table." . $row['Field'], + 'required' => 'default null', + 'current' => '0000-00-00 00:00:00', + 'result' => 'NO', + ]; + } + } + } + return $data; + } + + + public function importInitialData($sqlFile = '') + { + if (empty($sqlFile)) { + $sqlFile = ROOT_PATH . '_db/dbstructure_v1.6.sql'; + } + $string = file_get_contents($sqlFile); + $pattern = "/INSERT INTO `(\w+)` VALUES \(.*\);\n/i"; + preg_match_all($pattern, $string, $matches, PREG_SET_ORDER); + foreach ($matches as $match) { + $table = $match[1]; + $sql = trim($match[0]); + if (!in_array($table, $this->initializeTables)) { + continue; + } + //if table not empty, skip + $count = get_row_count($table); + if ($count > 0) { + $this->doLog("[IMPORT DATA] $table, not empty, skip"); + continue; + } + $this->doLog("[IMPORT DATA] $table, $sql"); + sql_query("truncate table $table"); + sql_query($sql); + } + return true; + } +} \ No newline at end of file diff --git a/nexus/Install/update/update.php b/nexus/Install/update/update.php new file mode 100644 index 00000000..4758a58c --- /dev/null +++ b/nexus/Install/update/update.php @@ -0,0 +1,219 @@ +currentStep(); +$maxStep = $update->maxStep(); +if (!$update->canAccessStep($currentStep)) { + $update->gotoStep(1); +} +$error = $copy = ''; + +//step 1 +if ($currentStep == 1) { + $requirements = $update->listRequirementTableRows(); + $pass = $requirements['pass']; + if ($isPost) { + $update->nextStep(); + } +} + +if ($currentStep == 2) { + $envExampleFile = "$rootpath.env.example"; + $envExampleData = readEnvFile($envExampleFile); + $envFormControls = $update->listEnvFormControls(); + $newData = array_column($envFormControls, 'value', 'name'); + while ($isPost) { + try { + $update->createEnvFile($_POST); + $update->nextStep(); + } catch (\Exception $exception) { + $error = $exception->getMessage(); + break; + } + break; + } + $tableRows = [ + [ + 'label' => '.env.example', + 'required' => 'exists && readable', + 'current' => $envExampleFile, + 'result' => $update->yesOrNo(file_exists($envExampleFile) && is_readable($envExampleFile)), + ], + ]; + $fails = array_filter($tableRows, function ($value) {return $value['result'] == 'NO';}); + $pass = empty($fails); +} + +if ($currentStep == 3) { + $pass = true; + $shouldCreateTable = $update->listShouldCreateTable(); + while ($isPost) { + try { + $update->createTable($shouldCreateTable); + $update->nextStep(); + } catch (\Exception $exception) { + $error = $exception->getMessage(); + break; + } + break; + } +} + +//if ($currentStep == 4) { +// $pass = true; +// while (true) { +// $shouldAlterTable = listShouldAlterTable(); +// if ($isPost) { +// if (!empty($shouldAlterTable)) { +// try { +// sql_query('SET sql_mode=(SELECT REPLACE(@@sql_mode,"NO_ZERO_DATE", ""));'); +// foreach ($shouldAlterTable as $table => $fields) { +// $sqlAlter = "alter table $table"; +// $sqlUpdate = "update $table"; +// $updateWhere = []; +// foreach ($fields as $field) { +// $sqlAlter .= " modify $field datetime default null,"; +// $sqlUpdate .= " set $field = null,"; +// $updateWhere[] = "$field = '0000-00-00 00:00:00'"; +// } +// $sqlAlter = rtrim($sqlAlter, ','); +// $sqlUpdate = rtrim($sqlUpdate, ',') . " where " . implode(' or ', $updateWhere); +// sql_query($sqlUpdate); +// sql_query($sqlAlter); +// } +// } catch (\Exception $e) { +// $error = $e->getMessage(); +// break; +// } +// } +// goStep($currentStep + 1); +// } +// break; +// } +//} + +if ($currentStep == 4) { + $settingTableRows = $update->listSettingTableRows(); + $settings = $settingTableRows['settings']; + $symbolicLinks = $settingTableRows['symbolic_links']; + $tableRows = $settingTableRows['table_rows']; + $pass = $settingTableRows['pass']; + while ($isPost) { + try { + $update->createSymbolicLinks($symbolicLinks); + $update->saveSettings($settings); + $update->importInitialData(); + $update->nextStep(); + } catch (\Exception $e) { + $error = $e->getMessage(); + break; + } + break; + } +} + +if ($currentStep == 5) { + if ($isPost) { + try { + $update->createAdministrator($_POST['username'], $_POST['email'], $_POST['password'], $_POST['confirm_password']); + $update->nextStep(); + } catch (\Exception $exception) { + $error = $exception->getMessage(); + } + } + $pass = true; + $userFormControls = [ + ['label' => '用户名', 'name' => 'username', 'value' => $_POST['username'] ?? ''], + ['label' => '邮箱', 'name' => 'email', 'value' => $_POST['email'] ?? ''], + ['label' => '密码', 'name' => 'password', 'value' => $_POST['password'] ?? ''], + ['label' => '确认密码', 'name' => 'confirm_password', 'value' => $_POST['confirm_password'] ?? ''], + ]; +} +?> + + + +
+ + + + +