2017-05-06 19:39:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Shaarli\Api\Controllers;
|
|
|
|
|
|
|
|
use Shaarli\Api\Exceptions\ApiBadParametersException;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class History
|
|
|
|
*
|
|
|
|
* REST API Controller: /history
|
|
|
|
*
|
|
|
|
* @package Shaarli\Api\Controllers
|
|
|
|
*/
|
|
|
|
class History extends ApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Service providing operation regarding Shaarli datastore and settings.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request.
|
|
|
|
* @param Response $response Slim response.
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*
|
|
|
|
* @throws ApiBadParametersException Invalid parameters.
|
|
|
|
*/
|
|
|
|
public function getHistory($request, $response)
|
|
|
|
{
|
2017-05-07 16:50:20 +02:00
|
|
|
$history = $this->history->getHistory();
|
2017-05-06 19:39:39 +02:00
|
|
|
|
|
|
|
// Return history operations from the {offset}th, starting from {since}.
|
|
|
|
$since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since'));
|
|
|
|
$offset = $request->getParam('offset');
|
|
|
|
if (empty($offset)) {
|
|
|
|
$offset = 0;
|
|
|
|
}
|
2018-02-28 22:34:40 +01:00
|
|
|
elseif (ctype_digit($offset)) {
|
2017-05-06 19:39:39 +02:00
|
|
|
$offset = (int) $offset;
|
|
|
|
} else {
|
|
|
|
throw new ApiBadParametersException('Invalid offset');
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit parameter is either a number of links or 'all' for everything.
|
|
|
|
$limit = $request->getParam('limit');
|
|
|
|
if (empty($limit)) {
|
|
|
|
$limit = count($history);
|
2018-02-28 22:34:40 +01:00
|
|
|
} elseif (ctype_digit($limit)) {
|
2017-05-06 19:39:39 +02:00
|
|
|
$limit = (int) $limit;
|
|
|
|
} else {
|
|
|
|
throw new ApiBadParametersException('Invalid limit');
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = [];
|
|
|
|
$i = 0;
|
|
|
|
foreach ($history as $entry) {
|
|
|
|
if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (++$i > $offset) {
|
|
|
|
$out[$i] = $entry;
|
|
|
|
$out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out = array_values($out);
|
|
|
|
|
|
|
|
return $response->withJson($out, 200, $this->jsonStyle);
|
|
|
|
}
|
|
|
|
}
|