<?

/** 
 * Redland PHP Wrapper Class
 * 
 * This class is mostly being developed for my personal uses in the PHP
 * Redland environhment.
 * @package Redland
 */

if (!extension_loaded("redland")) { 
  if (!
defined('PHP_SHLIB_SUFFIX')) {
    
define('PHP_SHLIB_SUFFIX'strtoupper(substr(PHP_OS0,3)) == 'WIN' 'dll' 'so');
  }
  if (!
defined('PHP_SHLIB_PREFIX')) {
    
define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' 'php_' '');
  }

  if (!
dl(PHP_SHLIB_PREFIX "redland" "." PHP_SHLIB_SUFFIX )){
    die(
'no redland?');
    exit;
  }
}

/**
 * Redland Class
 *
 * Main Class which is used to interact with Redland.
 * @return Redland
 */
class Redland {
    
/**
     * World variable stores the Redland world. Used in many functions.
     */
    
var $world;
    function 
Redland() {
        
$this->world librdf_php_get_world();
    }
    
/** 
     * @return Model
     */
    
function model($storage ''$dontknow '') {
        return new 
Model($this->world,$storage,$dontknow);
    }
    
/**
     * @return Parser
     */
    
function parser($type="application/rdf+xml",$uri="") {
        return new 
Parser($this->world,$type,$uri);
    }
    
/**
     * @return Query
     */
    
function query($querystring$querylang="rdql"$base "") {
        return new 
Query($this->world$querystring$querylang$base);
    }
    
/** 
     * @return Serializer
     */
    
function serializer($type="rdfxml"$mime "application/rdf+xml"$uri "") {
        return new 
Serializer($this->world$type$mime$uri);
    }
    function 
librdf_uri($uri "") {
        
$n = new Uri($this->world$uri);
        return 
$n->uri;
    }
    function 
uri($uri "") {
        return new 
Uri($this->world$uri);
    }
    function 
node($string "",$type="literal") {
        return new 
Node($this->world$string"literal");
    }
    function 
_destructor() {
        
librdf_free_world($this->world);
    }
}
class 
Model {
    
/**
     * World variable stores the Redland world. Used in many functions.
     */
    
var $world;
    
/**
     * This stores the actual Redland Model object.
     */
    
var $model;
    function 
Model($world$storage=''$dontknow='') {
        
$this->world $world;
        if (!
$storage)
            
$storage=librdf_new_storage($this->world,'hashes','dummy',"new=yes,hash-type='memory'");
        
$this->model librdf_new_model($this->world,$storage,$dontknow);
    }
    
/**
     * @return Stream
     */
    
function as_stream() {
        return new 
Stream(librdf_model_as_stream($this->model));
    }
    
/**
     * @return QueryResults
     */
    
function query($query) {
        return new 
QueryResults(librdf_model_query_execute($this->model,$query));
        
    }
}

/**
 * Node
 *
 * I don't really know what I need to do with this yet.
 */

class Node {
    var 
$node;
    var 
$uri;
    var 
$world;
    var 
$type;
    function 
Node($world$string=""$type "literal") {
        
$this->world $world;
        
$this->uri "";
        if (
$type == "literal") {
            
$this->node librdf_new_node_from_literal($this->world$string);
        }
        else if (
$type == "uri") {
            
$this->node librdf_new_node_from_uri_string($this->world$string);
            
$this->uri librdf_node_get_uri($this->node);
        } else if (
$type == "blank") {
            
$this->node librdf_new_node_from_uri_string($this->world,$string);
        }
        
$this->type librdf_node_get_type($this->node);
        
    }
    function 
copy($node) {
        
$this->node=$node;
    }
    function 
to_string() {
        return 
librdf_node_to_string($this->node);
    }
    
}
    
/**
 * Parser Class
 *
 * This class has functions for dealing with created parsers. It is best to
 * create the parser in the Redland class, and then use these methods to 
 * interact with it.
 */
