-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathApi_splits.php
More file actions
211 lines (173 loc) · 7.84 KB
/
Api_splits.php
File metadata and controls
211 lines (173 loc) · 7.84 KB
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
<?php
class Api_splits extends CI_Controller {
function __construct() {
parent::__construct();
$this->content_folder_modulo = 10000; // DO NOT CHANGE
$this->directory = DATA_PATH . '/.cache/tasks/';
$this->load->model('Dataset');
$this->load->model('Task');
$this->load->model('Task_inputs');
$this->load->model('Estimation_procedure');
$this->load->model('Log');
$this->load->model('Run');
$this->load->helper('file_upload');
$this->db = $this->load->database('read',true);
$this->task_types = array(1, 2, 3, 6, 7, 9, 10, 11);
$this->challenge_types = array(9);
$this->evaluation = APPPATH . 'third_party/OpenML/Java/evaluate.jar';
$this->eval_engine_config = " -config 'cache_allowed=false;server=".BASE_URL.";api_key=".API_KEY."' ";
}
function different_predictions($run_ids) {
if (is_safe($run_ids) == false) {
die('run id input not safe. ');
}
$runs = $this->Run->getWhere('`rid` IN (' . $run_ids . ')');
if (count($runs) == 0) {
die('no runs found.');
}
$task_id = $runs[0]->task_id;
// Escape all shell arguments to prevent command injection.
// is_safe() above is the first layer; escapeshellarg() is the second.
$command = 'java -jar ' . escapeshellarg($this->evaluation)
. ' -f ' . escapeshellarg('different_predictions')
. ' -t ' . escapeshellarg((string) $task_id)
. ' -r ' . escapeshellarg($run_ids)
. $this->eval_engine_config;
$this->Log->cmd('API Splits::different_predictions(' . $run_ids . ')', $command);
if (function_enabled('system')) {
header('Content-type: text/plain');
system(CMD_PREFIX . $command);
} else {
die('failed to generate arff file: php "system" function disabled. ');
}
}
function all_wrong($run_ids) {
if (is_safe($run_ids) == false) {
die('run id input not safe. ');
}
$runs = $this->Run->getWhere('`rid` IN (' . $run_ids . ')');
if (count($runs) == 0) {
die('no runs found.');
}
$task_id = $runs[0]->task_id;
// Escape all shell arguments to prevent command injection.
$command = 'java -jar ' . escapeshellarg($this->evaluation)
. ' -f ' . escapeshellarg('all_wrong')
. ' -t ' . escapeshellarg((string) $task_id)
. ' -r ' . escapeshellarg($run_ids)
. $this->eval_engine_config;
$this->Log->cmd('API Splits::all_wrong(' . $run_ids . ')', $command);
if (function_enabled('system')) {
header('Content-type: text/plain');
system(CMD_PREFIX . $command);
} else {
die('failed to generate arff file: php "system" function disabled. ');
}
}
function challenge($task_id, $testtrain, $offset_arg, $size_arg) {
if (is_numeric($task_id) == false) {
die('argument 1 should be numeric');
}
if (in_array($testtrain, array('test', 'train')) == false) {
die('argument 2 should be in {test,train}');
}
$offset = "";
$size = "";
if (is_numeric($offset_arg)) {
// Cast to int so only a bare integer reaches the shell.
$offset = ' -o ' . escapeshellarg((string)(int) $offset_arg) . ' ';
if (is_numeric($size_arg)) {
$size = ' -size ' . escapeshellarg((string)(int) $size_arg) . ' ';
}
}
$task = $this->Task->getById($task_id);
if ($task === false || in_array($task->ttid, $this->challenge_types) === false) {
die('Task not valid challenge.');
}
// Escape all shell arguments to prevent command injection.
// $testtrain is already validated against a whitelist above.
$command = 'java -jar ' . escapeshellarg($this->evaluation)
. ' -f ' . escapeshellarg('challenge')
. ' -t ' . escapeshellarg((string)(int) $task_id)
. ' -mode ' . escapeshellarg($testtrain)
. ' ' . $offset . $size
. $this->eval_engine_config;
$this->Log->cmd('API Splits::challenge(' . $task_id . ', ' . $testtrain . ')', $command);
if (function_enabled('system')) {
header('Content-type: text/plain');
system(CMD_PREFIX . $command);
} else {
die('failed to generate arff file: php "system" function disabled. ');
}
}
function merge_datasets($task_id) {
$dir_idx = floor($task_id / $this->content_folder_modulo) * $this->content_folder_modulo;
$directory = $this->directory . '/' . $dir_idx . '/' . $task_id;
$filepath = $directory . '/merged_dataset.arff';
if (file_exists($filepath) == false) {
$this->generate("merge_datasets", $task_id, $filepath);
}
header('Content-type: text/plain');
header('Content-Length: ' . filesize($filepath));
readfile_chunked($filepath);
}
function get($task_id) {
$dir_idx = floor($task_id / $this->content_folder_modulo) * $this->content_folder_modulo;
$directory = $this->directory . '/' . $dir_idx . '/' . $task_id;
$filepath = $directory . '/splits.arff';
if (file_exists($filepath) == false) {
$this->generate("generate_folds", $task_id, $filepath);
}
header('Content-type: text/plain');
header('Content-Length: ' . filesize($filepath));
readfile_chunked($filepath);
}
private function generate($function, $task_id, $filepath) {
$task = $this->Task->getById($task_id);
// JvR: in hindsight, this check belongs in the eval engine, and not here.
if ($task === false || in_array($task->ttid, $this->task_types) === false) {
http_response_code($this->config->item('general_http_error_code'));
die('Task not providing datasplits.');
}
$values = $this->Task_inputs->getTaskValuesAssoc($task_id);
// TODO: very important. sanity check input
$testset_str = array_key_exists('custom_testset', $values) && is_cs_natural_numbers($values['custom_testset']) ? '-test "' . $values['custom_testset'] . '"' : '';
// Escape all shell arguments to prevent command injection.
// $function is an internal string, but escaped as defence-in-depth.
$command = 'java -jar ' . escapeshellarg($this->evaluation)
. ' -f ' . escapeshellarg($function)
. ' -id ' . escapeshellarg((string)(int) $task_id)
. ' ' . $this->eval_engine_config;
if (array_key_exists('custom_testset', $values) && is_cs_natural_numbers($values['custom_testset'])) {
$command .= '-test ' . escapeshellarg($values['custom_testset']) . ' ';
}
if (!file_exists(dirname($filepath))) {
mkdir(dirname($filepath), 0755, true);
}
$command .= ' -o ' . escapeshellarg($filepath);
if (function_enabled('exec')) {
header('Content-type: text/plain');
$result_status = 0;
// note that result does not need to be displayed, this
// is just a generate function
$result = array();
exec(CMD_PREFIX . $command, $result, $return_status);
if ($return_status != 0 && defined('EMAIL_API_LOG')) {
$to = EMAIL_API_LOG;
$subject = 'OpenML API [' . $function . '] Exception: ' . $result_status;
$content = 'Time: ' . now() . "\nTask_id:" . $task_id . "\nOutput: " . implode("\n", $result);
sendEmail($to, $subject, $content, 'text');
http_response_code($this->config->item('general_http_error_code'));
die('failed to perform action ' . $function . '. Evaluation Engine result send to EMAIL_API_LOG account.');
}
if ($return_status != 0) {
http_response_code($this->config->item('general_http_error_code'));
die('failed to perform action ' . $function . '. Evaluation Engine result omitted (no EMAIL_API_LOG defined). ');
}
} else {
http_response_code($this->config->item('general_http_error_code'));
die('failed to perform action ' . $function . ': php "exec" function disabled. ');
}
}
}
?>