3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 C4::OAI::Sets - OAI Sets management functions
26 C4::OAI::Sets contains functions for managing storage and editing of OAI Sets.
28 OAI Set description can be found L<here|http://www.openarchives.org/OAI/openarchivesprotocol.html#Set>
35 use vars
qw(@ISA @EXPORT);
41 &GetOAISets &GetOAISet &GetOAISetBySpec &ModOAISet &DelOAISet &AddOAISet
42 &GetOAISetsMappings &GetOAISetMappings &ModOAISetMappings
43 &GetOAISetsBiblio &ModOAISetsBiblios &AddOAISetsBiblios
44 &CalcOAISetsBiblio &UpdateOAISetsBiblio &DelOAISetsBiblio
52 $oai_sets = GetOAISets;
54 GetOAISets return a array reference of hash references describing the sets.
55 The hash references looks like this:
70 my $dbh = C4
::Context
->dbh;
72 SELECT
* FROM oai_sets
74 my $sth = $dbh->prepare($query);
76 my $results = $sth->fetchall_arrayref({});
80 FROM oai_sets_descriptions
83 $sth = $dbh->prepare($query);
84 foreach my $set (@
$results) {
85 $sth->execute($set->{'id'});
86 my $desc = $sth->fetchall_arrayref({});
88 push @
{$set->{'descriptions'}}, $_->{'description'};
97 $set = GetOAISet($set_id);
99 GetOAISet returns a hash reference describing the set with the given set_id.
101 See GetOAISets to see what the hash looks like.
108 return unless $set_id;
110 my $dbh = C4
::Context
->dbh;
116 my $sth = $dbh->prepare($query);
117 $sth->execute($set_id);
118 my $set = $sth->fetchrow_hashref;
122 FROM oai_sets_descriptions
125 $sth = $dbh->prepare($query);
126 $sth->execute($set->{'id'});
127 my $desc = $sth->fetchall_arrayref({});
129 push @
{$set->{'descriptions'}}, $_->{'description'};
135 =head2 GetOAISetBySpec
137 my $set = GetOAISetBySpec($setSpec);
139 Returns a hash describing the set whose spec is $setSpec
143 sub GetOAISetBySpec
{
146 return unless defined $setSpec;
148 my $dbh = C4
::Context
->dbh;
155 my $sth = $dbh->prepare($query);
156 $sth->execute($setSpec);
158 return $sth->fetchrow_hashref;
164 'id' => $set_id, # mandatory
165 'spec' => $spec, # mandatory
166 'name' => $name, # mandatory
167 'descriptions => \@descriptions, # optional, [] to remove descriptions
171 ModOAISet modify a set in the database.
178 return unless($set && $set->{'spec'} && $set->{'name'});
180 if(!defined $set->{'id'}) {
181 warn "Set ID not defined, can't modify the set";
185 my $dbh = C4
::Context
->dbh;
192 my $sth = $dbh->prepare($query);
193 $sth->execute($set->{'spec'}, $set->{'name'}, $set->{'id'});
195 if($set->{'descriptions'}) {
197 DELETE FROM oai_sets_descriptions
200 $sth = $dbh->prepare($query);
201 $sth->execute($set->{'id'});
203 if(scalar @
{$set->{'descriptions'}} > 0) {
205 INSERT INTO oai_sets_descriptions
(set_id
, description
)
208 $sth = $dbh->prepare($query);
209 foreach (@
{ $set->{'descriptions'} }) {
210 $sth->execute($set->{'id'}, $_) if $_;
220 DelOAISet remove the set with the given set_id
227 return unless $set_id;
229 my $dbh = C4
::Context
->dbh;
231 DELETE oai_sets
, oai_sets_descriptions
, oai_sets_mappings
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
= ?
237 my $sth = $dbh->prepare($query);
238 $sth->execute($set_id);
244 'id' => $set_id, # mandatory
245 'spec' => $spec, # mandatory
246 'name' => $name, # mandatory
247 'descriptions => \@descriptions, # optional
249 my $set_id = AddOAISet($set);
251 AddOAISet adds a new set and returns its id, or undef if something went wrong.
258 return unless($set && $set->{'spec'} && $set->{'name'});
261 my $dbh = C4
::Context
->dbh;
263 INSERT INTO oai_sets
(spec
, name
)
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'}) {
271 INSERT INTO oai_sets_descriptions
(set_id
, description
)
274 $sth = $dbh->prepare($query);
275 foreach( @
{ $set->{'descriptions'} } ) {
276 $sth->execute($set_id, $_) if $_;
280 warn "AddOAISet failed";
286 =head2 GetOAISetsMappings
288 my $mappings = GetOAISetsMappings;
290 GetOAISetsMappings returns mappings for all OAI Sets.
292 Mappings define how biblios are categorized in sets.
293 A mapping is defined by four properties:
296 marcfield => 'XXX', # the MARC field to check
297 marcsubfield => 'Y', # the MARC subfield to check
298 operator => 'A', # the operator 'equal' or 'notequal'; 'equal' if ''
299 marcvalue => 'zzzz', # the value to check
302 If defined in a set mapping, a biblio which have at least one 'Y' subfield of
303 one 'XXX' field equal to 'zzzz' will belong to this set.
304 If multiple mappings are defined in a set, the biblio will belong to this set
305 if at least one condition is matched.
307 GetOAISetsMappings returns a hashref of arrayrefs of hashrefs.
308 The first hashref keys are the sets IDs, so it looks like this:
329 sub GetOAISetsMappings
{
330 my $dbh = C4
::Context
->dbh;
332 SELECT
* FROM oai_sets_mappings
334 my $sth = $dbh->prepare($query);
338 while(my $result = $sth->fetchrow_hashref) {
339 push @
{ $mappings->{$result->{'set_id'}} }, {
340 marcfield
=> $result->{'marcfield'},
341 marcsubfield
=> $result->{'marcsubfield'},
342 operator
=> $result->{'operator'},
343 marcvalue
=> $result->{'marcvalue'}
350 =head2 GetOAISetMappings
352 my $set_mappings = GetOAISetMappings($set_id);
354 Return mappings for the set with given set_id. It's an arrayref of hashrefs
358 sub GetOAISetMappings
{
361 return unless $set_id;
363 my $dbh = C4
::Context
->dbh;
366 FROM oai_sets_mappings
369 my $sth = $dbh->prepare($query);
370 $sth->execute($set_id);
373 while(my $result = $sth->fetchrow_hashref) {
375 marcfield
=> $result->{'marcfield'},
376 marcsubfield
=> $result->{'marcsubfield'},
377 operator
=> $result->{'operator'},
378 marcvalue
=> $result->{'marcvalue'}
385 =head2 ModOAISetMappings {
396 ModOAISetMappings($set_id, $mappings);
398 ModOAISetMappings modifies mappings of a given set.
402 sub ModOAISetMappings
{
403 my ($set_id, $mappings) = @_;
405 return unless $set_id;
407 my $dbh = C4
::Context
->dbh;
409 DELETE FROM oai_sets_mappings
412 my $sth = $dbh->prepare($query);
413 $sth->execute($set_id);
415 if(scalar @
$mappings > 0) {
417 INSERT INTO oai_sets_mappings
(set_id
, marcfield
, marcsubfield
, operator
, marcvalue
)
420 $sth = $dbh->prepare($query);
421 foreach (@
$mappings) {
422 $sth->execute($set_id, $_->{'marcfield'}, $_->{'marcsubfield'}, $_->{'operator'}, $_->{'marcvalue'});
427 =head2 GetOAISetsBiblio
429 $oai_sets = GetOAISetsBiblio($biblionumber);
431 Return the OAI sets where biblio appears.
433 Return value is an arrayref of hashref where each element of the array is a set.
434 Keys of hash are id, spec and name
438 sub GetOAISetsBiblio
{
439 my ($biblionumber) = @_;
441 my $dbh = C4
::Context
->dbh;
445 LEFT JOIN oai_sets_biblios ON oai_sets_biblios
.set_id
= oai_sets
.id
446 WHERE biblionumber
= ?
448 my $sth = $dbh->prepare($query);
450 $sth->execute($biblionumber);
451 return $sth->fetchall_arrayref({});
454 =head2 DelOAISetsBiblio
456 DelOAISetsBiblio($biblionumber);
458 Remove a biblio from all sets
462 sub DelOAISetsBiblio
{
463 my ($biblionumber) = @_;
465 return unless $biblionumber;
467 my $dbh = C4
::Context
->dbh;
469 DELETE FROM oai_sets_biblios
470 WHERE biblionumber
= ?
472 my $sth = $dbh->prepare($query);
473 return $sth->execute($biblionumber);
476 =head2 CalcOAISetsBiblio
478 my @sets = CalcOAISetsBiblio($record, $oai_sets_mappings);
480 Return a list of set ids the record belongs to. $record must be a MARC::Record
481 and $oai_sets_mappings (optional) must be a hashref returned by
486 sub CalcOAISetsBiblio
{
487 my ($record, $oai_sets_mappings) = @_;
489 return unless $record;
491 $oai_sets_mappings ||= GetOAISetsMappings
;
494 foreach my $set_id (keys %$oai_sets_mappings) {
495 foreach my $mapping (@
{ $oai_sets_mappings->{$set_id} }) {
496 next if not $mapping;
497 my $field = $mapping->{'marcfield'};
498 my $subfield = $mapping->{'marcsubfield'};
499 my $operator = $mapping->{'operator'};
500 my $value = $mapping->{'marcvalue'};
501 my @subfield_values = $record->subfield($field, $subfield);
502 if ($operator eq 'notequal') {
503 if(0 == grep /^$value$/, @subfield_values) {
504 push @biblio_sets, $set_id;
509 if(0 < grep /^$value$/, @subfield_values) {
510 push @biblio_sets, $set_id;
519 =head2 ModOAISetsBiblios
521 my $oai_sets_biblios = {
522 '1' => [1, 3, 4], # key is the set_id, and value is an array ref of biblionumbers
526 ModOAISetsBiblios($oai_sets_biblios);
528 ModOAISetsBiblios truncate oai_sets_biblios table and call AddOAISetsBiblios.
529 This table is then used in opac/oai.pl.
533 sub ModOAISetsBiblios
{
534 my $oai_sets_biblios = shift;
536 return unless ref($oai_sets_biblios) eq "HASH";
538 my $dbh = C4
::Context
->dbh;
540 TRUNCATE TABLE oai_sets_biblios
542 my $sth = $dbh->prepare($query);
544 AddOAISetsBiblios
($oai_sets_biblios);
547 =head2 UpdateOAISetsBiblio
549 UpdateOAISetsBiblio($biblionumber, $record);
551 Update OAI sets for one biblio. The two parameters are mandatory.
552 $record is a MARC::Record.
556 sub UpdateOAISetsBiblio
{
557 my ($biblionumber, $record) = @_;
559 return unless($biblionumber and $record);
562 my @sets = CalcOAISetsBiblio
($record);
564 push @
{ $sets_biblios->{$_} }, $biblionumber;
566 DelOAISetsBiblio
($biblionumber);
567 AddOAISetsBiblios
($sets_biblios);
570 =head2 AddOAISetsBiblios
572 my $oai_sets_biblios = {
573 '1' => [1, 3, 4], # key is the set_id, and value is an array ref of biblionumbers
577 ModOAISetsBiblios($oai_sets_biblios);
579 AddOAISetsBiblios insert given infos in oai_sets_biblios table.
580 This table is then used in opac/oai.pl.
584 sub AddOAISetsBiblios
{
585 my $oai_sets_biblios = shift;
587 return unless ref($oai_sets_biblios) eq "HASH";
589 my $dbh = C4
::Context
->dbh;
591 INSERT INTO oai_sets_biblios
(set_id
, biblionumber
)
594 my $sth = $dbh->prepare($query);
595 foreach my $set_id (keys %$oai_sets_biblios) {
596 foreach my $biblionumber (@
{$oai_sets_biblios->{$set_id}}) {
597 $sth->execute($set_id, $biblionumber);