finish update

This commit is contained in:
xiaomlove
2021-02-01 20:19:39 +08:00
parent 1347567c5d
commit 5ce5fc3e99
6 changed files with 201 additions and 156 deletions

File diff suppressed because one or more lines are too long

View File

@@ -51,7 +51,7 @@ class DB
}
$this->driver->query("SET NAMES UTF8");
$this->driver->query("SET collation_connection = 'utf8_general_ci'");
$this->driver->query("SET sql_mode=''");
// $this->driver->query("SET sql_mode=''");
$this->isConnected = true;
return true;
}

View File

@@ -15,7 +15,7 @@ class Install
protected $steps = ['环境检测', '添加 .env 文件', '创建数据表', '导入数据', '创建管理员账号'];
protected $initializeTables = [
'adminpanel', 'agent_allowed_exception', 'agent_allowed_family', 'allowedemails', 'audiocodecs', 'avps', 'bannedemails', 'categories',
'adminpanel', 'agent_allowed_exception', 'agent_allowed_family', 'allowedemails', 'audiocodecs', 'bannedemails', 'categories',
'caticons', 'codecs', 'countries', 'downloadspeed', 'faq', 'isp', 'language', 'media', 'modpanel', 'processings', 'rules', 'schools',
'searchbox', 'secondicons', 'sources', 'standards', 'stylesheets', 'sysoppanel', 'teams', 'torrents_state', 'uploadspeed', 'agent_allowed_family',
];
@@ -32,6 +32,11 @@ class Install
$this->currentStep = min(intval($_REQUEST['step'] ?? 1) ?: 1, count($this->steps) + 1);
}
public function listShouldInitializeTables()
{
return $this->initializeTables;
}
public function currentStep()
{
return $this->currentStep;
@@ -215,7 +220,7 @@ class Install
],
];
$requireDirs = [
'main' => ['bitbucket', 'torrent_dir'],
// 'main' => ['bitbucket', 'torrent_dir'],
'attachment' => ['savedirectory', ],
];
$symbolicLinks = [];

View File

