Exemple #1
0
 /** Get a list of types for a resource
  *
  * The types will each be a shortened URI as a string.
  * This method will return an empty array if the resource has no types.
  *
  * If $resource is null, then it will get the types for the URI of the graph.
  *
  * @return array All types assocated with the resource (e.g. foaf:Person)
  */
 public function types($resource = null)
 {
     $resources = $this->typesAsResources($resource);
     $types = array();
     foreach ($resources as $type) {
         $types[] = EasyRdf_Namespace::shorten($type);
     }
     return $types;
 }
Exemple #2
0
 /**
  * Return all types (RDFS/OWL classes) present in the specified vocabulary or all vocabularies.
  * @return array Array with URIs (string) as key and array of (label, superclassURI) as value
  */
 public function getTypes($vocid = null, $lang = null)
 {
     $sparql = isset($vocid) ? $this->getVocabulary($vocid)->getSparql() : $this->getDefaultSparql();
     $result = $sparql->queryTypes($lang);
     foreach ($result as $uri => $values) {
         if (empty($values)) {
             $shorteneduri = EasyRdf_Namespace::shorten($uri);
             if ($shorteneduri !== null) {
                 $trans = gettext($shorteneduri);
                 if ($trans) {
                     $result[$uri] = array('label' => $trans);
                 }
             }
         }
     }
     return $result;
 }
Exemple #3
0
 public function testShortenNonString()
 {
     $this->setExpectedException('InvalidArgumentException');
     EasyRdf_Namespace::shorten($this);
 }
 /** Get a shortened version of the resources URI.
  *
  * This method will return the full URI if the resource isn't part of any
  * registered namespace.
  *
  * @return string The shortened URI of this resource (e.g. foaf:name)
  */
 public function shorten()
 {
     return EasyRdf_Namespace::shorten($this->_uri);
 }
Exemple #5
0
 /**
  * Get the type of a resource from some RDF/PHP
  * (http://n2.talis.com/wiki/RDF_PHP_Specification)
  */
 private function getResourceType($data, $uri)
 {
     if (array_key_exists($uri, $data)) {
         $subj = $data[$uri];
         if (array_key_exists(self::RDF_TYPE_URI, $subj)) {
             $types = array();
             foreach ($subj[self::RDF_TYPE_URI] as $type) {
                 if ($type['type'] == 'uri') {
                     $type = EasyRdf_Namespace::shorten($type['value']);
                     if ($type) {
                         array_push($types, $type);
                     }
                 }
             }
             if (count($types) > 0) {
                 return $types;
             }
         }
     }
     return null;
 }
 /**
  * Namespace which is too short shouldn't apply
  */
 public function testShortNamespace()
 {
     EasyRdf_Namespace::set('ex', 'http://example.org/');
     $this->assertSame('ex:foo', EasyRdf_Namespace::shorten('http://example.org/foo'));
     $this->assertNull(EasyRdf_Namespace::shorten('http://example.org/bar/baz'));
 }
 public function testShortenNonString()
 {
     $this->setExpectedException('InvalidArgumentException', '$uri should be a string or EasyRdf_Resource');
     EasyRdf_Namespace::shorten($this);
 }
 /** Returns the shortened datatype URI of the literal.
  *
  * @return string  Datatype of this literal (e.g. xsd:integer).
  */
 public function getDatatype()
 {
     if ($this->_datatype) {
         return EasyRdf_Namespace::shorten($this->_datatype);
     } else {
         return null;
     }
 }
