Ejemplo n.º 1
0
 /** Constructor
  *
  */
 public function __construct($value, $lang = null, $datatype = null)
 {
     if (EasyRdf_Utils::is_associative_array($value)) {
         $this->_value = isset($value['value']) ? $value['value'] : null;
         $this->_lang = isset($value['lang']) ? $value['lang'] : null;
         $this->_datatype = isset($value['datatype']) ? $value['datatype'] : null;
     } else {
         $this->_value = $value;
         $this->_lang = $lang ? $lang : null;
         $this->_datatype = $datatype ? $datatype : null;
     }
     // Automatic datatype selection
     if ($this->_datatype == null) {
         if (is_float($this->_value)) {
             $this->_datatype = 'xsd:decimal';
         } else {
             if (is_int($this->_value)) {
                 $this->_datatype = 'xsd:integer';
             } else {
                 if (is_bool($this->_value)) {
                     $this->_datatype = 'xsd:boolean';
                 }
             }
         }
     }
     // Expand shortened URIs (qnames)
     if ($this->_datatype) {
         $this->_datatype = EasyRdf_Namespace::expand($this->_datatype);
     }
 }
Ejemplo n.º 2
0
 /**
  * Parse an RDF document into an EasyRdf_Graph
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     $json = EasyRdf_Utils::execCommandPipe($this->rapperCmd, array('--quiet', '--input', $format, '--output', 'json', '--ignore-errors', '--input-uri', $baseUri, '--output-uri', '-', '-'), $data);
     // Parse in the JSON
     return parent::parse($graph, $json, 'json', $baseUri);
 }
 /**
  * Serialise an EasyRdf_Graph to the RDF format of choice.
  *
  * @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);
     $ntriples = parent::serialise($graph, 'ntriples');
     // Hack to produce more concise RDF/XML
     if ($format == 'rdfxml') {
         $format = 'rdfxml-abbrev';
     }
     return EasyRdf_Utils::execCommandPipe($this->_rapperCmd, array('--quiet', '--input', 'ntriples', '--output', $format, '-', 'unknown://'), $ntriples);
 }
Ejemplo n.º 4
0
 /** @ignore */
 protected function endState4($t)
 {
     /* empty p | pClose after cdata | pClose after collection */
     if ($s = $this->getParentS()) {
         $b = isset($s['p_x_base']) && $s['p_x_base'] ? $s['p_x_base'] : (isset($s['x_base']) ? $s['x_base'] : '');
         if (isset($s['is_coll']) && $s['is_coll']) {
             $this->addTriple($s['value'], $this->_rdf . 'rest', $this->_rdf . 'nil', $s['type'], 'uri');
             /* back to collection start */
             while (!isset($s['p']) || $s['p'] != $t) {
                 $subS = $s;
                 $this->popS();
                 $s = $this->getParentS();
             }
             /* reification */
             if (isset($s['p_id']) && $s['p_id']) {
                 $this->reify(EasyRdf_Utils::resolveUriReference($b, '#' . $s['p_id']), $s['value'], $s['p'], $subS['value'], $s['type'], $subS['type']);
             }
             unset($s['p']);
             $this->updateS($s);
         } else {
             $dt = isset($s['o_datatype']) ? $s['o_datatype'] : null;
             $l = isset($s['p_x_lang']) && $s['p_x_lang'] ? $s['p_x_lang'] : (isset($s['x_lang']) ? $s['x_lang'] : null);
             $o = array('type' => 'literal', 'value' => $s['o_cdata']);
             $this->addTriple($s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l);
             /* reification */
             if (isset($s['p_id']) && $s['p_id']) {
                 $this->reify(EasyRdf_Utils::resolveUriReference($b, '#' . $s['p_id']), $s['value'], $s['p'], $o['value'], $s['type'], $o['type'], $dt, $l);
             }
             unset($s['o_cdata']);
             unset($s['o_datatype']);
             unset($s['p']);
             $this->updateS($s);
         }
         $this->_state = 2;
     }
 }
 /** Return pretty-print view of the literal
  *
  * @param  bool   $html  Set to true to format the dump using HTML
  * @param  string $color The colour of the text
  * @return string
  */
 public function dumpValue($html = true, $color = 'black')
 {
     return EasyRdf_Utils::dumpLiteralValue($this, $html, $color);
 }
Ejemplo n.º 6
0
 /** Delete a graph from the graph store
  *
  * The URI can either be a full absolute URI or
  * a URI relative to the URI of the graph store.
  *
  * @param string $uriRef The URI of graph to be added to
  * @return object EasyRdf_Http_Response The response from the graph store
  */
 public function delete($uriRef)
 {
     $graphUri = EasyRdf_Utils::resolveUriReference($this->_uri, $uriRef);
     $dataUrl = $this->urlForGraph($graphUri);
     $client = EasyRdf_Http::getDefaultHttpClient();
     $client->resetParameters(true);
     $client->setUri($dataUrl);
     $client->setMethod('DELETE');
     $response = $client->request();
     if (!$response->isSuccessful()) {
         throw new EasyRdf_Exception("HTTP request to delete {$dataUrl} failed: " . $response->getMessage());
     }
     return $response;
 }
