1
0
mirror of https://github.com/renbaoshuo/UOJ-Luogu-RemoteJudge.git synced 2024-12-12 23:26:27 +00:00

feat: import luogu problem using cli

This commit is contained in:
Baoshuo Ren 2023-03-20 16:53:58 +08:00
parent 2889b6df81
commit da9c4ced39
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
4 changed files with 199 additions and 2 deletions

View File

@ -2,7 +2,16 @@
## 前期准备
请先在服务器上安装 Node.js 18 LTS 和 `php-curl`
请先在服务器上安装以下软件/模块:
- Node.js 18 LTS
- php-curl
然后为 php-cli 启用 php-v8js 和 php-yaml 扩展:
```bash
sed -i -e '912a\extension=v8js.so\nextension=yaml.so' /etc/php/7.4/cli/php.ini
```
## 修改源码

129
web/app/cli.php Normal file
View File

@ -0,0 +1,129 @@
<?php
$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__);
require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php';
requirePHPLib('luogu');
requirePHPLib('data');
// TODO: more beautiful argv parser
$handlers = [
'upgrade:up' => function ($name) {
if (func_num_args() != 1) {
die("php cli.php upgrade:up <name>\n");
}
Upgrader::transaction(function() use ($name) {
Upgrader::up($name);
});
die("finished!\n");
},
'upgrade:down' => function ($name) {
if (func_num_args() != 1) {
die("php cli.php upgrade:down <name>\n");
}
Upgrader::transaction(function() use ($name) {
Upgrader::down($name);
});
die("finished!\n");
},
'upgrade:refresh' => function ($name) {
if (func_num_args() != 1) {
die("php cli.php upgrade:refresh <name>\n");
}
Upgrader::transaction(function() use ($name) {
Upgrader::refresh($name);
});
die("finished!\n");
},
'upgrade:remove' => function ($name) {
if (func_num_args() != 1) {
die("php cli.php upgrade:remove <name>\n");
}
Upgrader::transaction(function() use ($name) {
Upgrader::remove($name);
});
die("finished!\n");
},
'upgrade:latest' => function () {
if (func_num_args() != 0) {
die("php cli.php upgrade:latest\n");
}
Upgrader::transaction(function() {
Upgrader::upgradeToLatest();
});
die("finished!\n");
},
'upgrade:remove-all' => function () {
if (func_num_args() != 0) {
die("php cli.php upgrade:remove-all\n");
}
Upgrader::transaction(function() {
Upgrader::removeAll();
});
die("finished!\n");
},
'luogu:add-problem' => function () use ($argv) {
$rest_index = null;
$opts = getopt('', ['file::'], $rest_index);
if (!isset($opts['file'])) {
echo "No database file specified, fetching online instead.\n\n";
}
// TODO: read database from local file
$problems = array_slice($argv, $rest_index);
$problems = array_filter($problems, function ($id) {
if (!validateLuoguProblemId($id)) return false;
return true;
});
if (empty($problems)) {
echo "No problems to be added.\n";
return;
}
echo "Problems to be added: " . implode(', ', $problems) . "\n\n";
readline("Press Enter to continue, or Ctrl+C to abort.");
foreach ($problems as $pid) {
try {
$id = newLuoguRemoteProblem($pid);
echo "$pid: $id\n";
} catch (Exception $e) {
echo "$pid: failed\n";
}
}
},
'help' => 'showHelp',
];
function showHelp() {
global $handlers;
echo "UOJ Command-Line Interface\n";
echo "php cli.php <task-name> params1 params2 ...\n";
echo "\n";
echo "The following tasks are available:\n";
foreach ($handlers as $cmd => $handler) {
echo "\t$cmd\n";
}
}
if (count($argv) <= 1) {
showHelp();
die();
}
if (!isset($handlers[$argv[1]])) {
echo "Invalid parameters.\n";
showHelp();
die();
}
call_user_func_array($handlers[$argv[1]], array_slice($argv, 2));

58
web/app/cli.php.patch Normal file
View File

@ -0,0 +1,58 @@
--- UOJ-System/web/app/cli.php 2022-12-30 09:54:05.452022649 +0800
+++ UOJ-Luogu-RemoteJudge/web/app/cli.php 2023-03-20 16:47:12.734169684 +0800
@@ -4,6 +4,9 @@
require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php';
+requirePHPLib('luogu');
+requirePHPLib('data');
+
// TODO: more beautiful argv parser
$handlers = [
@@ -61,7 +64,44 @@
});
die("finished!\n");
},
- 'help' => 'showHelp'
+ 'luogu:add-problem' => function () use ($argv) {
+ $rest_index = null;
+ $opts = getopt('', ['file::'], $rest_index);
+
+ if (!isset($opts['file'])) {
+ echo "No database file specified, fetching online instead.\n\n";
+ }
+
+ // TODO: read database from local file
+
+ $problems = array_slice($argv, $rest_index);
+ $problems = array_filter($problems, function ($id) {
+ if (!validateLuoguProblemId($id)) return false;
+
+ return true;
+ });
+
+ if (empty($problems)) {
+ echo "No problems to be added.\n";
+
+ return;
+ }
+
+ echo "Problems to be added: " . implode(', ', $problems) . "\n\n";
+
+ readline("Press Enter to continue, or Ctrl+C to abort.");
+
+ foreach ($problems as $pid) {
+ try {
+ $id = newLuoguRemoteProblem($pid);
+
+ echo "$pid: $id\n";
+ } catch (Exception $e) {
+ echo "$pid: failed\n";
+ }
+ }
+ },
+ 'help' => 'showHelp',
];
function showHelp() {

View File

@ -47,6 +47,7 @@ function parseLuoguProblemData($problem) {
$statement .= $problem['hint'] . "\n";
return [
'pid' => $problem['pid'],
'title' => "【洛谷 {$problem['pid']}{$problem['title']}",
'time_limit' => (float)max($problem['limits']['time']) / 1000.0,
'memory_limit' => (float)max($problem['limits']['memory']) / 1024.0,
@ -90,7 +91,7 @@ function newLuoguRemoteProblem($pid) {
),
));
$esc_extra_config = json_encode(array(
"luogu_pid" => $pid,
"luogu_pid" => $problem['pid'],
"time_limit" => $problem['time_limit'],
"memory_limit" => $problem['memory_limit'],
"view_content_type" => "ALL",