Example #1
0
function insertImplementationFromXML($xml, $configuration, $implementation_base = array())
{
    $ci =& get_instance();
    $implementation_objects = all_tags_from_xml($xml, array_custom_filter($configuration, array('plain', 'array')));
    $implementation = all_tags_from_xml($xml, array_custom_filter($configuration, array('string', 'csv')), $implementation_base);
    // insert the implementation itself
    $version = $ci->Implementation->incrementVersionNumber($implementation['name']);
    $implementation['fullName'] = $implementation['name'] . '(' . $version . ')';
    $implementation['version'] = $version;
    if (array_key_exists('source_md5', $implementation)) {
        if (array_key_exists('external_version', $implementation) === false) {
            $implementation['external_version'] = $implementation['source_md5'];
        }
    } elseif (array_key_exists('binary_md5', $implementation)) {
        if (array_key_exists('external_version', $implementation) === false) {
            $implementation['external_version'] = $implementation['binary_md5'];
        }
    }
    if (array_key_exists('implements', $implementation)) {
        if (in_array($implementation['implements'], $ci->supportedMetrics) == false && in_array($implementation['implements'], $ci->supportedAlgorithms == false)) {
            return false;
        }
    }
    // information illegal to insert
    unset($implementation['source_md5']);
    unset($implementation['binary_md5']);
    // tags also not insertable. but handled differently.
    $tags = array();
    if (array_key_exists('tag', $implementation)) {
        $tags = str_getcsv($implementation['tag']);
        unset($implementation['tag']);
    }
    $res = $ci->Implementation->insert($implementation);
    if ($res === false) {
        return false;
    }
    foreach ($tags as $tag) {
        $error = -1;
        tag_item('implementation', $res, $tag, $implementation_base['uploader'], $error);
    }
    // add to elastic search index.
    $ci->elasticsearch->index('flow', $res);
    // insert all important "components"
    foreach ($implementation_objects as $key => $value) {
        if ($key == 'component') {
            foreach ($value as $entry) {
                $component = $entry->implementation->children('oml', true);
                $similarComponent = $ci->Implementation->compareToXml($entry->implementation);
                if ($similarComponent === false) {
                    $component->version = $ci->Implementation->incrementVersionNumber($component->name);
                    $componentFullName = $component->name . '(' . $component->version . ')';
                    $succes = insertImplementationFromXML($component, $configuration, array('uploadDate' => now(), 'uploader' => $implementation['uploader']));
                    if ($succes == false) {
                        return false;
                    }
                    $ci->Implementation->addComponent($res, $succes, trim($entry->identifier));
                } else {
                    $ci->Implementation->addComponent($res, $similarComponent, trim($entry->identifier));
                }
            }
        } elseif ($key == 'bibliographical_reference') {
            foreach ($value as $entry) {
                $children = $entry->children('oml', true);
                $children->implementation_id = $res;
                $succes = $ci->Bibliographical_reference->insert($children);
            }
        } elseif ($key == 'parameter') {
            foreach ($value as $entry) {
                $children = $entry->children('oml', true);
                $succes = $ci->Input->insert(array('fullName' => $implementation['fullName'] . '_' . $children->name, 'implementation_id' => $res, 'name' => trim($children->name), 'defaultValue' => property_exists($children, 'default_value') ? trim($children->default_value) : null, 'description' => property_exists($children, 'description') ? trim($children->description) : null, 'dataType' => property_exists($children, 'data_type') ? trim($children->data_type) : null, 'recommendedRange' => property_exists($children, 'recommended_range') ? trim($children->recommendedRange) : null));
            }
        }
    }
    return $res;
}
Example #2
0
 private function _openml_implementation_upload()
 {
     if (isset($_FILES['source']) && $_FILES['source']['error'] == 0) {
         $source = true;
     } else {
         $source = false;
         unset($_FILES['source']);
     }
     if (isset($_FILES['binary']) && $_FILES['binary']['error'] == 0) {
         $binary = true;
     } else {
         $binary = false;
         unset($_FILES['binary']);
     }
     if ($source == false && $binary == false) {
         $this->_returnError(162);
         return;
     }
     foreach ($_FILES as $key => $file) {
         if (check_uploaded_file($file) == false) {
             $this->_returnError(160);
             return;
         }
     }
     // get correct description
     if ($this->input->post('description')) {
         // get description from string upload
         $description = $this->input->post('description');
         $xmlErrors = "";
         if (validateXml($description, xsd('openml.implementation.upload'), $xmlErrors, false) == false) {
             $this->_returnError(163, $this->openmlGeneralErrorCode, $xmlErrors);
             return;
         }
         $xml = simplexml_load_string($description);
     } elseif (isset($_FILES['description'])) {
         // get description from file upload
         $description = $_FILES['description'];
         if (validateXml($description['tmp_name'], xsd('openml.implementation.upload'), $xmlErrors) == false) {
             $this->_returnError(163, $this->openmlGeneralErrorCode, $xmlErrors);
             return;
         }
         $xml = simplexml_load_file($description['tmp_name']);
         $similar = $this->Implementation->compareToXML($xml);
         if ($similar) {
             $this->_returnError(171, $this->openmlGeneralErrorCode, 'implementation_id:' . $similar);
             return;
         }
     } else {
         $this->_returnError(161);
         return;
     }
     $name = '' . $xml->children('oml', true)->{'name'};
     $implementation = array('uploadDate' => now(), 'uploader' => $this->user_id);
     foreach ($_FILES as $key => $file) {
         if ($key == 'description') {
             continue;
         }
         if (!in_array($key, array('description', 'source', 'binary'))) {
             $this->_returnError(167);
             return;
         }
         $file_id = $this->File->register_uploaded_file($_FILES[$key], $this->data_folders['implementation'] . $key . '/', $this->user_id, 'implementation');
         if ($file_id === false) {
             $this->_returnError(165);
             return;
         }
         $file_record = $this->File->getById($file_id);
         //$implementation[$key.'Url'] = $this->data_controller . 'download/' . $file_id . '/' . $file_record->filename_original;
         $implementation[$key . '_md5'] = $file_record->md5_hash;
         $implementation[$key . '_file_id'] = $file_id;
         //$implementation[$key.'Format'] = $file_record->md5_hash;
         if (property_exists($xml->children('oml', true), $key . '_md5')) {
             if ($xml->children('oml', true)->{$key . '_md5'} != $file_record->md5_hash) {
                 $this->_returnError(168);
                 return;
             }
         }
     }
     $impl = insertImplementationFromXML($xml->children('oml', true), $this->xml_fields_implementation, $implementation);
     if ($impl == false) {
         $this->_returnError(165);
         return;
     }
     $implementation = $this->Implementation->getById($impl);
     $this->_xmlContents('implementation-upload', $implementation);
 }