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

(-)a/C4/Context.pm (+64 lines)
Lines 97-102 use File::Spec; Link Here
97
use Module::Load::Conditional qw(can_load);
97
use Module::Load::Conditional qw(can_load);
98
use POSIX ();
98
use POSIX ();
99
use ZOOM;
99
use ZOOM;
100
use YAML;
100
101
101
use C4::Boolean;
102
use C4::Boolean;
102
use C4::Debug;
103
use C4::Debug;
Lines 762-767 sub restore_dbh Link Here
762
    # return something, then this function should, too.
763
    # return something, then this function should, too.
763
}
764
}
764
765
766
=head2 triplestore
767
768
  my $triplestore = C4::Context->triplestore($model_name);
769
770
Returns a handle to an initialised RDF::Trine::Model object
771
772
=cut
773
774
sub triplestore {
775
    my ($self,$name) = @_;
776
    die "Must provide a model name as an argument for the triplestore() method" unless $name;
777
    my $triplestore = ( $context->{triplestore} && $context->{triplestore}->{$name} ) ? $context->{triplestore}->{$name} : undef;
778
    if ( ! $triplestore ){
779
        my $config_file = $context->config('triplestore_config');
780
        if ( -f $config_file ){
781
            my $config = YAML::LoadFile($config_file);
782
            if ($config && $name){
783
                my $new_triplestore = $self->_new_triplestore($config,$name);
784
                if ( $new_triplestore && $new_triplestore->isa("RDF::Trine::Model") ){
785
                    $triplestore = $new_triplestore;
786
                    $context->{triplestore}->{$name} = $new_triplestore;
787
                }
788
            }
789
        }
790
    }
791
    return $triplestore;
792
}
793
794
=head2 _new_triplestore
795
796
=cut
797
798
sub _new_triplestore {
799
    my ($self, $config,$name) = @_;
800
    if ($config && $name){
801
        my $models = $config->{models};
802
        if ($models){
803
            my $model = $models->{$name};
804
            if ($model){
805
                my $module = $model->{module};
806
                if ($module && $module eq 'RDF::Trine::Store::SPARQL'){
807
                    if ($module && can_load( 'modules' => { $module => undef } ) ){
808
                        my $url = URI->new($model->{url});
809
                        if ($url){
810
                            my $ua = RDF::Trine->default_useragent;
811
                            if ($ua){
812
                                my $realm = $model->{realm};
813
                                my $username = $model->{username};
814
                                my $password = $model->{password};
815
                                $ua->credentials($url->host.":".$url->port, $realm, $username, $password);
816
                            }
817
                            my $store = RDF::Trine::Store::SPARQL->new($url);
818
                            my $model = RDF::Trine::Model->new($store);
819
                            return $model;
820
                        }
821
                    }
822
                }
823
            }
824
        }
825
    }
826
    return;
827
}
828
765
=head2 queryparser
829
=head2 queryparser
766
830
767
  $queryparser = C4::Context->queryparser
831
  $queryparser = C4::Context->queryparser
(-)a/C4/Installer/PerlDependencies.pm (+5 lines)
Lines 893-898 our $PERL_DEPS = { Link Here
893
        required => '1',
893
        required => '1',
894
        min_ver  => '0.37',
894
        min_ver  => '0.37',
895
    },
895
    },
896
    'RDF::Trine' => {
897
        'usage' => 'Managing RDF and triplestores',
898
        'required' => '1',
899
        'min_ver' => '1.017',
900
    },
896
};
901
};
897
902
898
1;
903
1;
(-)a/debian/scripts/koha-create (+4 lines)
Lines 656-661 eof Link Here
656
    generate_config_file zebra.passwd.in \
656
    generate_config_file zebra.passwd.in \
657
        "/etc/koha/sites/$name/zebra.passwd"
657
        "/etc/koha/sites/$name/zebra.passwd"
658
658
659
    # Generate and install triplestore config file
660
    generate_config_file triplestore.yaml.in \
661
        "/etc/koha/sites/$name/triplestore.yaml"
662
659
    # Create a GPG-encrypted file for requesting a DB to be set up.
663
    # Create a GPG-encrypted file for requesting a DB to be set up.
660
    if [ "$op" = request ]
664
    if [ "$op" = request ]
661
    then
665
    then
(-)a/debian/templates/koha-conf-site.xml.in (+3 lines)
Lines 390-394 __END_SRU_PUBLICSERVER__ Link Here
390
      server. e.g. Antarctica/South_Pole -->
390
      server. e.g. Antarctica/South_Pole -->
391
 <timezone>__TIMEZONE__</timezone>
391
 <timezone>__TIMEZONE__</timezone>
392
392
393
 <!-- Configuration for RDF triplestore -->
394
 <triplestore_config>/etc/koha/sites/__KOHASITE__/triplestore.yaml</triplestore_config>
395
393
</config>
396
</config>
394
</yazgfs>
397
</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 206-210 __PAZPAR2_TOGGLE_XML_POST__ Link Here
206
      server. e.g. Antarctica/South_Pole -->
206
      server. e.g. Antarctica/South_Pole -->
207
 <timezone></timezone>
207
 <timezone></timezone>
208
208
209
 <!-- Configuration for RDF triplestore -->
210
 <triplestore_config>__KOHA_CONF_DIR__/triplestore.yaml</triplestore_config>
211
209
</config>
212
</config>
210
</yazgfs>
213
</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