Exemple #9
0
 /**
  * Gets the values for the property in question in all other languages than the ui language.
  * @param string $property
  */
 public function getAllLabels($property)
 {
     $labels = array();
     if (EasyRdf_Namespace::shorten($property) !== null) {
         // shortening property labels if possible
         $property = EasyRdf_Namespace::shorten($property);
     } else {
         $property = "<{$property}>";
     }
     // EasyRdf requires full URIs to be in angle brackets
     foreach ($this->resource->allLiterals($property) as $lit) {
         $labels[Punic\Language::getName($lit->getLang(), $this->getEnvLang())][] = new ConceptPropertyValueLiteral($lit, $property);
     }
     ksort($labels);
     return $labels;
 }
 /**
  * Protected method to serialise a whole resource and its properties
  * @ignore
  */
 protected function rdfxmlResource($res, $showNodeId, $depth = 1)
 {
     // Keep track of the resources we have already serialised
     if (isset($this->_outputtedResources[$res->getUri()])) {
         return '';
     } else {
         $this->_outputtedResources[$res->getUri()] = true;
     }
     // If the resource has no properties - don't serialise it
     $properties = $res->propertyUris();
     if (count($properties) == 0) {
         return '';
     }
     $type = $res->type();
     if ($type) {
         $this->addPrefix($type);
     } else {
         $type = 'rdf:Description';
     }
     $indent = str_repeat('  ', $depth);
     $xml = "\n{$indent}<{$type}";
     if ($res->isBNode()) {
         if ($showNodeId) {
             $xml .= ' rdf:nodeID="' . htmlspecialchars($res->getNodeId()) . '"';
         }
     } else {
         $xml .= ' rdf:about="' . htmlspecialchars($res->getUri()) . '"';
     }
     $xml .= ">\n";
     foreach ($properties as $property) {
         $short = EasyRdf_Namespace::shorten($property, true);
         if ($short) {
             $this->addPrefix($short);
             $objects = $res->all("<{$property}>");
             if ($short == 'rdf:type') {
                 array_shift($objects);
             }
             foreach ($objects as $object) {
                 $xml .= $this->rdfxmlObject($short, $object, $depth + 1);
             }
         } else {
             throw new EasyRdf_Exception("It is not possible to serialse the property " . "'{$property}' to RDF/XML.");
         }
     }
     $xml .= "{$indent}</{$type}>\n";
     return $xml;
 }
Exemple #11
0
 /** Return pretty-print view of a literal
  *
  * This method is mainly intended for internal use and is used by
  * EasyRdf_Graph and EasyRdf_Sparql_Result to format a literal
  * for display.
  *
  * @param  mixed $resource An EasyRdf_Literal object or an associative array
  * @param  bool  $html     Set to true to format the dump using HTML
  * @param  string $color   The colour of the text
  * @return string
  */
 public static function dumpLiteralValue($literal, $html = true, $color = 'black')
 {
     if (is_object($literal)) {
         $literal = $literal->toArray();
     } else {
         if (!is_array($literal)) {
             $literal = array('value' => $literal);
         }
     }
     $text = '"' . $literal['value'] . '"';
     if (isset($literal['lang'])) {
         $text .= '@' . $literal['lang'];
     }
     if (isset($literal['datatype'])) {
         $datatype = EasyRdf_Namespace::shorten($literal['datatype']);
         $text .= "^^{$datatype}";
     }
     if ($html) {
         return "<span style='color:{$color}'>" . htmlentities($text, ENT_COMPAT, "UTF-8") . "</span>";
     } else {
         return $text;
     }
 }
