4 namespace Shaarli\Api\Controllers
;
6 use Shaarli\Api\Exceptions\ApiBadParametersException
;
8 use Slim\Http\Response
;
13 * REST API Controller: /history
15 * @package Shaarli\Api\Controllers
17 class HistoryController
extends ApiController
20 * Service providing operation regarding Shaarli datastore and settings.
22 * @param Request $request Slim request.
23 * @param Response $response Slim response.
25 * @return Response response.
27 * @throws ApiBadParametersException Invalid parameters.
29 public function getHistory($request, $response)
31 $history = $this->history
->getHistory();
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');
38 } elseif (ctype_digit($offset)) {
39 $offset = (int) $offset;
41 throw new ApiBadParametersException('Invalid offset');
44 // limit parameter is either a number of bookmarks or 'all' for everything.
45 $limit = $request->getParam('limit');
47 $limit = count($history);
48 } elseif (ctype_digit($limit)) {
49 $limit = (int) $limit;
51 throw new ApiBadParametersException('Invalid limit');
56 foreach ($history as $entry) {
57 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
62 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime
::ATOM
);
65 $out = array_values($out);
67 return $response->withJson($out, 200, $this->jsonStyle
);