| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
print "Testing Redland...\n"; |
| 29 |
$dlls = array("redland.so", "php_redland.dll", "redland.dylib", "redland.bundle"); |
| 30 |
foreach ($dlls as $dll) { |
| 31 |
if(file_exists($dll)) { |
| 32 |
dl($dll); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
$world=librdf_php_get_world(); |
| 37 |
|
| 38 |
print "Redland world opened\n"; |
| 39 |
|
| 40 |
$storage=librdf_new_storage($world,'hashes','dummy',"new=yes,hash-type='memory'"); |
| 41 |
print "Redland storage created\n"; |
| 42 |
|
| 43 |
$model=librdf_new_model($world,$storage,''); |
| 44 |
print "Redland model created\n"; |
| 45 |
|
| 46 |
$parser=librdf_new_parser($world,'rdfxml','application/rdf+xml',null); |
| 47 |
print "Redland parser created\n"; |
| 48 |
|
| 49 |
$uri=librdf_new_uri($world,'file:../data/dc.rdf'); |
| 50 |
|
| 51 |
print "Parsing...\n"; |
| 52 |
librdf_parser_parse_into_model($parser,$uri,$uri,$model); |
| 53 |
print "Done...\n"; |
| 54 |
|
| 55 |
librdf_free_uri($uri); |
| 56 |
|
| 57 |
librdf_free_parser($parser); |
| 58 |
|
| 59 |
|
| 60 |
$query = librdf_new_query($world, 'sparql', null, "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c ?d WHERE { ?a dc:title ?c . OPTIONAL { ?a dc:related ?d } }", null); |
| 61 |
print "Querying for dc:titles:\n"; |
| 62 |
$results=librdf_model_query_execute($model, $query); |
| 63 |
$count=1; |
| 64 |
while($results && !librdf_query_results_finished($results)) { |
| 65 |
print "result $count: {\n"; |
| 66 |
for ($i=0; $i < librdf_query_results_get_bindings_count($results); $i++) |
| 67 |
{ |
| 68 |
$val=librdf_query_results_get_binding_value($results, $i); |
| 69 |
if ($val) |
| 70 |
$nval=librdf_node_to_string($val); |
| 71 |
else |
| 72 |
$nval='(unbound)'; |
| 73 |
print " ".librdf_query_results_get_binding_name($results, $i)."=".$nval."\n"; |
| 74 |
} |
| 75 |
print "}\n"; |
| 76 |
librdf_query_results_next($results); |
| 77 |
$count++; |
| 78 |
} |
| 79 |
if ($results) |
| 80 |
print "Returned $count results\n"; |
| 81 |
$results=null; |
| 82 |
|
| 83 |
print "\nExecuting query again\n"; |
| 84 |
$results=librdf_model_query_execute($model, $query); |
| 85 |
if ($results) { |
| 86 |
$str=librdf_query_results_to_string($results, null, null); |
| 87 |
print "Query results serialized to an XML string size ".strlen($str)." bytes\n"; |
| 88 |
} else |
| 89 |
print "Query results couldn't be serialized to an XML string\n"; |
| 90 |
|
| 91 |
|
| 92 |
$serializer=librdf_new_serializer($world,'rdfxml',null, null); |
| 93 |
print "Redland serializer created\n"; |
| 94 |
|
| 95 |
$base=librdf_new_uri($world,'http://example.org/base.rdf'); |
| 96 |
|
| 97 |
print "Serializing...\n"; |
| 98 |
librdf_serializer_serialize_model_to_file($serializer,'./test-out.rdf',$base,$model); |
| 99 |
print "Done...\n"; |
| 100 |
|
| 101 |
librdf_free_serializer($serializer); |
| 102 |
|
| 103 |
librdf_free_uri($base); |
| 104 |
|
| 105 |
librdf_free_model($model); |
| 106 |
|
| 107 |
librdf_free_storage($storage); |
| 108 |
|
| 109 |
|
| 110 |
print "Done\n"; |
| 111 |
|
| 112 |
?> |