aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/HistoryController.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-13 12:07:13 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 12:07:13 +0200
commitd9f6275ebca035fec8331652c677981056793ccc (patch)
tree37a64baf4f0eba6b781040605965383d8aded2cc /application/api/controllers/HistoryController.php
parent38672ba0d1c722e5d6d33a58255ceb55e9410e46 (diff)
parentd63ff87a009313141ae684ec447b902562ff6ee7 (diff)
downloadShaarli-stable.tar.gz
Shaarli-stable.tar.zst
Shaarli-stable.zip
Merge branch 'v0.11' into stablestable
Diffstat (limited to 'application/api/controllers/HistoryController.php')
-rw-r--r--application/api/controllers/HistoryController.php69
1 files changed, 69 insertions, 0 deletions
diff --git a/application/api/controllers/HistoryController.php b/application/api/controllers/HistoryController.php
new file mode 100644
index 00000000..9afcfa26
--- /dev/null
+++ b/application/api/controllers/HistoryController.php
@@ -0,0 +1,69 @@
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 HistoryController 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 = $this->history->getHistory();
32
33 // Return history operations from the {offset}th, starting from {since}.
34 $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since'));
35 $offset = $request->getParam('offset');
36 if (empty($offset)) {
37 $offset = 0;
38 } elseif (ctype_digit($offset)) {
39 $offset = (int) $offset;
40 } else {
41 throw new ApiBadParametersException('Invalid offset');
42 }
43
44 // limit parameter is either a number of links or 'all' for everything.
45 $limit = $request->getParam('limit');
46 if (empty($limit)) {
47 $limit = count($history);
48 } elseif (ctype_digit($limit)) {
49 $limit = (int) $limit;
50 } else {
51 throw new ApiBadParametersException('Invalid limit');
52 }
53
54 $out = [];
55 $i = 0;
56 foreach ($history as $entry) {
57 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
58 break;
59 }
60 if (++$i > $offset) {
61 $out[$i] = $entry;
62 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM);
63 }
64 }
65 $out = array_values($out);
66
67 return $response->withJson($out, 200, $this->jsonStyle);
68 }
69}