Exemple #12
0
 /**
  * Method to serialise an EasyRdf_Graph to RDF/XML
  *
  * @param string  $graph   An EasyRdf_Graph object.
  * @param string  $format  The name of the format to convert to.
  * @return string          The RDF in the new desired format.
  */
 public function serialise($graph, $format)
 {
     parent::checkSerialiseParams($graph, $format);
     if ($format != 'rdfxml') {
         throw new EasyRdf_Exception("EasyRdf_Serialiser_RdfXml does not support: {$format}");
     }
     // store of namespaces to be appended to the rdf:RDF tag
     $this->_prefixes = array('rdf' => true);
     $xml = '';
     foreach ($graph->resources() as $resource) {
         $properties = $resource->propertyUris();
         if (count($properties) == 0) {
             continue;
         }
         $xml .= "\n  " . $this->rdfxmlResource($resource) . "\n";
         foreach ($properties as $property) {
             $short = EasyRdf_Namespace::shorten($property, true);
             if ($short) {
                 $this->addPrefix($short);
                 $objects = $resource->all($property);
                 foreach ($objects as $object) {
                     $xml .= $this->rdfxmlObject($short, $object);
                 }
             } else {
                 throw new EasyRdf_Exception("It is not possible to serialse the property " . "'{$property}' to RDF/XML.");
             }
         }
         $xml .= "  </rdf:Description>\n";
     }
     // iterate through namepsaces array prefix and output a string.
     $namespaceStr = '';
     foreach ($this->_prefixes as $prefix => $count) {
         $url = EasyRdf_Namespace::get($prefix);
         if (strlen($namespaceStr)) {
             $namespaceStr .= "\n        ";
         }
         $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"';
     }
     return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n";
 }
 /**
  * Protected method to serialise the properties of a resource
  * @ignore
  */
 protected function serialiseProperties($res, $depth = 1)
 {
     $properties = $res->propertyUris();
     $indent = str_repeat(' ', $depth * 2 - 1);
     $turtle = '';
     if (count($properties) > 1) {
         $turtle .= "\n{$indent}";
     }
     $pCount = 0;
     foreach ($properties as $property) {
         $short = EasyRdf_Namespace::shorten($property, true);
         if ($short) {
             if ($short == 'rdf:type') {
                 $pStr = 'a';
             } else {
                 $this->addPrefix($short);
                 $pStr = $short;
             }
         } else {
             $pStr = '<' . str_replace('>', '\\>', $property) . '>';
         }
         if ($pCount) {
             $turtle .= " ;\n{$indent}";
         }
         $turtle .= ' ' . $pStr;
         $oCount = 0;
         foreach ($res->all("<{$property}>") as $object) {
             if ($oCount) {
                 $turtle .= ',';
             }
             if ($object instanceof EasyRdf_Resource and $object->isBnode()) {
                 $id = $object->getNodeId();
                 $rpcount = $this->reversePropertyCount($object);
                 if ($rpcount <= 1 and !isset($this->_outputtedBnodes[$id])) {
                     // Nested unlabelled Blank Node
                     $this->_outputtedBnodes[$id] = true;
                     $turtle .= ' [';
                     $turtle .= $this->serialiseProperties($object, $depth + 1);
                     $turtle .= ' ]';
                 } else {
                     // Multiple properties pointing to this blank node
                     $turtle .= ' ' . $this->serialiseObject($object);
                 }
             } else {
                 $turtle .= ' ' . $this->serialiseObject($object);
             }
             $oCount++;
         }
         $pCount++;
     }
     if ($depth == 1) {
         $turtle .= " .";
         if ($pCount > 1) {
             $turtle .= "\n";
         }
     } elseif ($pCount > 1) {
         $turtle .= "\n" . str_repeat(' ', ($depth - 1) * 2 - 1);
     }
     return $turtle;
 }
Exemple #14
0
 /**
  * Serialise an EasyRdf_Graph to Turtle.
  *
  * @param object EasyRdf_Graph $graph   An EasyRdf_Graph object.
  * @param string  $format               The name of the format to convert to.
  * @return string                       The RDF in the new desired format.
  */
 public function serialise($graph, $format)
 {
     parent::checkSerialiseParams($graph, $format);
     if ($format != 'turtle' and $format != 'n3') {
         throw new EasyRdf_Exception("EasyRdf_Serialiser_Turtle does not support: {$format}");
     }
     $this->_prefixes = array('rdf' => true);
     $turtle = '';
     foreach ($graph->resources() as $subject) {
         $properties = $subject->propertyUris();
         if (count($properties) == 0) {
             continue;
         }
         $turtle .= $this->serialiseResource($subject);
         if (count($properties) > 1) {
             $turtle .= "\n   ";
         }
         $pCount = 0;
         foreach ($properties as $property) {
             $short = EasyRdf_Namespace::shorten($property, true);
             if ($short) {
                 $this->addPrefix($short);
                 $pStr = $short == 'rdf:type' ? 'a' : $short;
             } else {
                 $pStr = '<' . str_replace('>', '\\>', $property) . '>';
             }
             if ($pCount) {
                 $turtle .= " ;\n   ";
             }
             $turtle .= " " . $pStr;
             $objects = $subject->all($property);
             $oCount = 0;
             foreach ($objects as $object) {
                 if ($oCount) {
                     $turtle .= ",";
                 }
                 $turtle .= " " . $this->serialiseObject($object);
                 $oCount++;
             }
             $pCount++;
         }
         $turtle .= " .\n\n";
     }
     return $this->serialisePrefixes() . "\n" . $turtle;
 }
