Bug 29484: Make ListSets return noSetHierarchy when no sets defined
[koha.git] / Koha / OAI / Server / ResumptionToken.pm
1 # Copyright Tamil s.a.r.l. 2008-2015
2 # Copyright Biblibre 2008-2015
3 # Copyright The National Library of Finland, University of Helsinki 2016-2021
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20
21 package Koha::OAI::Server::ResumptionToken;
22
23 use Modern::Perl;
24 use HTTP::OAI;
25
26 use base ("HTTP::OAI::ResumptionToken");
27
28
29 # Extends HTTP::OAI::ResumptionToken
30 # A token is identified by:
31 # - metadataPrefix
32 # - from
33 # - until
34 # - cursor
35 # - deleted
36 # - deleted count (not used anymore, but preserved for back-compatibility)
37 # - nextId
38
39 sub new {
40     my ($class, %args) = @_;
41
42     my $self = $class->SUPER::new(%args);
43
44     my ($metadata_prefix, $cursor, $from, $until, $set, $deleted, $next_id);
45     if ( $args{ resumptionToken } ) {
46         # Note: undef in place of deleted record count for back-compatibility
47         ($metadata_prefix, $cursor, $from, $until, $set, $deleted, undef, $next_id)
48             = split( '/', $args{resumptionToken} );
49         $next_id = 0 unless $next_id;
50     }
51     else {
52         $metadata_prefix = $args{ metadataPrefix };
53         $from = $args{ from } || '';
54         $until = $args{ until } || '';
55         # Add times to the arguments, when necessary, so they correctly match against the DB timestamps
56         $from .= 'T00:00:00Z' if length($from) == 10;
57         $until .= 'T23:59:59Z' if length($until) == 10;
58         $cursor = $args{ cursor } // 0;
59         $set = $args{ set } || '';
60         $deleted = defined $args{ deleted } ? $args{ deleted } : 1;
61         $next_id = $args{ next_id } // 0;
62     }
63
64     # metadata_prefix can be undef (e.g. listSets)
65     $metadata_prefix //= '';
66
67     $self->{ metadata_prefix } = $metadata_prefix;
68     $self->{ cursor          } = $cursor;
69     $self->{ from            } = $from;
70     $self->{ until           } = $until;
71     $self->{ set             } = $set;
72     $self->{ from_arg        } = _strip_UTC_designators($from);
73     $self->{ until_arg       } = _strip_UTC_designators($until);
74     $self->{ deleted         } = $deleted;
75     $self->{ next_id         } = $next_id;
76
77     # Note: put zero where deleted record count used to be for back-compatibility
78     $self->resumptionToken(
79         join( '/', $metadata_prefix, $cursor, $from, $until, $set, $deleted, 0, $next_id ) );
80     $self->cursor( $cursor );
81
82     return $self;
83 }
84
85 sub _strip_UTC_designators {
86     my ( $timestamp ) = @_;
87     $timestamp =~ s/T/ /g;
88     $timestamp =~ s/Z//g;
89     return $timestamp;
90 }
91
92 1;