From 83da060ec6cb4d1700281bba981f82c2eb44f3c8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 13 Feb 2023 07:56:26 +0800 Subject: [PATCH] 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 85f7a73..2653a84 100644 --- a/web/app/controllers/problem_data_manage.php +++ b/web/app/controllers/problem_data_manage.php @@ -392,8 +392,8 @@ $data_form->config['confirm']['smart'] = true; $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 ec0cee3..b101629 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 @@ class SyncProblemDataHandler { } $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 @@ class SyncProblemDataHandler { ['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 ''; }