@@ -, +, @@
---
C4/Context.pm | 55 ++++++++++++++++++++++++++++++++
debian/scripts/koha-create | 4 +++
debian/templates/koha-conf-site.xml.in | 3 ++
debian/templates/triplestore.yaml.in | 9 ++++++
etc/koha-conf.xml | 3 ++
etc/triplestore.yaml | 9 ++++++
t/db_dependent/triplestore.t | 57 ++++++++++++++++++++++++++++++++++
7 files changed, 140 insertions(+)
create mode 100644 debian/templates/triplestore.yaml.in
create mode 100644 etc/triplestore.yaml
create mode 100644 t/db_dependent/triplestore.t
--- a/C4/Context.pm
+++ a/C4/Context.pm
@@ -96,6 +96,7 @@ use POSIX ();
use DateTime::TimeZone;
use Module::Load::Conditional qw(can_load);
use Carp;
+use YAML;
use C4::Boolean;
use C4::Debug;
@@ -774,6 +775,60 @@ sub restore_dbh
# return something, then this function should, too.
}
+=head2 triplestore
+
+ $triplestore = C4::Context->triplestore
+
+Returns a handle to an initialised RDF::Trine::Model object
+
+=cut
+
+sub triplestore {
+ my $self = shift;
+ unless (defined $context->{"triplestore"}) {
+ my $config_file = $context->config('triplestore_config');
+ if ( -f $config_file ){
+ my $config = YAML::LoadFile($config_file);
+ if ($config){
+ $context->{"triplestore"} = &_new_triplestore($config);
+ }
+ }
+ }
+ return
+ defined( $context->{"triplestore"} )
+ ? $context->{"triplestore"}
+ : undef;
+}
+
+=head _new_triplestore
+
+=cut
+
+sub _new_triplestore {
+ my ($config) = @_;
+ if ($config){
+ my $module = $config->{module};
+ if ($module eq 'RDF::Trine::Store::SPARQL'){
+ if ($module && can_load( 'modules' => { $module => undef } ) ){
+ my $url = URI->new($config->{url});
+ if ($url){
+ my $ua = RDF::Trine->default_useragent;
+ if ($ua){
+ my $realm = $config->{realm};
+ my $username = $config->{username};
+ my $password = $config->{password};
+ $ua->credentials($url->host.":".$url->port, $realm, $username, $password);
+ }
+ my $store = RDF::Trine::Store::SPARQL->new($url);
+ my $model = RDF::Trine::Model->new($store);
+ return $model;
+ }
+ }
+ }
+ }
+ return;
+}
+
=head2 queryparser
$queryparser = C4::Context->queryparser
--- a/debian/scripts/koha-create
+++ a/debian/scripts/koha-create
@@ -709,6 +709,10 @@ eof
generate_config_file zebra.passwd.in \
"/etc/koha/sites/$name/zebra.passwd"
+ # Generate and install triplestore config file
+ generate_config_file triplestore.yaml.in \
+ "/etc/koha/sites/$name/triplestore.yaml"
+
# Create a GPG-encrypted file for requesting a DB to be set up.
if [ "$op" = request ]
--- a/debian/templates/koha-conf-site.xml.in
+++ a/debian/templates/koha-conf-site.xml.in
@@ -322,5 +322,8 @@ __END_SRU_PUBLICSERVER__
50
2
+
+ /etc/koha/sites/__KOHASITE__/triplestore.yaml
+
--- a/debian/templates/triplestore.yaml.in
+++ a/debian/templates/triplestore.yaml.in
@@ -0,0 +1,9 @@
+---
+#module must be a valid RDF::Trine::Store::* module
+module: RDF::Trine::Store::SPARQL
+
+#When using RDF::Trine::Store::SPARQL:
+url:
+realm:
+username:
+password:
--- a/etc/koha-conf.xml
+++ a/etc/koha-conf.xml
@@ -153,5 +153,8 @@ __PAZPAR2_TOGGLE_XML_POST__
50
2
+
+ __KOHA_CONF_DIR__/triplestore.yaml
+
--- a/etc/triplestore.yaml
+++ a/etc/triplestore.yaml
@@ -0,0 +1,9 @@
+---
+#module must be a valid RDF::Trine::Store::* module
+module: RDF::Trine::Store::SPARQL
+
+#When using RDF::Trine::Store::SPARQL:
+url:
+realm:
+username:
+password:
--- a/t/db_dependent/triplestore.t
+++ a/t/db_dependent/triplestore.t
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+use Modern::Perl;
+use Test::More tests => 3;
+use YAML;
+use File::Temp qw/ tempfile /;
+
+use C4::Context;
+
+#Make a temporary configuration file for the triplestore
+my ($fh, $filename) = tempfile(undef, UNLINK => 1);
+my $temp_config = {
+ module => 'RDF::Trine::Store::SPARQL',
+ url => 'http://localhost/example/rdf',
+ realm => undef,
+ username => undef,
+ password => undef,
+};
+
+#Tell C4::Context to use the temporary configuration file as the triplestore_config
+my $context = $C4::Context::context;
+$context->{config}->{triplestore_config} = $filename;
+
+SUCCESS: {
+ YAML::DumpFile($filename, $temp_config);
+
+ my $context_object = C4::Context->new();
+ my $triplestore = $context_object->triplestore;
+ is(ref $triplestore, 'RDF::Trine::Model', 'C4::Context->new()->triplestore returns RDF::Trine::Model if module equals RDF::Trine::Store::SPARQL');
+}
+
+MISSING_URL: {
+ #Reset triplestore context
+ delete $context->{triplestore};
+
+ my $url = delete $temp_config->{url};
+ YAML::DumpFile($filename, $temp_config);
+
+ my $context_object = C4::Context->new();
+ my $triplestore = $context_object->triplestore;
+ is($triplestore, undef, 'C4::Context->new()->triplestore returns undef if missing url for SPARQL store');
+
+ #Restore url for other tests
+ $temp_config->{url} = $url;
+}
+
+BAD_MODULE: {
+ #Reset triplestore context
+ delete $context->{triplestore};
+
+ $temp_config->{module} = 'Local::RDF::Bad::Module';
+ YAML::DumpFile($filename, $temp_config);
+
+ my $context_object = C4::Context->new();
+ my $triplestore = $context_object->triplestore;
+ is($triplestore, undef, 'C4::Context->new()->triplestore returns undef if module equals Local::RDF::Bad::Module');
+}
--