Ejemplo n.º 7
0
 public function getResourceFromUri($uri)
 {
     EasyRdf_Format::unregister('json');
     // prevent parsing errors for sources which return invalid JSON
     // using apc cache for the resource if available
     if (function_exists('apc_store') && function_exists('apc_fetch')) {
         $key = 'fetch: ' . EasyRdf_Utils::removeFragmentFromUri($uri);
         $resource = apc_fetch($key);
         if ($resource === null || $resource === false) {
             // was not found in cache, or previous request failed
             $resource = $this->fetchResourceFromUri($uri);
             apc_store($key, $resource, $this->URI_FETCH_TTL);
         }
     } else {
         // APC not available, parse on every request
         $resource = $this->fetchResourceFromUri($uri);
     }
     return $resource;
 }
 /** Make a query to the SPARQL endpoint
  *
  * SELECT and ASK queries will return an object of type
  * EasyRdf_Sparql_Result.
  *
  * CONSTRUCT and DESCRIBE queries will return an object
  * of type EasyRdf_Graph.
  *
  * @param string $query The query string to be executed
  * @return object EasyRdf_Sparql_Result|EasyRdf_Graph Result of the query.
  */
 public function query($query)
 {
     # Add namespaces to the queryString
     $prefixes = '';
     foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) {
         if (strpos($query, "{$prefix}:") !== false and strpos($query, "PREFIX {$prefix}:") === false) {
             $prefixes .= "PREFIX {$prefix}: <{$uri}>\n";
         }
     }
     $client = EasyRdf_Http::getDefaultHttpClient();
     $client->resetParameters();
     $client->setUri($this->_uri);
     $client->setMethod('GET');
     $accept = EasyRdf_Format::getHttpAcceptHeader(array('application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8));
     $client->setHeaders('Accept', $accept);
     $client->setParameterGet('query', $prefixes . $query);
     $response = $client->request();
     if ($response->isSuccessful()) {
         list($type, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type'));
         if (strpos($type, 'application/sparql-results') === 0) {
             return new EasyRdf_Sparql_Result($response->getBody(), $type);
         } else {
             return new EasyRdf_Graph($this->_uri, $response->getBody(), $type);
         }
     } else {
         throw new EasyRdf_Exception("HTTP request for SPARQL query failed: " . $response->getBody());
     }
 }
Ejemplo n.º 9
0
 /** Add data to the graph
  *
  * The resource can either be a resource or the URI of a resource.
  *
  * The properties can either be a single property name or an
  * associate array of property names and values.
  *
  * The value can either be a single value or an array of values.
  *
  * Examples:
  *   $res = $graph->resource("http://www.example.com");
  *   $graph->add($res, 'prefix:property', 'value');
  *   $graph->add($res, 'prefix:property', array('value1',value2'));
  *   $graph->add($res, array('prefix:property' => 'value1'));
  *   $graph->add($res, 'foaf:knows', array( 'foaf:name' => 'Name'));
  *   $graph->add($res, array('foaf:knows' => array( 'foaf:name' => 'Name'));
  *
  * @param  mixed $resource   The resource to add data to
  * @param  mixed $properties The properties or property names
  * @param  mixed $value      The new value for the property
  */
 public function add($resource, $properties, $value = null)
 {
     if (!is_object($resource)) {
         $resource = $this->resource($resource);
     } else {
         if (!$resource instanceof EasyRdf_Resource) {
             throw new InvalidArgumentException("\$resource should be an instance of the EasyRdf_Resource class");
         }
     }
     if (EasyRdf_Utils::is_associative_array($properties)) {
         foreach ($properties as $property => $value) {
             $this->add($resource, $property, $value);
         }
         return;
     } else {
         if (EasyRdf_Utils::is_associative_array($value)) {
             if (isset($value['rdf:type'])) {
                 $bnode = $this->newBNode($value['rdf:type']);
             } else {
                 $bnode = $this->newBNode();
             }
             $bnode->add($value);
             $value = $bnode;
         }
         $resource->add($properties, $value);
     }
 }
 public function testExecCommandPipeNotFound()
 {
     $this->setExpectedException('EasyRdf_Exception', 'Error while executing command no_such_command');
     $output = EasyRdf_Utils::execCommandPipe('no_such_command');
 }
Ejemplo n.º 11
0
 /** Return pretty-print view of the literal
  *
  * @param  string $format Either 'html' or 'text'
  * @param  string $color  The colour of the text
  * @return string
  */
 public function dumpValue($format = 'html', $color = 'black')
 {
     return EasyRdf_Utils::dumpLiteralValue($this, $format, $color);
 }
 public function testDumpLiteralValueWithDatatype()
 {
     $literal = array('type' => 'literal', 'value' => '1', 'datatype' => 'http://www.w3.org/2001/XMLSchema#integer');
     $this->assertEquals('"1"^^xsd:integer', EasyRdf_Utils::dumpLiteralValue($literal, false));
     $this->assertEquals("<span style='color:black'>&quot;1&quot;^^xsd:integer</span>", EasyRdf_Utils::dumpLiteralValue($literal, true));
 }
Ejemplo n.º 13
0
 protected function request($type, $query)
 {
     // Check for undefined prefixes
     $prefixes = '';
     foreach (EasyRdf_Namespace::namespaces() as $prefix => $uri) {
         if (strpos($query, "{$prefix}:") !== false and strpos($query, "PREFIX {$prefix}:") === false) {
             $prefixes .= "PREFIX {$prefix}: <{$uri}>\n";
         }
     }
     $client = EasyRdf_Http::getDefaultHttpClient();
     $client->resetParameters();
     // Tell the server which response formats we can parse
     $accept = EasyRdf_Format::getHttpAcceptHeader(array('application/sparql-results+json' => 1.0, 'application/sparql-results+xml' => 0.8));
     $client->setHeaders('Accept', $accept);
     if ($type == 'update') {
         $client->setMethod('POST');
         $client->setUri($this->updateUri);
         $client->setRawData($prefixes . $query);
         $client->setHeaders('Content-Type', 'application/sparql-update');
     } elseif ($type == 'query') {
         // Use GET if the query is less than 2kB
         // 2046 = 2kB minus 1 for '?' and 1 for NULL-terminated string on server
         $encodedQuery = 'query=' . urlencode($prefixes . $query);
         if (strlen($encodedQuery) + strlen($this->queryUri) <= 2046) {
             $delimiter = $this->queryUri_has_params ? '&' : '?';
             $client->setMethod('GET');
             $client->setUri($this->queryUri . $delimiter . $encodedQuery);
         } else {
             // Fall back to POST instead (which is un-cacheable)
             $client->setMethod('POST');
             $client->setUri($this->queryUri);
             $client->setRawData($encodedQuery);
             $client->setHeaders('Content-Type', 'application/x-www-form-urlencoded');
         }
     }
     $response = $client->request();
     if ($response->getStatus() == 204) {
         // No content
         return $response;
     } elseif ($response->isSuccessful()) {
         list($type, $params) = EasyRdf_Utils::parseMimeType($response->getHeader('Content-Type'));
         if (strpos($type, 'application/sparql-results') === 0) {
             return new EasyRdf_Sparql_Result($response->getBody(), $type);
         } else {
             return new EasyRdf_Graph($this->queryUri, $response->getBody(), $type);
         }
     } else {
         throw new EasyRdf_Exception("HTTP request for SPARQL query failed: " . $response->getBody());
     }
 }
Ejemplo n.º 14
0
 public function testIsAssocIntPreppend()
 {
     $arr = array('foo' => 'bar');
     array_unshift($arr, 'rat');
     $this->assertFalse(EasyRdf_Utils::is_associative_array($arr));
 }
Ejemplo n.º 15
0
 /** Add values to an existing property
  * 
  * The properties can either be a single property name or an
  * associate array of property names and values.
  *
  * The value can either be a single value or an array of values.
  *
  * Examples:
  *   $resource->add('prefix:property', 'value');
  *   $resource->add('prefix:property', array('value1',value2'));
  *   $resource->add(array('prefix:property' => 'value1'));
  *
  * @param  mixed $resource   The resource to add data to
  * @param  mixed $properties The properties or property names
  * @param  mixed $value      The new value for the property
  * @return array             Array of all values associated with property.
  */
 public function add($properties, $value = null)
 {
     if ($properties == null or $properties == '') {
         throw new InvalidArgumentException("\$properties cannot be null or empty");
     }
     // Have multiple properties been given?
     if (is_array($properties)) {
         if (EasyRdf_Utils::is_associative_array($properties)) {
             foreach ($properties as $property => $value) {
                 $this->add($property, $value);
             }
             return;
         } else {
             foreach ($properties as $property) {
                 $this->add($property, $value);
             }
             return;
         }
     } else {
         $property = $properties;
     }
     // No value given?
     if ($value == null) {
         return null;
     }
     // Get the existing values for the property
     if (array_key_exists($property, $this->_properties)) {
         $values = $this->_properties[$property];
     } else {
         $values = array();
     }
     // Add to array of values, if it isn't already there
     if (is_array($value)) {
         foreach ($value as $v) {
             if (!in_array($v, $values)) {
                 array_push($values, $v);
             }
         }
     } else {
         if (!in_array($value, $values)) {
             array_push($values, $value);
         }
     }
     return $this->set($property, $values);
 }
Ejemplo n.º 16
0
 /** Return pretty-print view of the resource
  *
  * @param  string $format Either 'html' or 'text'
  * @param  string $color The colour of the text
  * @return string
  */
 public function dumpValue($format = 'html', $color = 'blue')
 {
     return EasyRdf_Utils::dumpResourceValue($this, $format, $color);
 }
Ejemplo n.º 17
0
 /** Return a human readable view of the resource and its properties
  *
  * This method is intended to be a debugging aid and will
  * print a resource and its properties.
  *
  * @param  bool  $html  Set to true to format the dump using HTML
  * @return string
  */
 public function dumpResource($resource, $html = true)
 {
     $this->checkResourceParam($resource, true);
     if (isset($this->_index[$resource])) {
         $properties = $this->_index[$resource];
     } else {
         return '';
     }
     $plist = array();
     foreach ($properties as $property => $values) {
         $olist = array();
         foreach ($values as $value) {
             if ($value['type'] == 'literal') {
                 $olist[] = EasyRdf_Utils::dumpLiteralValue($value, $html, 'black');
             } else {
                 $olist[] = EasyRdf_Utils::dumpResourceValue($value['value'], $html, 'blue');
             }
         }
         $pstr = EasyRdf_Namespace::shorten($property);
         if ($pstr == null) {
             $pstr = $property;
         }
         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 ($html) {
         return "<div id='" . htmlentities($resource) . "' " . "style='font-family:arial; padding:0.5em; " . "background-color:lightgrey;border:dashed 1px grey;'>\n" . "<div>" . EasyRdf_Utils::dumpResourceValue($resource, true, 'blue') . " " . "<span style='font-size: 0.8em'>(" . $this->classForResource($resource) . ")</span></div>\n" . "<div style='padding-left: 3em'>\n" . "<div>" . join("</div>\n<div>", $plist) . "</div>" . "</div></div>\n";
     } else {
         return $resource . " (" . $this->classForResource($resource) . ")\n" . join("\n", $plist) . "\n\n";
     }
 }
 /** Return pretty-print view of the resource
  *
  * @param  bool   $html  Set to true to format the dump using HTML
  * @param  string $color The colour of the text
  * @return string
  */
 public function dumpValue($html = true, $color = 'blue')
 {
     return EasyRdf_Utils::dumpResourceValue($this, $html, $color);
 }
Ejemplo n.º 19
0
 /**
  * Internal function to render a graph into an image
  *
  * @ignore
  */
 public function renderImage($graph, $format = 'png')
 {
     $dot = $this->serialiseDot($graph);
     return EasyRdf_Utils::execCommandPipe($this->dotCommand, array("-T{$format}"), $dot);
 }
Ejemplo n.º 20
0
 /** Add values to an existing property
  *
  * The properties can either be a single property name or an
  * associate array of property names and values.
  *
  * The value can either be a single value or an array of values.
  *
  * Examples:
  *   $resource->add('prefix:property', 'value');
  *   $resource->add('prefix:property', array('value1',value2'));
  *   $resource->add(array('prefix:property' => 'value1'));
  *
  * @param  mixed $resource   The resource to add data to
  * @param  mixed $properties The properties or property names
  * @param  mixed $value      The new value for the property
  * @return array             Array of all values associated with property.
  */
 public function add($properties, $values = null)
 {
     if ($properties == null or $properties == '') {
         throw new InvalidArgumentException("\$properties cannot be null or empty");
     }
     // Have multiple properties been given?
     if (is_array($properties)) {
         if (EasyRdf_Utils::is_associative_array($properties)) {
             foreach ($properties as $property => $value) {
                 $this->add($property, $value);
             }
             return;
         } else {
             foreach ($properties as $property) {
                 $this->add($property, $values);
             }
             return;
         }
     } else {
         $property = $properties;
     }
     // No value given?
     if ($values == null) {
         return null;
     }
     // Create the property if it doesn't already exist
     $property = EasyRdf_Namespace::expand($property);
     if (!isset($this->_properties[$property])) {
         $this->_properties[$property] = array();
     }
     if (!is_array($values)) {
         $values = array($values);
     }
     // Convert literal values into objects
     $objects = array();
     foreach ($values as $value) {
         if (is_object($value)) {
             $objects[] = $value;
         } else {
             $objects[] = new EasyRdf_Literal($value);
         }
     }
     // Add the objects, if they don't already exist
     foreach ($objects as $object) {
         if (!$this->matches($property, $object)) {
             array_push($this->_properties[$property], $object);
             if ($object instanceof EasyRdf_Resource) {
                 $object->addInverse($property, $this);
             }
         }
     }
     return $this->_properties[$property];
 }