Tamil/RecordReader.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 259 CC 28 SCAR 42.3
CC
28
Cov
0%
BUILD
182.0
get_biblio_xml
42.0
get_biblio_marc
42.0
CRAP 68.4
1 package Koha::Contrib::Tamil::RecordReader;
2 #ABSTRACT: Koha biblio/authority records reader
3 $Koha::Contrib::Tamil::RecordReader::VERSION = '0.074';
4 use Moose;
5  
6 with 'MooseX::RW::Reader';
7  
8  
9 use Modern::Perl;
10 use utf8;
11 use Moose::Util::TypeConstraints;
12 use MARC::Record;
13 use MARC::File::XML;
14 use C4::Context;
15 use C4::Biblio qw/ GetMarcFromKohaField /;
16 use C4::Items qw/ Item2Marc /;
17  
18  
19 subtype 'Koha::RecordType'
20 => as 'Str',
21 => where { /biblio|authority/i },
22 => message { "$_ is not a valid Koha::RecordType (biblio or authority" };
23  
24 subtype 'Koha::RecordSelect'
25 => as 'Str',
26 => where { /all|queue|queue_update|queue_delete/ },
27 => message {
28 "$_ is not a valide Koha::RecordSelect " .
29 "(all or queue or queue_update or queue_delete)"
30 };
31  
32 has koha => ( is => 'rw', isa => 'Koha::Contrib::Tamil::Koha', required => 1 );
33  
34 has source => (
35 is => 'rw',
36 isa => 'Koha::RecordType',
37 required => 1,
38 default => 'biblio',
39 );
40  
41 has select => (
42 is => 'rw',
43 isa => 'Koha::RecordSelect',
44 required => 1,
45 default => 'all',
46 );
47  
48 has xml => ( is => 'rw', isa => 'Bool', default => '0' );
49  
50 has sth => ( is => 'rw' );
51  
52 # Last returned record biblionumber;
53 has id => ( is => 'rw' );
54  
55 # Items extraction required
56 has itemsextraction => ( is => 'rw', isa => 'Bool', default => 0 );
57  
58 # Biblio records normalizer, if necessary
59 has normalizer => ( is => 'rw' );
60  
61 # Read all records? (or queued records)
62 has allrecords => ( is => 'rw', isa => 'Bool', default => 1 );
63  
64 # Mark as done an entry is Zebra queue
65 has sth_queue_done => ( is => 'rw' );
66  
67 # SQL statement to get marcxml record
68 has sth_biblio => ( is => 'rw' );
69 has sth_biblio_del => ( is => 'rw' );
70  
71 # Items tag
72 has itemtag => ( is => 'rw' );
73  
74 # Las returned record frameworkcode
75 # FIXME: a KohaRecord class should contain this information
76 has frameworkcode => ( is => 'rw', isa => 'Str' );
77  
78  
79 sub BUILD {
80 my $self = shift;
81 my $dbh = $self->koha->dbh;
82  
83 # Récupération du tag contenant les exemplaires
84 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",'');
85 $self->itemtag($itemtag);
86  
87 # Koha version => items extraction if >= 3.4
88 my $version = C4::Context->preference('Version');
89 $self->itemsextraction(
90 $version =~ /^3/ && $version ge '3.04' || $version =~ /^[0-9]{2}/ ? 1 : 0 );
91  
92 if ( $version ge '3.09' && $self->source =~ /biblio/i &&
93 C4::Context->preference('IncludeSeeFromInSearches') )
94 {
95 require Koha::RecordProcessor;
96 my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
97 $self->normalizer($normalizer);
98 # Necessary for as_xml method
99 MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') );
100 }
101  
102 # Since version 17.05 marcxml biblio record is stored in biblio_metadata table.
103 if ( $version =~ /^([0-9]{2})/ && $1 >= 17 ) {
104 $self->sth_biblio( $dbh->prepare(
105 "SELECT metadata FROM biblio_metadata WHERE biblionumber=? " ) );
106 $self->sth_biblio_del( $dbh->prepare(
107 "SELECT metadata FROM deletedbiblio_metadata WHERE biblionumber=? " ) );
108 }
109 else {
110 $self->sth_biblio( $dbh->prepare(
111 "SELECT marcxml FROM biblioitems WHERE biblionumber=? " ) );
112 $self->sth_biblio_del( $dbh->prepare(
113 "SELECT marcxml FROM deletedbiblioitems WHERE biblionumber=? " ) );
114 }
115  
116 my $operation = $self->select =~ /update/i
117 ? 'specialUpdate'
118 : 'recordDelete';
119 $self->allrecords( $self->select =~ /all/i ? 1 : 0 );
120 my $sql =
121 $self->source =~ /biblio/i
122 ? $self->allrecords
123 ? "SELECT NULL, biblionumber FROM biblio"
124 : "SELECT id, biblio_auth_number FROM zebraqueue
125 WHERE server = 'biblioserver'
126 AND operation = '$operation' AND done = 0"
127 : $self->allrecords
128 ? "SELECT NULL, authid FROM auth_header"
129 : "SELECT id, biblio_auth_number FROM zebraqueue
130 WHERE server = 'authorityserver'
131 AND operation = '$operation' AND done = 0";
132 my $sth = $self->koha->dbh->prepare( $sql );
133 $sth->execute();
134 $self->sth( $sth );
135  
136 unless ( $self->allrecords ) {
137 $self->sth_queue_done( $self->koha->dbh->prepare(
138 "UPDATE zebraqueue SET done=1 WHERE id=?" ) );
139 }
140
141 __PACKAGE__->meta->add_method( 'get' =>
142 $self->source =~ /biblio/i
143 ? $self->xml && !$self->normalizer
144 ? \&get_biblio_xml
145 : \&get_biblio_marc
146 : $self->xml
147 ? \&get_auth_xml
148 : \&get_auth_marc
149 );
150 }
151  
152  
153  
154 sub read {
155 my $self = shift;
156 while ( my ($queue_id, $id) = $self->sth->fetchrow ) {
157 # Suppress entry in zebraqueue table
158 $self->sth_queue_done->execute($queue_id) if $queue_id;
159 if ( my $record = $self->get( $id ) ) {
160 $record = $self->normalizer->process($record) if $self->normalizer;
161 $self->count($self->count+1);
162 $self->id( $id );
163 return $record;
164 }
165 }
166 return 0;
167 }
168  
169  
170  
171 sub get_biblio_xml {
172 my ($self, $id) = @_;
173  
174 $self->sth_biblio->execute( $id );
175 my ($marcxml) = $self->sth_biblio->fetchrow;
176 unless ( $marcxml ) {
177 $self->sth_biblio_del->execute( $id );
178 ($marcxml) = $self->sth_biblio_del->fetchrow;
179 }
180  
181 # Items extraction if Koha v3.4 and above
182 # FIXME: It slows down drastically biblio records export
183 if ( $self->itemsextraction ) {
184 my @items = @{ $self->koha->dbh->selectall_arrayref(
185 "SELECT * FROM items WHERE biblionumber=$id",
186 {Slice => {} } ) };
187 if (@items){
188 my $record = MARC::Record->new;
189 $record->encoding('UTF-8');
190 my @itemsrecord;
191 my $not_onloan_count = 0;
192 foreach my $item (@items) {
193 $not_onloan_count++ unless $item->{onloan};
194 my $record = Item2Marc($item, $id);
195 push @itemsrecord, $record->field($self->itemtag);
196 }
197 push @itemsrecord, MARC::Field->new('999', ' ', ' ', 'x' => $not_onloan_count);
198 $record->insert_fields_ordered(@itemsrecord);
199 my $itemsxml = $record->as_xml_record();
200 $marcxml =
201 substr($marcxml, 0, length($marcxml)-10) .
202 substr($itemsxml, index($itemsxml, "</leader>\n", 0) + 10);
203 }
204 }
205 if ( C4::Context->preference('Frantiq') ) {
206 require Frantiq::Pactol::BiblioNormalizer;
207 Frantiq::Pactol::BiblioNormalizer::process(\$marcxml);
208 }
209 return $marcxml;
210 }
211  
212  
213 # Same as Koha::Contrib::Tamil::get_biblio_marc, but if the record doesn't
214 # exist in biblioitems, it is search in deletedbiblioitems.
215 sub get_biblio_marc {
216 my ( $self, $id ) = @_;
217  
218 $self->sth_biblio->execute( $id );
219 my ($marcxml) = $self->sth_biblio->fetchrow;
220 unless ( $marcxml ) {
221 $self->sth_biblio_del->execute( $id );
222 ($marcxml) = $self->sth_biblio_del->fetchrow;
223 }
224  
225 $marcxml =~ s/[^\x09\x0A\x0D\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//g;
226 my $record = MARC::Record->new();
227 if ($marcxml) {
228 $record = eval {
229 MARC::Record::new_from_xml( $marcxml, "utf8" ) };
230 if ($@) { warn " problem with: $id : $@ \n$marcxml"; }
231  
232 # Items extraction if Koha v3.4 and above
233 # FIXME: It slows down drastically biblio records export
234 if ( $self->itemsextraction ) {
235 my @items = @{ $self->koha->dbh->selectall_arrayref(
236 "SELECT * FROM items WHERE biblionumber=$id",
237 {Slice => {} } ) };
238 if (@items){
239 my @itemsrecord;
240 foreach my $item (@items) {
241 my $record = Item2Marc($item, $id);
242 push @itemsrecord, $record->field($self->itemtag);
243 }
244 $record->insert_fields_ordered(@itemsrecord);
245 }
246 }
247 return $record;
248 }
249 return;
250 }
251  
252  
253 sub get_auth_xml {
254 my ( $self, $id ) = @_;
255 my $sth = $self->koha->dbh->prepare(
256 "select marcxml from auth_header where authid=? " );
257 $sth->execute( $id );
258 my ($xml) = $sth->fetchrow;
259  
260 # If authority isn't found we build a mimimalist record
261 # Usefull for delete Zebra requests
262 unless ( $xml ) {
263 return
264 "<record
265 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
266 xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\"
267 xmlns=\"http://www.loc.gov/MARC21/slim\">
268 <leader> </leader>
269 <controlfield tag=\"001\">$id</controlfield>
270 </record>\n";
271 }
272  
273 my $new_xml = '';
274 foreach ( split /\n/, $xml ) {
275 next if /^<collection|^<\/collection/;
276 $new_xml .= "$_\n";
277 }
278 return $new_xml;
279 }
280  
281  
282 no Moose;
283 1;
284  
285 __END__