3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Api\ApiUtils
;
6 use Shaarli\Api\Exceptions\ApiBadParametersException
;
7 use Shaarli\Api\Exceptions\ApiLinkNotFoundException
;
9 use Slim\Http\Response
;
14 * REST API Controller: all services related to bookmarks collection.
16 * @package Api\Controllers
17 * @see http://shaarli.github.io/api-documentation/#links-links-collection
19 class Links
extends ApiController
22 * @var int Number of bookmarks returned if no limit is provided.
24 public static $DEFAULT_LIMIT = 20;
27 * Retrieve a list of bookmarks, allowing different filters.
29 * @param Request $request Slim request.
30 * @param Response $response Slim response.
32 * @return Response response.
34 * @throws ApiBadParametersException Invalid parameters.
36 public function getLinks($request, $response)
38 $private = $request->getParam('visibility');
40 // Return bookmarks from the {offset}th link, starting from 0.
41 $offset = $request->getParam('offset');
42 if (! empty($offset) && ! ctype_digit($offset)) {
43 throw new ApiBadParametersException('Invalid offset');
45 $offset = ! empty($offset) ? intval($offset) : 0;
47 // limit parameter is either a number of bookmarks or 'all' for everything.
48 $limit = $request->getParam('limit');
50 $limit = self
::$DEFAULT_LIMIT;
51 } elseif (ctype_digit($limit)) {
52 $limit = intval($limit);
53 } elseif ($limit === 'all') {
56 throw new ApiBadParametersException('Invalid limit');
59 $searchResult = $this->bookmarkService
->search(
61 'searchtags' => $request->getParam('searchtags', ''),
62 'searchterm' => $request->getParam('searchterm', ''),
71 'allowOutOfBounds' => true,
75 // 'environment' is set by Slim and encapsulate $_SERVER.
76 $indexUrl = index_url($this->ci
['environment']);
79 foreach ($searchResult->getBookmarks() as $bookmark) {
80 $out[] = ApiUtils
::formatLink($bookmark, $indexUrl);
83 return $response->withJson($out, 200, $this->jsonStyle
);
87 * Return a single formatted link by its ID.
89 * @param Request $request Slim request.
90 * @param Response $response Slim response.
91 * @param array $args Path parameters. including the ID.
93 * @return Response containing the link array.
95 * @throws ApiLinkNotFoundException generating a 404 error.
97 public function getLink($request, $response, $args)
99 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
100 if ($id === null || ! $this->bookmarkService
->exists($id)) {
101 throw new ApiLinkNotFoundException();
103 $index = index_url($this->ci
['environment']);
104 $out = ApiUtils
::formatLink($this->bookmarkService
->get($id), $index);
106 return $response->withJson($out, 200, $this->jsonStyle
);
110 * Creates a new link from posted request body.
112 * @param Request $request Slim request.
113 * @param Response $response Slim response.
115 * @return Response response.
117 public function postLink($request, $response)
119 $data = (array) ($request->getParsedBody() ?? []);
120 $bookmark = ApiUtils
::buildBookmarkFromRequest(
122 $this->conf
->get('privacy.default_private_links'),
123 $this->conf
->get('general.tags_separator', ' ')
125 // duplicate by URL, return 409 Conflict
127 ! empty($bookmark->getUrl())
128 && ! empty($dup = $this->bookmarkService
->findByUrl($bookmark->getUrl()))
130 return $response->withJson(
131 ApiUtils
::formatLink($dup, index_url($this->ci
['environment'])),
137 $this->bookmarkService
->add($bookmark);
138 $out = ApiUtils
::formatLink($bookmark, index_url($this->ci
['environment']));
139 $redirect = $this->ci
->router
->pathFor('getLink', ['id' => $bookmark->getId()]);
140 return $response->withAddedHeader('Location', $redirect)
141 ->withJson($out, 201, $this->jsonStyle
);
145 * Updates an existing link from posted request body.
147 * @param Request $request Slim request.
148 * @param Response $response Slim response.
149 * @param array $args Path parameters. including the ID.
151 * @return Response response.
153 * @throws ApiLinkNotFoundException generating a 404 error.
155 public function putLink($request, $response, $args)
157 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
158 if ($id === null || !$this->bookmarkService
->exists($id)) {
159 throw new ApiLinkNotFoundException();
162 $index = index_url($this->ci
['environment']);
163 $data = $request->getParsedBody();
165 $requestBookmark = ApiUtils
::buildBookmarkFromRequest(
167 $this->conf
->get('privacy.default_private_links'),
168 $this->conf
->get('general.tags_separator', ' ')
170 // duplicate URL on a different link, return 409 Conflict
172 ! empty($requestBookmark->getUrl())
173 && ! empty($dup = $this->bookmarkService
->findByUrl($requestBookmark->getUrl()))
174 && $dup->getId() != $id
176 return $response->withJson(
177 ApiUtils
::formatLink($dup, $index),
183 $responseBookmark = $this->bookmarkService
->get($id);
184 $responseBookmark = ApiUtils
::updateLink($responseBookmark, $requestBookmark);
185 $this->bookmarkService
->set($responseBookmark);
187 $out = ApiUtils
::formatLink($responseBookmark, $index);
188 return $response->withJson($out, 200, $this->jsonStyle
);
192 * Delete an existing link by its ID.
194 * @param Request $request Slim request.
195 * @param Response $response Slim response.
196 * @param array $args Path parameters. including the ID.
198 * @return Response response.
200 * @throws ApiLinkNotFoundException generating a 404 error.
202 public function deleteLink($request, $response, $args)
204 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
205 if ($id === null || !$this->bookmarkService
->exists($id)) {
206 throw new ApiLinkNotFoundException();
208 $bookmark = $this->bookmarkService
->get($id);
209 $this->bookmarkService
->remove($bookmark);
211 return $response->withStatus(204);