aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/History.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-05-06 19:39:39 +0200
committerArthurHoaro <arthur@hoa.ro>2017-05-07 16:03:40 +0200
commit61d406933e7311a3eb3c0379f1dea8b790459722 (patch)
tree3585b6c24c808e75eec93978682bba1a051f7817 /application/api/controllers/History.php
parentb8fcb7d4403a344158ab5d2c8979bdd002e6001d (diff)
downloadShaarli-61d406933e7311a3eb3c0379f1dea8b790459722.tar.gz
Shaarli-61d406933e7311a3eb3c0379f1dea8b790459722.tar.zst
Shaarli-61d406933e7311a3eb3c0379f1dea8b790459722.zip
API: Get History endpoint
See http://shaarli.github.io/api-documentation/#links-history-get
Diffstat (limited to 'application/api/controllers/History.php')
-rw-r--r--application/api/controllers/History.php71
1 files changed, 71 insertions, 0 deletions
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 @@
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
6use Shaarli\Api\Exceptions\ApiBadParametersException;
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class History
12 *
13 * REST API Controller: /history
14 *
15 * @package Shaarli\Api\Controllers
16 */
17class History extends ApiController
18{
19 /**
20 * Service providing operation regarding Shaarli datastore and settings.
21 *
22 * @param Request $request Slim request.
23 * @param Response $response Slim response.
24 *
25 * @return Response response.
26 *
27 * @throws ApiBadParametersException Invalid parameters.
28 */
29 public function getHistory($request, $response)
30 {
31 $history = (new \History($this->conf->get('resource.history')))->getHistory();
32 $history = array_reverse($history);
33
34 // Return history operations from the {offset}th, starting from {since}.
35 $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since'));
36 $offset = $request->getParam('offset');
37 if (empty($offset)) {
38 $offset = 0;
39 }
40 else if (ctype_digit($offset)) {
41 $offset = (int) $offset;
42 } else {
43 throw new ApiBadParametersException('Invalid offset');
44 }
45
46 // limit parameter is either a number of links or 'all' for everything.
47 $limit = $request->getParam('limit');
48 if (empty($limit)) {
49 $limit = count($history);
50 } else if (ctype_digit($limit)) {
51 $limit = (int) $limit;
52 } else {
53 throw new ApiBadParametersException('Invalid limit');
54 }
55
56 $out = [];
57 $i = 0;
58 foreach ($history as $entry) {
59 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
60 break;
61 }
62 if (++$i > $offset) {
63 $out[$i] = $entry;
64 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM);
65 }
66 }
67 $out = array_values($out);
68
69 return $response->withJson($out, 200, $this->jsonStyle);
70 }
71}