]>
Commit | Line | Data |
---|---|---|
c3b00963 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Api\Controllers; | |
4 | ||
5 | use Shaarli\Api\ApiUtils; | |
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | |
16e3d006 | 7 | use Shaarli\Api\Exceptions\ApiLinkNotFoundException; |
c3b00963 A |
8 | use Slim\Http\Request; |
9 | use Slim\Http\Response; | |
10 | ||
11 | /** | |
12 | * Class Links | |
13 | * | |
14 | * REST API Controller: all services related to links collection. | |
15 | * | |
16 | * @package Api\Controllers | |
17 | * @see http://shaarli.github.io/api-documentation/#links-links-collection | |
18 | */ | |
19 | class Links extends ApiController | |
20 | { | |
21 | /** | |
22 | * @var int Number of links returned if no limit is provided. | |
23 | */ | |
24 | public static $DEFAULT_LIMIT = 20; | |
25 | ||
26 | /** | |
27 | * Retrieve a list of links, allowing different filters. | |
28 | * | |
29 | * @param Request $request Slim request. | |
30 | * @param Response $response Slim response. | |
31 | * | |
32 | * @return Response response. | |
33 | * | |
34 | * @throws ApiBadParametersException Invalid parameters. | |
35 | */ | |
36 | public function getLinks($request, $response) | |
37 | { | |
c37a6f82 | 38 | $private = $request->getParam('visibility'); |
c3b00963 A |
39 | $links = $this->linkDb->filterSearch( |
40 | [ | |
41 | 'searchtags' => $request->getParam('searchtags', ''), | |
42 | 'searchterm' => $request->getParam('searchterm', ''), | |
43 | ], | |
44 | false, | |
c37a6f82 | 45 | $private |
c3b00963 A |
46 | ); |
47 | ||
48 | // Return links from the {offset}th link, starting from 0. | |
49 | $offset = $request->getParam('offset'); | |
50 | if (! empty($offset) && ! ctype_digit($offset)) { | |
51 | throw new ApiBadParametersException('Invalid offset'); | |
52 | } | |
53 | $offset = ! empty($offset) ? intval($offset) : 0; | |
54 | if ($offset > count($links)) { | |
55 | return $response->withJson([], 200, $this->jsonStyle); | |
56 | } | |
57 | ||
58 | // limit parameter is either a number of links or 'all' for everything. | |
59 | $limit = $request->getParam('limit'); | |
60 | if (empty($limit)) { | |
61 | $limit = self::$DEFAULT_LIMIT; | |
16e3d006 | 62 | } else if (ctype_digit($limit)) { |
c3b00963 A |
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 | } | |
16e3d006 A |
86 | |
87 | /** | |
88 | * Return a single formatted link by its ID. | |
89 | * | |
90 | * @param Request $request Slim request. | |
91 | * @param Response $response Slim response. | |
92 | * @param array $args Path parameters. including the ID. | |
93 | * | |
94 | * @return Response containing the link array. | |
95 | * | |
96 | * @throws ApiLinkNotFoundException generating a 404 error. | |
97 | */ | |
98 | public function getLink($request, $response, $args) | |
99 | { | |
100 | if (! isset($this->linkDb[$args['id']])) { | |
101 | throw new ApiLinkNotFoundException(); | |
102 | } | |
103 | $index = index_url($this->ci['environment']); | |
104 | $out = ApiUtils::formatLink($this->linkDb[$args['id']], $index); | |
105 | return $response->withJson($out, 200, $this->jsonStyle); | |
106 | } | |
c3b00963 | 107 | } |