- correct 'from' and 'until' parameter in ListRecords request (oai)

This commit is contained in:
Arno Kaimbacher 2020-07-27 16:06:15 +02:00
parent 3125df767b
commit 77fb177f5e

View File

@ -356,6 +356,12 @@ class RequestController extends Controller
return checkdate($tempDate[1], $tempDate[2], $tempDate[0]); return checkdate($tempDate[1], $tempDate[2], $tempDate[0]);
} }
private function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = \DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
/** /**
* Helper method for handling lists. * Helper method for handling lists.
* *
@ -439,32 +445,59 @@ class RequestController extends Controller
if (array_key_exists('until', $oaiRequest)) { if (array_key_exists('until', $oaiRequest)) {
$until = $oaiRequest['until']; $until = $oaiRequest['until'];
if ($this->checkmydate($until) == true) { try {
$untilDate = \Illuminate\Support\Carbon::parse($until);
// if (strtotime($untilDate) > 0) {
$earliestPublicationDate = Dataset::earliestPublicationDate()->server_date_published;//->format('Y-m-d\TH:i:s\Z'); $earliestPublicationDate = Dataset::earliestPublicationDate()->server_date_published;//->format('Y-m-d\TH:i:s\Z');
$untilDate = \Illuminate\Support\Carbon::parse($until);
if ($earliestPublicationDate->gt($untilDate)) { if ($earliestPublicationDate->gt($untilDate)) {
throw new OaiModelException( throw new OaiModelException(
"Given until date is greater than 'earliestDatestamp'. The given values results in an empty list.", "earliestDatestamp is greater than given until date. The given values results in an empty list.",
OaiModelError::NORECORDSMATCH OaiModelError::NORECORDSMATCH
); );
} else { } else {
$finder->whereDate('server_date_published', '<=', date($until)); $finder->where('server_date_published', '<=', $untilDate);
$test = $finder->toSql();
} }
} }
catch (OaiModelException $e) {
throw new OaiModelException(
"earliestDatestamp is greater than given until date. The given values results in an empty list.",
OaiModelError::NORECORDSMATCH
);
}
catch (\Exception $e) {
throw new OaiModelException(
'The until date argument is not valid.',
OaiModelError::BADARGUMENT
);
}
} }
if (array_key_exists('from', $oaiRequest)) { if (array_key_exists('from', $oaiRequest)) {
$from = $oaiRequest['from']; $from = $oaiRequest['from'];
if ($this->checkmydate($from) == true) { try {
$fromDate = \Illuminate\Support\Carbon::parse($from);
// if (strtotime($fromDate) > 0) {
$now = new Carbon(); $now = new Carbon();
$fromDate = \Illuminate\Support\Carbon::parse($from);
if ($fromDate->gt($now)) { if ($fromDate->gt($now)) {
throw new OaiModelException( throw new OaiModelException(
"Given from date is grater than now. The given values results in an empty list.", "Given from date is greater than now. The given values results in an empty list.",
OaiModelError::NORECORDSMATCH OaiModelError::NORECORDSMATCH
); );
} else { } else {
$finder->whereDate('server_date_published', '>=', date($from)); $finder->where('server_date_published', '>=', $fromDate);
} }
}
catch (OaiModelException $e) {
throw new OaiModelException(
"Given from date is greater than now. The given values results in an empty list.",
OaiModelError::NORECORDSMATCH
);
}
catch (\Exception $e) {
throw new OaiModelException(
'The from date argument is not valid.',
OaiModelError::BADARGUMENT
);
} }
} }