/** * @author "Lionel Lecaque, <*****@*****.**>" * @param string $namespace * @param string $data xml content */ public function createModel($namespace, $data) { $modelId = $this->getModelId($namespace); if ($modelId === false) { common_Logger::d('modelId not found, need to add namespace ' . $namespace); $this->addNewModel($namespace); //TODO bad way, need to find better $modelId = $this->getModelId($namespace); } $modelDefinition = new EasyRdf_Graph($namespace); if (is_file($data)) { $modelDefinition->parseFile($data); } else { $modelDefinition->parse($data); } $graph = $modelDefinition->toRdfPhp(); $resources = $modelDefinition->resources(); $format = EasyRdf_Format::getFormat('php'); $data = $modelDefinition->serialise($format); foreach ($data as $subjectUri => $propertiesValues) { foreach ($propertiesValues as $prop => $values) { foreach ($values as $k => $v) { $this->addStatement($modelId, $subjectUri, $prop, $v['value'], isset($v['lang']) ? $v['lang'] : null); } } } return true; }
/** * Method to serialise an EasyRdf_Graph to RDF/JSON * * http://n2.talis.com/wiki/RDF_JSON_Specification * docs/appendix-a-rdf-formats-json.md * * @param EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @param array $options * @throws EasyRdf_Exception * @return string The RDF in the new desired format. */ public function serialise($graph, $format, array $options = array()) { $nodes = array(); // cache for id-to-node association foreach ($graph->toRdfPhp() as $resource => $properties) { echo $resource; } }
/** * Helper function to test the formatter's RDFa. * * @param array $formatter * An associative array describing the formatter to test and its settings * containing: * - type: The machine name of the field formatter to test. * - settings: The settings of the field formatter to test. * @param string $property * The property that should be found. * @param array $expected_rdf_value * An associative array describing the expected value of the property * containing: * - value: The actual value of the string or URI. * - type: The type of RDF value, e.g. 'literal' for a string, or 'uri'. * Defaults to 'literal'. * - datatype: (optional) The datatype of the value (e.g. xsd:dateTime). */ protected function assertFormatterRdfa($formatter, $property, $expected_rdf_value) { $expected_rdf_value += array('type' => 'literal'); // The field formatter will be rendered inside the entity. Set the field // formatter in the entity display options before rendering the entity. entity_get_display('entity_test', 'entity_test', 'default')->setComponent($this->fieldName, $formatter)->save(); $build = entity_view($this->entity, 'default'); $output = \Drupal::service('renderer')->renderRoot($build); $graph = new \EasyRdf_Graph($this->uri, $output, 'rdfa'); $this->setRawContent($output); // If verbose debugging is turned on, display the HTML and parsed RDF // in the results. if ($this->debug) { print_r($output); print_r($graph->toRdfPhp()); } $this->assertTrue($graph->hasProperty($this->uri, $property, $expected_rdf_value), "Formatter {$formatter['type']} exposes data correctly for {$this->fieldType} fields."); }
private function render_rdf($body) { global $iso_countries; require_once 'vendor/autoload.php'; try { $rdf_doc = new EasyRdf_Graph($this->uriCache->uri, $body); } catch (Exception $e) { return false; } // the RDF may be rubbish if ($rdf_doc->countTriples() == 0) { return false; } // easier to work with it as a php array $rdf_array = $rdf_doc->toRdfPhp(); // we can get RDF that isn't about the specimen! if (!array_key_exists($this->uriCache->uri, $rdf_array)) { return false; } $rdf_array = $rdf_array[$this->uriCache->uri]; // image - comes first to make floating easy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/associatedMedia'][0]['value'])) { $image_uri = $rdf_array['http://rs.tdwg.org/dwc/terms/associatedMedia'][0]['value']; echo "<a href=\"{$image_uri}\"><img class=\"cetaf-image\" src=\"{$image_uri}\" /></a>"; } // title if (isset($rdf_array['http://purl.org/dc/terms/title'][0]['value'])) { echo '<p class="cetaf-title"><a href="' . $this->uriCache->uri . '">' . $rdf_array['http://purl.org/dc/terms/title'][0]['value'] . '</a></p>'; } echo '<div class="cetaf-taxonomy">'; // scientificName if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/scientificName'][0]['value'])) { echo '<p class="cetaf-scientificName">' . $rdf_array['http://rs.tdwg.org/dwc/terms/scientificName'][0]['value'] . '</p>'; } // family if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/family'][0]['value'])) { $family = $rdf_array['http://rs.tdwg.org/dwc/terms/family'][0]['value']; $family = ucfirst(strtolower($family)); echo '<p class="cetaf-family"><a href="https://en.wikipedia.org/wiki/' . $family . '" >' . $family . '</a></p>'; } echo '</div>'; echo '<div class="cetaf-collection">'; // recordedBy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/recordedBy'][0]['value'])) { echo '<p class="cetaf-recordedBy">' . $rdf_array['http://rs.tdwg.org/dwc/terms/recordedBy'][0]['value'] . '</p>'; } // recordedBy if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/recordNumber'][0]['value'])) { echo '<p class="cetaf-recordNumber">' . $rdf_array['http://rs.tdwg.org/dwc/terms/recordNumber'][0]['value'] . '</p>'; } // recorded date if (isset($rdf_array['http://purl.org/dc/terms/created'][0]['value'])) { echo '<p class="cetaf-created">' . $rdf_array['http://purl.org/dc/terms/created'][0]['value'] . '</p>'; } echo '</div>'; echo '<div class="cetaf-geography">'; // country if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/countryCode'][0]['value'])) { $country_iso = strtoupper($rdf_array['http://rs.tdwg.org/dwc/terms/countryCode'][0]['value']); require_once 'inc/iso_countries.php'; if (array_key_exists($country_iso, $iso_countries)) { $country_name = $iso_countries[$country_iso]; echo "<p class=\"cetaf-country cetaf-country-{$country_iso}\"><a href=\"https://en.wikipedia.org/wiki/{$country_name}\">{$country_name}</a></p>"; } else { $country_name = $country_iso; echo "<p class=\"cetaf-country cetaf-country-{$country_iso}\">{$country_iso}</p>"; } } // lon lat if (isset($rdf_array['http://rs.tdwg.org/dwc/terms/decimalLongitude'][0]['value']) && isset($rdf_array['http://rs.tdwg.org/dwc/terms/decimalLatitude'][0]['value'])) { $lat = $rdf_array['http://rs.tdwg.org/dwc/terms/decimalLatitude'][0]['value']; $lon = $rdf_array['http://rs.tdwg.org/dwc/terms/decimalLongitude'][0]['value']; $lat_lon = "{$lat},{$lon}"; echo '<p class="cetaf-lat-lon"><a href="http://maps.google.com?q=' . $lat_lon . '">' . $lat_lon . '</a></p>'; } echo '</div>'; echo '<div class="cetaf-meta">'; // source if (isset($rdf_array['http://purl.org/dc/terms/publisher'][0]['value'])) { $publisher = $rdf_array['http://purl.org/dc/terms/publisher'][0]['value']; if (filter_var($publisher, FILTER_VALIDATE_URL)) { // fixme - is this a known publisher? if so render their logo echo "<p class=\"cetaf-publisher\"><a href=\"{$publisher}\">{$publisher}</a></p>"; } else { echo '<p class="cetaf-publisher">' . $publisher . '</p>'; } } echo '</div>'; return true; }
$xml = file_get_contents($filePath); $xmlObject = simplexml_load_string($xml); $namespace = 'xsd'; if ($vocabulary == 'xsd') { $namespace = 'xs'; } $children = $xmlObject->children($namespace, true); foreach ($children->complexType as $v) { $results[] = $vocabulary . ':' . (string) $v->attributes()->name; } foreach ($children->simpleType as $v) { $results[] = $vocabulary . ':' . (string) $v->attributes()->name; } } elseif ($value['extension'] == 'rdf' || $value['extension'] == 'html') { $graph = new EasyRdf_Graph(); $graph->parseFile($filePath); $rdfPhp = $graph->toRdfPhp(); foreach ($rdfPhp as $key => $resource) { if (strpos($key, $value['namespace']) === 0) { $key = str_replace($value['namespace'], $vocabulary . ':', $key); $results[] = $key; } } } } } $app->response->headers->set('Content-Type', 'application/json'); $app->response->write(json_encode($results)); }); }); });
public static function processFile($file) { $rdf = new EasyRdf_Graph(); $rdf->parseFile($file); return $rdf->toRdfPhp(); }
/** * Rebase the graph on triples with the start fragment to our own base URI * * @param string $start_fragment * @param EasyRdf_Graph $graph * * @return EasyRdf_Graph */ private function rebaseGraph($start_fragment, $graph) { // Filter out the #dataset meta-data (if present) and change the URI's to our base URI $collections = $graph->allOfType('hydra:Collection'); // Fetch all of the subject URI's that bring forth hydra meta-data (and are thus irrelevant) $ignore_subjects = array(); if (empty($collection)) { $collections = $graph->allOfType('hydra:PagedCollection'); } if (!empty($collections)) { foreach ($collections as $collection) { array_push($ignore_subjects, $collection->getUri()); } } // Fetch the bnode of the hydra mapping (property is hydra:search) $hydra_mapping = $graph->getResource($start_fragment . '#dataset', 'hydra:search'); if (!empty($hydra_mapping)) { // Hydra mapping's will be a bnode structure array_push($ignore_subjects, '_:' . $hydra_mapping->getBNodeId()); $mapping_nodes = $hydra_mapping->all('hydra:mapping'); foreach ($mapping_nodes as $mapping_node) { if ($mapping_node->isBNode()) { array_push($ignore_subjects, '_:' . $mapping_node->getBNodeId()); } } $graph->deleteResource($start_fragment . '#dataset', 'hydra:search', '_:genid1'); $graph->deleteResource('_:genid1', 'hydra:mapping', '_:genid2'); // Delete all of the mapping related resources $triples = $graph->toRdfPhp(); } else { // Change all of the base (startfragment) URI's to our own base URI $triples = $graph->toRdfPhp(); } // Unset the #dataset unset($triples[$start_fragment . '#dataset']); foreach ($ignore_subjects as $ignore_subject) { unset($triples[$ignore_subject]); } $adjusted_graph = new \EasyRdf_Graph(); foreach ($triples as $subject => $triple) { foreach ($triple as $predicate => $objects) { foreach ($objects as $object) { $adjusted_graph->add($subject, $predicate, $object['value']); } } } return $adjusted_graph; }