View | Details | Raw Unified | Return to bug 18585
Collapse All | Expand All

(-)a/C4/Context.pm (+64 lines)
Lines 97-102 use POSIX (); Link Here
97
use DateTime::TimeZone;
97
use DateTime::TimeZone;
98
use Module::Load::Conditional qw(can_load);
98
use Module::Load::Conditional qw(can_load);
99
use Carp;
99
use Carp;
100
use YAML;
100
101
101
use C4::Boolean;
102
use C4::Boolean;
102
use C4::Debug;
103
use C4::Debug;
Lines 776-781 sub restore_dbh Link Here
776
    # return something, then this function should, too.
777
    # return something, then this function should, too.
777
}
778
}
778
779
780
=head2 triplestore
781
782
  my $triplestore = C4::Context->triplestore($model_name);
783
784
Returns a handle to an initialised RDF::Trine::Model object
785
786
=cut
787
788
sub triplestore {
789
    my ($self,$name) = @_;
790
    die "Must provide a model name as an argument for the triplestore() method" unless $name;
791
    my $triplestore = ( $context->{triplestore} && $context->{triplestore}->{$name} ) ? $context->{triplestore}->{$name} : undef;
792
    if ( ! $triplestore ){
793
        my $config_file = $context->config('triplestore_config');
794
        if ( -f $config_file ){
795
            my $config = YAML::LoadFile($config_file);
796
            if ($config && $name){
797
                my $new_triplestore = $self->_new_triplestore($config,$name);
798
                if ( $new_triplestore && $new_triplestore->isa("RDF::Trine::Model") ){
799
                    $triplestore = $new_triplestore;
800
                    $context->{triplestore}->{$name} = $new_triplestore;
801
                }
802
            }
803
        }
804
    }
805
    return $triplestore;
806
}
807
808
=head2 _new_triplestore
809
810
=cut
811
812
sub _new_triplestore {
813
    my ($self, $config,$name) = @_;
814
    if ($config && $name){
815
        my $models = $config->{models};
816
        if ($models){
817
            my $model = $models->{$name};
818
            if ($model){
819
                my $module = $model->{module};
820
                if ($module && $module eq 'RDF::Trine::Store::SPARQL'){
821
                    if ($module && can_load( 'modules' => { $module => undef } ) ){
822
                        my $url = URI->new($model->{url});
823
                        if ($url){
824
                            my $ua = RDF::Trine->default_useragent;
825
                            if ($ua){
826
                                my $realm = $model->{realm};
827
                                my $username = $model->{username};
828
                                my $password = $model->{password};
829
                                $ua->credentials($url->host.":".$url->port, $realm, $username, $password);
830
                            }
831
                            my $store = RDF::Trine::Store::SPARQL->new($url);
832
                            my $model = RDF::Trine::Model->new($store);
833
                            return $model;
834
                        }
835
                    }
836
                }
837
            }
838
        }
839
    }
840
    return;
841
}
842
779
=head2 queryparser
843
=head2 queryparser
780
844
781
  $queryparser = C4::Context->queryparser
845
  $queryparser = C4::Context->queryparser
(-)a/C4/Installer/PerlDependencies.pm (+5 lines)
Lines 867-872 our $PERL_DEPS = { Link Here
867
        'required' => '0',
867
        'required' => '0',
868
        'min_ver'  => '0.17',
868
        'min_ver'  => '0.17',
869
    },
869
    },
870
    'RDF::Trine' => {
871
        'usage' => 'Managing RDF and triplestores',
872
        'required' => '1',
873
        'min_ver' => '1.017',
874
    },
870
};
875
};
871
876
872
1;
877
1;
(-)a/debian/scripts/koha-create (+4 lines)
Lines 743-748 eof Link Here
743
    generate_config_file zebra.passwd.in \
743
    generate_config_file zebra.passwd.in \
744
        "/etc/koha/sites/$name/zebra.passwd"
744
        "/etc/koha/sites/$name/zebra.passwd"
745
745
746
    # Generate and install triplestore config file
747
    generate_config_file triplestore.yaml.in \
748
        "/etc/koha/sites/$name/triplestore.yaml"
749
746
    # Create a GPG-encrypted file for requesting a DB to be set up.
750
    # Create a GPG-encrypted file for requesting a DB to be set up.
747
    if [ "$op" = request ]
751
    if [ "$op" = request ]
748
    then
752
    then
(-)a/debian/templates/koha-conf-site.xml.in (+3 lines)
Lines 345-349 __END_SRU_PUBLICSERVER__ Link Here
345
     <partner_code>ILLLIBS</partner_code>
345
     <partner_code>ILLLIBS</partner_code>
346
 </interlibrary_loans>
346
 </interlibrary_loans>
347
347
348
 <!-- Configuration for RDF triplestore -->
349
 <triplestore_config>/etc/koha/sites/__KOHASITE__/triplestore.yaml</triplestore_config>
