protected function getTriples($file)
 {
     if (!file_exists($file)) {
         throw new Exception($file . ' not found');
     }
     // validate the file to import
     $parser = new tao_models_classes_Parser($file, array('extension' => 'rdf'));
     $parser->validate();
     if (!$parser->isValid()) {
         throw new common_Exception('Invalid RDF file ' . $file);
     }
     $modelDefinition = new EasyRdf_Graph();
     $modelDefinition->parseFile($file);
     /*
     $graph = $modelDefinition->toRdfPhp();
     $resources = $modelDefinition->resources();
     */
     $format = EasyRdf_Format::getFormat('php');
     $data = $modelDefinition->serialise($format);
     $triples = array();
     foreach ($data as $subjectUri => $propertiesValues) {
         foreach ($propertiesValues as $prop => $values) {
             foreach ($values as $k => $v) {
                 $triples[] = array('s' => $subjectUri, 'p' => $prop, 'o' => $v['value'], 'l' => isset($v['lang']) ? $v['lang'] : '');
             }
         }
     }
     return $triples;
 }
예제 #2
0
 /**
  * (non-PHPdoc)
  * @see tao_models_classes_import_ImportHandler::import()
  */
 public function import($class, $form)
 {
     $fileInfo = $form->getValue('source');
     $file = $fileInfo['uploaded_file'];
     //validate the file to import
     $parser = new tao_models_classes_Parser($file, array('extension' => 'rdf'));
     $parser->validate();
     if (!$parser->isValid()) {
         $report = common_report_Report::createFailure(__('Nothing imported'));
         $report->add($parser->getReport());
         return $report;
     } else {
         return $this->flatImport($file, $class);
     }
 }
예제 #3
0
 /**
  * Short description of method run
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @return void
  */
 public function run()
 {
     $userService = tao_models_classes_UserService::singleton();
     $this->outVerbose("Connecting to TAO as '" . $this->options['user'] . "' ...");
     if ($userService->loginUser($this->options['user'], $this->options['password'])) {
         $this->outVerbose("Connected to TAO as '" . $this->options['user'] . "'.");
         //determine the target namespace.
         $targetNamespace = rtrim(common_ext_NamespaceManager::singleton()->getLocalNamespace()->getUri(), '#');
         if (!empty($this->options['model'])) {
             $targetNamespace = $this->options['model'];
         } else {
             // Look for XML Base.
             $this->outVerbose("Looking up for the value of xml:base as the model URI.");
             try {
                 $dom = new DOMDocument('1.0', TAO_DEFAULT_ENCODING);
                 if (false !== $dom->load($this->options['input'])) {
                     $this->outVerbose("RDF-XML document loaded.");
                     // try to find the 'xml:base' attribute on the root node.
                     $roots = $dom->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'RDF');
                     if ($roots->length > 0) {
                         $this->outVerbose("Root RDF element found.");
                         $root = $roots->item(0);
                         $node = $root->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'base');
                         if (!empty($node)) {
                             $targetNamespace = $node->value;
                         } else {
                             $this->outVerbose("No xml:base attribute found. The local model will be used.");
                         }
                     }
                 } else {
                     $this->err("RDF-XML could not be loaded.", true);
                 }
             } catch (DOMException $e) {
                 $this->err("RDF-XML parsing error: " . $e->getMessage(), true);
             }
         }
         //validate the file to import
         $parser = new tao_models_classes_Parser($this->options['input'], array('extension' => 'rdf'));
         $parser->validate();
         $this->outVerbose("Model URI is '{$targetNamespace}'.");
         if (!$parser->isValid()) {
             foreach ($parser->getErrors() as $error) {
                 $this->outVerbose("RDF-XML parsing error in '" . $error['file'] . "' at line '" . $error['line'] . "': '" . $error['message'] . "'.");
             }
             $userService->logout();
             $this->err("RDF-XML parsing error.", true);
         } else {
             //initialize the adapter (no target class but a target namespace)
             $adapter = new tao_helpers_data_GenerisAdapterRdf();
             if ($adapter->import($this->options['input'], null, $targetNamespace)) {
                 $this->outVerbose("RDF 'input' file successfully imported.");
                 $userService->logout();
             } else {
                 $userService->logout();
                 $this->err("An error occured during RDF-XML import.", true);
             }
         }
     } else {
         $this->err("Unable to connect to TAO as '" . $this->options['user'] . "'.", true);
     }
 }