Apache-SPARQL-RDFStore

 view release on metacpan or  search on metacpan

lib/Apache/SPARQL/RDFStore.pm  view on Meta::CPAN

package Apache::SPARQL::RDFStore;

use strict;
use base qw (Apache::SPARQL);

$Apache::SPARQL::RDFStore::VERSION = '0.3';

use RDFStore::Model;

use DBI;

# for output-xslt parameter fancyness
use XML::LibXML;
use XML::LibXSLT;

sub query {
        my ($class, $ap, $query, $query_uri, $graph_id, $output_xslt, $output_type, $format, $limit ) = @_;

	$query = $class->_cat($ap, $query_uri)
		if(    ! $query or
			$query eq '' or
			$query =~ m/^\s+$/ );

	my $smart = $class->_param($ap, 'smart'); #1/0 flag whether or not use simple RDF/S inferencing
	$smart = ( $smart and $smart !~ m/(0|no|off)/ ) ? 1 : 0 ;

	if( defined $limit and $limit < 0 ) {
        	$class->_error( $ap, "Negative LIMIT $limit" );
		return $Apache::SPARQL::Responses{ 'OperationPointError' };
		};

	if( $format !~ s/^\s*(rdfxml|ntriples|xml)\s*$/$1/ ) {
		$class->_error( $ap, "Format $format is not supported" );
		return $Apache::SPARQL::Responses{ 'OperationPointError' };
		};

	#
	# more advanced stuff
	#
	# Any other CGI paramter starting with the 'rdflet_' prefix is passed down to the SPARQL query itself, and can be referred
	# into the SPARQL query as $$param_name. Multiple parameters result into a OR-ed list of value - each value can be either
	# a full qulified URI resource or a literal (with it xml:lang or rdf:datatype attached), using some N-Triples/Turtle like syntax.
	#	
	# Example:
	#
	# Given /sparql/?sparqlet_URL=<http://foo.bar/com.html>
	#
	# and the following SPARQL '&query' paramter
	#
	#       prefix dc: <http://purl.org/dc/elements/1.1/>
	#       select ?title
	#       where ( $$URL dc:title ?title )
	#
	# would re-write the query as:
	#
	#       prefix dc: <http://purl.org/dc/elements/1.1/>
	#       select ?title
	#       where ( <http://foo.bar/com.html> dc:title ?title )
	#
	# which will get the title of item 'http://foo.bar/com.html' - similarly if sparqler_title="foo bar title"@ch and so on..

	my %sparqlet_parameters = ();
	my @params = $class->_param( $ap ); #any param
	map {
		my $name = $_;
		if ( $name =~ s/^sparqlet_// ) {
			$sparqlet_parameters{ $name } = { 'r' => [], 'l' => [] }
				unless( exists $sparqlet_parameters{ $name } );

			my @values = $class->_param( $ap, 'sparqlet_'.$name );
			foreach my $value ( @values ) {
				if ( $value =~ s/^\s*\<([^>]+)\>\s*// ) {
					push @{ $sparqlet_parameters{ $name }->{ 'r' } }, $1;
				} elsif( $value =~ s/^\s*(%?[\"\']((([^\"\'\\\n\r])|(\\([ntbrf\\'\"])|([0-7][0-7?)|([0-3][0-7][0-7])))*)[\"\'](\@([a-z0-9]+(-[a-z0-9]+)?))?%?)\s*// ) {
					push @{ $sparqlet_parameters{ $name }->{ 'l' } }, $1;
					};
				};
			};
		} @params;

	# merge graph-id/s and 
	#if( $class->_SPARQLhas_Source( $query ) ) {
	#} else {
	#	};
	my $data = new RDFStore::Model;
	if($graph_id) {
		foreach my $graph_id ( @{ $graph_id } ) {
			# bad - try to guess out format from file extension
			eval {
				$data->getReader( ($graph_id =~ /\.(nt|ntriples)$/) ? 'N-Triples' : 'RDF/XML' )->parsestring(
					$class->_cat( $ap, $graph_id ) );
				};
			if($@) {
        			$class->_error( $ap, "Cannot process graph-id $graph_id: $@ " );
				return $Apache::SPARQL::Responses{ 'OperationPointError' };
				};
			};
		};

	my $dbh = DBI->connect("DBI:RDFStore:", "sparqler", 0, {
			'sourceModel' => $data,
			'smarter' => $smart } );



( run in 0.560 second using v1.01-cache-2.11-cpan-e1769b4cff6 )