| 1 | package Koha::Contrib::Tamil::Koha; | ||
| 2 | #ABSTRACT: Class exposing info about a Koha instance. | ||
| 3 | $Koha::Contrib::Tamil::Koha::VERSION = '0.074'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use Modern::Perl; | ||
| 7 | use Carp; | ||
| 8 | use XML::Simple; | ||
| 9 | use DBI; | ||
| 10 | use ZOOM; | ||
| 11 | use MARC::Record; | ||
| 12 | use MARC::File::XML; | ||
| 13 | use YAML; | ||
| 14 | use Search::Elasticsearch; | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | has conf_file => ( is => 'rw', isa => 'Str' ); | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | has dbh => ( is => 'rw' ); | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | has conf => ( is => 'rw' ); | ||
| 27 | |||
| 28 | |||
| 29 | has _zconn => ( is => 'rw', isa => 'HashRef' ); | ||
| 30 | |||
| 31 | has es => ( is => 'rw' ); | ||
| 32 | |||
| 33 | has es_index => ( is=> 'rw' ); | ||
| 34 | |||
| 35 | has koha_version => ( is => 'rw', isa => 'Num', default => 0 ); | ||
| 36 | |||
| 37 | |||
| 38 | sub BUILD { | ||
| 39 | my $self = shift; | ||
| 40 | |||
| 41 | # Use KOHA_CONF environment variable by default | ||
| 42 | $self->conf_file( $ENV{KOHA_CONF} ) unless $self->conf_file; | ||
| 43 | |||
| 44 | $self->conf( XMLin( $self->conf_file, | ||
| 45 | keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], | ||
| 46 | suppressempty => ' ') ); | ||
| 47 | |||
| 48 | # Database Handler | ||
| 49 | my $c = $self->conf->{config}; | ||
| 50 | $self->dbh( DBI->connect( | ||
| 51 | "DBI:" . $c->{db_scheme} . | ||
| 52 | ":dbname=" . $c->{database} . | ||
| 53 | ";host=" . $c->{hostname} . | ||
| 54 | ";port=" . $c->{port}, | ||
| 55 | $c->{user}, $c->{pass} ) | ||
| 56 | ) or carp $DBI::errstr; | ||
| 57 | if ( $c->{db_scheme} eq 'mysql' ) { | ||
| 58 | # Force utf8 communication between MySQL and koha | ||
| 59 | $self->dbh->{ mysql_enable_utf8 } = 1; | ||
| 60 | $self->dbh->do( "set NAMES 'utf8'" ); | ||
| 61 | $self->dbh->{ mysql_auto_reconnect } = 1; | ||
| 62 | my $tz = $ENV{TZ}; | ||
| 63 | ($tz) and $self->dbh->do( qq(SET time_zone = "$tz") ); | ||
| 64 | } | ||
| 65 | |||
| 66 | # Zebra connections | ||
| 67 | $self->_zconn( { biblio => undef, auth => undef } ); | ||
| 68 | |||
| 69 | # ElasticSearch | ||
| 70 | if ( my $param = $c->{elasticsearch} ) { | ||
| 71 | my $es = Search::Elasticsearch->new( nodes => $param->{server} ); | ||
| 72 | $self->es( $es ); | ||
| 73 | $self->es_index( { | ||
| 74 | biblios => $param->{index_name} . '_biblios', | ||
| 75 | authorities => $param->{index_name} . '_authorities', | ||
| 76 | } ); | ||
| 77 | } | ||
| 78 | |||
| 79 | my $version = C4::Context->preference('Version'); | ||
| 80 | if ( $version =~ /^([0-9]{2})\.([0-9]{2})/ ) { | ||
| 81 | $version = "$1.$2"; | ||
| 82 | $version += 0; | ||
| 83 | $self->koha_version($version); | ||
| 84 | if ($version < 22.11) { | ||
| 85 | require C4::Biblio; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | require Koha::Biblios; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | sub zconn_reset { | ||
| 97 | my $self = shift; | ||
| 98 | my $zcs = $self->_zconn; | ||
| 99 | for my $server ( keys %$zcs ) { | ||
| 100 | my $zc = $zcs->{$server}; | ||
| 101 | $zc->destroy() if $zc; | ||
| 102 | undef $zcs->{$server}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | sub zconn { | ||
| 109 | my ($self, $server) = @_; | ||
| 110 | |||
| 111 | my $zc = $self->_zconn->{$server}; | ||
| 112 | #return $zc if $zc && $zc->errcode() == 0 && $zc->_check(); | ||
| 113 | return $zc if $zc; | ||
| 114 | |||
| 115 | #FIXME: à réactiver pour s'assurer que de nouvelles connexion ne sont | ||
| 116 | # créées inutilement. | ||
| 117 | #print "zconn: nouvelle connexion\n"; | ||
| 118 | my $c = $self->conf; | ||
| 119 | my $name = $server eq 'biblio' ? 'biblioserver' : 'authorityserver'; | ||
| 120 | my $syntax = $c->{server}->{$name}->{retrievalinfo}->{retrieval}; | ||
| 121 | $syntax = [ grep { $_->{name} && $_->{name} eq 'F' && $_->{syntax} ne 'xml' } @$syntax ]; | ||
| 122 | $syntax = $syntax->[0]->{syntax}; | ||
| 123 | my $host = $c->{listen}->{$name}->{content}; | ||
| 124 | my $user = $c->{serverinfo}->{$name}->{user}; | ||
| 125 | my $password = $c->{serverinfo}->{$name}->{password}; | ||
| 126 | my $auth = $user && $password; | ||
| 127 | |||
| 128 | # set options | ||
| 129 | my $o = new ZOOM::Options(); | ||
| 130 | if ( $user && $password ) { | ||
| 131 | $o->option( user => $user ); | ||
| 132 | $o->option( password => $password ); | ||
| 133 | } | ||
| 134 | #$o->option(async => 1) if $async; | ||
| 135 | #$o->option(count => $piggyback) if $piggyback; | ||
| 136 | $o->option( cqlfile => $c->{server}->{$name}->{cql2rpn} ); | ||
| 137 | $o->option( cclfile => $c->{serverinfo}->{$name}->{ccl2rpn} ); | ||
| 138 | $o->option( preferredRecordSyntax => $syntax ); | ||
| 139 | $o->option( elementSetName => "F"); # F for 'full' as opposed to B for 'brief' | ||
| 140 | $o->option( databaseName => $server eq 'biblio' ? "biblios" : "authorities"); | ||
| 141 | |||
| 142 | $zc = create ZOOM::Connection( $o ); | ||
| 143 | $zc->connect($host, 0); | ||
| 144 | carp "something wrong with the connection: ". $zc->errmsg() | ||
| 145 | if $zc->errcode; | ||
| 146 | |||
| 147 | $self->_zconn->{$server} = $zc; | ||
| 148 | return $zc; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | sub zbiblio { | ||
| 154 | shift->zconn( 'biblio' ); | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | sub zauth { | ||
| 160 | shift->zconn( 'auth' ); | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | sub get_biblio_marc { | ||
| 166 | my ( $self, $id ) = @_; | ||
| 167 | my $sth = $self->dbh->prepare( | ||
| 168 | "SELECT metadata FROM biblio_metadata WHERE biblionumber=? "); | ||
| 169 | $sth->execute( $id ); | ||
| 170 | my ($marcxml) = $sth->fetchrow; | ||
| 171 | return unless $marcxml; | ||
| 172 | $marcxml =~ | ||
| 173 | s/[^\x09\x0A\x0D\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g; | ||
| 174 | #MARC::File::XML->default_record_format( | ||
| 175 | #C4::Context->preference('marcflavour') ); | ||
| 176 | my $record = MARC::Record->new(); | ||
| 177 | if ($marcxml) { | ||
| 178 | $record = eval { | ||
| 179 | MARC::Record::new_from_xml( $marcxml, "utf8" ) }; | ||
| 180 | if ($@) { warn " problem with: $id : $@ \n$marcxml"; } | ||
| 181 | return $record; | ||
| 182 | } | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | sub get_biblio { | ||
| 189 | my ($self, $id) = @_; | ||
| 190 | |||
| 191 | my $sth = $self->dbh->prepare( | ||
| 192 | "SELECT metadata FROM biblio_metadata WHERE biblionumber=? "); | ||
| 193 | $sth->execute( $id ); | ||
| 194 | my ($marcxml) = $sth->fetchrow; | ||
| 195 | return unless $marcxml; | ||
| 196 | $marcxml =~ | ||
| 197 | s/[^\x09\x0A\x0D\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g; | ||
| 198 | my $record = MARC::Moose::Record::new_from($marcxml, 'Marcxml'); | ||
| 199 | return $record; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | __PACKAGE__->meta->make_immutable; | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | __END__ |