Exemple #15
0
 /**
  * Queries whether the property should be shown with all the label language variations.
  * @param string $property
  * @return boolean
  */
 public function hasMultiLingualProperty($property)
 {
     $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty");
     foreach ($resources as $res) {
         $prop = $res->getURI();
         if (EasyRdf_Namespace::shorten($prop) !== null) {
             $prop = EasyRdf_Namespace::shorten($prop);
         }
         if ($prop === $property) {
             return true;
         }
     }
     return false;
 }
Exemple #16
0
 /** Return pretty-print view of a literal
  *
  * This method is mainly intended for internal use and is used by
  * EasyRdf_Graph and EasyRdf_Sparql_Result to format a literal
  * for display.
  *
  * @param  mixed  $literal  An EasyRdf_Literal object or an associative array
  * @param  string $format   Either 'html' or 'text'
  * @param  string $color    The colour of the text
  * @return string
  */
 public static function dumpLiteralValue($literal, $format = 'html', $color = 'black')
 {
     if (!preg_match('/^#?[-\\w]+$/', $color)) {
         throw new InvalidArgumentException("\$color must be a legal color code or name");
     }
     if (is_object($literal)) {
         $literal = $literal->toRdfPhp();
     } elseif (!is_array($literal)) {
         $literal = array('value' => $literal);
     }
     $text = '"' . $literal['value'] . '"';
     if (isset($literal['lang'])) {
         $text .= '@' . $literal['lang'];
     }
     if (isset($literal['datatype'])) {
         $short = EasyRdf_Namespace::shorten($literal['datatype']);
         if ($short) {
             $text .= "^^{$short}";
         } else {
             $text .= "^^<" . $literal['datatype'] . ">";
         }
     }
     if ($format == 'html') {
         return "<span style='color:{$color}'>" . htmlentities($text, ENT_COMPAT, "UTF-8") . "</span>";
     } else {
         return $text;
     }
 }
Exemple #17
0
 /**
  * Return a SPARQL representation of another resource
  * We have to do this because other resource does not always extend the EasySpinRdf_Resource element
  * @param $resource
  * @param $concat
  * @return string
  * @todo Change the EasyRdf type mapper to define a default resource class and use a default getSparql method
  */
 public function resourceToSparql($resource, $concat = " ")
 {
     // if the resource is a collection, we concat sparql with the $concat param
     if (is_a($resource, 'EasyRdf_Collection')) {
         $resources = array();
         foreach ($resource as $item) {
             $resources[] = $this->resourceToSparql($item);
         }
         return join($concat, $resources);
     }
     // if the resource implement getSparql method, just use it
     if (method_exists($resource, 'getSparql')) {
         return $resource->getSparql();
     }
     // if the resource is a literal return its value
     if (is_a($resource, 'EasyRdf_Literal')) {
         $value = $resource->getValue();
         return is_int($value) ? $value : '"' . $value . '"';
     }
     // if the resource has a sp:varName property, use it as a variable
     if ($varName = $resource->get('sp:varName')) {
         return "?" . $varName->getValue();
     }
     // if the resource is a bnode, lets adapt it to SPIN specifications
     if ($resource->isBNode()) {
         return '?' . substr($resource->getUri(), 2);
     }
     list($prefix, $suffix) = \EasyRdf_Namespace::splitUri($resource);
     if (in_array($prefix, array('sp', 'spin')) && $suffix[0] == "_") {
         $resource = '?' . substr($suffix, 1);
     } elseif (\EasyRdf_Namespace::shorten($resource)) {
         $resource = \EasyRdf_Namespace::shorten($resource);
     } else {
         $resource = "<" . $resource . ">";
     }
     return $resource;
 }
