Skip to content

Commit

Permalink
HOJ 的 json-zip 导入
Browse files Browse the repository at this point in the history
  • Loading branch information
zhblue authored Sep 29, 2023
1 parent c575b65 commit 0e1ef4e
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 20 deletions.
31 changes: 11 additions & 20 deletions trunk/web/admin/problem_import.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function writable($path) {
echo "<center><h3>".$MSG_PROBLEM."-".$MSG_IMPORT."</h3></center>";

?>

<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -60,68 +61,58 @@ function writable($path) {
?>

<?php if ($show_form) { ?>
- Import Problem XML a.k.a FPS <br><br>
- Import Problem XML<br><br>
<form class='form-inline' action='problem_import_xml.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
</div>
<center>
<div class='form-group'>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
</center>
<?php require_once("../include/set_post_key.php");?>
</form>
- Import Problem from QDUOJ - json - zip<br>应该是真的QDUOJ,未严格测试,感谢[温十六中]吴晓阳提供例子文件<br>
<form class='form-inline' action='problem_import_qduoj.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
</div>
<center>
<div class='form-group'>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
</center>
<?php require_once("../include/set_post_key.php");?>
</form>
- Import Problem from unkownOJ - json - zip<br>曾经以为是QDUOJ,但似乎不是,谁知道请告诉我<br>
<form class='form-inline' action='problem_import_unkownoj.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
</div>
<center>
<div class='form-group'>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
</center>
<?php require_once("../include/set_post_key.php");?>
</form>
- Import Problem from SYZOJ - zip<br><br>
<form class='form-inline' action='problem_import_syzoj.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
</div>
<center>
<div class='form-group'>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
</center>
<?php require_once("../include/set_post_key.php");?>
</form>

- Import Problem from HydroOJ - zip<br><br>
<form class='form-inline' action='problem_import_hydro.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
<center>
<?php require_once("../include/set_post_key.php");?>
</form>
- Import Problem from HOJ - zip<br><br>
<form class='form-inline' action='problem_import_hoj.php' method=post enctype="multipart/form-data">
<div class='form-group'>
<input class='form-control' type=file name=fps>
<button class='btn btn-default btn-sm' type=submit>Upload to HUSTOJ</button>
</div>
</center>
<?php require_once("../include/set_post_key.php");?>
</form>
<?php } ?>

<br><br>

<?php if ($OJ_LANG == "cn") { ?>
免费题目<a href="https://github.com/zhblue/freeproblemset/tree/master/fps-examples" target="_blank">下载</a><br>
更多题目请到 <a href="http://tk.hustoj.com/problemset.php?search=free" target="_blank">TK 题库免费专区</a>。
Expand Down
189 changes: 189 additions & 0 deletions trunk/web/admin/problem_import_hoj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
require_once ("admin-header.php");
//require_once("../include/check_post_key.php");

if (!(isset($_SESSION[$OJ_NAME.'_'.'administrator'])||isset($_SESSION[$OJ_NAME.'_problem_editor']) )) {
echo "<a href='../loginpage.php'>Please Login First!</a>";
exit(1);
}

if (isset($OJ_LANG)) {
require_once("../lang/$OJ_LANG.php");
}

require_once ("../include/const.inc.php");
require_once ("../include/problem.php");
?>

<?php
?>

<hr>
&nbsp;&nbsp;- Import Problem ... <br>
&nbsp;&nbsp;- 如果导入失败,请参考 <a href="https://github.com/zhblue/hustoj/blob/master/wiki/FAQ.md#%E5%90%8E%E5%8F%B0%E5%AF%BC%E5%85%A5%E9%97%AE%E9%A2%98%E5%A4%B1%E8%B4%A5" target="_blank">FAQ</a>。
<br><br>

<?php
function strip($Node, $TagName) {
$len=mb_strlen($TagName);
$i=mb_strpos($Node,"<".$TagName.">");
$j=mb_strpos($Node,"</".$TagName.">");

return mb_substr($Node,$i+$len+2,$j-($i+$len+2));
}

function getAttribute($Node, $TagName,$attribute) {
return $Node->children()->$TagName->attributes()->$attribute;
}

function hasProblem($title) {
//return false;
$md5 = md5($title);
$sql = "SELECT 1 FROM problem WHERE md5(title)=?";
$result = pdo_query($sql, $md5);
$rows_cnt = count($result);
//echo "row->$rows_cnt";
return ($rows_cnt>0);
}

function mkpta($pid,$prepends,$node) {
$language_ext = $GLOBALS['language_ext'];
$OJ_DATA = $GLOBALS['OJ_DATA'];

foreach ($prepends as $prepend) {
$language = $prepend->attributes()->language;
$lang = getLang($language);
$file_ext = $language_ext[$lang];
$basedir = "$OJ_DATA/$pid";
$file_name = "$basedir/$node.$file_ext";
file_put_contents($file_name,$prepend);
}
}

function get_extension($file) {
$info = pathinfo($file);
return $info['extension'];
}

function import_json($json) {
global $OJ_DATA,$OJ_SAE,$OJ_REDIS,$OJ_REDISSERVER,$OJ_REDISPORT,$OJ_REDISQNAME,$domain,$DOMAIN;
$qduoj_problem=json_decode($json)->{'problem'};
echo( "<br> ".$qduoj_problem->{'problemId'});
echo( $qduoj_problem->{'title'})."<br>";

$title = $qduoj_problem->{'title'};

$time_limit = floatval($qduoj_problem->{'timeLimit'});
$unit = "ms";
//echo $unit;

$time_limit /= 1000;

$memory_limit = floatval($qduoj_problem->{'memoryLimit'});
$unit = "M";

if ($unit=='kb')
$memory_limit /= 1024;

$description = "[md]\n".$qduoj_problem->{'description'}."\n[/md]";
$input = "";
$output = "";
$examples=$qduoj_problem->{'examples'};
$sample_input = strip($examples,"input");
$sample_output = strip($examples,"output");;
echo "sin:".$sample_input."<br>";
echo "sout:".$sample_output."<br>";
$hint = "";
$source ="";
echo json_decode($json)->{'tags'}->{'value'};
$spj=0;

echo "($title, $time_limit, $memory_limit, $description, $input, $output, $sample_input, $sample_output, $hint, $source, $spj )";
$pid = addproblem($title, $time_limit, $memory_limit, $description, $input, $output, $sample_input, $sample_output, $hint, $source, $spj, $OJ_DATA);
return $pid;
}


if ($_FILES ["fps"] ["error"] > 0) {
echo "&nbsp;&nbsp;- Error: ".$_FILES ["fps"] ["error"]."File size is too big, change in PHP.ini<br />";
}
else {
$tempdir = sys_get_temp_dir()."/import_hoj".time();
echo $tempdir ;
mkdir($tempdir);
$tempfile = $_FILES ["fps"] ["tmp_name"];
if (get_extension( $_FILES ["fps"] ["name"])=="zip") {
echo "&nbsp;&nbsp;- zip file, only HOJ exported file is supported";
$resource = zip_open($tempfile);

$i = 1;
while ($dir_resource = zip_read($resource)) {
if (zip_entry_open($resource,$dir_resource)) {
$file_name = $path.zip_entry_name($dir_resource);
$file_path = substr($file_name,0,strrpos($file_name, "/"));
if (!is_dir($file_name)) {
$file_size = zip_entry_filesize($dir_resource);
$file_content = zip_entry_read($dir_resource,$file_size);
//echo "$file_name"."<br>";
if(get_extension($file_name)=="json")
{
// import_json($file_content);
}else{
//echo "$file_name"."<br>";
mkdir($tempdir."/".dirname($file_name));
file_put_contents($tempdir."/".$file_name,$file_content);
}
}else{
echo $file_name;
}
zip_entry_close($dir_resource);
}
}
zip_close($resource);
$resource = zip_open($tempfile);
$cmds=array();
$i = 1;
while ($dir_resource = zip_read($resource)) {
if (zip_entry_open($resource,$dir_resource)) {
$file_name = $path.zip_entry_name($dir_resource);
$file_path = substr($file_name,0,strrpos($file_name, "/"));
if (!is_dir($file_name)) {
$file_size = zip_entry_filesize($dir_resource);
$file_content = zip_entry_read($dir_resource,$file_size);
if(get_extension($file_name)=="json")
{
$pid=import_json($file_content);
// $dir=$tempdir."/".basename($file_name,".json");
mkdir("$OJ_DATA/$pid");
$data_dir=basename($file_name,".json");
array_push ($cmds,"mv $tempdir/$data_dir/* $OJ_DATA/$pid/");
array_push ($cmds,"rmdir $tempdir/$data_dir/");
$i++;
}else{
//echo "$file_name"."<br>";
mkdir($tempdir."/".dirname($file_name),0755,true);
file_put_contents($tempdir."/".$file_name,$file_content);
}
}else{
echo $file_name;
}
zip_entry_close($dir_resource);
}
}
zip_close($resource);
unlink ( $_FILES ["fps"] ["tmp_name"] );
foreach($cmds as $cmd){
// echo $cmd."<br>";
system($cmd);

}
system ("rmdir $tempdir");
}
else {
echo ($tempfile);
}

}


?>

0 comments on commit 0e1ef4e

Please sign in to comment.