From 61d406933e7311a3eb3c0379f1dea8b790459722 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 6 May 2017 19:39:39 +0200 Subject: API: Get History endpoint See http://shaarli.github.io/api-documentation/#links-history-get --- application/api/controllers/History.php | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 application/api/controllers/History.php (limited to 'application/api') diff --git a/application/api/controllers/History.php b/application/api/controllers/History.php new file mode 100644 index 00000000..c4ff3e5d --- /dev/null +++ b/application/api/controllers/History.php @@ -0,0 +1,71 @@ +conf->get('resource.history')))->getHistory(); + $history = array_reverse($history); + + // Return history operations from the {offset}th, starting from {since}. + $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since')); + $offset = $request->getParam('offset'); + if (empty($offset)) { + $offset = 0; + } + else if (ctype_digit($offset)) { + $offset = (int) $offset; + } else { + throw new ApiBadParametersException('Invalid offset'); + } + + // limit parameter is either a number of links or 'all' for everything. + $limit = $request->getParam('limit'); + if (empty($limit)) { + $limit = count($history); + } else if (ctype_digit($limit)) { + $limit = (int) $limit; + } else { + throw new ApiBadParametersException('Invalid limit'); + } + + $out = []; + $i = 0; + foreach ($history as $entry) { + if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) { + break; + } + if (++$i > $offset) { + $out[$i] = $entry; + $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM); + } + } + $out = array_values($out); + + return $response->withJson($out, 200, $this->jsonStyle); + } +} -- cgit v1.2.3