Sunday, March 21, 2010

Redland in PHP / Ubuntu

The PHP docs are a little lacking; so...

Install in Ubuntu: apt-get install php5-librdf


roll: great, the redland php bindings are also in darwin ports. just $port install redland-bindings +php5. time to integrate it in my project :)



Checking its installed:


clockwerx@clockwerx-desktop:~$ php -i | grep rdf
redland librdf version => 1.0.9


How do you use it? Here's a port of example1.c

<?php
$world=librdf_new_world();
librdf_world_open($world);

$uri=librdf_new_uri($world, "http://librdf.org/bindings/bindings.rdf");
if(!$uri) {
die("Failed to create URI\n");
}

$storage=librdf_new_storage($world, "memory", "test", NULL);
if(!$storage) {
die("Failed to create new storage\n");
}

$model=librdf_new_model($world, $storage, NULL);
if(!$model) {
die("Failed to create model\n");
}

$parser_name = "";
$parser = librdf_new_parser($world, $parser_name, NULL, NULL);

if(!$parser) {
die("Failed to create new parser\n");
}

function librdf_uri_as_string($uri) {
return (string)$uri;
}

print "Parsing URI " . librdf_uri_as_string($uri) . "\n";
if(librdf_parser_parse_into_model($parser, $uri, $uri, $model)) {
die("Failed to parse RDF into model\n");
return(1);
}
librdf_free_parser($parser);


$statement2 = librdf_new_statement_from_nodes($world, librdf_new_node_from_uri_string($world, "http://www.dajobe.org/"),
librdf_new_node_from_uri_string($world, "http://purl.org/dc/elements/1.1/title"),
librdf_new_node_from_literal($world, "My Home Page", NULL, 0)
);
librdf_model_add_statement($model, $statement2);

/* Free what we just used to add to the model - now it should be stored */
librdf_free_statement($statement2);


/* Print out the model*/

print "Resulting model is:\n";
print librdf_model_to_string($model, $uri, "");

/* Construct the query predicate (arc) and object (target)
* and partial statement bits
*/
$subject=librdf_new_node_from_uri_string($world, "http://www.dajobe.org/");
$predicate=librdf_new_node_from_uri_string($world, "http://purl.org/dc/elements/1.1/title");
if(!$subject || !$predicate) {
die("Failed to create nodes for searching\n");
}
$partial_statement=librdf_new_statement($world);
librdf_statement_set_subject($partial_statement, $subject);
librdf_statement_set_predicate($partial_statement, $predicate);


/* QUERY TEST 1 - use find_statements to match */

print "Trying to find_statements\n";
$stream=librdf_model_find_statements($model, $partial_statement);
if(!$stream) {
die("librdf_model_find_statements returned NULL stream\n");
} else {
$count=0;
while(!librdf_stream_end($stream)) {
$statement=librdf_stream_get_object($stream);
if(!$statement) {
die("librdf_stream_next returned NULL\n");
break;
}

echo(" Matched statement: ");
print librdf_statement_to_string($statement);
print "\n";

librdf_stream_next($stream);
$count++;
}
librdf_free_stream($stream);
print "got " . $count . " matching statements\n";
}


/* QUERY TEST 2 - use get_targets to do match */
print "Trying to get targets\n";
$iterator=librdf_model_get_targets($model, $subject, $predicate);
if(!$iterator) {
die("librdf_model_get_targets failed to return iterator for searching\n");
}

$count=0;
while(!librdf_iterator_end($iterator)) {

$target=librdf_iterator_get_object($iterator);
if(!$target) {
die("librdf_iterator_get_object returned NULL\n");
}

print " Matched target: ";
print librdf_node_to_string($target);
print "
";

$count++;
librdf_iterator_next($iterator);
}
librdf_free_iterator($iterator);
printf("got %d target nodes\n", $count);

librdf_free_statement($partial_statement);
/* the above does this since they are still attached */
/* librdf_free_node(predicate); */
/* librdf_free_node(object); */

librdf_free_model($model);

librdf_free_storage($storage);

librdf_free_uri($uri);

librdf_free_world($world);
Post a Comment