--- UOJ-System/web/app/cli.php 2022-12-30 09:54:05.452022649 +0800 +++ UOJ-Luogu-RemoteJudge/web/app/cli.php 2023-03-29 19:17:31.611110921 +0800 @@ -4,7 +4,21 @@ require $_SERVER['DOCUMENT_ROOT'] . '/app/libs/uoj-lib.php'; +error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED); + +requirePHPLib('luogu'); +requirePHPLib('data'); + // TODO: more beautiful argv parser +$my_args = array(); + +for ($i = 1; $i < count($argv); $i++) { + if (preg_match('/^--([^=]+)[=](.*)/', $argv[$i], $match)) { + $my_args[$match[1]] = $match[2]; + } else if (preg_match('/^--(.*)/', $argv[$i], $match)) { + $my_args[$match[1]] = $argv[++$i]; + } +} $handlers = [ 'upgrade:up' => function ($name) { @@ -61,7 +75,67 @@ }); die("finished!\n"); }, - 'help' => 'showHelp' + 'luogu:add-problem' => function () use ($argv, $my_args) { + DB::init(); + + $db = array(); + + if (!isset($my_args['file'])) { + echo "No database file specified, fetching online instead.\n\n"; + } else { + echo "Reading local database: {$my_args['file']}\n\n"; + + $file = file_get_contents($my_args['file']); + + foreach (explode("\n", $file) as $line) { + if (strlen($line) == 0) continue; + + $line_data = json_decode($line, true); + $db[$line_data['pid']] = $line_data; + } + + $db_count = count($db); + echo "Loaded {$db_count} items.\n\n"; + } + + // TODO: read database from local file + + $problems = array_filter($argv, 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 { + if (!isset($db[$pid])) { + if (!empty($db)) { + echo "[WARN] $pid: fallback to fetch data online\n"; + } + + $id = newLuoguRemoteProblem($pid); + } else { + $parsed = parseLuoguProblemData($db[$pid]); + $id = newLuoguRemoteProblemFromData($parsed); + } + + echo "$pid: $id\n"; + } catch (Exception $e) { + echo "$pid: failed\n"; + } + } + }, + 'help' => 'showHelp', ]; function showHelp() {