-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathapi_helper.php
More file actions
293 lines (254 loc) · 9.19 KB
/
api_helper.php
File metadata and controls
293 lines (254 loc) · 9.19 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
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
282
283
284
285
286
287
288
289
290
291
292
293
<?php
function validateXml( $xmlDocument, $xsdDocument, &$xmlErrors, $from_file = true ) {
$xmlErrors = '';
libxml_use_internal_errors(true);
$xml = new DOMDocument();
if($from_file)
$xml->load( $xmlDocument );
else
$xml->loadXML( $xmlDocument );
foreach (libxml_get_errors() as $error) {
$xmlErrors .= $error->message . '. ';
}
if ( $xml->schemaValidate( $xsdDocument ) ) {
return true;
} else {
$xmlErrors .= 'XML does not correspond to XSD schema. ';
foreach (libxml_get_errors() as $error) {
$xmlErrors .= 'Error ' . $error->message . ' on line ' . $error->line . ' column ' . $error->column . '. ';
}
return false;
}
}
/**
* Puts quotes around all non-numeric values in an array-like string
*/
function quote_array_strings($in){
$array = explode(",",str_replace(array('[',']'),'',$in));
return "[".implode(',', array_map(function($value) {
if(!is_numeric($value)) {
return '"' . trim(utf8_decode($value)) . '"';
//adds double quotes, but if you prefer single quotes, use:
//return "'" . $value . "'";
} else {
return $value;
}
}, $array))."]";
}
/**
* @function all_tags_from_xml()
* returns an asociative array containing all selected tag-names as keys
* and all tag content as the values.
*
* @param $xml (object): The an XML object containing the relevant children
* obtained from the XML. Could be obtained by XML->children('oml', true)
* or something similar, to get all childeren in the OML namespace.
* @param $configuration (2d string array): an array configuring which fields to be
* included in the return value. $configuration can contain 4 sub arrays, 'array',
* 'csv', 'plain' and 'string'. Putting a field in one of these sub arrays, deter-
* mines
* @param $return_array (string array): The base array to add tags to. Usually, when
* some fields are already set, these can be joined with the fields to
* be set within this function.
*
* @return (MULTIPLE VALUES array): asociative array containing all selected tag-names
* as keys and all tag content as the values.
*/
function all_tags_from_xml( $xml, $configuration = array(), $return_array = array() ) {
$csv_tags = array();
$include = array_collapse($configuration);
foreach( $xml as $key => $value ) {
if( in_array( $key, $include ) ) {
if( array_key_exists( 'array', $configuration ) && in_array( $key, $configuration['array'] ) ) {
// returned in plain array
if( !array_key_exists( $key, $return_array ) ) {
$return_array[$key] = array();
}
$return_array[$key][] = $value;
} elseif( array_key_exists( 'csv', $configuration ) && in_array( $key, $configuration['csv'] ) ) {
// returned in CSV format
if( !array_key_exists( $key, $csv_tags ) ) {
$csv_tags[$key] = array();
}
$csv_tags[$key][] = trim( $value );
} elseif( array_key_exists( 'plain', $configuration ) && in_array( $key , $configuration['plain'] ) ) {
// returned plain (xml object)
$return_array[$key] = $value;
} elseif( array_key_exists( 'string', $configuration ) && in_array( $key , $configuration['string'] ) ) {
// returned as string
$return_array[$key] = trim($value);
} else {
// an illegal or undefined category
}
}
}
foreach( $csv_tags as $key => $value ) {
$return_array[$key] = putcsv( $value );
}
return $return_array;
}
function xsd( $name, $controller, $versionName = null ) {
$version = '';
if ($versionName) {
$version = $versionName . '/';
}
$filename = APPPATH.'views/pages/' . $controller . '/' . $version . 'xsd/' . $name . '.xsd';
if (file_exists($filename) == false) {
return false;
} else{
return $filename;
}
}
function sub_xml( $xmlFile, $source, $version = 0 ) {
$ci = &get_instance();
if ($version === 0) {
$view = 'pages/rest_api/xml/'.$xmlFile.'.tpl.php';
} else {
$view = 'pages/api_new/'.$version.'/xml/'.$xmlFile.'.tpl.php';
}
$ci->load->view( $view, $source );
}
function camelcaseToUnderscores( $string ) {
return strtolower( preg_replace( '/(?<=\\w)(?=[A-Z])/', "_$1", $string ) );
}
function underscoresToCammelcase( $string ) {
return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
}
function get_arff_features( $datasetUrl, $class = false ) {
$ci = &get_instance();
$eval = PATH . APPPATH . 'third_party/OpenML/Java/evaluate.jar';
$res = array();
$code = 0;
$heapSize = $ci->input->is_cli_request()
? $ci->config->item('java_heap_space_cli')
: $ci->config->item('java_heap_space_web');
// Escape all arguments passed to the shell to prevent command injection.
$command = 'java ' . escapeshellarg('-Xmx' . $heapSize)
. ' -jar ' . escapeshellarg($eval)
. ' -f data_features'
. ' -d ' . escapeshellarg($datasetUrl);
if ($class != false) {
$command .= ' -c ' . escapeshellarg((string) $class);
}
$ci->Log->cmd( 'ARFF Feature Extractor', $command );
if (function_enabled('exec') === false) {
return false;
}
exec( CMD_PREFIX . $command, $res, $code );
if ( $code == 0 && is_array( $res ) ) {
return json_decode( implode( "\n", $res ) );
} else {
return false;
}
}
// replaces a tmp uploaded file with one that has been imported and exported by weka
function validate_arff( $to_folder, $filepath, $name, $did ) {
$ci = &get_instance();
$weka = PATH . APPPATH . 'third_party/OpenML/Java/weka.jar';
$res = array();
$code = 0;
$oldUrl = DATA_PATH . $filepath;
$newpath = strrev(implode(strrev('/openml_'), explode('/', strrev($filepath), 2)));
$newUrl = DATA_PATH . $newpath;
$heap = '-Xmx' . ( $ci->input->is_cli_request() ?
$ci->config->item('java_heap_space_cli') :
$ci->config->item('java_heap_space_web') );
$command = "java $heap -cp $weka weka.core.converters.ArffLoader $oldUrl > $newUrl";
$ci->Log->cmd( 'ARFF Validation', $command );
if(function_enabled('exec') === false ) {
return false;
}
exec( CMD_PREFIX . $command, $res, $code );
// Prepend dataset info comment using pure PHP file I/O.
// This replaces the previous `sed` shell invocation, eliminating the
// command-injection risk that existed when $name contained shell metacharacters.
$safeName = str_replace(["%", "\r", "\n"], ['', '', ''], $name);
$comment = '% Data set "' . $safeName . '". For more information, see https://openml.org/d/' . (int) $did . "\n";
$original = file_get_contents($newUrl);
if ($original === false || file_put_contents($newUrl, $comment . $original) === false) {
return false;
}
$code = 0; // success — treat prepend as equivalent to sed exit code 0
if( $code == 0 ) {
return $newpath;
} else {
return false;
}
}
function features_array_contains( $value, $array, $case_insensitive = false ) {
foreach( $array as $item ) {
if( $item->name == $value ) {
return true;
} elseif( $case_insensitive && strtolower($item->name) == strtolower($value) ) {
return true;
}
}
return false;
}
function insert_arff_features( $did, $features ) {
$ci = &get_instance();
foreach( $features as $f ) {
$feature_array = (array) $f;
$feature_array['did'] = $did;
$ci->Data_feature->insert( $feature_array );
}
}
function insert_arff_qualities($did, $qualities) {
$ci = &get_instance();
foreach( $qualities as $q ) {
$quality = array( 'data' => $did, 'quality' => $q->name, 'value' => $q->value );
if( property_exists($q, 'label') ) {
$quality['label'] = $q->label;
}
if( $ci->Data_quality->getWhere( assoc_to_string( $quality, array('value') ) ) === false ) {
$ci->Data_quality->insert( $quality );
}
}
}
// @param: $filter: all entries of filter will NOT be used in result
function assoc_to_string( $arr, $filter ) {
$new_arr = array();
foreach( $arr as $key => $value ) {
if( in_array( $key, $filter ) == false ) {
$new_arr[] = $key . ' = "' . $value . '" ';
}
}
return implode( ' AND ', $new_arr );
}
function xml_size( $xml_resource, $xml_field ) {
if( $xml_resource->children('oml', true)->{$xml_field} ) {
return count( $xml_resource->children('oml', true)->{$xml_field} );
} else {
return 0;
}
}
function xml_subsize( $xml_resource, $xml_field, $subfield ) {
if( $xml_resource->children('oml', true)->{$xml_field} ) {
return count( $xml_resource->children('oml', true)->{$xml_field}->children('oml', true)->{$subfield} );
} else {
return 0;
}
}
function xml2object ( $xmlObject, $attributes = false ) {
$out = new stdClass ();
foreach ( (array) $xmlObject as $index => $node )
$out->{$index} = ( is_object ( $node ) ) ? xml2object ( $node ) : $node;
if( $attributes ) {
foreach ( $xmlObject->attributes() as $index => $node ) {
$out->{$index} = ''. $node;
}
}
return $out;
}
function xml2assoc ( $xmlObject, $attributes = false ) {
$out = array ();
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2object ( $node ) : $node;
if( $attributes ) {
foreach ( $xmlObject->attributes() as $index => $node ) {
$out[$index] = ''. $node;
}
}
return $out;
}
?>