From 443485ade4fc04abc79e073a340b36d463f720f7 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 5 Sep 2013 15:21:51 +1000 Subject: [PATCH] Bug 10824 - OAI-PMH repository/server not handling time in "from" argument This patch remotes the DATE() function from around timestamp, and adds a sub that strips the UTC designators "T" and "Z" from incoming "from" and "until" arguments in OAI-PMH requests so that they're more compliant with MySQL (and probably other databases as well). TEST PLAN: 0) Note down a selection of timestamps from your biblio table 1) Enable your OAI-PMH server through the global system preferences Web services tab. 2) Craft and submit a similar request to the following in your browser: KOHAINSTANCE/cgi-bin/koha/oai.pl?verb=ListRecords&metadataPrefix=oai_dc& from=2013-09-02T13:44:33Z&until=2013-09-05T13:44:33Z Change the exact dates to accord with your timestamps, but keep the YYYY-MM-DDTHH:MM:SSZ format. 3) Note the unexpected behaviour. A "from" argument with the timestamp 2013-09-02T13:44:33Z will show records from 2013-09-03 but not records from 2013-09-02 even though the timestamp in the database will say "2013-09-02 13:44:33". 4) APPLY THE PATCH 5) Resubmit the links you tried above 6) Note that the applicable records now appear! -- Developer Note: We could've not stripped the UTC designators and used DATE() around the parameters in the SQL queries, but that would have lost the whole purpose of using times in the "from" arguments, since they would've been generalized to just the dates. I think this is probably the best solution. Admittedly, creating "form_arg" and "until_arg" hashrefs in the ResumptionToken object might not be ideal, but I preferred that to copying the _strip_UTC_designator subroutine into two other objects. Perhaps this sub could go somewhere else and be imported into those other two objects but this seemed to be the most sensible decision. I'm open to other opinions though. --- opac/oai.pl | 17 +++++++++++++---- 1 files changed, 13 insertions(+), 4 deletions(-) diff --git a/opac/oai.pl b/opac/oai.pl index 7aa5a7d..d5241db 100755 --- a/opac/oai.pl +++ b/opac/oai.pl @@ -108,6 +108,8 @@ sub new { $self->{ from } = $from; $self->{ until } = $until; $self->{ set } = $set; + $self->{ from_arg } = _strip_UTC_designators($from); + $self->{ until_arg } = _strip_UTC_designators($until); $self->resumptionToken( join( ':', $metadata_prefix, $offset, $from, $until, $set ) ); @@ -116,6 +118,13 @@ sub new { return $self; } +sub _strip_UTC_designators { + my ( $timestamp ) = @_; + $timestamp =~ s/T/ /g; + $timestamp =~ s/Z//g; + return $timestamp; +} + # __END__ C4::OAI::ResumptionToken @@ -319,14 +328,14 @@ sub new { FROM biblioitems "; $sql .= " JOIN oai_sets_biblios ON biblioitems.biblionumber = oai_sets_biblios.biblionumber " if defined $set; - $sql .= " WHERE DATE(timestamp) >= ? AND DATE(timestamp) <= ? "; + $sql .= " WHERE timestamp >= ? AND timestamp <= ? "; $sql .= " AND oai_sets_biblios.set_id = ? " if defined $set; $sql .= " LIMIT $repository->{'koha_max_count'} OFFSET $token->{'offset'} "; my $sth = $dbh->prepare( $sql ); - my @bind_params = ($token->{'from'}, $token->{'until'}); + my @bind_params = ($token->{'from_arg'}, $token->{'until_arg'}); push @bind_params, $set->{'id'} if defined $set; $sth->execute( @bind_params ); @@ -472,7 +481,7 @@ sub new { FROM biblioitems "; $sql .= " JOIN oai_sets_biblios ON biblioitems.biblionumber = oai_sets_biblios.biblionumber " if defined $set; - $sql .= " WHERE DATE(timestamp) >= ? AND DATE(timestamp) <= ? "; + $sql .= " WHERE timestamp >= ? AND timestamp <= ? "; $sql .= " AND oai_sets_biblios.set_id = ? " if defined $set; $sql .= " LIMIT $repository->{'koha_max_count'} @@ -480,7 +489,7 @@ sub new { "; my $sth = $dbh->prepare( $sql ); - my @bind_params = ($token->{'from'}, $token->{'until'}); + my @bind_params = ($token->{'from_arg'}, $token->{'until_arg'}); push @bind_params, $set->{'id'} if defined $set; $sth->execute( @bind_params ); -- 1.7.7.4