From 6ff83e810ba1092be17f11f6d365e599911684fe Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Feb 2023 06:51:04 +0800 Subject: [PATCH 1/5] refactor(problem): store limits into extra_config --- web/app/controllers/problem.php | 11 ++--------- web/app/libs/uoj-data-lib.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/web/app/controllers/problem.php b/web/app/controllers/problem.php index 9d1c3555..5920a7a2 100644 --- a/web/app/controllers/problem.php +++ b/web/app/controllers/problem.php @@ -259,8 +259,6 @@ function(response_text) { $answer_form->runAtServer(); } -$conf = UOJProblem::cur()->getProblemConf(); - if (UOJContest::cur()) { $pageTitle = UOJProblem::cur()->getTitle(['with' => 'letter', 'simplify' => true]); } else { @@ -291,13 +289,8 @@ function(response_text) { getVal('time_limit', 1) : null; - $memory_limit = $conf instanceof UOJProblemConf ? $conf->getVal('memory_limit', 256) : null; - } else if (UOJProblem::info('type') == 'remote') { - $time_limit = UOJProblem::cur()->getExtraConfig('time_limit'); - $memory_limit = UOJProblem::cur()->getExtraConfig('memory_limit'); - } + $time_limit = UOJProblem::cur()->getExtraConfig('time_limit'); + $memory_limit = UOJProblem::cur()->getExtraConfig('memory_limit'); ?>
时间限制: diff --git a/web/app/libs/uoj-data-lib.php b/web/app/libs/uoj-data-lib.php index aad2fb78..38bca6ea 100644 --- a/web/app/libs/uoj-data-lib.php +++ b/web/app/libs/uoj-data-lib.php @@ -482,6 +482,22 @@ private function _sync() { ['mv', "{$this->id}.next.zip", "{$this->id}.zip", '-f'], ]); + DB::update([ + "update problems", + "set", [ + "extra_config" => DB::json_set( + 'extra_config', + '$.time_limit', + $this->final_problem_conf['time_limit'] ? (int)$this->final_problem_conf['time_limit'] : null, + '$.memory_limit', + $this->final_problem_conf['memory_limit'] ? (int)$this->final_problem_conf['memory_limit'] : null, + ), + ], + "where", [ + "id" => $this->id, + ], + ]); + return ''; } From 15a4b23b8003beea6e20ab8e47be84af203f5112 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Feb 2023 06:53:53 +0800 Subject: [PATCH 2/5] fix(problem): force encoding extra_config as object --- web/app/controllers/new_remote_problem.php | 2 +- web/app/controllers/problem_data_manage.php | 2 +- web/app/controllers/problem_statement_manage.php | 2 +- web/app/upgrade/20_problem_difficulty/upgrade.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/web/app/controllers/new_remote_problem.php b/web/app/controllers/new_remote_problem.php index 3ab1497e..7e71a000 100644 --- a/web/app/controllers/new_remote_problem.php +++ b/web/app/controllers/new_remote_problem.php @@ -91,7 +91,7 @@ 'time_limit' => $data['time_limit'], 'memory_limit' => $data['memory_limit'], ]; - $enc_extra_config = json_encode($extra_config); + $enc_extra_config = json_encode($extra_config, JSON_FORCE_OBJECT); DB::insert([ "insert into problems", diff --git a/web/app/controllers/problem_data_manage.php b/web/app/controllers/problem_data_manage.php index 56a26328..85f7a732 100644 --- a/web/app/controllers/problem_data_manage.php +++ b/web/app/controllers/problem_data_manage.php @@ -167,7 +167,7 @@ function echoFilePre($file_name) { if ($extra_config === null) { return '不是合法的JSON'; } - $vdata['extra_config'] = json_encode($extra_config); + $vdata['extra_config'] = json_encode($extra_config, JSON_FORCE_OBJECT); }, ]); $info_form->handle = function (&$vdata) use ($problem) { diff --git a/web/app/controllers/problem_statement_manage.php b/web/app/controllers/problem_statement_manage.php index 88849b48..2de60370 100644 --- a/web/app/controllers/problem_statement_manage.php +++ b/web/app/controllers/problem_statement_manage.php @@ -142,7 +142,7 @@ 'time_limit' => $data['time_limit'], 'memory_limit' => $data['memory_limit'], ]; - $enc_extra_config = json_encode($extra_config); + $enc_extra_config = json_encode($extra_config, JSON_FORCE_OBJECT); DB::update([ "update problems", diff --git a/web/app/upgrade/20_problem_difficulty/upgrade.php b/web/app/upgrade/20_problem_difficulty/upgrade.php index ab1635cc..55e0dfb6 100644 --- a/web/app/upgrade/20_problem_difficulty/upgrade.php +++ b/web/app/upgrade/20_problem_difficulty/upgrade.php @@ -32,7 +32,7 @@ DB::update([ "update problems", "set", [ - "extra_config" => json_encode($extra_config), + "extra_config" => json_encode($extra_config, JSON_FORCE_OBJECT), ], "where", [ "id" => $problem->info['id'], From 252b2c0cdc69094154936e4509bb04fe150a59e8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Feb 2023 06:56:31 +0800 Subject: [PATCH 3/5] fix: 6ff83e810ba1092be17f11f6d365e599911684fe --- web/app/libs/uoj-data-lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/app/libs/uoj-data-lib.php b/web/app/libs/uoj-data-lib.php index 38bca6ea..ec0cee32 100644 --- a/web/app/libs/uoj-data-lib.php +++ b/web/app/libs/uoj-data-lib.php @@ -488,7 +488,7 @@ private function _sync() { "extra_config" => DB::json_set( 'extra_config', '$.time_limit', - $this->final_problem_conf['time_limit'] ? (int)$this->final_problem_conf['time_limit'] : null, + $this->final_problem_conf['time_limit'] ? (float)$this->final_problem_conf['time_limit'] : null, '$.memory_limit', $this->final_problem_conf['memory_limit'] ? (int)$this->final_problem_conf['memory_limit'] : null, ), From 83da060ec6cb4d1700281bba981f82c2eb44f3c8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 13 Feb 2023 07:56:26 +0800 Subject: [PATCH 4/5] refactor: clear limits after clear data --- web/app/controllers/problem_data_manage.php | 4 +- web/app/libs/uoj-data-lib.php | 50 ++++++++++++--------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/web/app/controllers/problem_data_manage.php b/web/app/controllers/problem_data_manage.php index 85f7a732..2653a848 100644 --- a/web/app/controllers/problem_data_manage.php +++ b/web/app/controllers/problem_data_manage.php @@ -392,8 +392,8 @@ function getDataDisplayer() { $data_form->runAtServer(); $clear_data_form = new UOJForm('clear_data'); -$clear_data_form->handle = function () use ($problem) { - dataClearProblemData($problem); +$clear_data_form->handle = function () { + dataClearProblemData(UOJProblem::cur()); }; $clear_data_form->config['submit_container']['class'] = ''; $clear_data_form->config['submit_button']['class'] = 'btn btn-danger d-block w-100'; diff --git a/web/app/libs/uoj-data-lib.php b/web/app/libs/uoj-data-lib.php index ec0cee32..b101629c 100644 --- a/web/app/libs/uoj-data-lib.php +++ b/web/app/libs/uoj-data-lib.php @@ -14,18 +14,36 @@ function dataNewProblem($id) { ]); } -function dataClearProblemData($problem) { - $id = $problem['id']; +function dataClearProblemData(UOJProblem $problem) { + $id = $problem->info['id']; if (!validateUInt($id)) { UOJLog::error("dataClearProblemData: hacker detected"); return "invalid problem id"; } - UOJLocalRun::exec(['rm', "/var/uoj_data/$id", '-r']); - UOJLocalRun::exec(['rm', "/var/uoj_data/upload/$id", '-r']); + UOJLocalRun::exec(['rm', $problem->getDataFolderPath(), '-r']); + UOJLocalRun::exec(['rm', $problem->getUploadFolderPath(), '-r']); + dataUpdateProblemLimits($problem, null, null); dataNewProblem($id); } +function dataUpdateProblemLimits(UOJProblem $problem, $time_limit, $memory_limit) { + $extra_config = $problem->getExtraConfig(); + + $extra_config['time_limit'] = $time_limit; + $extra_config['memory_limit'] = $memory_limit; + + DB::update([ + "update problems", + "set", [ + 'extra_config' => json_encode($extra_config, JSON_FORCE_OBJECT), + ], + "where", [ + "id" => $problem->info['id'], + ], + ]); +} + class SyncProblemDataHandler { private UOJProblem $problem; private $user; @@ -302,13 +320,15 @@ private function _sync() { } $this->requirement = []; - $this->problem_extra_config = $this->problem->getExtraConfig();; + $this->problem_extra_config = $this->problem->getExtraConfig(); + if (!is_file("{$this->upload_dir}/problem.conf")) { throw new UOJFileNotFoundException("problem.conf"); } $this->problem_conf = getUOJConf("{$this->upload_dir}/problem.conf"); $this->final_problem_conf = $this->problem_conf; + if ($this->problem_conf === -1) { throw new UOJFileNotFoundException("problem.conf"); } elseif ($this->problem_conf === -2) { @@ -482,21 +502,11 @@ private function _sync() { ['mv', "{$this->id}.next.zip", "{$this->id}.zip", '-f'], ]); - DB::update([ - "update problems", - "set", [ - "extra_config" => DB::json_set( - 'extra_config', - '$.time_limit', - $this->final_problem_conf['time_limit'] ? (float)$this->final_problem_conf['time_limit'] : null, - '$.memory_limit', - $this->final_problem_conf['memory_limit'] ? (int)$this->final_problem_conf['memory_limit'] : null, - ), - ], - "where", [ - "id" => $this->id, - ], - ]); + dataUpdateProblemLimits( + $this->problem, + $this->final_problem_conf['time_limit'] ? (float)$this->final_problem_conf['time_limit'] : 1, + $this->final_problem_conf['memory_limit'] ? (int)$this->final_problem_conf['memory_limit'] : 256 + ); return ''; } From be6dd872b7b5a3712c923fd8d50b3986f0e721bc Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 13 Feb 2023 07:56:44 +0800 Subject: [PATCH 5/5] chore: add upgrader for #39. --- web/app/libs/uoj-lib.php | 2 +- .../39_limits_in_extra_config/upgrade.php | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 web/app/upgrade/39_limits_in_extra_config/upgrade.php diff --git a/web/app/libs/uoj-lib.php b/web/app/libs/uoj-lib.php index d43a11bc..afea464d 100644 --- a/web/app/libs/uoj-lib.php +++ b/web/app/libs/uoj-lib.php @@ -13,7 +13,7 @@ function requireLib($name) { // html lib $REQUIRE_LIB[$name] = ''; } function requirePHPLib($name) { // uoj php lib - require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-' . $name . '-lib.php'; + require_once $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-' . $name . '-lib.php'; } requirePHPLib('expection'); diff --git a/web/app/upgrade/39_limits_in_extra_config/upgrade.php b/web/app/upgrade/39_limits_in_extra_config/upgrade.php new file mode 100644 index 00000000..04fc26e5 --- /dev/null +++ b/web/app/upgrade/39_limits_in_extra_config/upgrade.php @@ -0,0 +1,37 @@ +getExtraConfig(); + $problem_conf = $problem->getProblemConf(); + + if (!($problem_conf instanceof UOJProblemConf)) { + continue; + } + + $extra_config['time_limit'] = (float)$problem_conf->getVal('time_limit', 1); + $extra_config['memory_limit'] = (int)$problem_conf->getVal('memory_limit', 256); + + DB::update([ + "update problems", + "set", [ + "extra_config" => json_encode($extra_config, JSON_FORCE_OBJECT), + ], + "where", [ + "id" => $problem->info['id'], + ], + ]); + + echo "Problem {$problem->info['id']} upgraded.\n"; + } + } +};