350
348
</config>
351
</config>
349
</yazgfs>
352
</yazgfs>
(-)a/debian/templates/triplestore.yaml.in (+30 lines)
Line 0 Link Here
1
---
2
models:
3
    query:
4
        #module must be a valid RDF::Trine::Store::* module
5
        module: RDF::Trine::Store::SPARQL
6
7
        #When using RDF::Trine::Store::SPARQL:
8
        url:
9
        realm:
10
        username:
11
        password:
12
    update:
13
        #module must be a valid RDF::Trine::Store::* module
14
        module: RDF::Trine::Store::SPARQL
15
16
        #When using RDF::Trine::Store::SPARQL:
17
        url:
18
        realm:
19
        username:
20
        password:
21
    #NOTE: Only the above models are likely to be used in Koha
22
    read/write:
23
        #module must be a valid RDF::Trine::Store::* module
24
        module: RDF::Trine::Store::SPARQL
25
26
        #When using RDF::Trine::Store::SPARQL:
27
        url:
28
        realm:
29
        username:
30
        password:
(-)a/etc/koha-conf.xml (+3 lines)
Lines 171-175 __PAZPAR2_TOGGLE_XML_POST__ Link Here
171
     <partner_code>ILLLIBS</partner_code>
171
     <partner_code>ILLLIBS</partner_code>
172
 </interlibrary_loans>
172
 </interlibrary_loans>
173
173
174
 <!-- Configuration for RDF triplestore -->
175
 <triplestore_config>__KOHA_CONF_DIR__/triplestore.yaml</triplestore_config>
176
174
</config>
177
</config>
175
</yazgfs>
178
</yazgfs>
(-)a/etc/triplestore.yaml (+30 lines)
Line 0 Link Here
1
---
2
models:
3
    query:
4
        #module must be a valid RDF::Trine::Store::* module
5
        module: RDF::Trine::Store::SPARQL
6
7
        #When using RDF::Trine::Store::SPARQL:
8
        url:
9
        realm:
10
        username:
11
        password:
12
    update:
13
        #module must be a valid RDF::Trine::Store::* module
14
        module: RDF::Trine::Store::SPARQL
15
16
        #When using RDF::Trine::Store::SPARQL:
17
        url:
18
        realm:
19
        username:
20
        password:
21
    #NOTE: Only the above models are likely to be used in Koha
22
    read/write:
23
        #module must be a valid RDF::Trine::Store::* module
24
        module: RDF::Trine::Store::SPARQL
25
26
        #When using RDF::Trine::Store::SPARQL:
27
        url:
28
        realm:
29
        username:
30
        password:
(-)a/t/db_dependent/triplestore.t (-1 / +67 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 4;
5
use Test::Exception;
6
use YAML;
7
use File::Temp qw/ tempfile /;
8
9
use C4::Context;
10
11
#Make a temporary configuration file for the triplestore
12
my ($fh, $filename) = tempfile(undef, UNLINK => 1);
13
my $temp_config = {
14
    models => {
15
        test => {
16
            module => 'RDF::Trine::Store::SPARQL',
17
            url => 'http://localhost/example/rdf',
18
            realm => undef,
19
            username => undef,
20
            password => undef,
21
        }
22
    }
23
};
24
25
#Tell C4::Context to use the temporary configuration file as the triplestore_config
26
my $context = $C4::Context::context;
27
$context->{config}->{triplestore_config} = $filename;
28
29
subtest 'success' => sub {
30
    YAML::DumpFile($filename, $temp_config);
31
32
    my $context_object = C4::Context->new();
33
    my $triplestore = $context_object->triplestore("test");
34
    is(ref $triplestore, 'RDF::Trine::Model', 'C4::Context->new()->triplestore returns RDF::Trine::Model if module equals RDF::Trine::Store::SPARQL');
35
};
36
37
subtest 'missing url' => sub {
38
    #Reset triplestore context
39
    delete $context->{triplestore}->{test};
40
41
    my $url = delete $temp_config->{models}->{test}->{url};
42
    YAML::DumpFile($filename, $temp_config);
43
44
    my $context_object = C4::Context->new();
45
    my $triplestore = $context_object->triplestore("test");
46
    is($triplestore, undef, 'C4::Context->new()->triplestore returns undef if missing url for SPARQL store');
47
48
    #Restore url for other tests
49
    $temp_config->{url} = $url;
50
};
51
52
subtest 'bad module' => sub {
53
    #Reset triplestore context
54
    delete $context->{triplestore}->{test};
55
56
    $temp_config->{module} = 'Local::RDF::Bad::Module';
57
    YAML::DumpFile($filename, $temp_config);
58
59
    my $context_object = C4::Context->new();
60
    my $triplestore = $context_object->triplestore("test");
61
    is($triplestore, undef, 'C4::Context->new()->triplestore returns undef if module equals Local::RDF::Bad::Module');
62
};
63
64
subtest 'missing model name' => sub {
65
    my $context_object = C4::Context->new();
66
    dies_ok { $context_object->triplestore } 'C4::Context::triplestore() method dies if no model name provided';
67
};

Return to bug 18585