diff --git a/openml_OS/models/api/v1/Api_data.php b/openml_OS/models/api/v1/Api_data.php index c2c52de77..7d86bb5b3 100644 --- a/openml_OS/models/api/v1/Api_data.php +++ b/openml_OS/models/api/v1/Api_data.php @@ -79,6 +79,11 @@ function bootstrap($format, $segments, $request_type, $user_id) { 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; @@ -232,6 +237,81 @@ private function data_list($segs) { $this->xmlContents('data', $this->version, array('datasets' => $datasets)); } + 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; + } + // check if user owns dataset + if($dataset->uploader != $this->user_id and !$this->user_has_admin_rights) { + $this->returnError( 1065, $this->version ); + return; + } + + $update_result = $this->Dataset->update($data_id, $update_fields); + // If result returns error + if( $update_result == false ) { + $this->returnError( 1066, $this->version ); + return; + } + + // Return data id, for user to verify changes + $this->xmlContents( 'data-edit', $this->version, array( 'dataset' => $dataset ) ); + } + + private function data($data_id) { if( $data_id == false ) { $this->returnError( 110, $this->version ); diff --git a/openml_OS/views/pages/api_new/v1/xml/data-edit.tpl.php b/openml_OS/views/pages/api_new/v1/xml/data-edit.tpl.php new file mode 100644 index 000000000..623052446 --- /dev/null +++ b/openml_OS/views/pages/api_new/v1/xml/data-edit.tpl.php @@ -0,0 +1,3 @@ + + did; ?> + diff --git a/openml_OS/views/pages/api_new/v1/xml/pre.php b/openml_OS/views/pages/api_new/v1/xml/pre.php index e4bbf62a7..c54338450 100644 --- a/openml_OS/views/pages/api_new/v1/xml/pre.php +++ b/openml_OS/views/pages/api_new/v1/xml/pre.php @@ -509,4 +509,15 @@ $this->apiErrors[1053] = 'Study not owned by you'; $this->apiErrors[1054] = 'Problem inserting in database'; + +//openml.data.edit +//openml.data.edit +$this->apiErrors[1060] = 'Problem validating edit_parameters xml'; +$this->apiErrors[1061] = 'Please provide edit_parameters xml'; +$this->apiErrors[1062] = 'Data ID is required'; +$this->apiErrors[1063] = 'Unknown dataset'; +$this->apiErrors[1064] = 'Please provide atleast one field among description, creator, contributor, collection_date, language, citation, original_data_url or paper_url to edit. '; +$this->apiErrors[1065] = 'Dataset is not owned by you'; +$this->apiErrors[1066] = 'Dataset update failed'; + ?> diff --git a/openml_OS/views/pages/api_new/v1/xsd/openml.data.edit.xsd b/openml_OS/views/pages/api_new/v1/xsd/openml.data.edit.xsd new file mode 100644 index 000000000..5adc33db9 --- /dev/null +++ b/openml_OS/views/pages/api_new/v1/xsd/openml.data.edit.xsd @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +