API: Get History endpoint
See http://shaarli.github.io/api-documentation/#links-history-get
This commit is contained in:
parent
b8fcb7d440
commit
61d406933e
4 changed files with 375 additions and 0 deletions
application/api/controllers
71
application/api/controllers/History.php
Normal file
71
application/api/controllers/History.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?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)
|
||||
{
|
||||
$history = (new \History($this->conf->get('resource.history')))->getHistory();
|
||||
$history = array_reverse($history);
|
||||
|
||||
// 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;
|
||||
}
|
||||
else if (ctype_digit($offset)) {
|
||||
$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);
|
||||
} else if (ctype_digit($limit)) {
|
||||
$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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue