2021-02-01 02:33:45 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Nexus\Install;
|
|
|
|
|
|
2021-06-13 22:21:42 +08:00
|
|
|
use App\Models\Attendance;
|
2021-05-29 21:48:50 +08:00
|
|
|
use App\Models\Category;
|
2021-06-11 20:32:57 +08:00
|
|
|
use App\Models\Exam;
|
|
|
|
|
use App\Models\ExamUser;
|
2021-05-29 21:48:50 +08:00
|
|
|
use App\Models\Icon;
|
2021-06-06 13:41:05 +08:00
|
|
|
use App\Models\Setting;
|
2021-06-11 20:32:57 +08:00
|
|
|
use App\Repositories\ExamRepository;
|
2021-06-06 13:41:05 +08:00
|
|
|
use Illuminate\Support\Str;
|
2021-06-08 10:42:39 +08:00
|
|
|
use Nexus\Database\NexusDB;
|
2021-03-19 00:34:11 +08:00
|
|
|
|
2021-02-01 02:33:45 +08:00
|
|
|
class Update extends Install
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected $steps = ['环境检测', '添加 .env 文件', '修改&创建数据表', '导入数据'];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getLogFile()
|
|
|
|
|
{
|
2021-06-05 17:04:53 +08:00
|
|
|
return sprintf('%s/nexus-update-%s.log', sys_get_temp_dir(), date('Ymd'));
|
2021-02-01 02:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUpdateDirectory()
|
|
|
|
|
{
|
|
|
|
|
return ROOT_PATH . 'public/update';
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 20:19:39 +08:00
|
|
|
public function listTableFieldsFromCreateTable($createTableSql)
|
|
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
}
|
2021-02-01 02:33:45 +08:00
|
|
|
|
2021-02-01 20:19:39 +08:00
|
|
|
public function listTableFieldsFromDb($table)
|
2021-02-01 02:33:45 +08:00
|
|
|
{
|
2021-02-01 20:19:39 +08:00
|
|
|
$sql = "desc $table";
|
|
|
|
|
$res = sql_query($sql);
|
2021-02-01 02:33:45 +08:00
|
|
|
$data = [];
|
2021-02-01 20:19:39 +08:00
|
|
|
while ($row = mysql_fetch_assoc($res)) {
|
|
|
|
|
$data[$row['Field']] = $row;
|
2021-02-01 02:33:45 +08:00
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 00:34:11 +08:00
|
|
|
public function runExtraQueries()
|
|
|
|
|
{
|
|
|
|
|
//custom field menu
|
|
|
|
|
$url = 'fields.php';
|
|
|
|
|
$table = 'adminpanel';
|
|
|
|
|
$count = get_row_count($table, "where url = " . sqlesc($url));
|
|
|
|
|
if ($count == 0) {
|
|
|
|
|
$insert = [
|
|
|
|
|
'name' => 'Custom Field Manage',
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'info' => 'Manage custom fields',
|
|
|
|
|
];
|
2021-06-08 10:42:39 +08:00
|
|
|
$id = NexusDB::insert($table, $insert);
|
2021-03-19 00:34:11 +08:00
|
|
|
$this->doLog("[ADD CUSTOM FIELD MENU] insert: " . json_encode($insert) . " to table: $table, id: $id");
|
|
|
|
|
}
|
2021-06-06 13:41:05 +08:00
|
|
|
//since beta8
|
2021-06-08 10:42:39 +08:00
|
|
|
if (WITH_LARAVEL && NexusDB::schema()->hasColumn('categories', 'icon_id')) {
|
2021-06-01 01:28:46 +08:00
|
|
|
$this->doLog('[INIT CATEGORY ICON_ID]');
|
2021-05-29 21:48:50 +08:00
|
|
|
$icon = Icon::query()->orderBy('id', 'asc')->first();
|
|
|
|
|
if ($icon) {
|
|
|
|
|
Category::query()->where('icon_id', 0)->update(['icon_id' => $icon->id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-06 13:41:05 +08:00
|
|
|
//fix base url, since beta8
|
2021-06-08 10:42:39 +08:00
|
|
|
if (WITH_LARAVEL && NexusDB::schema()->hasTable('settings')) {
|
2021-06-06 13:41:05 +08:00
|
|
|
$settingBasic = get_setting('basic');
|
|
|
|
|
if (isset($settingBasic['BASEURL']) && Str::startsWith($settingBasic['BASEURL'], 'localhost')) {
|
|
|
|
|
$this->doLog('[RESET CONFIG basic.BASEURL]');
|
|
|
|
|
Setting::query()->where('name', 'basic.BASEURL')->update(['value' => '']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($settingBasic['announce_url']) && Str::startsWith($settingBasic['announce_url'], 'localhost')) {
|
|
|
|
|
$this->doLog('[RESET CONFIG basic.announce_url]');
|
|
|
|
|
Setting::query()->where('name', 'basic.announce_url')->update(['value' => '']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 01:28:46 +08:00
|
|
|
//torrent support sticky second level
|
|
|
|
|
if (WITH_LARAVEL) {
|
2021-06-08 10:42:39 +08:00
|
|
|
$columnInfo = NexusDB::getMysqlColumnInfo('torrents', 'pos_state');
|
2021-06-06 13:41:05 +08:00
|
|
|
$this->doLog("[TORRENT POS_STATE], column info: " . json_encode($columnInfo));
|
|
|
|
|
if ($columnInfo['DATA_TYPE'] == 'enum') {
|
2021-06-01 01:28:46 +08:00
|
|
|
$sql = "alter table torrents modify `pos_state` varchar(32) NOT NULL DEFAULT 'normal'";
|
|
|
|
|
$this->doLog("[ALTER TORRENT POS_STATE TYPE TO VARCHAR], $sql");
|
|
|
|
|
sql_query($sql);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-13 22:21:42 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.6.0-beta9
|
|
|
|
|
*
|
|
|
|
|
* attendance change, do migrate
|
|
|
|
|
*/
|
|
|
|
|
if (WITH_LARAVEL && VERSION_NUMBER == '1.6.0-beta9' && NexusDB::schema()->hasColumn('attendance', 'total_points')) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function migrateAttendance()
|
|
|
|
|
{
|
|
|
|
|
$page = 1;
|
|
|
|
|
$size = 1000;
|
|
|
|
|
$sub = Attendance::query()->orderBy('id', 'desc');
|
|
|
|
|
while (true) {
|
|
|
|
|
$logPrefix = "[MIGRATE_ATTENDANCE], page: $page, size: $size";
|
|
|
|
|
$result = Attendance::query()
|
|
|
|
|
->fromSub($sub, 'a')
|
|
|
|
|
->groupBy('id, uid')
|
|
|
|
|
->selectRaw('id, uid, count(*) as counts')
|
|
|
|
|
->forPage($page, $size)
|
|
|
|
|
->get();
|
|
|
|
|
$this->doLog("$logPrefix, " . last_query() . ", count: " . $result->count());
|
|
|
|
|
if ($result->isEmpty()) {
|
|
|
|
|
$this->doLog("$logPrefix, no more data...");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
foreach ($result as $row) {
|
|
|
|
|
$update = [
|
|
|
|
|
'total_days' => $row->counts,
|
|
|
|
|
];
|
|
|
|
|
$updateResult = $row->update($update);
|
|
|
|
|
$this->doLog(sprintf(
|
|
|
|
|
"$logPrefix, update user: %s(%s) => %s, result: %s",
|
|
|
|
|
$row->uid, $row->id, json_encode($update), var_export($updateResult, true)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
$page++;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 20:32:57 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-01 02:02:01 +08:00
|
|
|
}
|