class Parser {
    
/**
     * World variable stores the Redland world. Used in many functions.
     */
    
var $world;
    var 
$parser;
    function 
Parser($world$type="application/rdf+xml"$uri) {
        
$this->world $world;
        
$uri = new Uri($world$uri);
        
$this->parser librdf_new_parser($this->world,'raptor',$type,$uri->uri);
    }
    
/**
     * @param string URI of feature to set.
     * @param string String to set it to.
     */
    
function set_feature($uri ""$value "") {
        
$u = new Uri($this->world$uri);
        
$n = new Node($this->world$value"literal");
        
librdf_parser_set_feature($this->parser$u->uri$n->node);
    }
    
/**
     * @param Model
     * @param string URI to parse.
     */
    
function parse_into_model($model$uri "") {
        
$u = new Uri($this->world$uri);
        
librdf_parser_parse_into_model($this->parser,$u->uri,$u->uri,$model->model);
    }
}
class 
Query {
    var 
$querystring;
    var 
$querylang;
    var 
$query;
    var 
$world;
    function 
Query($world$querystring$querylang="rdql"$baseuri="") {
        
$this->world $world;
        
$this->query librdf_new_query($world$querylanglibrdf_new_uri($world,""), $querystringlibrdf_new_uri($world""));
    }
    function 
execute($model) {
        return new 
QueryResults(librdf_query_execute($this->query$model->model));
    }
}
class 
QueryResults {
    var 
$result;
    function 
QueryResults($result) {
        
$this->result $result;
    }
    function 
finished() {
        return (!
librdf_query_results_finished($this->result));
    }
    
/**
     * @param string Base URI.
     * @return string Query results, in Sparql XML format.
     */
    
function to_string($uri "") {
        
$r = new Redland;
        
$format $r->uri("http://www.w3.org/TR/2004/WD-rdf-sparql-XMLres-20041221/");
        
$base $r->uri($uri);
        return 
librdf_query_results_to_string($this->result$format->uri$base->uri);
    }
    function 
is_bindings() {
        return 
librdf_query_results_is_bindings($this->result);
    }
    function 
is_graph() {
        return 
librdf_query_results_is_graph($this->result);
    }
    function 
is_boolean() {
        return 
librdf_query_results_is_boolean($this->result);
    }
    function 
as_stream() {
        if (
$self->is_graph()) 
            return 
Stream(librdf_query_results_as_stream($this->result));
        else
            return 
NULL;
    }
    function 
next() {
        return 
librdf_query_results_next($this->result);
    }
    function 
result() {
        
$count librdf_query_results_get_bindings_count($this->result);
        
$result = array();
        for (
$i=0$i<$count$i++)
            
$result[librdf_query_results_get_binding_name($this->result$i)] = librdf_query_results_get_binding_value($this->result$i);
        return 
$result;
    }
    function 
_destructor() {
        
librdf_free_query_results($this->result);
    }
}

class 
Serializer {
    
/**
     * World variable stores the Redland world. Used in many functions.
     */
    
var $world;
    
/**
     * This stores the actual Redland Serializer object.
     */
    
var $serializer;
    function 
Serializer($world$type="rdfxml"$mime$uri) {
        
$this->world $world;
        
$u = new Uri($world$uri);
        
$this->serializer librdf_new_serializer($this->world,$type,$mime,$u->uri);
    }
    
/**
     * Serialize a model to a file.
     * @param Model
     * @param string A file to serialize to.
     * @param string Base URI to serialize against.
     */
    
function model_to_file($model$file$base="") {
        
$base = new Uri($this->world$base);
        
librdf_serializer_serialize_model_to_file($this->serializer,$file,$base->uri,$model->model);
    }
    function 
librdf_serializer_set_namespace($uri ""$prefix="") {
    }        
}
/**
 * Stream
 * 
 * This provides convenience functions for working with a Redland stream
 * object, which is returned from several other classes.
 */
class Stream {
    
/**
     * This stores the actual Redland Stream object.
     */
    
var $stream;
    
/** 
     * @param RedlandStream a Redland Stream object.
     */
    
function Stream($stream) {
        
$this->stream $stream;
    }
    function 
context() {
        return 
librdf_stream_get_context($this->stream);
    }
    
/**
     * Return the current statement in the stream.
     * @return Statement
     */
    
function current() {
        return new 
Statement(librdf_stream_get_object($this->stream));
    }
    
/** 
     * @return int non-zero if stream is ended.
     */
    
function end() {
        return 
librdf_stream_end($this->stream);
    }
    
/**
     * Advance the stream one.
     * @return int Non-zero if at the end of the stream.
     */
    
function next() {
        return 
librdf_stream_next($this->stream);
    }
}
class 
Statement {
    var 
$statement;
    var 
$subject;
    var 
$predicate;
    var 
$object;
    function 
Statement($statement) {
        
$this->statement $statement;
        
$this->subject librdf_statement_get_subject($statement);
        
$this->predicate librdf_statement_get_predicate($statement);
        
$this->object librdf_statement_get_object($statement);
    }
    
/**
     * Convert Statement to a string.
     * @return string
     */
    
function to_string() {
        return 
librdf_statement_to_string($this->statement);
    }
    
}
class 
Uri {
    var 
$world;
    var 
$uri;
    function 
Uri($world$string) {
        
$this->uri librdf_new_uri($world,$string);
    }
}
?>