-
Notifications
You must be signed in to change notification settings - Fork 248
/
testcase.php
281 lines (236 loc) · 9.76 KB
/
testcase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* View/edit testcases
*
* Part of the DOMjudge Programming Contest Jury System and licenced
* under the GNU GPL. See README and COPYING for details.
*/
$pagename = basename($_SERVER['PHP_SELF']);
$probid = @$_REQUEST['probid'];
require('init.php');
$INOROUT = array('input','output');
// Download testcase
if ( isset ($_GET['fetch']) && in_array($_GET['fetch'], $INOROUT)) {
$rank = $_GET['rank'];
$fetch = $_GET['fetch'];
$filename = $probid . $rank . "." . substr($fetch,0,-3);
$size = $DB->q("MAYBEVALUE SELECT OCTET_LENGTH($fetch)
FROM testcase WHERE probid = %s AND rank = %i",
$probid, $rank);
// sanity check before we start to output headers
if ( $size===NULL || !is_numeric($size)) error("Problem while fetching testcase");
header("Content-Type: application/octet-stream; name=\"$filename\"");
header("Content-Disposition: inline; filename=\"$filename\"");
header("Content-Length: $size");
// This may not be good enough for large testsets, but streaming them
// directly from the database query result seems overkill to implement.
echo $DB->q("VALUE SELECT SQL_NO_CACHE $fetch FROM testcase
WHERE probid = %s AND rank = %i", $probid, $rank);
exit(0);
}
$data = $DB->q('KEYTABLE SELECT rank AS ARRAYKEY, testcaseid, rank, description,
OCTET_LENGTH(input) AS size_input, md5sum_input,
OCTET_LENGTH(output) AS size_output, md5sum_output
FROM testcase WHERE probid = %s ORDER BY rank', $probid);
// Reorder testcases
if ( isset ($_GET['move']) ) {
$move = $_GET['move'];
$rank = (int)$_GET['rank'];
// First find testcase to switch with
$last = NULL;
$other = NULL;
foreach( $data as $curr => $row ) {
if ( $curr==$rank && $move=='up' ) {
$other = $last;
break;
}
if ( $rank==$last && $move=='down' && $last!==NULL ) {
$other = $curr;
break;
}
$last = $curr;
}
if ( $other!==NULL ) {
// (probid, rank) is a unique key, so we must switch via a
// temporary rank, and use a transaction.
$tmprank = 999999;
$DB->q('START TRANSACTION');
$DB->q('UPDATE testcase SET rank = %i
WHERE probid = %s AND rank = %i', $tmprank, $probid, $other);
$DB->q('UPDATE testcase SET rank = %i
WHERE probid = %s AND rank = %i', $other, $probid, $rank);
$DB->q('UPDATE testcase SET rank = %i
WHERE probid = %s AND rank = %i', $rank, $probid, $tmprank);
$DB->q('COMMIT');
auditlog('testcase', $probid, 'switch rank', "$rank <=> $other");
}
// Redirect to the original page to prevent accidental redo's
header('Location: testcase.php?probid=' . urlencode($probid));
return;
}
$title = 'Testcases for problem '.htmlspecialchars(@$probid);
require(LIBWWWDIR . '/header.php');
if ( ! $probid ) error("Missing or invalid problem id");
echo "<h1>" . $title ."</h1>\n\n";
$result = '';
if ( isset($_POST['probid']) && IS_ADMIN ) {
$maxrank = 0;
foreach($data as $rank => $row) {
foreach($INOROUT as $inout) {
if ( $rank>$maxrank ) $maxrank = $rank;
$fileid = 'update_'.$inout;
if ( !empty($_FILES[$fileid]['name'][$rank]) ) {
// Check for upload errors:
checkFileUpload ( $_FILES[$fileid]['error'][$rank] );
$content = file_get_contents($_FILES[$fileid]['tmp_name'][$rank]);
if ( $DB->q("VALUE SELECT count(testcaseid)
FROM testcase WHERE probid = %s AND rank = %i",
$probid, $rank) ) {
$DB->q("UPDATE testcase SET md5sum_$inout = %s, $inout = %s
WHERE probid = %s AND rank = %i",
md5($content), $content, $probid, $rank);
auditlog('testcase', $probid, 'updated', "$inout rank $rank");
} else {
$DB->q("INSERT INTO testcase (probid,rank,md5sum_$inout,$inout)
VALUES (%s,%i,%s,%s)",
$probid, $rank, md5($content), $content);
auditlog('testcase', $probid, 'added', "$inout rank $rank");
}
$result .= "<li>Updated $inout for testcase $rank from " .
htmlspecialchars($_FILES[$fileid]['name'][$rank]) .
" (" . htmlspecialchars($_FILES[$fileid]['size'][$rank]) .
" B)</li>\n";
}
}
if ( isset($_POST['sample'][$rank]) ) {
$DB->q('UPDATE testcase SET sample = %i WHERE probid = %s
AND rank = %i', $_POST['sample'][$rank], $probid, $rank);
$result .= "<li>Set testcase $rank to be " .
($_POST['sample'][$rank] ? "" : "not ") .
"a sample testcase</li>\n";
}
if ( isset($_POST['description'][$rank]) ) {
$DB->q('UPDATE testcase SET description = %s WHERE probid = %s
AND rank = %i', $_POST['description'][$rank], $probid, $rank);
auditlog('testcase', $probid, 'updated description', "rank $rank");
$result .= "<li>Updated description for testcase $rank</li>\n";
}
} // end: foreach $data
if ( !empty($_FILES['add_input']['name']) ||
!empty($_FILES['add_output']['name']) ) {
$content = array();
$rank = $maxrank + 1;
foreach($INOROUT as $inout) {
if ( empty($_FILES['add_'.$inout]['name']) ) {
warning("No $inout file specified for new testcase, ignoring.");
} else {
checkFileUpload ( $_FILES['add_'.$inout]['error'] );
$content[$inout] = file_get_contents($_FILES['add_'.$inout]['tmp_name']);
}
}
if ( !empty($content['input']) && !empty($content['output']) ) {
$DB->q("INSERT INTO testcase
(probid,rank,md5sum_input,md5sum_output,input,output,description,sample)
VALUES (%s,%i,%s,%s,%s,%s,%s,%i)",
$probid, $rank, md5(@$content['input']), md5(@$content['output']),
@$content['input'], @$content['output'], @$_POST['add_desc'],
@$_POST['add_sample']);
auditlog('testcase', $probid, 'added', "rank $rank");
$result .= "<li>Added new testcase $rank from " .
htmlspecialchars($_FILES['add_input']['name']) .
" (" . htmlspecialchars($_FILES['add_input']['size']) . " B) and " .
htmlspecialchars($_FILES['add_output']['name']) .
" (" . htmlspecialchars($_FILES['add_output']['size']) . " B)</li>\n";
}
}
}
if ( !empty($result) ) {
echo "<ul>\n$result</ul>\n\n";
// Reload testcase data after updates
$data = $DB->q('KEYTABLE SELECT rank AS ARRAYKEY, testcaseid, rank,
description, sample,
OCTET_LENGTH(input) AS size_input, md5sum_input,
OCTET_LENGTH(output) AS size_output, md5sum_output
FROM testcase WHERE probid = %s ORDER BY rank', $probid);
}
echo "<p><a href=\"problem.php?id=" . urlencode($probid) . "\">back to problem " .
htmlspecialchars($probid) . "</a></p>\n\n";
if ( IS_ADMIN ) {
echo addForm('', 'post', null, 'multipart/form-data') .
addHidden('probid', $probid);
}
if ( count($data)==0 ) {
echo "<p class=\"nodata\">No testcase(s) yet.</p>\n";
} else {
?>
<table class="list testcases">
<thead><tr>
<th scope="col">#</th><th scope="col">download</th>
<th scope="col">size</th><th scope="col">md5</th>
<?php
if ( IS_ADMIN ) echo '<th scope="col">upload new</th>';
?><th scope="col">sample</th><th scope="col">description</th>
</tr></thead>
<tbody>
<?php
}
foreach( $data as $rank => $row ) {
foreach($INOROUT as $inout) {
echo "<tr>";
if ( $inout=='input' ) {
echo "<td rowspan=\"2\" class=\"testrank\">" .
"<a href=\"./testcase.php?probid=" . urlencode($probid) .
"&rank=$rank&move=up\">↑</a>$rank" .
"<a href=\"./testcase.php?probid=" . urlencode($probid) .
"&rank=$rank&move=down\">↓</a></td>";
}
echo "<td class=\"filename\"><a href=\"./testcase.php?probid=" .
urlencode($probid) . "&rank=$rank&fetch=" . $inout . "\">" .
htmlspecialchars($probid) . $rank . "." . substr($inout,0,-3) . "</a></td>" .
"<td class=\"testsize\">" . htmlspecialchars($row["size_$inout"]) . " B</td>" .
"<td class=\"testmd5\">" . htmlspecialchars($row["md5sum_$inout"]) . "</td>";
if ( IS_ADMIN ) {
echo "<td>" . addFileField("update_".$inout."[$rank]") . "</td>";
}
if ( $inout=='input' ) {
if ( IS_ADMIN ) {
echo "<td rowspan=\"2\" align=\"center]\" onclick=\"editTcSample($rank)\">" .
addSelect("sample[$rank]",array("no", "yes"), $row['sample'], true) . "</td>";
// hide sample dropdown field if javascript is enabled
echo "<script type=\"text/javascript\" language=\"JavaScript\">" .
"hideTcSample($rank, '". printyn($row['sample'])."');</script>";
echo "<td rowspan=\"2\" class=\"testdesc\" onclick=\"editTcDesc($rank)\">" .
"<textarea id=\"tcdesc_$rank\" name=\"description[$rank]\" cols=\"50\" rows=\"2\">" .
htmlspecialchars($row['description']) . "</textarea></td>" .
"<td rowspan=\"2\" class=\"editdel\">" .
"<a href=\"delete.php?table=testcase&testcaseid=$row[testcaseid]&referrer=" .
urlencode('testcase.php?probid='.$probid) . "\">" .
"<img src=\"../images/delete.png\" alt=\"delete\"" .
" title=\"delete this testcase\" class=\"picto\" /></a></td>";
// hide edit field if javascript is enabled
echo "<script type=\"text/javascript\" language=\"JavaScript\">" .
"hideTcDescEdit($rank);</script>";
} else {
echo "<td rowspan=\"2\" align=\"center\">" .
printyn($row['issample']) . "</td>";
echo "<td rowspan=\"2\" class=\"testdesc\">" .
htmlspecialchars($row['description']) . "</td>";
}
}
echo "</tr>\n";
}
}
if ( count($data)!=0 ) echo "</tbody>\n</table>\n";
if ( IS_ADMIN ) {
?>
<h3>Create new testcase</h3>
<table>
<tr><td>Input testdata: </td><td><?php echo addFileField('add_input') ?></td></tr>
<tr><td>Output testdata:</td><td><?php echo addFileField('add_output') ?></td></tr>
<tr><td>Sample testcase:</td><td><?php echo addSelect('add_sample', array("no","yes"), 0, true);?></td></tr>
<tr><td>Description: </td><td><?php echo addInput('add_desc','',30); ?></td></tr>
</table>
<?php
echo "<br />" . addSubmit('Submit all changes') . addEndForm();
}
require(LIBWWWDIR . '/footer.php');