Koha/Contrib/Sudoc/Koha.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 207 CC 17 SCAR 25.2
CC
17
Cov
0%
zconn
42.0
BUILD
12.0
get_biblio
12.0
CRAP 12.4
1 package Koha::Contrib::Sudoc::Koha;
2 # ABSTRACT: Lien à Koha
3 $Koha::Contrib::Sudoc::Koha::VERSION = '2.20';
4 use Moose;
5 use Modern::Perl;
6 use Carp;
7 use XML::Simple;
8 use DBI;
9 use ZOOM;
10 use MARC::Moose::Record;
11 use C4::Biblio;
12 use YAML;
13 use Try::Tiny;
14  
15  
16 has conf_file => ( is => 'rw', isa => 'Str' );
17  
18 has dbh => ( is => 'rw' );
19  
20 has conf => ( is => 'rw' );
21  
22 has _zconn => ( is => 'rw', isa => 'HashRef' );
23  
24  
25  
26 sub BUILD {
27 my $self = shift;
28  
29 # Use KOHA_CONF environment variable by default
30 $self->conf_file( $ENV{KOHA_CONF} ) unless $self->conf_file;
31  
32 $self->conf( XMLin( $self->conf_file,
33 keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'],
34 suppressempty => ' ') );
35  
36 # Database Handler
37 my $c = $self->conf->{config};
38 $self->dbh( DBI->connect(
39 "DBI:" . $c->{db_scheme} .
40 ":dbname=" . $c->{database} .
41 ";host=" . $c->{hostname} .
42 ";port=" . $c->{port},
43 $c->{user}, $c->{pass} )
44 ) or carp $DBI::errstr;
45 if ( $c->{db_scheme} eq 'mysql' ) {
46 # Force utf8 communication between MySQL and koha
47 $self->dbh->{ mysql_enable_utf8 } = 1;
48 $self->dbh->do( "set NAMES 'utf8'" );
49 my $tz = $ENV{TZ};
50 ($tz) and $self->dbh->do( qq(SET time_zone = "$tz") );
51 }
52  
53 # Zebra connections
54 $self->_zconn( { biblio => undef, auth => undef } );
55 }
56  
57  
58 # Réinitialisation des deux connexions
59 sub zconn_reset {
60 my $self = shift;
61 my $zcs = $self->_zconn;
62 for my $server ( keys %$zcs ) {
63 my $zc = $zcs->{$server};
64 $zc->destroy() if $zc;
65 undef $zcs->{$server};
66 }
67 }
68  
69  
70 sub zconn {
71 my ($self, $server) = @_;
72  
73 my $zc = $self->_zconn->{$server};
74 #return $zc if $zc && $zc->errcode() == 0 && $zc->_check();
75 return $zc if $zc;
76  
77 #FIXME: à réactiver pour s'assurer que de nouvelles connexions ne sont
78 # créées inutilement.
79 #print "zconn: nouvelle connexion\n";
80 my $c = $self->conf;
81 my $name = $server eq 'biblio' ? 'biblioserver' : 'authorityserver';
82 #my $syntax = "Unimarc";
83 my $host = $c->{listen}->{$name}->{content};
84 my $user = $c->{serverinfo}->{$name}->{user};
85 my $password = $c->{serverinfo}->{$name}->{password};
86 my $auth = $user && $password;
87  
88 # set options
89 my $o = new ZOOM::Options();
90 if ( $user && $password ) {
91 $o->option( user => $user );
92 $o->option( password => $password );
93 }
94 #$o->option(async => 1) if $async;
95 #$o->option(count => $piggyback) if $piggyback;
96 $o->option( cqlfile => $c->{server}->{$name}->{cql2rpn} );
97 $o->option( cclfile => $c->{serverinfo}->{$name}->{ccl2rpn} );
98 #$o->option( preferredRecordSyntax => $syntax );
99 $o->option( elementSetName => "F"); # F for 'full' as opposed to B for 'brief'
100 $o->option( databaseName => $server eq 'biblio' ? "biblios" : "authorities");
101  
102 $zc = create ZOOM::Connection( $o );
103 $zc->connect($host, 0);
104 carp "something wrong with the connection: ". $zc->errmsg()
105 if $zc->errcode;
106  
107 $self->_zconn->{$server} = $zc;
108 return $zc;
109 }
110  
111  
112 sub zbiblio {
113 shift->zconn( 'biblio' );
114 }
115  
116  
117 sub zauth {
118 shift->zconn( 'auth' );
119 }
120  
121  
122 sub get_biblionumber {
123 my ($self, $record) = @_;
124 my ($tag, $letter) = GetMarcFromKohaField("biblio.biblionumber", '');
125 $tag < 10
126 ? $record->field($tag)->value
127 : $record->field($tag)->subfield($letter);
128 }
129  
130  
131 sub get_biblionumber_framework {
132 my ($self, $record) = @_;
133 my $biblionumber = $self->get_biblionumber($record);
134 ( $biblionumber, GetFrameworkCode($biblionumber) );
135 }
136  
137  
138 # Return a MARC::Moose::Record from its biblionumber,
139 # and the record framework: (framework, record)
140 sub get_biblio {
141 my ($self, $biblionumber) = @_;
142 my $sth = $self->dbh->prepare(
143 "SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
144 $sth->execute( $biblionumber );
145 my ($marcxml) = $sth->fetchrow;
146 return undef unless $marcxml;
147 my $record = MARC::Moose::Record::new_from($marcxml, 'Marcxml');
148 return (undef, undef) unless $record;
149  
150 (GetFrameworkCode($biblionumber), $record);
151 }
152  
153  
154 # Lecture d'une notice biblio par son PPN
155 sub get_biblio_by_ppn {
156 my ($self, $ppn) = @_;
157 my ($rs, $record, $biblionumber, $framework);;
158 try {
159 $rs = $self->zbiblio()->search_pqf( "\@attr 1=PPN $ppn" );
160 if ( $rs->size() >= 1 ) {
161 $record = $rs->record(0);
162 $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' );
163 return (undef, undef, undef) unless $record;
164 ($biblionumber, $framework) = $self->get_biblionumber_framework($record);
165 }
166 } catch {
167 warn "ZOOM error: $_";
168 };
169 return ($biblionumber, $framework, $record);
170 }
171  
172  
173 sub get_biblios_by_authid {
174 my ($self, $authid) = @_;
175  
176 my @records;
177 try {
178 my $rs = $self->zbiblio()->search_pqf( "\@attr 1=Koha-Auth-Number $authid" );
179 for ( my $i = 0; $i < $rs->size(); $i++ ) {
180 my $record = $rs->record($i);
181 $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' );
182 next unless $record;
183 my ($biblionumber, $framework) = $self->get_biblionumber_framework($record);
184 push @records, [$biblionumber, $framework, $record];
185 }
186 } catch {
187 warn "ZOOM error: $_";
188 };
189 return @records;
190 }
191  
192  
193 sub get_auth_by_ppn {
194 my ($self, $ppn) = @_;
195 my ($rs, $authid, $record);
196 try {
197 $rs = $self->zauth()->search_pqf( "\@attr 1=PPN $ppn" );
198 if ( $rs->size() >= 1 ) {
199 $record = $rs->record(0);
200 $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' );
201 # FIXME: En dur, le authid Koha en 001, comme dans Koha lui-même
202 $authid = $record->field('001')->value if $record;
203 }
204 };
205 return ($authid, $record);
206 }
207  
208  
209 __PACKAGE__->meta->make_immutable;
210  
211 1;
212  
213 __END__
214  
215 =pod
216  
217 =encoding UTF-8
218  
219 =head1 NAME
220  
221 Koha::Contrib::Sudoc::Koha - Lien à Koha
222  
223 =head1 VERSION
224  
225 version 2.20
226  
227 =head1 DESCRIPTION
228  
229 =head1 METHODS
230  
231 =head2 get_biblios_by_authid
232  
233 Retrouve les notices associées à une autorité Koha identifiée par son
234 C<authid> Retourne un tableau de (biblionumner, framework, record)
235  
236 =head2 get_auth_by_ppn
237  
238 Lecture d'une autorité par son PPN. Renvoie un tableau contenant deux entrées:
239 l'authid de l'autorité et l'enregistrement de l'autorité trouvée.
240  
241 =head1 SYNOPSYS
242  
243 # Default Koha instance, defined in default koha-conf.xml file,
244 # identified by KOHA_CONF environment variable
245 my $k1 = Koha->new();
246  
247 my $k2 = Koha->new( conf_file => '/usr/koha/world-library/etc/koha-conf.xml' );
248  
249 =head1 AUTHOR
250  
251 Frédéric Demians <f.demians@tamil.fr>
252  
253 =head1 COPYRIGHT AND LICENSE
254  
255 This software is Copyright (c) 2016 by Fréderic Demians.
256  
257 This is free software, licensed under:
258  
259 The GNU General Public License, Version 3, June 2007
260  
261 =cut