Bug 19539: (follow-up) Fix column index shift in cirulation rules
[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-2017
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 # - offset
35 # - deleted
36
37 sub new {
38     my ($class, %args) = @_;
39
40     my $self = $class->SUPER::new(%args);
41
42     my ($metadata_prefix, $offset, $from, $until, $set, $deleted, $deleted_count);
43     if ( $args{ resumptionToken } ) {
44         ($metadata_prefix, $offset, $from, $until, $set, $deleted, $deleted_count)
45             = split( '/', $args{resumptionToken} );
46     }
47     else {
48         $metadata_prefix = $args{ metadataPrefix };
49         $from = $args{ from } || '1970-01-01';
50         $until = $args{ until };
51         unless ( $until) {
52             my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime( time );
53             $until = sprintf( "%.4d-%.2d-%.2d", $year+1900, $mon+1,$mday );
54         }
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         $offset = $args{ offset } || 0;
59         $set = $args{ set } || '';
60         $deleted = defined $args{ deleted } ? $args{ deleted } : 1;
61         $deleted_count = defined $args{ deleted_count } ? $args{ deleted_count } : 0;
62     }
63
64     $self->{ metadata_prefix } = $metadata_prefix;
65     $self->{ offset          } = $offset;
66     $self->{ from            } = $from;
67     $self->{ until           } = $until;
68     $self->{ set             } = $set;
69     $self->{ from_arg        } = _strip_UTC_designators($from);
70     $self->{ until_arg       } = _strip_UTC_designators($until);
71     $self->{ deleted         } = $deleted;
72     $self->{ deleted_count   } = $deleted_count;
73
74     $self->resumptionToken(
75         join( '/', $metadata_prefix, $offset, $from, $until, $set, $deleted, $deleted_count ) );
76     $self->cursor( $offset );
77
78     return $self;
79 }
80
81 sub _strip_UTC_designators {
82     my ( $timestamp ) = @_;
83     $timestamp =~ s/T/ /g;
84     $timestamp =~ s/Z//g;
85     return $timestamp;
86 }
87
88 1;