Bug 14939: Modularize OAI Server existing classes
[koha.git] / Koha / OAI / Server / ResumptionToken.pm
1 # Copyright Tamil s.a.r.l. 2008-2015
2 # Copyright Biblibre 2008-2015
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
20 package Koha::OAI::Server::ResumptionToken;
21
22 use Modern::Perl;
23 use HTTP::OAI;
24
25 use base ("HTTP::OAI::ResumptionToken");
26
27
28 # Extends HTTP::OAI::ResumptionToken
29 # A token is identified by:
30 # - metadataPrefix
31 # - from
32 # - until
33 # - offset
34
35
36 sub new {
37     my ($class, %args) = @_;
38
39     my $self = $class->SUPER::new(%args);
40
41     my ($metadata_prefix, $offset, $from, $until, $set);
42     if ( $args{ resumptionToken } ) {
43         ($metadata_prefix, $offset, $from, $until, $set)
44             = split( '/', $args{resumptionToken} );
45     }
46     else {
47         $metadata_prefix = $args{ metadataPrefix };
48         $from = $args{ from } || '1970-01-01';
49         $until = $args{ until };
50         unless ( $until) {
51             my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime( time );
52             $until = sprintf( "%.4d-%.2d-%.2d", $year+1900, $mon+1,$mday );
53         }
54         #Add times to the arguments, when necessary, so they correctly match against the DB timestamps
55         $from .= 'T00:00:00Z' if length($from) == 10;
56         $until .= 'T23:59:59Z' if length($until) == 10;
57         $offset = $args{ offset } || 0;
58         $set = $args{set} || '';
59     }
60
61     $self->{ metadata_prefix } = $metadata_prefix;
62     $self->{ offset          } = $offset;
63     $self->{ from            } = $from;
64     $self->{ until           } = $until;
65     $self->{ set             } = $set;
66     $self->{ from_arg        } = _strip_UTC_designators($from);
67     $self->{ until_arg       } = _strip_UTC_designators($until);
68
69     $self->resumptionToken(
70         join( '/', $metadata_prefix, $offset, $from, $until, $set ) );
71     $self->cursor( $offset );
72
73     return $self;
74 }
75
76 sub _strip_UTC_designators {
77     my ( $timestamp ) = @_;
78     $timestamp =~ s/T/ /g;
79     $timestamp =~ s/Z//g;
80     return $timestamp;
81 }
82
83 1;