aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/api/controllers/HistoryController.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2019-01-12 23:55:38 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-13 00:04:42 +0100
commitdea72c711ff740b3b829d238fcf85648465143a0 (patch)
tree5e926e36816d510e3b3a10e20b94c23f43b55092 /application/api/controllers/HistoryController.php
parenta43e7842e44068584302ec1d6349155b571d9c96 (diff)
downloadShaarli-dea72c711ff740b3b829d238fcf85648465143a0.tar.gz
Shaarli-dea72c711ff740b3b829d238fcf85648465143a0.tar.zst
Shaarli-dea72c711ff740b3b829d238fcf85648465143a0.zip
Optimize and cleanup imports
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
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}