1 package Koha::OAI::Server::ListBase;
3 # Copyright The National Library of Finland, University of Helsinki 2016-2017
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 Koha::OAI::Server::ListBase - OAI ListIdentifiers/ListRecords shared functionality
26 Koha::OAI::Server::ListBase contains OAI-PMH functions shared by ListIdentifiers and ListRecords.
33 use Koha::OAI::Server::ResumptionToken;
34 use Koha::OAI::Server::Record;
35 use Koha::OAI::Server::DeletedRecord;
40 my ($class, $self, $repository, $metadata, %args) = @_;
42 my $token = Koha::OAI::Server::ResumptionToken->new( %args );
43 my $dbh = C4::Context->dbh;
45 if ( defined $token->{'set'} ) {
46 $set = GetOAISetBySpec($token->{'set'});
48 my $deleted = defined $token->{deleted} ? $token->{deleted} : 0;
49 my $max = $repository->{koha_max_count};
51 my $format = $args{metadataPrefix} || $token->{metadata_prefix};
52 my $include_items = $repository->items_included( $format );
53 my $from = $token->{from_arg};
54 my $until = $token->{until_arg};
55 my $next_id = $token->{next_id};
57 # Since creating a union of normal and deleted record tables would be a heavy
58 # operation in a large database, build results in two stages:
59 # first deleted records ($deleted == 1), then normal records ($deleted == 0)
61 for ( ; $deleted >= 0; $deleted-- ) {
62 my $table = $deleted ? 'deletedbiblio_metadata' : 'biblio_metadata';
64 my @part_bind_params = ($next_id);
66 my $where = "biblionumber >= ?";
68 $where .= " AND timestamp >= ?";
69 push @part_bind_params, $from;
72 $where .= " AND timestamp <= ?";
73 push @part_bind_params, $until;
76 $where .= " AND biblionumber in (SELECT osb.biblionumber FROM oai_sets_biblios osb WHERE osb.set_id = ?)";
77 push @part_bind_params, $set->{'id'};
80 my @bind_params = @part_bind_params;
82 my $criteria = "WHERE $where ORDER BY biblionumber LIMIT " . ($max + 1);
90 # If items are included, fetch a set of potential biblionumbers from items tables as well.
91 # Then merge them, sort them and take the required number of them from the resulting list.
92 # This may seem counter-intuitive as in worst case we fetch 3 times the biblionumbers needed,
93 # but avoiding joins or subqueries makes this so much faster that it does not matter.
94 if ( $include_items ) {
95 $sql = "($sql) UNION (SELECT DISTINCT(biblionumber) FROM deleteditems $criteria)";
96 push @bind_params, @part_bind_params;
98 $sql .= " UNION (SELECT DISTINCT(biblionumber) FROM items $criteria)";
99 push @bind_params, @part_bind_params;
101 $sql = "SELECT biblionumber FROM ($sql) bibnos ORDER BY biblionumber LIMIT " . ($max + 1);
104 my $sth = $dbh->prepare( $sql ) || die( 'Could not prepare statement: ' . $dbh->errstr );
105 $sth->execute( @bind_params ) || die( 'Could not execute statement: ' . $sth->errstr );
108 if ( $include_items ) {
111 SELECT MAX(timestamp)
113 SELECT timestamp FROM deletedbiblio_metadata WHERE biblionumber = ?
115 SELECT timestamp FROM deleteditems WHERE biblionumber = ?
120 SELECT MAX(timestamp)
122 SELECT timestamp FROM biblio_metadata WHERE biblionumber = ?
124 SELECT timestamp FROM deleteditems WHERE biblionumber = ?
126 SELECT timestamp FROM items WHERE biblionumber = ?
131 $ts_sql = "SELECT timestamp FROM $table WHERE biblionumber = ?";
133 my $ts_sth = $dbh->prepare( $ts_sql ) || die( 'Could not prepare statement: ' . $dbh->errstr );
135 foreach my $row (@{ $sth->fetchall_arrayref() }) {
136 my $biblionumber = $row->[0];
138 if ( $count > $max ) {
139 $self->resumptionToken(
140 Koha::OAI::Server::ResumptionToken->new(
141 metadataPrefix => $token->{metadata_prefix},
142 from => $token->{from},
143 until => $token->{until},
144 cursor => $token->{cursor} + $max,
145 set => $token->{set},
147 next_id => $biblionumber
152 my @params = ($biblionumber);
153 if ( $include_items ) {
154 push @params, $deleted ? ( $biblionumber ) : ( $biblionumber, $biblionumber );
156 $ts_sth->execute( @params ) || die( 'Could not execute statement: ' . $ts_sth->errstr );
158 my ($timestamp) = $ts_sth->fetchrow;
160 my $oai_sets = GetOAISetsBiblio($biblionumber);
162 foreach ( @$oai_sets ) {
163 push @setSpecs, $_->{spec};
166 my $marcxml = !$deleted ? $repository->get_biblio_marcxml($biblionumber, $format) : undef;
168 $self->record( Koha::OAI::Server::Record->new(
169 $repository, $marcxml, $timestamp, \@setSpecs,
170 identifier => $repository->{ koha_identifier } . ':' . $biblionumber,
171 metadataPrefix => $token->{metadata_prefix}
174 $self->record( Koha::OAI::Server::DeletedRecord->new(
175 $timestamp, \@setSpecs, identifier => $repository->{ koha_identifier } . ':' . $biblionumber
179 $timestamp =~ s/ /T/;
181 $self->identifier( HTTP::OAI::Header->new(
182 identifier => $repository->{ koha_identifier} . ':' . $biblionumber,
183 datestamp => $timestamp,
184 status => $deleted ? 'deleted' : undef
188 # Reset $next_id for the next stage