Bug 14939: Modularize OAI Server existing classes
[koha.git] / Koha / OAI / Server / ListIdentifiers.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 package Koha::OAI::Server::ListIdentifiers;
20
21 use Modern::Perl;
22 use HTTP::OAI;
23 use C4::OAI::Sets;
24
25 use base ("HTTP::OAI::ListIdentifiers");
26
27
28 sub new {
29     my ($class, $repository, %args) = @_;
30
31     my $self = HTTP::OAI::ListIdentifiers->new(%args);
32
33     my $token = new Koha::OAI::Server::ResumptionToken( %args );
34     my $dbh = C4::Context->dbh;
35     my $set;
36     if(defined $token->{'set'}) {
37         $set = GetOAISetBySpec($token->{'set'});
38     }
39     my $max = $repository->{koha_max_count};
40     my $sql = "
41         (SELECT biblioitems.biblionumber, biblioitems.timestamp
42         FROM biblioitems
43     ";
44     $sql .= " JOIN oai_sets_biblios ON biblioitems.biblionumber = oai_sets_biblios.biblionumber " if defined $set;
45     $sql .= " WHERE timestamp >= ? AND timestamp <= ? ";
46     $sql .= " AND oai_sets_biblios.set_id = ? " if defined $set;
47     $sql .= ") UNION
48         (SELECT deletedbiblio.biblionumber, timestamp FROM deletedbiblio";
49     $sql .= " JOIN oai_sets_biblios ON deletedbiblio.biblionumber = oai_sets_biblios.biblionumber " if defined $set;
50     $sql .= " WHERE DATE(timestamp) >= ? AND DATE(timestamp) <= ? ";
51     $sql .= " AND oai_sets_biblios.set_id = ? " if defined $set;
52
53     $sql .= ") ORDER BY biblionumber
54         LIMIT " . ($max+1) . "
55         OFFSET $token->{offset}
56     ";
57     my $sth = $dbh->prepare( $sql );
58     my @bind_params = ($token->{'from_arg'}, $token->{'until_arg'});
59     push @bind_params, $set->{'id'} if defined $set;
60     push @bind_params, ($token->{'from'}, $token->{'until'});
61     push @bind_params, $set->{'id'} if defined $set;
62     $sth->execute( @bind_params );
63
64     my $count = 0;
65     while ( my ($biblionumber, $timestamp) = $sth->fetchrow ) {
66         $count++;
67         if ( $count > $max ) {
68             $self->resumptionToken(
69                 new Koha::OAI::Server::ResumptionToken(
70                     metadataPrefix  => $token->{metadata_prefix},
71                     from            => $token->{from},
72                     until           => $token->{until},
73                     offset          => $token->{offset} + $max,
74                     set             => $token->{set}
75                 )
76             );
77             last;
78         }
79         $timestamp =~ s/ /T/, $timestamp .= 'Z';
80         $self->identifier( new HTTP::OAI::Header(
81             identifier => $repository->{ koha_identifier} . ':' . $biblionumber,
82             datestamp  => $timestamp,
83         ) );
84     }
85
86     # Return error if no results
87     unless ($count) {
88         return HTTP::OAI::Response->new(
89             requestURL => $repository->self_url(),
90             errors     => [ new HTTP::OAI::Error( code => 'noRecordsMatch' ) ],
91         );
92     }
93
94     return $self;
95 }
96
97 1;