-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathApi_data.php
More file actions
2443 lines (2206 loc) · 90.4 KB
/
Api_data.php
File metadata and controls
2443 lines (2206 loc) · 90.4 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class Api_data extends MY_Api_Model {
protected $version = 'v1';
function __construct() {
parent::__construct();
// load models
$this->load->model('Data_processed');
$this->load->model('Dataset');
$this->load->model('Dataset_status');
$this->load->model('Dataset_tag');
$this->load->model('Dataset_topic');
$this->load->model('Dataset_description');
$this->load->model('Data_feature');
$this->load->model('Data_feature_description');
$this->load->model('Data_feature_value');
$this->load->model('Data_quality');
$this->load->model('Feature_quality');
$this->load->model('Data_quality_interval');
$this->load->model('Quality');
$this->load->model('File');
$this->load->model('Study_tag');
$this->load->helper('file_upload');
$this->db = $this->Database_singleton->getWriteConnection();
$this->legal_formats = array('arff', 'sparse_arff');
}
function bootstrap($format, $segments, $request_type, $user_id) {
$this->outputFormat = $format;
$getpost = array('get','post');
if (count($segments) >= 1 && $segments[0] == 'list') {
array_shift($segments);
$this->data_list($segments);
return;
}
if (count($segments) == 1 && is_numeric($segments[0]) && in_array($request_type, $getpost)) {
$this->data($segments[0]);
return;
}
$order_values = array('random', 'normal');
if (count($segments) == 3 && $segments[0] == 'unprocessed' && is_numeric($segments[1]) && in_array($segments[2], $order_values)) {
$this->data_unprocessed($segments[1], $segments[2]);
return;
}
if (count($segments) >= 4 && count($segments) <= 6 && $segments[0] == 'qualities' && $segments[1] == 'unprocessed' && is_numeric($segments[2]) && in_array($segments[3], $order_values)) {
$feature = (count($segments) > 4 && $segments[4] == 'feature');
// oops, badly defined api call with two optional parameters. boolean feature and string priority tag.
// we will try to fix this here.
if ($feature && count($segments) == 6) {
$priorityTag = $segments[5];
} elseif ($feature == false && count($segments) == 5) {
$priorityTag = $segments[4];
} else {
$priorityTag = null;
}
$this->dataqualities_unprocessed($segments[2], $segments[3], $feature, $priorityTag);
return;
}
if (count($segments) == 1 && is_numeric($segments[0]) && $request_type == 'delete') {
$this->data_delete($segments[0]);
return;
}
if (count($segments) == 2 && $segments[0] == 'reset' && is_numeric($segments[1]) && $request_type == 'post') {
$this->data_reset($segments[1]);
return;
}
if (count($segments) == 0 && $request_type == 'post') {
$this->data_upload();
return;
}
if ( $segments[0] == 'fork' && $request_type == 'post') {
$this->data_fork();
return;
}
if ( $segments[0] == 'edit' && $request_type == 'post') {
$this->data_edit();
return;
}
if (count($segments) == 2 && $segments[0] == 'features' && is_numeric($segments[1]) && in_array($request_type, $getpost)) {
$this->data_features($segments[1]);
return;
}
if (count($segments) == 1 && $segments[0] == 'features' && $request_type == 'post') {
$this->data_features_upload($segments[0]);
return;
}
if (count($segments) == 2 && $segments[0] == 'qualities' && $segments[1] == 'list' && in_array($request_type, $getpost)) {
$this->data_qualities_list($segments[1]);
return;
}
if (count($segments) == 3 && $segments[0] == 'description' && $segments[1] == 'list' && is_numeric($segments[2]) && in_array($request_type, $getpost)) {
$this->data_description_list($segments[2]);
return;
}
if (count($segments) == 2 && $segments[0] == 'qualities' && is_numeric($segments[1]) && in_array($request_type, $getpost)) {
$this->data_qualities($segments[1], $this->config->item('default_evaluation_engine_id'));
return;
} elseif(count($segments) == 3 && $segments[0] == 'qualities' && is_numeric($segments[1]) && is_numeric($segments[2]) && in_array($request_type, $getpost)) {
$this->data_qualities($segments[1], $segments[2]);
return;
}
if (count($segments) == 3 && $segments[0] == 'features' && $segments[1] == 'qualities' && $segments[2] == 'list' && in_array($request_type, $getpost)) {
$this->feature_qualities_list($segments[2]);
return;
}
if (count($segments) == 3 && $segments[0] == 'features' && $segments[1] == 'qualities' && is_numeric($segments[2]) && in_array($request_type, $getpost)) {
$this->feature_qualities($segments[2], $this->config->item('default_evaluation_engine_id'));
return;
} elseif (count($segments) == 4 && $segments[0] == 'features' && $segments[1] == 'qualities' && is_numeric($segments[2]) && is_numeric($segments[3]) && in_array($request_type, $getpost)) {
$this->feature_qualities($segments[2], $segments[3]);
return;
}
if (count($segments) == 1 && $segments[0] == 'qualities' && $request_type == 'post') {
$this->data_qualities_upload($segments[0]);
return;
}
if (count($segments) == 1 && $segments[0] == 'tag' && $request_type == 'post') {
$this->data_tag($this->input->post('data_id'), $this->input->post('tag'));
return;
}
if (count($segments) == 1 && $segments[0] == 'untag' && $request_type == 'post') {
$this->data_untag($this->input->post('data_id'), $this->input->post('tag'));
return;
}
if ( $segments[0] == 'topicadd' && $request_type == 'post') {
$this->data_add_topic($this->input->post('data_id'), $this->input->post('topic'));
return;
}
if ( $segments[0] == 'topicdelete' && $request_type == 'post') {
$this->data_delete_topic($this->input->post('data_id'), $this->input->post('topic'));
return;
}
if (count($segments) == 2 && $segments[0] == 'tag' && $segments[1] == 'list') {
$this->list_tags('dataset', 'data');
return;
}
if (count($segments) == 3 && $segments[0] == 'feature' && $segments[1] == 'ontology' && $segments[2] == 'add' && $request_type == 'post') {
$this->data_feature_description($this->input->post('data_id'), $this->input->post('index'), $this->input->post('ontology'), 'ontology', true);
return;
}
if (count($segments) == 3 && $segments[0] == 'feature' && $segments[1] == 'ontology' && $segments[2] == 'remove' && $request_type == 'post') {
$this->data_feature_description($this->input->post('data_id'), $this->input->post('index'), $this->input->post('ontology'), 'ontology', false);
return;
}
if (count($segments) == 2 && $segments[0] == 'status' && $segments[1] == 'update') {
$this->status_update($this->input->post('data_id'), $this->input->post('status'));
return;
}
$this->returnError(100, $this->version);
}
private function data_feature_description($data_id, $feature_idx, $description, $description_type, $do_add) {
if ($data_id === false || $feature_idx === false || $description === false) {
$this->returnError(1100, $this->version);
return false;
}
if (strlen($description) > 256) {
$this->returnError(1105, $this->version);
return false;
}
if ($description_type == 'ontology' && !filter_var($description, FILTER_VALIDATE_URL)) {
$this->returnError(1106, $this->version);
return false;
}
if ($do_add) {
$descriptions = $this->Data_feature_description->getColumnWhere('value', '`did` = "' . $data_id . '" AND `index` = "'. $feature_idx . '" AND `description_type` = "' . $description_type . '"');
if($descriptions != false && in_array($description, $descriptions)) {
$this->returnError(1101, $this->version, 450, 'id=' . $data_id . '; description=' . $description);
return false;
}
// todo discuss policy: who is allowed to add ontology to a feature?
$description_data = array(
'did' => $data_id,
'index' => $feature_idx,
'description_type' => $description_type,
'value' => $description,
'uploader' => $this->user_id,
'date' => now()
);
$res = $this->Data_feature_description->insert($description_data);
if ($res == false) {
$this->returnError(1102, $this->version, 450, 'id=' . $data_id . '; description=' . $description);
return false;
}
} else {
$description_record = $this->Data_feature_description->getWhereSingle('did = ' . $data_id . ' AND index = "' . $feature_idx . '" AND `description_type` = "' . $description_type . '" AND `value` = "' . $description . '"');
if ($description_record == false) {
$this->returnError(1103, $this->version);
return false;
}
// todo discuss policy: who is allowed to remove ontology from a feature?
$is_admin = $this->ion_auth->is_admin($this->user_id);
if ($description_record->uploader != $this->user_id && $is_admin == false) {
$this->returnError(1104, $this->version);
return false;
}
$this->Data_feature_description->delete(array($data_id, $feature_idx, $description));
}
try {
$this->elasticsearch->index('data', $data_id);
} catch (Exception $e) { }
$descriptions = $this->Data_feature_description->getColumnWhere('value', 'did = ' . $data_id . ' AND index = "' . $feature_idx . '" AND `description_type` = "' . $description_type . '"');
$this->xmlContents(
'data-feature-description',
$this->version,
array(
'id' => $data_id,
'description_type' => $description_type,
'xml_tag_name' => 'feature_description' . '_' . ($do_add ? 'add' : 'remove'),
'descriptions' => $descriptions)
);
}
/**
*@OA\Post(
* path="/data/tag",
* tags={"data"},
* summary="Tag a dataset",
* description="Tags a dataset.",
* @OA\Parameter(
* name="data_id",
* in="query",
* @OA\Schema(
* type="integer"
* ),
* description="Id of the dataset.",
* required=true,
* ),
* @OA\Parameter(
* name="tag",
* in="query",
* @OA\Schema(
* type="string"
* ),
* description="Tag name",
* required=true,
* ),
* @OA\Parameter(
* name="api_key",
* in="query",
* description="Api key to authenticate the user",
* required=true,
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Response(
* response=200,
* description="The id of the tagged dataset",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="data_tag",
* ref="#/components/schemas/inline_response_200_2_data_tag",
* ),
* example={
* "data_tag": {
* "id": "2"
* }
* }
* ),
* ),
* @OA\Response(
* response=412,
* description="Precondition failed. An error code and message are returned.\n470 - In order to add a tag, please upload the entity id (either data_id, flow_id, run_id) and tag (the name of the tag).\n471 - Entity not found. The provided entity_id {data_id, flow_id, run_id} does not correspond to an existing entity.\n472 - Entity already tagged by this tag. The entity {dataset, flow, run} already had this tag.\n473 - Something went wrong inserting the tag. Please contact OpenML Team.\n474 - Internal error tagging the entity. Please contact OpenML Team.\n",
* @OA\JsonContent(
* ref="#/components/schemas/Error",
* ),
* ),
*)
*/
private function data_tag($data_id, $tag) {
$this->data_tag_untag($data_id, $tag, false);
}
/**
*@OA\Post(
* path="/data/untag",
* tags={"data"},
* summary="Untag a dataset",
* description="Untags a dataset.",
* @OA\Parameter(
* name="data_id",
* in="query",
* @OA\Schema(
* type="integer"
* ),
* description="Id of the dataset.",
* required=true,
* ),
* @OA\Parameter(
* name="tag",
* in="query",
* @OA\Schema(
* type="string"
* ),
* description="Tag name",
* required=true,
* ),
* @OA\Parameter(
* name="api_key",
* in="query",
* @OA\Schema(
* type="string"
* ),
* description="Api key to authenticate the user",
* required=true,
* ),
* @OA\Response(
* response=200,
* description="The ID of the untagged dataset",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="data_untag",
* ref="#/components/schemas/inline_response_200_3_data_untag",
* ),
* example={
* "data_untag": {
* "id": "2"
* }
* }
* ),
* ),
* @OA\Response(
* response=412,
* description="Precondition failed. An error code and message are returned.\n475 - Please give entity_id {data_id, flow_id, run_id} and tag. In order to remove a tag, please upload the entity id (either data_id, flow_id, run_id) and tag (the name of the tag).\n476 - Entity {dataset, flow, run} not found. The provided entity_id {data_id, flow_id, run_id} does not correspond to an existing entity.\n477 - Tag not found. The provided tag is not associated with the entity {dataset, flow, run}.\n478 - Tag is not owned by you. The entity {dataset, flow, run} was tagged by another user. Hence you cannot delete it.\n479 - Internal error removing the tag. Please contact OpenML Team.\n",
* @OA\JsonContent(
* ref="#/components/schemas/Error",
* ),
* ),
*)
*/
private function data_untag($data_id, $tag) {
$this->data_tag_untag($data_id, $tag, true);
}
private function data_tag_untag($data_id,$tag, $do_untag) {
// forward action to superclass
$this->entity_tag_untag('dataset', $data_id, $tag, $do_untag, 'data');
}
/**
*@OA\Get(
* path="/data/list/{filters}",
* tags={"data"},
* summary="List and filter datasets",
* description="List datasets, possibly filtered by a range of properties. Any number of properties can be combined by listing them one after the other in the form '/data/list/{filter}/{value}/{filter}/{value}/...' Returns an array with all datasets that match the constraints.",
* @OA\Parameter(
* name="filters",
* in="path",
* @OA\Schema(
* type="string"
* ),
* description="Any combination of these filters
/limit/{limit}/offset/{offset} - returns only {limit} results starting from result number {offset}. Useful for paginating results. With /limit/5/offset/10, results 11..15 will be returned. Both limit and offset need to be specified.
/status/{status} - returns only datasets with a given status, either 'active', 'deactivated', or 'in_preparation'.
/tag/{tag} - returns only datasets tagged with the given tag.
/{data_quality}/{range} - returns only tasks for which the underlying datasets have certain qualities. {data_quality} can be data_id, data_name, data_version, number_instances, number_features, number_classes, number_missing_values. {range} can be a specific value or a range in the form 'low..high'. Multiple qualities can be combined, as in 'number_instances/0..50/number_features/0..10'.
",
* required=true,
* ),
* @OA\Response(
* response=200,
* description="A list of datasets with the given task",
* @OA\JsonContent(
* ref="#/components/schemas/DataList",
* example={
* "data": {
* "dataset": {
* {
* "did":"1",
* "name":"anneal",
* "status":"active",
* "format":"ARFF",
* "quality":{
* {
* "name":"MajorityClassSize",
* "value":"684"
* },
* {
* "name":"MaxNominalAttDistinctValues",
* "value":"10.0"
* },
* {
* "name":"MinorityClassSize"
* ,"value":"0"
* },
* {
* "name":"NumBinaryAtts",
* "value":"14.0"
* },
* {
* "name":"NumberOfClasses",
* "value":"6"
* },
* {
* "name":"NumberOfFeatures",
* "value":"39"
* },
* {
* "name":"NumberOfInstances",
* "value":"898"
* },
* {
* "name":"NumberOfInstancesWithMissingValues",
* "value":"0"
* },
* {
* "name":"NumberOfMissingValues",
* "value":"0"
* },
* {
* "name":"NumberOfNumericFeatures",
* "value":"6"
* },
* {
* "name":"NumberOfSymbolicFeatures",
* "value":"32"
* }
* }
* }
* }
* }
* }
* ),
* ),
* @OA\Response(
* response=412,
* description="Precondition failed. An error code and message are returned.\n370 - Illegal filter specified.\n371 - Filter values/ranges not properly specified.\n372 - No results. There where no matches for the given constraints.\n373 - Can not specify an offset without a limit.\n",
* @OA\JsonContent(
* ref="#/components/schemas/Error",
* ),
* ),
*)
*/
private function data_list($segs) {
$legal_filters = array('tag', 'status', 'limit', 'offset', 'data_id', 'data_name', 'data_version', 'uploader', 'number_instances', 'number_features', 'number_classes', 'number_missing_values');
list($query_string, $illegal_filters) = $this->parse_filters($segs, $legal_filters);
if (count($illegal_filters) > 0) {
$this->returnError(370, $this->version, $this->openmlGeneralErrorCode, 'Legal filter operators: ' . implode(',', $legal_filters) .'. Found illegal filter(s): ' . implode(', ', $illegal_filters));
return;
}
$illegal_filter_inputs = $this->check_filter_inputs($query_string, $legal_filters, array('tag', 'status', 'data_name', 'number_instances', 'number_features', 'number_classes', 'number_missing_values'));
if (count($illegal_filter_inputs) > 0) {
$this->returnError(371, $this->version, $this->openmlGeneralErrorCode, 'Filters with illegal values: ' . implode(',', $illegal_filter_inputs));
return;
}
$tag = element('tag', $query_string, null);
$name = element('data_name', $query_string, null);
$data_id = element('data_id', $query_string, null);
$uploader = element('uploader', $query_string, null);
if ($uploader !== null) {
$uploader = (int) $uploader;
}
$version = element('data_version', $query_string, null);
$status = element('status', $query_string, null);
$limit = element('limit', $query_string, null);
$offset = element('offset', $query_string, null);
$nr_insts = element('number_instances', $query_string, null);
$nr_feats = element('number_features', $query_string, null);
$nr_class = element('number_classes', $query_string, null);
$nr_miss = element('number_missing_values', $query_string, null);
if ($offset && !$limit) {
$this->returnError(373, $this->version);
return;
}
$where_tag = $tag === null ? '' : ' AND `d`.`did` IN (select id from dataset_tag where tag="' . $tag . '") ';
$where_did = $data_id === null ? '' : ' AND `d`.`did` IN ('. $data_id . ') ';
$where_name = $name === null ? '' : ' AND `name` = "' . $name . '"';
$where_uploader = $uploader === null ? '' : ' AND `uploader` = ' . $uploader . ' ';
$where_version = $version === null ? '' : ' AND `version` = "' . $version . '" ';
$where_insts = $nr_insts === null ? '' : ' AND `d`.`did` IN (select data from data_quality dq where quality="NumberOfInstances" and value ' . (strpos($nr_insts, '..') !== false ? 'BETWEEN ' . str_replace('..',' AND ',$nr_insts) : '= '. $nr_insts) . ') ';
$where_feats = $nr_feats === null ? '' : ' AND `d`.`did` IN (select data from data_quality dq where quality="NumberOfFeatures" and value ' . (strpos($nr_feats, '..') !== false ? 'BETWEEN ' . str_replace('..',' AND ',$nr_feats) : '= '. $nr_feats) . ') ';
$where_class = $nr_class === null ? '' : ' AND `d`.`did` IN (select data from data_quality dq where quality="NumberOfClasses" and value ' . (strpos($nr_class, '..') !== false ? 'BETWEEN ' . str_replace('..',' AND ',$nr_class) : '= '. $nr_class) . ') ';
$where_miss = $nr_miss === null ? '' : ' AND `d`.`did` IN (select data from data_quality dq where quality="NumberOfMissingValues" and value ' . (strpos($nr_miss, '..') !== false ? 'BETWEEN ' . str_replace('..',' AND ',$nr_miss) : '= '. $nr_miss) . ') ';
// by default, only return active datasets
$status_sql_variable = 'IFNULL(`s`.`status`, \'' . $this->config->item('default_dataset_status') . '\')';
$where_status = $status === null ? ' AND ' . $status_sql_variable . ' = "active" ' : ($status != "all" ? ' AND ' . $status_sql_variable . ' = "'. $status . '" ' : '');
$where_total = $where_tag . $where_did . $where_name . $where_version . $where_uploader . $where_insts . $where_feats . $where_class . $where_miss . $where_status;
$where_limit = $limit === null ? '' : ' LIMIT ' . $limit;
if($limit && $offset){
$where_limit = ' LIMIT ' . $offset . ',' . $limit;
}
$sql = 'SELECT d.*, ' . $status_sql_variable . ' AS `status` '.
'FROM dataset d ' .
'LEFT JOIN (SELECT `did`, MAX(`status`) AS `status` FROM `dataset_status` GROUP BY `did`) s ON d.did = s.did ' .
'WHERE (visibility = "public" or uploader='.$this->user_id.') '. $where_total . $where_limit;
$datasets_res = $this->Dataset->query($sql);
if( is_array( $datasets_res ) == false || count( $datasets_res ) == 0 ) {
$this->returnError( 372, $this->version );
return;
}
// make associative
$datasets = array();
foreach( $datasets_res as $dataset ) {
$dataset->qualities = array();
$datasets[$dataset->did] = $dataset;
}
# JvR: This is a BAD idea and this will break in the future, when OpenML grows.
$sql =
'SELECT data, quality, value FROM data_quality ' .
'WHERE `data` IN (' . implode(',', array_keys($datasets)) . ') ' .
'AND evaluation_engine_id = ' . $this->config->item('default_evaluation_engine_id') . ' ' .
'AND quality IN ("' . implode('","', $this->config->item('basic_qualities')) . '") ' .
'AND value IS NOT NULL ' .
'ORDER BY `data`;';
$dq = $this->Data_quality->query($sql);
if ($dq != false) {
foreach($dq as $quality) {
$datasets[$quality->data]->qualities[$quality->quality] = $quality->value;
}
}
$this->xmlContents('data', $this->version, array('datasets' => $datasets));
}
private function data_fork() {
// get data id
$data_id = $this->input->post('data_id');
// If data id is not given
if( $data_id == false ) {
$this->returnError( 1070, $this->version );
return;
}
// If dataset does not exist
$dataset = $this->Dataset->getById( $data_id );
if( $dataset == false ) {
$this->returnError( 1071, $this->version );
return;
}
// create a copy
$dataset->uploader = $this->user_id;
$latest_version = $this->Dataset-> getWhereSingle('`name` = "' . $dataset->name . '"', 'CAST(`version` AS DECIMAL) DESC');
$dataset->version = $latest_version->version + 1;
unset($dataset->did);
$new_data_id = $this->Dataset->insert($dataset);
if (!$new_data_id) {
$this->returnError(1072, $this->version);
return;
}
// create a copy of the latest description
$description_record = $this->Dataset_description->getWhereSingle('did =' . $data_id, 'version DESC');
$description_record->did = $new_data_id;
$description_record->version = "1";
$this->Dataset_description->insert($description_record);
// update elastic search index.
try {
$this->elasticsearch->index('data', $new_data_id);
} catch (Exception $e) {
$this->returnError(105, $this->version, $this->openmlGeneralErrorCode, $e->getMessage());
return;
}
// Return data id, for user to verify changes
$this->xmlContents( 'data-fork', $this->version, array( 'id' => $new_data_id) );
}
private function data_edit() {
// get data id
$data_id = $this->input->post('data_id');
// get edit parameters as xml
$xsdFile = xsd('openml.data.edit', $this->controller, $this->version);
if($this->input->post('edit_parameters')) {
// get fields from string upload
$edit_parameters = $this->input->post('edit_parameters', false);
if(validateXml($edit_parameters, $xsdFile, $xmlErrors, false ) == false) {
$this->returnError(1060, $this->version, $this->openmlGeneralErrorCode, $xmlErrors);
return;
}
$xml = simplexml_load_string( $edit_parameters );
} elseif (isset($_FILES['edit_parameters'])) {
$uploadError = '';
$xmlErrors = '';
if (check_uploaded_file($_FILES['edit_parameters'], false, $uploadError) == false) {
$this->returnError(1061, $this->version, $this->openmlGeneralErrorCode, $uploadError);
}
// get fields from file upload
$edit_parameters = $_FILES['edit_parameters'];
if (validateXml($edit_parameters['tmp_name'], $xsdFile, $xmlErrors) == false) {
$this->returnError(1060, $this->version, $this->openmlGeneralErrorCode, $xmlErrors);
return;
}
$xml = simplexml_load_file($edit_parameters['tmp_name']);
} else {
$this->returnError(1061, $this->version);
return;
}
// create an array of update fields for the update
$update_fields = array();
foreach($xml->children('oml', true) as $input) {
// iterate over all fields, does not check for legal fields, as it wont match the xsd.
$name = $input->getName() . '';
$update_fields[$name] = $input . '';
}
// If data id is not given
if( $data_id == false ) {
$this->returnError( 1062, $this->version );
return;
}
// If dataset does not exist
$dataset = $this->Dataset->getById( $data_id );
if( $dataset == false ) {
$this->returnError( 1063, $this->version );
return;
}
// If all the fields are false, there is nothing to update, return error
if(!$update_fields) {
$this->returnError( 1064, $this->version );
return;
}
// If critical fields need to be edited
if (isset($update_fields['default_target_attribute']) || isset($update_fields['row_id_attribute']) || isset($update_fields['ignore_attribute'])) {
# Only owner can edit critical features
if($dataset->uploader != $this->user_id and !$this->user_has_admin_rights) {
$this->returnError(1065, $this->version);
return;
}
# Only datasets without tasks can allow critical feature edits
$tasks = $this->Task->getTasksWithValue( array( 'source_data' => $dataset->did ) );
if( $tasks !== false ) {
$this->returnError( 1066, $this->version );
return;
}
}
// Add description in the description table as a new version
if (isset($update_fields['description'])) {
$description_record = $this->Dataset_description->getWhereSingle('did =' . $data_id, 'version DESC');
$version_new = $description_record->version + 1;
$desc = array(
'did'=>$data_id,
'version' => $version_new,
'description'=>$update_fields['description'],
'uploader' => $dataset->uploader
);
unset($update_fields['description']);
$desc_id = $this->Dataset_description->insert($desc);
if (!$desc_id) {
$this->returnError(1067, $this->version);
return;
}
}
if ($update_fields) {
$update_result = $this->Dataset->update($data_id, $update_fields);
// If result returns error
if($update_result == false) {
$this->returnError(1068, $this->version);
return;
}
}
// update elastic search index.
try {
$this->elasticsearch->index('data', $data_id);
} catch (Exception $e) {
$this->returnError(105, $this->version, $this->openmlGeneralErrorCode, $e->getMessage());
}
// Return data id, for user to verify changes
$this->xmlContents( 'data-edit', $this->version, array( 'id' => $data_id) );
}
private function data_description_list($data_id) {
// Get descriptions for given id
$description_records = $this->Dataset_description->getWhere('did =' . $data_id, 'version DESC');
if( is_array( $description_records ) == false || count( $description_records ) == 0 ) {
$this->returnError( 1090, $this->version );
return;
}
// Return history
$this->xmlContents( 'data-description-list', $this->version, array('descriptions' => $description_records));
}
/**
*@OA\Get(
* path="/data/{id}",
* tags={"data"},
* summary="Get dataset description",
* description="Returns information about a dataset. The information includes the name, information about the creator, URL to download it and more.",
* @OA\Parameter(
* name="id",
* in="path",
* @OA\Schema(
* type="integer"
* ),
* description="Id of the dataset.",
* required=true,
* ),
* @OA\Response(
* response=200,
* description="A dataset description",
* @OA\JsonContent(
* ref="#/components/schemas/Data",
* example={
* "data_set_description": {
* "id": "1",
* "name": "anneal",
* "version": "2",
* "description": "...",
* "format": "ARFF",
* "upload_date": "2014-04-06 23:19:20",
* "licence": "Public",
* "url": "https://www.openml.org/data/download/1/dataset_1_anneal.arff",
* "file_id": "1",
* "default_target_attribute": "class",
* "version_label": "2",
* "tag": {
* "study_1",
* "uci"
* },
* "visibility": "public",
* "original_data_url": "https://www.openml.org/d/2",
* "status": "active",
* "md5_checksum": "d01f6ccd68c88b749b20bbe897de3713"
* }
* }
* ),
* ),
* @OA\Response(
* response=412,
* description="Precondition failed. An error code and message are returned\n110 - Please provide data_id.\n111 - Unknown dataset. Data set description with data_id was not found in the database.\n112 - No access granted. This dataset is not shared with you.\n",
* @OA\JsonContent(
* ref="#/components/schemas/Error",
* ),
* ),
*)
*/
private function data($data_id) {
if( $data_id == false ) {
$this->returnError( 110, $this->version );
return;
}
$dataset = $this->Dataset->getById( $data_id );
if( $dataset === false ) {
$this->returnError( 111, $this->version );
return;
}
if($dataset->visibility != 'public' &&
$dataset->uploader != $this->user_id &&
!$this->user_has_admin_rights) {
$this->returnError( 112, $this->version );
return;
}
// overwrite url field
if ($dataset->file_id != NULL) {
$dataset->url = BASE_URL . 'data/v1/download/' . $dataset->file_id . '/' . htmlspecialchars($dataset->name) . '.' . strtolower($dataset->format);
}
$file = $this->File->getById($dataset->file_id);
if (!$file) {
$this->returnError(113, $this->version);
return;
}
$dataset->md5_checksum = $file->md5_hash;
$tags = $this->Dataset_tag->getColumnWhere('tag', 'id = ' . $dataset->did);
$dataset->tag = $tags != false ? '"' . implode( '","', $tags ) . '"' : array();
$description_record = $this->Dataset_description->getWhereSingle('did =' . $data_id, 'version DESC');
$dataset->description_version = $description_record->version;
$dataset->description = $description_record->description;
foreach( $this->xml_fields_dataset['csv'] as $field ) {
$dataset->{$field} = getcsv( $dataset->{$field} );
}
$data_processed = $this->Data_processed->getById(array($data_id, $this->config->item('default_evaluation_engine_id')));
$relevant_fields = array('processing_date', 'error', 'warning');
foreach ($relevant_fields as $field) {
if ($data_processed !== false) {
$dataset->{$field} = $data_processed->{$field};
} else {
$dataset->{$field} = null;
}
}
$dataset->status = $this->config->item('default_dataset_status');
$data_status = $this->Dataset_status->getWhereSingle('did =' . $data_id, 'status_date DESC');
if ($data_status != false) {
$dataset->status = $data_status->status;
}
// The BASE_URL check prevents servering parquet urls from the test server,
// which is needed as long as the test server does not have its own dedicated
// MinIO with parquet files. TODO: Remove this after running MinIO on test
if ($dataset->format != 'Sparse_ARFF' && BASE_URL != "https://test.openml.org/") {
$bracket = sprintf('%04d', floor($data_id / 10000));
$padded_id = sprintf('%04d', $data_id);
$url = MINIO_URL . 'datasets/' . $bracket . '/' . $padded_id . '/dataset_' . $data_id . '.pq';
$dataset->parquet_url = $url;
$dataset->minio_url = $url;
}
$this->xmlContents( 'data-get', $this->version, $dataset );
}
private function data_reset($data_id) {
$dataset = $this->Dataset->getById($data_id);
if ($dataset == false) {
$this->returnError(1021, $this->version);
return;
}
if($dataset->uploader != $this->user_id and !$this->user_has_admin_rights) {
$this->returnError(1022, $this->version);
return;
}
$result = $this->Data_processed->deleteWhere('`did` = "' . $dataset->did . '" ');
if ($result == false) {
$this->returnError(1023, $this->version);
return;
}
// Temporary fix: makes sure that feature values are also removed when data is reset
// A new foreign key on Data_processed should be added to replace this
$result = $this->Data_feature_value->deleteWhere('`did` = "' . $dataset->did . '" ');
if ($result == false) {
$this->returnError(1023, $this->version);
return;
}
$this->xmlContents('data-reset', $this->version, array('dataset' => $dataset));
}
/**
*@OA\Delete(
* path="/data/{id}",
* tags={"data"},
* summary="Delete dataset",
* description="Deletes a dataset. Upon success, it returns the ID of the deleted dataset.",
* @OA\Parameter(
* name="id",
* in="path",
* @OA\Schema(
* type="integer"
* ),
* description="Id of the dataset.",
* required=true,
* ),
* @OA\Parameter(
* name="api_key",
* in="query",
* @OA\Schema(
* type="string"
* ),
* description="Api key to authenticate the user",
* required=true,
* ),
* @OA\Response(
* response=200,
* description="ID of the deleted dataset",
* @OA\JsonContent(
* type="object",
* @OA\Property(
* property="data_delete",
* ref="#/components/schemas/inline_response_200_data_delete",
* ),
* example={
* "data_delete": {
* "id": "4328"
* }
* }
* ),
* ),
* @OA\Response(
* response=412,
* description="Precondition failed. An error code and message are returned\n- 350 - Please provide API key. In order to remove your content, please authenticate.\n- 351 - Authentication failed. The API key was not valid. Please try to login again, or contact api administrators.\n- 352 - Dataset does not exists. The data ID could not be linked to an existing dataset.\n- 353 - Dataset is not owned by you. The dataset is owned by another user. Hence you cannot delete it.\n- 354 - Dataset is in use by other content. Can not be deleted. The data is used in tasks or runs. Delete other content before deleting this dataset.\n- 355 - Deleting dataset failed. Deleting the dataset failed. Please contact support team.\n",
* @OA\JsonContent(
* ref="#/components/schemas/Error",
* ),
* ),
*)
*/
private function data_add_topic($id, $topic) {
# Data id and topic are required
if ($id == false || $topic == false) {
$this->returnError(1080, $this->version);
return false;
}
# If dataset does not exist
$dataset = $this->Dataset->getById($id);
if($dataset == false) {
$this->returnError(1081, $this->version);
return;
}
# Restrict only to admin
if(!$this->user_has_admin_rights) {
$this->returnError(1082, $this->version);
return;
}
# Check if topic and id combination exists
$topics = $this->Dataset_topic->getColumnWhere('topic', 'id = ' . $id);
if($topics != false && in_array($topic, $topics)) {
$this->returnError(1083, $this->version);
return false;
}
$currentTime = now();
$topic_data = array(
'id' => $id,
'topic' => $topic,
'uploader' => $this->user_id,
'date' => $currentTime
);
# Insert into DB
$res = $this->Dataset_topic->insert($topic_data);
if ($res == false) {
$this->returnError(1084, $this->version);
return false;
}
try {
//update index
$this->elasticsearch->update_topics($id);
} catch (Exception $e) {
$this->returnError(105, $this->version, $this->openmlGeneralErrorCode, $e->getMessage(), false, $surpressOutput);
return false;
}
$this->xmlContents( 'data-topic', $this->version, array( 'id' => $id) );
}
private function data_delete_topic($id, $topic) {
# Data id and topic are required
if ($id == false || $topic == false) {
$this->returnError(1080, $this->version);
return false;
}
# If dataset does not exist
$dataset = $this->Dataset->getById($id);
if($dataset == false) {
$this->returnError(1081, $this->version);
return;