3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Api\Exceptions\ApiBadParametersException
;
7 use Slim\Http\Response
;
12 * REST API Controller: /history
14 * @package Shaarli\Api\Controllers
16 class HistoryController
extends ApiController
19 * Service providing operation regarding Shaarli datastore and settings.
21 * @param Request $request Slim request.
22 * @param Response $response Slim response.
24 * @return Response response.
26 * @throws ApiBadParametersException Invalid parameters.
28 public function getHistory($request, $response)
30 $history = $this->history
->getHistory();
32 // Return history operations from the {offset}th, starting from {since}.
33 $since = \DateTime
::createFromFormat(\DateTime
::ATOM
, $request->getParam('since'));
34 $offset = $request->getParam('offset');
37 } elseif (ctype_digit($offset)) {
38 $offset = (int) $offset;
40 throw new ApiBadParametersException('Invalid offset');
43 // limit parameter is either a number of bookmarks or 'all' for everything.
44 $limit = $request->getParam('limit');
46 $limit = count($history);
47 } elseif (ctype_digit($limit)) {
48 $limit = (int) $limit;
50 throw new ApiBadParametersException('Invalid limit');
55 foreach ($history as $entry) {
56 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
61 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime
::ATOM
);
64 $out = array_values($out);
66 return $response->withJson($out, 200, $this->jsonStyle
);