diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-01-15 16:49:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-15 16:49:50 +0100 |
commit | 9977c418d6d0de9e22e4ec276e7d476e184b5d01 (patch) | |
tree | b24cef1d52ea5451080534c98e177554d679947d /application | |
parent | 5fbab3edb300ad770d88f40ba1cc50d5b93a5bb8 (diff) | |
parent | c3b00963fe22479e87998c82bc83827a54c8d972 (diff) | |
download | Shaarli-9977c418d6d0de9e22e4ec276e7d476e184b5d01.tar.gz Shaarli-9977c418d6d0de9e22e4ec276e7d476e184b5d01.tar.zst Shaarli-9977c418d6d0de9e22e4ec276e7d476e184b5d01.zip |
Merge pull request #727 from ArthurHoaro/api/getlinks
REST API: implement getLinks service
Diffstat (limited to 'application')
-rw-r--r-- | application/api/ApiUtils.php | 31 | ||||
-rw-r--r-- | application/api/controllers/Links.php | 86 |
2 files changed, 117 insertions, 0 deletions
diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index a419c396..d4015865 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php | |||
@@ -46,4 +46,35 @@ class ApiUtils | |||
46 | throw new ApiAuthorizationException('Invalid JWT issued time'); | 46 | throw new ApiAuthorizationException('Invalid JWT issued time'); |
47 | } | 47 | } |
48 | } | 48 | } |
49 | |||
50 | /** | ||
51 | * Format a Link for the REST API. | ||
52 | * | ||
53 | * @param array $link Link data read from the datastore. | ||
54 | * @param string $indexUrl Shaarli's index URL (used for relative URL). | ||
55 | * | ||
56 | * @return array Link data formatted for the REST API. | ||
57 | */ | ||
58 | public static function formatLink($link, $indexUrl) | ||
59 | { | ||
60 | $out['id'] = $link['id']; | ||
61 | // Not an internal link | ||
62 | if ($link['url'][0] != '?') { | ||
63 | $out['url'] = $link['url']; | ||
64 | } else { | ||
65 | $out['url'] = $indexUrl . $link['url']; | ||
66 | } | ||
67 | $out['shorturl'] = $link['shorturl']; | ||
68 | $out['title'] = $link['title']; | ||
69 | $out['description'] = $link['description']; | ||
70 | $out['tags'] = preg_split('/\s+/', $link['tags'], -1, PREG_SPLIT_NO_EMPTY); | ||
71 | $out['private'] = $link['private'] == true; | ||
72 | $out['created'] = $link['created']->format(\DateTime::ATOM); | ||
73 | if (! empty($link['updated'])) { | ||
74 | $out['updated'] = $link['updated']->format(\DateTime::ATOM); | ||
75 | } else { | ||
76 | $out['updated'] = ''; | ||
77 | } | ||
78 | return $out; | ||
79 | } | ||
49 | } | 80 | } |
diff --git a/application/api/controllers/Links.php b/application/api/controllers/Links.php new file mode 100644 index 00000000..1c7b41cd --- /dev/null +++ b/application/api/controllers/Links.php | |||
@@ -0,0 +1,86 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use Shaarli\Api\ApiUtils; | ||
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | ||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Class Links | ||
12 | * | ||
13 | * REST API Controller: all services related to links collection. | ||
14 | * | ||
15 | * @package Api\Controllers | ||
16 | * @see http://shaarli.github.io/api-documentation/#links-links-collection | ||
17 | */ | ||
18 | class Links extends ApiController | ||
19 | { | ||
20 | /** | ||
21 | * @var int Number of links returned if no limit is provided. | ||
22 | */ | ||
23 | public static $DEFAULT_LIMIT = 20; | ||
24 | |||
25 | /** | ||
26 | * Retrieve a list of links, allowing different filters. | ||
27 | * | ||
28 | * @param Request $request Slim request. | ||
29 | * @param Response $response Slim response. | ||
30 | * | ||
31 | * @return Response response. | ||
32 | * | ||
33 | * @throws ApiBadParametersException Invalid parameters. | ||
34 | */ | ||
35 | public function getLinks($request, $response) | ||
36 | { | ||
37 | $private = $request->getParam('private'); | ||
38 | $links = $this->linkDb->filterSearch( | ||
39 | [ | ||
40 | 'searchtags' => $request->getParam('searchtags', ''), | ||
41 | 'searchterm' => $request->getParam('searchterm', ''), | ||
42 | ], | ||
43 | false, | ||
44 | $private === 'true' || $private === '1' | ||
45 | ); | ||
46 | |||
47 | // Return links from the {offset}th link, starting from 0. | ||
48 | $offset = $request->getParam('offset'); | ||
49 | if (! empty($offset) && ! ctype_digit($offset)) { | ||
50 | throw new ApiBadParametersException('Invalid offset'); | ||
51 | } | ||
52 | $offset = ! empty($offset) ? intval($offset) : 0; | ||
53 | if ($offset > count($links)) { | ||
54 | return $response->withJson([], 200, $this->jsonStyle); | ||
55 | } | ||
56 | |||
57 | // limit parameter is either a number of links or 'all' for everything. | ||
58 | $limit = $request->getParam('limit'); | ||
59 | if (empty($limit)) { | ||
60 | $limit = self::$DEFAULT_LIMIT; | ||
61 | } | ||
62 | else if (ctype_digit($limit)) { | ||
63 | $limit = intval($limit); | ||
64 | } else if ($limit === 'all') { | ||
65 | $limit = count($links); | ||
66 | } else { | ||
67 | throw new ApiBadParametersException('Invalid limit'); | ||
68 | } | ||
69 | |||
70 | // 'environment' is set by Slim and encapsulate $_SERVER. | ||
71 | $index = index_url($this->ci['environment']); | ||
72 | |||
73 | $out = []; | ||
74 | $cpt = 0; | ||
75 | foreach ($links as $link) { | ||
76 | if (count($out) >= $limit) { | ||
77 | break; | ||
78 | } | ||
79 | if ($cpt++ >= $offset) { | ||
80 | $out[] = ApiUtils::formatLink($link, $index); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return $response->withJson($out, 200, $this->jsonStyle); | ||
85 | } | ||
86 | } | ||