| 1 | package Koha::Contrib::Sudoc::Koha; | ||
| 2 | # ABSTRACT: Lien à Koha | ||
| 3 | $Koha::Contrib::Sudoc::Koha::VERSION = '2.50'; | ||
| 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 qw/ GetFrameworkCode /;; | ||
| 12 | use Search::Elasticsearch; | ||
| 13 | use Koha::SearchEngine::Elasticsearch; | ||
| 14 | use YAML; | ||
| 15 | use MIME::Base64; | ||
| 16 | use Try::Tiny; | ||
| 17 | |||
| 18 | |||
| 19 | has conf_file => ( is => 'rw', isa => 'Str' ); | ||
| 20 | |||
| 21 | has dbh => ( is => 'rw' ); | ||
| 22 | |||
| 23 | has conf => ( is => 'rw' ); | ||
| 24 | |||
| 25 | has _zconn => ( is => 'rw', isa => 'HashRef' ); | ||
| 26 | |||
| 27 | has es => ( is => 'rw' ); | ||
| 28 | |||
| 29 | has es_index => ( is=> 'rw' ); | ||
| 30 | |||
| 31 | has sth_biblio => (is => 'rw' ); | ||
| 32 | |||
| 33 | has biblionumber_where => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'Str', | ||
| 36 | lazy => 1, | ||
| 37 | default => sub { | ||
| 38 | my $self = shift; | ||
| 39 | my $dbh = $self->dbh; | ||
| 40 | my $where = $self->dbh->selectall_arrayref(" | ||
| 41 | SELECT tagfield, tagsubfield | ||
| 42 | FROM marc_subfield_structure | ||
| 43 | WHERE kohafield ='biblio.biblionumber' | ||
| 44 | AND frameworkcode=''", | ||
| 45 | {} | ||
| 46 | ); | ||
| 47 | $where = join('', @{$where->[0]}); | ||
| 48 | my $tag = substr($where, 0, 3); | ||
| 49 | $where = substr($where, 0, 3) if $tag lt '010'; | ||
| 50 | return $where; | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | |||
| 55 | sub BUILD { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | # Use KOHA_CONF environment variable by default | ||
| 59 | $self->conf_file( $ENV{KOHA_CONF} ) unless $self->conf_file; | ||
| 60 | |||
| 61 | $self->conf( XMLin( $self->conf_file, | ||
| 62 | keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], | ||
| 63 | suppressempty => ' ') ); | ||
| 64 | |||
| 65 | # Database Handler | ||
| 66 | my $c = $self->conf->{config}; | ||
| 67 | $self->dbh( DBI->connect( | ||
| 68 | "DBI:" . $c->{db_scheme} . | ||
| 69 | ":dbname=" . $c->{database} . | ||
| 70 | ";host=" . $c->{hostname} . | ||
| 71 | ";port=" . $c->{port}, | ||
| 72 | $c->{user}, $c->{pass} ) | ||
| 73 | ) or carp $DBI::errstr; | ||
| 74 | if ( $c->{db_scheme} eq 'mysql' ) { | ||
| 75 | # Force utf8 communication between MySQL and koha | ||
| 76 | $self->dbh->{ mysql_enable_utf8 } = 1; | ||
| 77 | $self->dbh->do( "set NAMES 'utf8'" ); | ||
| 78 | my $tz = $ENV{TZ}; | ||
| 79 | ($tz) and $self->dbh->do( qq(SET time_zone = "$tz") ); | ||
| 80 | } | ||
| 81 | |||
| 82 | if ( C4::Context->preference('SearchEngine') eq 'Elasticsearch' ) { | ||
| 83 | my $param = $c->{elasticsearch}; | ||
| 84 | my $es = Search::Elasticsearch->new( | ||
| 85 | Koha::SearchEngine::Elasticsearch::get_elasticsearch_params() | ||
| 86 | ); | ||
| 87 | $self->es( $es ); | ||
| 88 | $self->es_index( { | ||
| 89 | biblios => $param->{index_name} . '_biblios', | ||
| 90 | authorities => $param->{index_name} . '_authorities', | ||
| 91 | } ); | ||
| 92 | } | ||
| 93 | |||
| 94 | # Zebra connections | ||
| 95 | $self->_zconn( { biblio => undef, auth => undef } ); | ||
| 96 | |||
| 97 | # Récupération d'une notice biblio brute | ||
| 98 | # Since version 17.05 marcxml biblio record is stored in biblio_metadata table. | ||
| 99 | my $version = C4::Context->preference('Version'); | ||
| 100 | $self->sth_biblio( $self->dbh->prepare( | ||
| 101 | $version =~ /^([0-9]{2})/ && $1 >= 17 | ||
| 102 | ? "SELECT metadata FROM biblio_metadata WHERE biblionumber=? " | ||
| 103 | : "SELECT marcxml FROM biblioitems WHERE biblionumber=? " | ||
| 104 | ) ); | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | # Réinitialisation des deux connexions | ||
| 109 | sub zconn_reset { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | return if $self->es; # Ne rien faire en mode ES | ||
| 113 | |||
| 114 | my $zcs = $self->_zconn; | ||
| 115 | for my $server ( keys %$zcs ) { | ||
| 116 | my $zc = $zcs->{$server}; | ||
| 117 | $zc->destroy() if $zc; | ||
| 118 | undef $zcs->{$server}; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | sub zconn { | ||
| 124 | my ($self, $server) = @_; | ||
| 125 | |||
| 126 | my $zc = $self->_zconn->{$server}; | ||
| 127 | #return $zc if $zc && $zc->errcode() == 0 && $zc->_check(); | ||
| 128 | return $zc if $zc; | ||
| 129 | |||
| 130 | #FIXME: à réactiver pour s'assurer que de nouvelles connexions ne sont | ||
| 131 | # créées inutilement. | ||
| 132 | #print "zconn: nouvelle connexion\n"; | ||
| 133 | my $c = $self->conf; | ||
| 134 | my $name = $server eq 'biblio' ? 'biblioserver' : 'authorityserver'; | ||
| 135 | #my $syntax = "Unimarc"; | ||
| 136 | my $host = $c->{listen}->{$name}->{content}; | ||
| 137 | my $user = $c->{serverinfo}->{$name}->{user}; | ||
| 138 | my $password = $c->{serverinfo}->{$name}->{password}; | ||
| 139 | my $auth = $user && $password; | ||
| 140 | |||
| 141 | # set options | ||
| 142 | my $o = new ZOOM::Options(); | ||
| 143 | if ( $user && $password ) { | ||
| 144 | $o->option( user => $user ); | ||
| 145 | $o->option( password => $password ); | ||
| 146 | } | ||
| 147 | #$o->option(async => 1) if $async; | ||
| 148 | #$o->option(count => $piggyback) if $piggyback; | ||
| 149 | $o->option( cqlfile => $c->{server}->{$name}->{cql2rpn} ); | ||
| 150 | $o->option( cclfile => $c->{serverinfo}->{$name}->{ccl2rpn} ); | ||
| 151 | #$o->option( preferredRecordSyntax => $syntax ); | ||
| 152 | $o->option( elementSetName => "F"); # F for 'full' as opposed to B for 'brief' | ||
| 153 | $o->option( databaseName => $server eq 'biblio' ? "biblios" : "authorities"); | ||
| 154 | |||
| 155 | $zc = create ZOOM::Connection( $o ); | ||
| 156 | $zc->connect($host, 0); | ||
| 157 | carp "something wrong with the connection: ". $zc->errmsg() | ||
| 158 | if $zc->errcode; | ||
| 159 | |||
| 160 | $self->_zconn->{$server} = $zc; | ||
| 161 | return $zc; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | sub zbiblio { | ||
| 166 | shift->zconn( 'biblio' ); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub zauth { | ||
| 171 | shift->zconn( 'auth' ); | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub get_biblionumber { | ||
| 176 | my ($self, $record) = @_; | ||
| 177 | |||
| 178 | my $where = $self->biblionumber_where; | ||
| 179 | my $tag = substr($where, 0, 3); | ||
| 180 | my $letter = substr($where, 3); | ||
| 181 | $letter | ||
| 182 | ? $record->field($tag)->subfield($letter) | ||
| 183 | : $record->field($tag)->value; | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | sub get_biblionumber_framework { | ||
| 188 | my ($self, $record) = @_; | ||
| 189 | my $biblionumber = $self->get_biblionumber($record); | ||
| 190 | my $framework = $self->dbh->selectall_arrayref(" | ||
| 191 | SELECT frameworkcode FROM biblio WHERE biblionumber=$biblionumber", | ||
| 192 | {} | ||
| 193 | ); | ||
| 194 | $framework = $framework->[0]->[0]; | ||
| 195 | ($biblionumber, $framework); | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | sub get_biblio { | ||
| 200 | my ($self, $biblionumber) = @_; | ||
| 201 | |||
| 202 | return (undef, undef) unless $biblionumber; | ||
| 203 | $self->sth_biblio->execute($biblionumber); | ||
| 204 | my ($xml) = $self->sth_biblio->fetchrow; | ||
| 205 | return (undef, undef) unless $xml; | ||
| 206 | |||
| 207 | my $record = MARC::Moose::Record::new_from($xml, 'MarcXml'); | ||
| 208 | return (undef, undef) unless $record; | ||
| 209 | |||
| 210 | (GetFrameworkCode($biblionumber), $record); | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub get_biblio_by_ppn { | ||
| 215 | my ($self, $ppn) = @_; | ||
| 216 | |||
| 217 | my ($record, $biblionumber, $framework);; | ||
| 218 | |||
| 219 | if ( my $es = $self->es ) { # Elasticsearch | ||
| 220 | my $res = $es->search( | ||
| 221 | index => $self->es_index->{biblios}, | ||
| 222 | body => { | ||
| 223 | query => { match => { ppn => $ppn } } | ||
| 224 | } | ||
| 225 | ); | ||
| 226 | my $total = $res->{hits}->{total}; | ||
| 227 | $total = $total->{value} if ref($total) eq 'HASH'; | ||
| 228 | if ( $total != 0 ) { | ||
| 229 | my $source = $res->{hits}->{hits}->[0]->{_source}; | ||
| 230 | unless ($source) { | ||
| 231 | say Dump($res); | ||
| 232 | exit; | ||
| 233 | } | ||
| 234 | $record = _record_from_es($source); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | try { | ||
| 239 | my $rs = $self->zbiblio()->search_pqf( "\@attr 1=PPN $ppn" ); | ||
| 240 | if ( $rs->size() >= 1 ) { | ||
| 241 | $record = $rs->record(0); | ||
| 242 | $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' ); | ||
| 243 | } | ||
| 244 | } catch { | ||
| 245 | warn "ZOOM error: $_"; | ||
| 246 | }; | ||
| 247 | } | ||
| 248 | |||
| 249 | if ( $record ) { | ||
| 250 | ($biblionumber, $framework) = $self->get_biblionumber_framework($record); | ||
| 251 | return ($biblionumber, $framework, $record); | ||
| 252 | } | ||
| 253 | return (undef, undef, undef) unless $record; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | sub get_biblios_by_authid { | ||
| 258 | my ($self, $authid) = @_; | ||
| 259 | |||
| 260 | my @records; | ||
| 261 | if ( my $es = $self->es ) { # Elasticsearch | ||
| 262 | my $res = $es->search( | ||
| 263 | index => $self->es_index->{biblios}, | ||
| 264 | body => { | ||
| 265 | query => { match => { "Koha-Auth-Number" => $authid } } | ||
| 266 | } | ||
| 267 | ); | ||
| 268 | my $hits = $res->{hits}->{hits}; | ||
| 269 | if ( @$hits != 0 ) { | ||
| 270 | for my $source ( @$hits ) { | ||
| 271 | $source = $source->{_source}; | ||
| 272 | my $record = _record_from_es($source); | ||
| 273 | next unless $record; | ||
| 274 | my ($biblionumber, $framework) = $self->get_biblionumber_framework($record); | ||
| 275 | push @records, [$biblionumber, $framework, $record]; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | try { | ||
| 281 | my $rs = $self->zbiblio()->search_pqf( "\@attr 1=Koha-Auth-Number $authid" ); | ||
| 282 | for ( my $i = 0; $i < $rs->size(); $i++ ) { | ||
| 283 | my $record = $rs->record($i); | ||
| 284 | $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' ); | ||
| 285 | next unless $record; | ||
| 286 | my ($biblionumber, $framework) = $self->get_biblionumber_framework($record); | ||
| 287 | push @records, [$biblionumber, $framework, $record]; | ||
| 288 | } | ||
| 289 | } catch { | ||
| 290 | warn "ZOOM error: $_"; | ||
| 291 | }; | ||
| 292 | } | ||
| 293 | return @records; | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | sub _record_from_es { | ||
| 298 | my $source = shift; | ||
| 299 | |||
| 300 | my $record = MARC::Moose::Record->new(); | ||
| 301 | |||
| 302 | if (my $raw = $source->{marc_data_array}) { | ||
| 303 | my @fields; | ||
| 304 | for my $field ( @{$raw->{fields}} ) { | ||
| 305 | my $tag = [ keys %$field ]->[0]; | ||
| 306 | my $value = $field->{$tag}; | ||
| 307 | if ( $tag le '009' ) { | ||
| 308 | push @fields, MARC::Moose::Field::Control->new( | ||
| 309 | tag => $tag, value => $value ); | ||
| 310 | } | ||
| 311 | else { | ||
| 312 | my $f = MARC::Moose::Field::Std->new( | ||
| 313 | tag => $tag, ind1 => $value->{ind1}, ind2 => $value->{ind2} ); | ||
| 314 | my @subf; | ||
| 315 | for (@{$value->{subfields}}) { | ||
| 316 | my $letter = [ keys %$_ ]->[0]; | ||
| 317 | my $val = $_->{$letter}; | ||
| 318 | push @subf, [ $letter, $val ]; | ||
| 319 | } | ||
| 320 | $f->subf(\@subf); | ||
| 321 | push @fields, $f; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | $record->fields( \@fields ); | ||
| 325 | $record->_leader($raw->{leader}); | ||
| 326 | return $record; | ||
| 327 | } | ||
| 328 | |||
| 329 | my $format = $source->{marc_format}; | ||
| 330 | unless ($format) { | ||
| 331 | say Dump($source); | ||
| 332 | exit; | ||
| 333 | } | ||
| 334 | my $data = $source->{marc_data}; | ||
| 335 | $record = $format eq 'base64ISO2709' | ||
| 336 | ? MARC::Moose::Record::new_from(decode_base64($data), 'Iso2709') | ||
| 337 | : MARC::Moose::Record::new_from($data, 'Marcxml'); | ||
| 338 | return $record; | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | sub get_auth_by_ppn { | ||
| 343 | my ($self, $ppn) = @_; | ||
| 344 | |||
| 345 | my ($authid, $record); | ||
| 346 | |||
| 347 | if ( my $es = $self->es ) { # Elasticsearch | ||
| 348 | my $res = $es->search( | ||
| 349 | index => $self->es_index->{authorities}, | ||
| 350 | body => { | ||
| 351 | query => { match => { ppn => $ppn } } | ||
| 352 | } | ||
| 353 | ); | ||
| 354 | my $total = $res->{hits}->{total}; | ||
| 355 | $total = $total->{value} if ref($total) eq 'HASH'; | ||
| 356 | if ( $total != 0 ) { | ||
| 357 | my $source = $res->{hits}->{hits}->[0]->{_source}; | ||
| 358 | $record = _record_from_es($source); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | else { # Zebra | ||
| 362 | try { | ||
| 363 | my $rs = $self->zauth()->search_pqf( "\@attr 1=PPN $ppn" ); | ||
| 364 | if ( $rs->size() >= 1 ) { | ||
| 365 | $record = $rs->record(0); | ||
| 366 | $record = MARC::Moose::Record::new_from( $record->raw(), 'Iso2709' ); | ||
| 367 | } | ||
| 368 | }; | ||
| 369 | } | ||
| 370 | |||
| 371 | $authid = $record->field('001')->value if $record; | ||
| 372 | return ($authid, $record); | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | __PACKAGE__->meta->make_immutable; | ||
| 377 | |||
| 378 | 1; | ||
| 379 | |||
| 380 | __END__ |