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 History
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');
39 else if (ctype_digit($offset)) {
40 $offset = (int) $offset;
42 throw new ApiBadParametersException('Invalid offset');
45 // limit parameter is either a number of links or 'all' for everything.
46 $limit = $request->getParam('limit');
48 $limit = count($history);
49 } else if (ctype_digit($limit)) {
50 $limit = (int) $limit;
52 throw new ApiBadParametersException('Invalid limit');
57 foreach ($history as $entry) {
58 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
63 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime
::ATOM
);
66 $out = array_values($out);
68 return $response->withJson($out, 200, $this->jsonStyle
);