Exemple #18
0
 /** Return pretty-print view of the literal
  *
  * @param  bool  $html  Set to true to format the dump using HTML
  */
 public function dumpValue($html = true)
 {
     $text = '"' . $this->_value . '"';
     if ($this->_lang) {
         $text .= '@' . $this->_lang;
     }
     if ($this->_datatype) {
         $datatype = EasyRdf_Namespace::shorten($this->_datatype);
         $text .= "^^{$datatype}";
     }
     if ($html) {
         return "<span style='color:blue'>" . htmlentities($text, ENT_COMPAT, "UTF-8") . "</span>";
     } else {
         return $text;
     }
 }
 /**
  * Given a an EasyRdf_Resource or URI, convert it into a string, suitable to
  * be written to a Turtle document. URIs will be shortened into CURIES
  * where possible.
  *
  * @param  EasyRdf_Resource $resource   The resource to convert to a Turtle string
  * @param  boolean $createNamespace     If true, a new namespace may be created
  * @return string
  */
 public function serialiseResource($resource, $createNamespace = false)
 {
     if (is_object($resource)) {
         if ($resource->isBNode()) {
             return $resource->getUri();
         } else {
             $resource = $resource->getUri();
         }
     }
     $short = EasyRdf_Namespace::shorten($resource, $createNamespace);
     if ($short) {
         $this->addPrefix($short);
         return $short;
     } else {
         return self::escapeIri($resource);
     }
 }
Exemple #20
0
 /** Get a list of types for a resource.
  *
  * The types will each be a shortened URI as a string.
  * This method will return an empty array if the resource has no types.
  *
  * If $resource is null, then it will get the types for the URI of the graph.
  *
  * @return array All types assocated with the resource (e.g. foaf:Person)
  */
 public function types($resource = null)
 {
     $this->checkResourceParam($resource, true);
     $types = array();
     if ($resource) {
         foreach ($this->all($resource, 'rdf:type', 'resource') as $type) {
             $types[] = EasyRdf_Namespace::shorten($type);
         }
     }
     return $types;
 }
