refactor: clear limits after clear data

This commit is contained in:
Baoshuo Ren 2023-02-13 07:56:26 +08:00
parent 252b2c0cdc
commit 83da060ec6
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 32 additions and 22 deletions

View File

@ -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';

View File

@ -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 '';
}