/** * Create a new LibRDF_Serializer. * * Name is the name of the serializer to use. Common choices are * "rdfxml", "ntriples" and "turtle". * * The "rdfxml" serializer is not pretty, outputing a flat list of * one XML element per statement. "rdfxml-abbrev" is a bit nicer, but * slower. * * @param string $name The name of the serializer to use * @param string $mime_type The MIME type of the syntax * @param string $type_uri The URI of the syntax * @return void * @throws LibRDF_Error If unable to create a new serializer * @access public */ public function __construct($name, $mime_type = NULL, $type_uri = NULL) { if ($type_uri) { $type_uri = new LibRDF_URI($type_uri); } $this->serializer = librdf_new_serializer(librdf_php_get_world(), $name, $mime_type, $type_uri ? $type_uri->getURI : $type_uri); if (!$this->serializer) { throw new LibRDF_Error("Unable to create new serializer"); } foreach (self::$namespaces as $uri => $prefix) { $this->setNamespace($uri, $prefix); } }
$nval = '(unbound)'; } print " " . librdf_query_results_get_binding_name($results, $i) . "=" . $nval . "\n"; } print "}\n"; librdf_query_results_next($results); $count++; } if ($results) { print "Returned {$count} results\n"; } $results = null; print "\nExecuting query again\n"; $results = librdf_model_query_execute($model, $query); if ($results) { $str = librdf_query_results_to_string($results, null, null); print "Query results serialized to an XML string size " . strlen($str) . " bytes\n"; } else { print "Query results couldn't be serialized to an XML string\n"; } $serializer = librdf_new_serializer($world, 'rdfxml', null, null); print "Redland serializer created\n"; $base = librdf_new_uri($world, 'http://example.org/base.rdf'); print "Serializing...\n"; librdf_serializer_serialize_model_to_file($serializer, './test-out.rdf', $base, $model); print "Done...\n"; librdf_free_serializer($serializer); librdf_free_uri($base); librdf_free_model($model); librdf_free_storage($storage); print "Done\n";
function CONSTRUCT($query, $base_uri = null) { if (is_null($base_uri)) { $base_uri = $this->_base_uri; } timings($query); $q = librdf_new_query($this->_world, 'sparql', null, $query, $base_uri); $r = librdf_model_query_execute($this->_model, $q); $r_stream = librdf_query_results_as_stream($r); $r_store = librdf_new_storage($this->_world, 'memory', '', null); $r_model = librdf_new_model($this->_world, $r_store, null); librdf_model_add_statements($r_model, $r_stream); librdf_free_stream($r_stream); $serializer = librdf_new_serializer($this->_world, 'json', null, null); $r = librdf_serializer_serialize_model_to_string($serializer, null, $r_model); librdf_free_serializer($serializer); $r = json_decode($r, 1); if (is_null($r)) { $r = array(); } librdf_free_model($r_model); librdf_free_storage($r_store); librdf_free_query($q); timings(); return $r; }