Exemple #21
0
 /**
  * Loads the vocabulary metadata. And wraps the result in a json-ld object.
  * @param Request $request
  */
 public function vocabularyStatistics($request)
 {
     $this->setLanguageProperties($request->getLang());
     $arrayClass = $request->getVocab()->getConfig()->getArrayClassURI();
     $groupClass = $request->getVocab()->getConfig()->getGroupClassURI();
     $vocab_stats = $request->getVocab()->getStatistics($request->getQueryParam('lang'), $arrayClass, $groupClass);
     $types = array('http://www.w3.org/2004/02/skos/core#Concept', 'http://www.w3.org/2004/02/skos/core#Collection', $arrayClass, $groupClass);
     $subTypes = array();
     foreach ($vocab_stats as $subtype) {
         if (!in_array($subtype['type'], $types)) {
             $subTypes[] = $subtype;
         }
     }
     /* encode the results in a JSON-LD compatible array */
     $ret = array('@context' => array('rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'skos' => 'http://www.w3.org/2004/02/skos/core#', 'void' => 'http://rdfs.org/ns/void#', 'onki' => 'http://schema.onki.fi/onki#', 'uri' => '@id', 'id' => 'onki:vocabularyIdentifier', 'concepts' => 'void:classPartition', 'label' => 'rdfs:label', 'class' => array('@id' => 'void:class', '@type' => '@id'), 'subTypes' => array('@id' => 'void:class', '@type' => '@id'), 'count' => 'void:entities', '@language' => $request->getLang()), 'uri' => '', 'id' => $request->getVocab()->getId(), 'title' => $request->getVocab()->getConfig()->getTitle(), 'concepts' => array('class' => 'http://www.w3.org/2004/02/skos/core#Concept', 'label' => gettext('skos:Concept'), 'count' => $vocab_stats['http://www.w3.org/2004/02/skos/core#Concept']['count']), 'subTypes' => $subTypes);
     if (isset($vocab_stats['http://www.w3.org/2004/02/skos/core#Collection'])) {
         $ret['conceptGroups'] = array('class' => 'http://www.w3.org/2004/02/skos/core#Collection', 'label' => gettext('skos:Collection'), 'count' => $vocab_stats['http://www.w3.org/2004/02/skos/core#Collection']['count']);
     } else {
         if (isset($vocab_stats[$groupClass])) {
             $ret['conceptGroups'] = array('class' => $groupClass, 'label' => isset($vocab_stats[$groupClass]['label']) ? $vocab_stats[$groupClass]['label'] : gettext(EasyRdf_Namespace::shorten($groupClass)), 'count' => $vocab_stats[$groupClass]['count']);
         } else {
             if (isset($vocab_stats[$arrayClass])) {
                 $ret['arrays'] = array('class' => $arrayClass, 'label' => isset($vocab_stats[$arrayClass]['label']) ? $vocab_stats[$arrayClass]['label'] : gettext(EasyRdf_Namespace::shorten($arrayClass)), 'count' => $vocab_stats[$arrayClass]['count']);
             }
         }
     }
     return $this->returnJson($ret);
 }
Exemple #22
0
 /**
  * Internal function to serialise an EasyRdf_Graph into a DOT formatted string
  *
  * @ignore
  */
 protected function serialiseDot($graph)
 {
     $result = "digraph {\n";
     // Write the graph attributes
     foreach ($this->attributes as $k => $v) {
         $result .= '  ' . $this->escape($k) . '=' . $this->escape($v) . ";\n";
     }
     // Go through each of the properties and write the edges
     $nodes = array();
     $result .= "\n  // Edges\n";
     foreach ($graph->resources() as $resource) {
         $name1 = $this->nodeName($resource);
         foreach ($resource->propertyUris() as $property) {
             $label = null;
             if ($this->useLabels) {
                 $label = $graph->resource($property)->label();
             }
             if ($label === null) {
                 if ($this->onlyLabelled == true) {
                     continue;
                 } else {
                     $label = EasyRdf_Namespace::shorten($property);
                 }
             }
             foreach ($resource->all("<{$property}>") as $value) {
                 $name2 = $this->nodeName($value);
                 $nodes[$name1] = $resource;
                 $nodes[$name2] = $value;
                 $result .= $this->serialiseRow($name1, $name2, array('label' => $label));
             }
         }
     }
     ksort($nodes);
     $result .= "\n  // Nodes\n";
     foreach ($nodes as $name => $node) {
         $type = substr($name, 0, 1);
         $label = '';
         if ($type == 'R') {
             if ($this->useLabels) {
                 $label = $node->label();
             }
             if (!$label) {
                 $label = $node->shorten();
             }
             if (!$label) {
                 $label = $node->getURI();
             }
             $result .= $this->serialiseRow($name, null, array('URL' => $node->getURI(), 'label' => $label, 'shape' => 'ellipse', 'color' => 'blue'));
         } elseif ($type == 'B') {
             if ($this->useLabels) {
                 $label = $node->label();
             }
             $result .= $this->serialiseRow($name, null, array('label' => $label, 'shape' => 'circle', 'color' => 'green'));
         } else {
             $result .= $this->serialiseRow($name, null, array('label' => strval($node), 'shape' => 'record'));
         }
     }
     $result .= "}\n";
     return $result;
 }
Exemple #23
0
 /** Return view of the resource and its properties
  *
  * This method is intended to be a debugging aid and will
  * print a resource and its properties to the screen.
  *
  * @param  bool  $html  Set to true to format the dump using HTML
  */
 public function dump($html = true)
 {
     $plist = array();
     foreach ($this->_properties as $prop => $values) {
         $olist = array();
         foreach ($values as $value) {
             $olist[] = $value->dumpValue($html);
         }
         $pstr = EasyRdf_Namespace::shorten($prop);
         if ($pstr == null) {
             $pstr = $prop;
         }
         if ($html) {
             $plist[] = "<span style='font-size:130%'>&rarr;</span> " . "<span style='text-decoration:none;color:green'>" . htmlentities($pstr) . "</span> " . "<span style='font-size:130%'>&rarr;</span> " . join(", ", $olist);
         } else {
             $plist[] = "  -> {$pstr} -> " . join(", ", $olist);
         }
     }
     if (count($plist)) {
         if ($html) {
             return "<div id='" . htmlentities($this->_uri) . "' " . "style='font-family:arial; padding:0.5em; " . "background-color:lightgrey;border:dashed 1px grey;'>\n" . "<div>" . $this->dumpValue(true, 'blue') . " " . "<span style='font-size: 0.8em'>(" . get_class($this) . ")</span></div>\n" . "<div style='padding-left: 3em'>\n" . "<div>" . join("</div>\n<div>", $plist) . "</div>" . "</div></div>\n";
         } else {
             return $this->_uri . " (" . get_class($this) . ")\n" . join("\n", $plist) . "\n\n";
         }
     } else {
         return '';
     }
 }