diff options
Diffstat (limited to 'application/api/controllers/HistoryController.php')
-rw-r--r-- | application/api/controllers/HistoryController.php | 69 |
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 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | ||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Class History | ||
12 | * | ||
13 | * REST API Controller: /history | ||
14 | * | ||
15 | * @package Shaarli\Api\Controllers | ||
16 | */ | ||
17 | class 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 | } | ||