Filename | /usr/share/koha/lib/C4/OAI/Sets.pm |
Statements | Executed 13 statements in 2.54ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 40µs | 174µs | BEGIN@32 | C4::OAI::Sets::
1 | 1 | 1 | 32µs | 32µs | BEGIN@37 | C4::OAI::Sets::
1 | 1 | 1 | 15µs | 18µs | BEGIN@33 | C4::OAI::Sets::
1 | 1 | 1 | 11µs | 94µs | BEGIN@35 | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | AddOAISet | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | AddOAISetsBiblios | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | CalcOAISetsBiblio | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | DelOAISet | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | DelOAISetsBiblio | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISet | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISetBySpec | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISetMappings | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISets | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISetsBiblio | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | GetOAISetsMappings | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | ModOAISet | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | ModOAISetMappings | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | ModOAISetsBiblios | C4::OAI::Sets::
0 | 0 | 0 | 0s | 0s | UpdateOAISetsBiblio | C4::OAI::Sets::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package C4::OAI::Sets; | ||||
2 | |||||
3 | # Copyright 2011 BibLibre | ||||
4 | # | ||||
5 | # This file is part of Koha. | ||||
6 | # | ||||
7 | # Koha is free software; you can redistribute it and/or modify it under the | ||||
8 | # terms of the GNU General Public License as published by the Free Software | ||||
9 | # Foundation; either version 2 of the License, or (at your option) any later | ||||
10 | # version. | ||||
11 | # | ||||
12 | # Koha is distributed in the hope that it will be useful, but WITHOUT ANY | ||||
13 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||||
14 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||
15 | # | ||||
16 | # You should have received a copy of the GNU General Public License along | ||||
17 | # with Koha; if not, write to the Free Software Foundation, Inc., | ||||
18 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
19 | |||||
20 | =head1 NAME | ||||
21 | |||||
- - | |||||
32 | 3 | 54µs | 2 | 307µs | # spent 174µs (40+133) within C4::OAI::Sets::BEGIN@32 which was called:
# once (40µs+133µs) by C4::Biblio::BEGIN@39 at line 32 # spent 174µs making 1 call to C4::OAI::Sets::BEGIN@32
# spent 133µs making 1 call to Modern::Perl::import |
33 | 3 | 34µs | 2 | 22µs | # spent 18µs (15+3) within C4::OAI::Sets::BEGIN@33 which was called:
# once (15µs+3µs) by C4::Biblio::BEGIN@39 at line 33 # spent 18µs making 1 call to C4::OAI::Sets::BEGIN@33
# spent 3µs making 1 call to C4::Context::import |
34 | |||||
35 | 3 | 86µs | 2 | 177µs | # spent 94µs (11+83) within C4::OAI::Sets::BEGIN@35 which was called:
# once (11µs+83µs) by C4::Biblio::BEGIN@39 at line 35 # spent 94µs making 1 call to C4::OAI::Sets::BEGIN@35
# spent 83µs making 1 call to vars::import |
36 | |||||
37 | # spent 32µs within C4::OAI::Sets::BEGIN@37 which was called:
# once (32µs+0s) by C4::Biblio::BEGIN@39 at line 46 | ||||
38 | 3 | 35µs | require Exporter; | ||
39 | @ISA = qw(Exporter); | ||||
40 | @EXPORT = qw( | ||||
41 | &GetOAISets &GetOAISet &GetOAISetBySpec &ModOAISet &DelOAISet &AddOAISet | ||||
42 | &GetOAISetsMappings &GetOAISetMappings &ModOAISetMappings | ||||
43 | &GetOAISetsBiblio &ModOAISetsBiblios &AddOAISetsBiblios | ||||
44 | &CalcOAISetsBiblio &UpdateOAISetsBiblio | ||||
45 | ); | ||||
46 | 1 | 2.32ms | 1 | 32µs | } # spent 32µs making 1 call to C4::OAI::Sets::BEGIN@37 |
47 | |||||
48 | =head1 FUNCTIONS | ||||
49 | |||||
- - | |||||
69 | sub GetOAISets { | ||||
70 | my $dbh = C4::Context->dbh; | ||||
71 | my $query = qq{ | ||||
72 | SELECT * FROM oai_sets | ||||
73 | }; | ||||
74 | my $sth = $dbh->prepare($query); | ||||
75 | $sth->execute; | ||||
76 | my $results = $sth->fetchall_arrayref({}); | ||||
77 | |||||
78 | $query = qq{ | ||||
79 | SELECT description | ||||
80 | FROM oai_sets_descriptions | ||||
81 | WHERE set_id = ? | ||||
82 | }; | ||||
83 | $sth = $dbh->prepare($query); | ||||
84 | foreach my $set (@$results) { | ||||
85 | $sth->execute($set->{'id'}); | ||||
86 | my $desc = $sth->fetchall_arrayref({}); | ||||
87 | foreach (@$desc) { | ||||
88 | push @{$set->{'descriptions'}}, $_->{'description'}; | ||||
89 | } | ||||
90 | } | ||||
91 | |||||
92 | return $results; | ||||
93 | } | ||||
94 | |||||
95 | =head2 GetOAISet | ||||
96 | |||||
- - | |||||
105 | sub GetOAISet { | ||||
106 | my ($set_id) = @_; | ||||
107 | |||||
108 | return unless $set_id; | ||||
109 | |||||
110 | my $dbh = C4::Context->dbh; | ||||
111 | my $query = qq{ | ||||
112 | SELECT * | ||||
113 | FROM oai_sets | ||||
114 | WHERE id = ? | ||||
115 | }; | ||||
116 | my $sth = $dbh->prepare($query); | ||||
117 | $sth->execute($set_id); | ||||
118 | my $set = $sth->fetchrow_hashref; | ||||
119 | |||||
120 | $query = qq{ | ||||
121 | SELECT description | ||||
122 | FROM oai_sets_descriptions | ||||
123 | WHERE set_id = ? | ||||
124 | }; | ||||
125 | $sth = $dbh->prepare($query); | ||||
126 | $sth->execute($set->{'id'}); | ||||
127 | my $desc = $sth->fetchall_arrayref({}); | ||||
128 | foreach (@$desc) { | ||||
129 | push @{$set->{'descriptions'}}, $_->{'description'}; | ||||
130 | } | ||||
131 | |||||
132 | return $set; | ||||
133 | } | ||||
134 | |||||
135 | =head2 GetOAISetBySpec | ||||
136 | |||||
- - | |||||
143 | sub GetOAISetBySpec { | ||||
144 | my $setSpec = shift; | ||||
145 | |||||
146 | return unless defined $setSpec; | ||||
147 | |||||
148 | my $dbh = C4::Context->dbh; | ||||
149 | my $query = qq{ | ||||
150 | SELECT * | ||||
151 | FROM oai_sets | ||||
152 | WHERE spec = ? | ||||
153 | LIMIT 1 | ||||
154 | }; | ||||
155 | my $sth = $dbh->prepare($query); | ||||
156 | $sth->execute($setSpec); | ||||
157 | |||||
158 | return $sth->fetchrow_hashref; | ||||
159 | } | ||||
160 | |||||
161 | =head2 ModOAISet | ||||
162 | |||||
- - | |||||
175 | sub ModOAISet { | ||||
176 | my ($set) = @_; | ||||
177 | |||||
178 | return unless($set && $set->{'spec'} && $set->{'name'}); | ||||
179 | |||||
180 | if(!defined $set->{'id'}) { | ||||
181 | warn "Set ID not defined, can't modify the set"; | ||||
182 | return; | ||||
183 | } | ||||
184 | |||||
185 | my $dbh = C4::Context->dbh; | ||||
186 | my $query = qq{ | ||||
187 | UPDATE oai_sets | ||||
188 | SET spec = ?, | ||||
189 | name = ? | ||||
190 | WHERE id = ? | ||||
191 | }; | ||||
192 | my $sth = $dbh->prepare($query); | ||||
193 | $sth->execute($set->{'spec'}, $set->{'name'}, $set->{'id'}); | ||||
194 | |||||
195 | if($set->{'descriptions'}) { | ||||
196 | $query = qq{ | ||||
197 | DELETE FROM oai_sets_descriptions | ||||
198 | WHERE set_id = ? | ||||
199 | }; | ||||
200 | $sth = $dbh->prepare($query); | ||||
201 | $sth->execute($set->{'id'}); | ||||
202 | |||||
203 | if(scalar @{$set->{'descriptions'}} > 0) { | ||||
204 | $query = qq{ | ||||
205 | INSERT INTO oai_sets_descriptions (set_id, description) | ||||
206 | VALUES (?,?) | ||||
207 | }; | ||||
208 | $sth = $dbh->prepare($query); | ||||
209 | foreach (@{ $set->{'descriptions'} }) { | ||||
210 | $sth->execute($set->{'id'}, $_) if $_; | ||||
211 | } | ||||
212 | } | ||||
213 | } | ||||
214 | } | ||||
215 | |||||
216 | =head2 DelOAISet | ||||
217 | |||||
- - | |||||
224 | sub DelOAISet { | ||||
225 | my ($set_id) = @_; | ||||
226 | |||||
227 | return unless $set_id; | ||||
228 | |||||
229 | my $dbh = C4::Context->dbh; | ||||
230 | my $query = qq{ | ||||
231 | DELETE oai_sets, oai_sets_descriptions, oai_sets_mappings | ||||
232 | FROM oai_sets | ||||
233 | LEFT JOIN oai_sets_descriptions ON oai_sets_descriptions.set_id = oai_sets.id | ||||
234 | LEFT JOIN oai_sets_mappings ON oai_sets_mappings.set_id = oai_sets.id | ||||
235 | WHERE oai_sets.id = ? | ||||
236 | }; | ||||
237 | my $sth = $dbh->prepare($query); | ||||
238 | $sth->execute($set_id); | ||||
239 | } | ||||
240 | |||||
241 | =head2 AddOAISet | ||||
242 | |||||
- - | |||||
255 | sub AddOAISet { | ||||
256 | my ($set) = @_; | ||||
257 | |||||
258 | return unless($set && $set->{'spec'} && $set->{'name'}); | ||||
259 | |||||
260 | my $set_id; | ||||
261 | my $dbh = C4::Context->dbh; | ||||
262 | my $query = qq{ | ||||
263 | INSERT INTO oai_sets (spec, name) | ||||
264 | VALUES (?,?) | ||||
265 | }; | ||||
266 | my $sth = $dbh->prepare($query); | ||||
267 | if( $sth->execute($set->{'spec'}, $set->{'name'}) ) { | ||||
268 | $set_id = $dbh->last_insert_id(undef, undef, 'oai_sets', undef); | ||||
269 | if($set->{'descriptions'}) { | ||||
270 | $query = qq{ | ||||
271 | INSERT INTO oai_sets_descriptions (set_id, description) | ||||
272 | VALUES (?,?) | ||||
273 | }; | ||||
274 | $sth = $dbh->prepare($query); | ||||
275 | foreach( @{ $set->{'descriptions'} } ) { | ||||
276 | $sth->execute($set_id, $_) if $_; | ||||
277 | } | ||||
278 | } | ||||
279 | } else { | ||||
280 | warn "AddOAISet failed"; | ||||
281 | } | ||||
282 | |||||
283 | return $set_id; | ||||
284 | } | ||||
285 | |||||
286 | =head2 GetOAISetsMappings | ||||
287 | |||||
- - | |||||
327 | sub GetOAISetsMappings { | ||||
328 | my $dbh = C4::Context->dbh; | ||||
329 | my $query = qq{ | ||||
330 | SELECT * FROM oai_sets_mappings | ||||
331 | }; | ||||
332 | my $sth = $dbh->prepare($query); | ||||
333 | $sth->execute; | ||||
334 | |||||
335 | my $mappings = {}; | ||||
336 | while(my $result = $sth->fetchrow_hashref) { | ||||
337 | push @{ $mappings->{$result->{'set_id'}} }, { | ||||
338 | marcfield => $result->{'marcfield'}, | ||||
339 | marcsubfield => $result->{'marcsubfield'}, | ||||
340 | marcvalue => $result->{'marcvalue'} | ||||
341 | }; | ||||
342 | } | ||||
343 | |||||
344 | return $mappings; | ||||
345 | } | ||||
346 | |||||
347 | =head2 GetOAISetMappings | ||||
348 | |||||
- - | |||||
355 | sub GetOAISetMappings { | ||||
356 | my ($set_id) = @_; | ||||
357 | |||||
358 | return unless $set_id; | ||||
359 | |||||
360 | my $dbh = C4::Context->dbh; | ||||
361 | my $query = qq{ | ||||
362 | SELECT * | ||||
363 | FROM oai_sets_mappings | ||||
364 | WHERE set_id = ? | ||||
365 | }; | ||||
366 | my $sth = $dbh->prepare($query); | ||||
367 | $sth->execute($set_id); | ||||
368 | |||||
369 | my @mappings; | ||||
370 | while(my $result = $sth->fetchrow_hashref) { | ||||
371 | push @mappings, { | ||||
372 | marcfield => $result->{'marcfield'}, | ||||
373 | marcsubfield => $result->{'marcsubfield'}, | ||||
374 | marcvalue => $result->{'marcvalue'} | ||||
375 | }; | ||||
376 | } | ||||
377 | |||||
378 | return \@mappings; | ||||
379 | } | ||||
380 | |||||
381 | =head2 ModOAISetMappings { | ||||
382 | |||||
- - | |||||
397 | sub ModOAISetMappings { | ||||
398 | my ($set_id, $mappings) = @_; | ||||
399 | |||||
400 | return unless $set_id; | ||||
401 | |||||
402 | my $dbh = C4::Context->dbh; | ||||
403 | my $query = qq{ | ||||
404 | DELETE FROM oai_sets_mappings | ||||
405 | WHERE set_id = ? | ||||
406 | }; | ||||
407 | my $sth = $dbh->prepare($query); | ||||
408 | $sth->execute($set_id); | ||||
409 | |||||
410 | if(scalar @$mappings > 0) { | ||||
411 | $query = qq{ | ||||
412 | INSERT INTO oai_sets_mappings (set_id, marcfield, marcsubfield, marcvalue) | ||||
413 | VALUES (?,?,?,?) | ||||
414 | }; | ||||
415 | $sth = $dbh->prepare($query); | ||||
416 | foreach (@$mappings) { | ||||
417 | $sth->execute($set_id, $_->{'marcfield'}, $_->{'marcsubfield'}, $_->{'marcvalue'}); | ||||
418 | } | ||||
419 | } | ||||
420 | } | ||||
421 | |||||
422 | =head2 GetOAISetsBiblio | ||||
423 | |||||
- - | |||||
433 | sub GetOAISetsBiblio { | ||||
434 | my ($biblionumber) = @_; | ||||
435 | |||||
436 | my $dbh = C4::Context->dbh; | ||||
437 | my $query = qq{ | ||||
438 | SELECT oai_sets.* | ||||
439 | FROM oai_sets | ||||
440 | LEFT JOIN oai_sets_biblios ON oai_sets_biblios.set_id = oai_sets.id | ||||
441 | WHERE biblionumber = ? | ||||
442 | }; | ||||
443 | my $sth = $dbh->prepare($query); | ||||
444 | |||||
445 | $sth->execute($biblionumber); | ||||
446 | return $sth->fetchall_arrayref({}); | ||||
447 | } | ||||
448 | |||||
449 | =head2 DelOAISetsBiblio | ||||
450 | |||||
- - | |||||
457 | sub DelOAISetsBiblio { | ||||
458 | my ($biblionumber) = @_; | ||||
459 | |||||
460 | return unless $biblionumber; | ||||
461 | |||||
462 | my $dbh = C4::Context->dbh; | ||||
463 | my $query = qq{ | ||||
464 | DELETE FROM oai_sets_biblios | ||||
465 | WHERE biblionumber = ? | ||||
466 | }; | ||||
467 | my $sth = $dbh->prepare($query); | ||||
468 | return $sth->execute($biblionumber); | ||||
469 | } | ||||
470 | |||||
471 | =head2 CalcOAISetsBiblio | ||||
472 | |||||
- - | |||||
481 | sub CalcOAISetsBiblio { | ||||
482 | my ($record, $oai_sets_mappings) = @_; | ||||
483 | |||||
484 | return unless $record; | ||||
485 | |||||
486 | $oai_sets_mappings ||= GetOAISetsMappings; | ||||
487 | |||||
488 | my @biblio_sets; | ||||
489 | foreach my $set_id (keys %$oai_sets_mappings) { | ||||
490 | foreach my $mapping (@{ $oai_sets_mappings->{$set_id} }) { | ||||
491 | next if not $mapping; | ||||
492 | my $field = $mapping->{'marcfield'}; | ||||
493 | my $subfield = $mapping->{'marcsubfield'}; | ||||
494 | my $value = $mapping->{'marcvalue'}; | ||||
495 | |||||
496 | my @subfield_values = $record->subfield($field, $subfield); | ||||
497 | if(0 < grep /^$value$/, @subfield_values) { | ||||
498 | push @biblio_sets, $set_id; | ||||
499 | last; | ||||
500 | } | ||||
501 | } | ||||
502 | } | ||||
503 | return @biblio_sets; | ||||
504 | } | ||||
505 | |||||
506 | =head2 ModOAISetsBiblios | ||||
507 | |||||
- - | |||||
520 | sub ModOAISetsBiblios { | ||||
521 | my $oai_sets_biblios = shift; | ||||
522 | |||||
523 | return unless ref($oai_sets_biblios) eq "HASH"; | ||||
524 | |||||
525 | my $dbh = C4::Context->dbh; | ||||
526 | my $query = qq{ | ||||
527 | TRUNCATE TABLE oai_sets_biblios | ||||
528 | }; | ||||
529 | my $sth = $dbh->prepare($query); | ||||
530 | $sth->execute; | ||||
531 | AddOAISetsBiblios($oai_sets_biblios); | ||||
532 | } | ||||
533 | |||||
534 | =head2 UpdateOAISetsBiblio | ||||
535 | |||||
- - | |||||
543 | sub UpdateOAISetsBiblio { | ||||
544 | my ($biblionumber, $record) = @_; | ||||
545 | |||||
546 | return unless($biblionumber and $record); | ||||
547 | |||||
548 | my $sets_biblios; | ||||
549 | my @sets = CalcOAISetsBiblio($record); | ||||
550 | foreach (@sets) { | ||||
551 | push @{ $sets_biblios->{$_} }, $biblionumber; | ||||
552 | } | ||||
553 | DelOAISetsBiblio($biblionumber); | ||||
554 | AddOAISetsBiblios($sets_biblios); | ||||
555 | } | ||||
556 | |||||
557 | =head2 AddOAISetsBiblios | ||||
558 | |||||
- - | |||||
571 | sub AddOAISetsBiblios { | ||||
572 | my $oai_sets_biblios = shift; | ||||
573 | |||||
574 | return unless ref($oai_sets_biblios) eq "HASH"; | ||||
575 | |||||
576 | my $dbh = C4::Context->dbh; | ||||
577 | my $query = qq{ | ||||
578 | INSERT INTO oai_sets_biblios (set_id, biblionumber) | ||||
579 | VALUES (?,?) | ||||
580 | }; | ||||
581 | my $sth = $dbh->prepare($query); | ||||
582 | foreach my $set_id (keys %$oai_sets_biblios) { | ||||
583 | foreach my $biblionumber (@{$oai_sets_biblios->{$set_id}}) { | ||||
584 | $sth->execute($set_id, $biblionumber); | ||||
585 | } | ||||
586 | } | ||||
587 | } | ||||
588 | |||||
589 | 1 | 7µs | 1; |