@@ -24,30 +24,33 @@ class Update extends Install
return ROOT_PATH . 'public/update';
}
public function listShouldAlterTableTableRows()
public function listTableFieldsFromCreateTable($createTableSql)
{
$tables = $this->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',
];
}
$arr = preg_split("/[\r\n]+/", $createTableSql);
$result = [];
foreach ($arr as $value) {
$value = trim($value);
if (substr($value, 0, 1) != '`') {
continue;
}
$pos = strpos($value, '`', 1);
$field = substr($value, 1, $pos - 1);
$result[$field] = rtrim($value, ',');
}
return $result;
}
public function listTableFieldsFromDb($table)
{
$sql = "desc $table";
$res = sql_query($sql);
$data = [];
while ($row = mysql_fetch_assoc($res)) {
$data[$row['Field']] = $row;
}
return $data;
}
public function importInitialData($sqlFile = '')
{
if (empty($sqlFile)) {

View File

@@ -207,7 +207,7 @@ if ($currentStep == 5) {
</form>
</div>
</div>
<div class="mt-10 text-center">
<div class="m-10 text-center">
欢迎使用 NexusPHP 安装程序,如有疑问,点击<a href="http://nexusphp.org/" target="_blank" class="text-blue-500 p-1">这里</a>获取帮助。
</div>
</body>

View File

@@ -55,10 +55,102 @@ if ($currentStep == 2) {
if ($currentStep == 3) {
$pass = true;
$shouldCreateTable = $update->listShouldCreateTable();
$createTables = $update->listAllTableCreate();
$existsTables = $update->listExistsTable();
$tableRows = [];
$toCreateTable = $toAlterTable = $toUpdateTable = [];
foreach ($createTables as $table => $tableCreate) {
//Table not exists
if (!in_array($table, $existsTables)) {
$tableRows[] = [
"label" => "Table: $table",
"required" => "exists",
"current" => "",
"result" => 'NO',
];
$toCreateTable[$table] = $tableCreate;
continue;
}
$tableShouldHaveFields = $update->listTableFieldsFromCreateTable($tableCreate);
$tableHaveFields = $update->listTableFieldsFromDb($table);
foreach ($tableShouldHaveFields as $field => $fieldCreate) {
if (!isset($tableHaveFields[$field])) {
//Field not exists
$tableRows[] = [
"label" => "Field: $table.$field",
"required" => "exists",
"current" => "",
"result" => 'NO',
];
$toAlterTable[$table][$field] = "add column $fieldCreate";
continue;
}
$fieldInfo = $tableHaveFields[$field];
//Field invalid
if ($fieldInfo['Type'] == 'datetime' && $fieldInfo['Default'] == '0000-00-00 00:00:00') {
$tableRows[] = [
'label' => "Field: $table.$field",
'required' => 'default null',
'current' => '0000-00-00 00:00:00',
'result' => 'NO',
];
$toAlterTable[$table][$field] = "modify $fieldCreate";
$toUpdateTable[$table][$field] = "$field = null";
continue;
}
//Field invalid
if ($fieldInfo['Null'] == 'NO' && $fieldInfo['Default'] === null && $fieldInfo['Key'] != 'PRI') {
$typePrefix = $fieldInfo['Type'];
if (($pos = strpos($typePrefix, '(')) !== false) {
$typePrefix = substr($typePrefix, 0, $pos);
}
if (preg_match('/varchar/', $typePrefix)) {
$tableRows[] = [
'label' => "Field: $table.$field",
'required' => "default ''",
'current' => 'null',
'result' => 'NO',
];
$toAlterTable[$table][$field] = "modify $field {$fieldInfo['Type']} not null default ''";
continue;
}
if (preg_match('/int/', $typePrefix)) {
$tableRows[] = [
'label' => "Field: $table.$field",
'required' => "default 0",
'current' => 'null',
'result' => 'NO',
];
$toAlterTable[$table][$field] = "modify $field {$fieldInfo['Type']} not null default 0";
continue;
}
}
}
}
while ($isPost) {
try {
$update->createTable($shouldCreateTable);
sql_query('SET sql_mode=(SELECT REPLACE(@@sql_mode,"NO_ZERO_DATE", ""))');
foreach ($toCreateTable as $query) {
$update->doLog("[CREATE TABLE] $query");
sql_query($query);
}
foreach ($toAlterTable as $table => $modies) {
$query = "alter table $table " . implode(', ', $modies);
$update->doLog("[ALTER TABLE] $query");
sql_query($query);
}
foreach ($toUpdateTable as $table => $updates) {
$whereArr = [];
foreach ($updates as $updateField => $updateValue) {
$whereArr[] = "($updateField = '0000-00-00 00:00:00')";
}
if (!empty($whereArr)) {
$query = sprintf("update %s set %s where (%s)", $table, implode(', ', $updates), implode(' or ', $whereArr));
$update->doLog("[UPDATE TABLE] $query");
sql_query($query);
}
}
$update->nextStep();
} catch (\Exception $exception) {
$error = $exception->getMessage();
@@ -68,39 +160,6 @@ if ($currentStep == 3) {
}
}
//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'];
@@ -111,7 +170,6 @@ if ($currentStep == 4) {
try {
$update->createSymbolicLinks($symbolicLinks);
$update->saveSettings($settings);
$update->importInitialData();
$update->nextStep();
} catch (\Exception $e) {
$error = $e->getMessage();
@@ -120,24 +178,6 @@ if ($currentStep == 4) {
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'] ?? ''],
];
}
?>
<!doctype html>
@@ -166,24 +206,22 @@ if ($currentStep == 5) {
echo $update->renderForm($envFormControls);
} elseif ($currentStep == 3) {
echo '<h1 class="mb-4 text-lg font-bold">需要建以下数据表</h1>';
if (empty($shouldCreateTable)) {
echo '<div class="text-green-600 text-center">恭喜,需要的表均已创建!</div>';
echo '<h1 class="mb-4 text-lg font-bold">需要修改或创建以下数据表(字段)</h1>';
if (empty($tableRows)) {
echo '<div class="text-green-600 text-center">恭喜,需要的表(字段)均符合要求!</div>';
} else {
echo sprintf('<div class="h-64 text-left inline-block w-2/3"><code class="bolck w-px-100">%s</code></div>', implode(', ', array_keys($shouldCreateTable)));
echo $update->renderTable($header, $tableRows);
}
} elseif ($currentStep == 4) {
echo $update->renderTable($header, $tableRows);
echo '<div class="text-blue-500 pt-10">';
echo sprintf('这一步会把 <code>%s</code> 的数据合并到 <code>%s</code>, 然后插入数据库中。', $tableRows[1]['label'], $tableRows[0]['label']);
echo '</div>';
} elseif ($currentStep == 5) {
echo $update->renderForm($userFormControls, '1/3', '1/4', '3/4');
} elseif ($currentStep > $maxStep) {
echo '<div class="text-green-900 text-6xl p-10">恭喜,一切就绪!</div>';
echo '<div class="mb-6">有问题可查阅安装日志:<code>' . $update->getLogFile() . '</code></div>';
echo '<div class="mb-6">有问题可查阅升级日志:<code>' . $update->getLogFile() . '</code></div>';
echo '<div class="text-red-500">为安全起见,请删除以下目录</div>';
echo '<div class="text-red-500"><code>' . $update->getInsallDirectory() . '</code></div>';
echo '<div class="text-red-500"><code>' . $update->getUpdateDirectory() . '</code></div>';
}
echo'</div>';
@@ -207,7 +245,7 @@ if ($currentStep == 5) {
</form>
</div>
</div>
<div class="mt-10 text-center">
<div class="m-10 text-center">
欢迎使用 NexusPHP 升级程序(v1.5 ~ v1.6),如有疑问,点击<a href="http://nexusphp.org/" target="_blank" class="text-blue-500 p-1">这里</a>获取帮助。
</div>
</body>