]>
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 | * | |
cf92b4dd | 14 | * REST API Controller: all services related to bookmarks collection. |
c3b00963 A |
15 | * |
16 | * @package Api\Controllers | |
17 | * @see http://shaarli.github.io/api-documentation/#links-links-collection | |
18 | */ | |
19 | class Links extends ApiController | |
20 | { | |
21 | /** | |
cf92b4dd | 22 | * @var int Number of bookmarks returned if no limit is provided. |
c3b00963 A |
23 | */ |
24 | public static $DEFAULT_LIMIT = 20; | |
25 | ||
26 | /** | |
cf92b4dd | 27 | * Retrieve a list of bookmarks, allowing different filters. |
c3b00963 A |
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 | 39 | |
cf92b4dd | 40 | // Return bookmarks from the {offset}th link, starting from 0. |
c3b00963 A |
41 | $offset = $request->getParam('offset'); |
42 | if (! empty($offset) && ! ctype_digit($offset)) { | |
43 | throw new ApiBadParametersException('Invalid offset'); | |
44 | } | |
45 | $offset = ! empty($offset) ? intval($offset) : 0; | |
c3b00963 | 46 | |
cf92b4dd | 47 | // limit parameter is either a number of bookmarks or 'all' for everything. |
c3b00963 A |
48 | $limit = $request->getParam('limit'); |
49 | if (empty($limit)) { | |
50 | $limit = self::$DEFAULT_LIMIT; | |
d2d4f993 | 51 | } elseif (ctype_digit($limit)) { |
c3b00963 | 52 | $limit = intval($limit); |
d2d4f993 | 53 | } elseif ($limit === 'all') { |
9b8c0a45 | 54 | $limit = null; |
c3b00963 A |
55 | } else { |
56 | throw new ApiBadParametersException('Invalid limit'); | |
57 | } | |
58 | ||
9b8c0a45 A |
59 | $searchResult = $this->bookmarkService->search( |
60 | [ | |
61 | 'searchtags' => $request->getParam('searchtags', ''), | |
62 | 'searchterm' => $request->getParam('searchterm', ''), | |
63 | ], | |
64 | $private, | |
65 | false, | |
66 | false, | |
67 | false, | |
68 | [ | |
69 | 'limit' => $limit, | |
70 | 'offset' => $offset, | |
71 | 'allowOutOfBounds' => true, | |
72 | ] | |
73 | ); | |
74 | ||
c3b00963 | 75 | // 'environment' is set by Slim and encapsulate $_SERVER. |
d3f42ca4 | 76 | $indexUrl = index_url($this->ci['environment']); |
c3b00963 A |
77 | |
78 | $out = []; | |
9b8c0a45 A |
79 | foreach ($searchResult->getBookmarks() as $bookmark) { |
80 | $out[] = ApiUtils::formatLink($bookmark, $indexUrl); | |
c3b00963 A |
81 | } |
82 | ||
83 | return $response->withJson($out, 200, $this->jsonStyle); | |
84 | } | |
16e3d006 A |
85 | |
86 | /** | |
87 | * Return a single formatted link by its ID. | |
88 | * | |
89 | * @param Request $request Slim request. | |
90 | * @param Response $response Slim response. | |
91 | * @param array $args Path parameters. including the ID. | |
92 | * | |
93 | * @return Response containing the link array. | |
94 | * | |
95 | * @throws ApiLinkNotFoundException generating a 404 error. | |
96 | */ | |
97 | public function getLink($request, $response, $args) | |
98 | { | |
efb7d21b A |
99 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
100 | if ($id === null || ! $this->bookmarkService->exists($id)) { | |
16e3d006 A |
101 | throw new ApiLinkNotFoundException(); |
102 | } | |
103 | $index = index_url($this->ci['environment']); | |
efb7d21b | 104 | $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index); |
68016e37 | 105 | |
16e3d006 A |
106 | return $response->withJson($out, 200, $this->jsonStyle); |
107 | } | |
68016e37 A |
108 | |
109 | /** | |
110 | * Creates a new link from posted request body. | |
111 | * | |
112 | * @param Request $request Slim request. | |
113 | * @param Response $response Slim response. | |
114 | * | |
115 | * @return Response response. | |
116 | */ | |
117 | public function postLink($request, $response) | |
118 | { | |
efb7d21b | 119 | $data = (array) ($request->getParsedBody() ?? []); |
0640c1a6 A |
120 | $bookmark = ApiUtils::buildBookmarkFromRequest( |
121 | $data, | |
122 | $this->conf->get('privacy.default_private_links'), | |
123 | $this->conf->get('general.tags_separator', ' ') | |
124 | ); | |
68016e37 | 125 | // duplicate by URL, return 409 Conflict |
53054b2b A |
126 | if ( |
127 | ! empty($bookmark->getUrl()) | |
cf92b4dd A |
128 | && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl())) |
129 | ) { | |
68016e37 A |
130 | return $response->withJson( |
131 | ApiUtils::formatLink($dup, index_url($this->ci['environment'])), | |
132 | 409, | |
133 | $this->jsonStyle | |
134 | ); | |
135 | } | |
136 | ||
cf92b4dd A |
137 | $this->bookmarkService->add($bookmark); |
138 | $out = ApiUtils::formatLink($bookmark, index_url($this->ci['environment'])); | |
b37ca790 | 139 | $redirect = $this->ci->router->pathFor('getLink', ['id' => $bookmark->getId()]); |
68016e37 A |
140 | return $response->withAddedHeader('Location', $redirect) |
141 | ->withJson($out, 201, $this->jsonStyle); | |
142 | } | |
cf9181dd A |
143 | |
144 | /** | |
145 | * Updates an existing link from posted request body. | |
146 | * | |
147 | * @param Request $request Slim request. | |
148 | * @param Response $response Slim response. | |
149 | * @param array $args Path parameters. including the ID. | |
150 | * | |
151 | * @return Response response. | |
152 | * | |
153 | * @throws ApiLinkNotFoundException generating a 404 error. | |
154 | */ | |
155 | public function putLink($request, $response, $args) | |
156 | { | |
efb7d21b A |
157 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
158 | if ($id === null || !$this->bookmarkService->exists($id)) { | |
cf9181dd A |
159 | throw new ApiLinkNotFoundException(); |
160 | } | |
161 | ||
162 | $index = index_url($this->ci['environment']); | |
163 | $data = $request->getParsedBody(); | |
164 | ||
0640c1a6 A |
165 | $requestBookmark = ApiUtils::buildBookmarkFromRequest( |
166 | $data, | |
167 | $this->conf->get('privacy.default_private_links'), | |
168 | $this->conf->get('general.tags_separator', ' ') | |
169 | ); | |
cf9181dd | 170 | // duplicate URL on a different link, return 409 Conflict |
53054b2b A |
171 | if ( |
172 | ! empty($requestBookmark->getUrl()) | |
cf92b4dd | 173 | && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl())) |
efb7d21b | 174 | && $dup->getId() != $id |
cf9181dd A |
175 | ) { |
176 | return $response->withJson( | |
177 | ApiUtils::formatLink($dup, $index), | |
178 | 409, | |
179 | $this->jsonStyle | |
180 | ); | |
181 | } | |
182 | ||
efb7d21b | 183 | $responseBookmark = $this->bookmarkService->get($id); |
cf92b4dd A |
184 | $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark); |
185 | $this->bookmarkService->set($responseBookmark); | |
cf9181dd | 186 | |
cf92b4dd | 187 | $out = ApiUtils::formatLink($responseBookmark, $index); |
cf9181dd A |
188 | return $response->withJson($out, 200, $this->jsonStyle); |
189 | } | |
0843848c A |
190 | |
191 | /** | |
192 | * Delete an existing link by its ID. | |
193 | * | |
194 | * @param Request $request Slim request. | |
195 | * @param Response $response Slim response. | |
196 | * @param array $args Path parameters. including the ID. | |
197 | * | |
198 | * @return Response response. | |
199 | * | |
200 | * @throws ApiLinkNotFoundException generating a 404 error. | |
201 | */ | |
202 | public function deleteLink($request, $response, $args) | |
203 | { | |
efb7d21b A |
204 | $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null; |
205 | if ($id === null || !$this->bookmarkService->exists($id)) { | |
0843848c A |
206 | throw new ApiLinkNotFoundException(); |
207 | } | |
efb7d21b | 208 | $bookmark = $this->bookmarkService->get($id); |
cf92b4dd | 209 | $this->bookmarkService->remove($bookmark); |
0843848c A |
210 | |
211 | return $response->withStatus(204); | |
212 | } | |
c3b00963 | 213 | } |