]> git.immae.eu Git - github/shaarli/Shaarli.git/blame_incremental - application/api/controllers/Links.php
Merge pull request #1672 from ArthurHoaro/feature/api-parse-tags-string
[github/shaarli/Shaarli.git] / application / api / controllers / Links.php
... / ...
CommitLineData
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5use Shaarli\Api\ApiUtils;
6use Shaarli\Api\Exceptions\ApiBadParametersException;
7use Shaarli\Api\Exceptions\ApiLinkNotFoundException;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class Links
13 *
14 * REST API Controller: all services related to bookmarks collection.
15 *
16 * @package Api\Controllers
17 * @see http://shaarli.github.io/api-documentation/#links-links-collection
18 */
19class Links extends ApiController
20{
21 /**
22 * @var int Number of bookmarks returned if no limit is provided.
23 */
24 public static $DEFAULT_LIMIT = 20;
25
26 /**
27 * Retrieve a list of bookmarks, 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 {
38 $private = $request->getParam('visibility');
39 $bookmarks = $this->bookmarkService->search(
40 [
41 'searchtags' => $request->getParam('searchtags', ''),
42 'searchterm' => $request->getParam('searchterm', ''),
43 ],
44 $private
45 );
46
47 // Return bookmarks 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($bookmarks)) {
54 return $response->withJson([], 200, $this->jsonStyle);
55 }
56
57 // limit parameter is either a number of bookmarks or 'all' for everything.
58 $limit = $request->getParam('limit');
59 if (empty($limit)) {
60 $limit = self::$DEFAULT_LIMIT;
61 } elseif (ctype_digit($limit)) {
62 $limit = intval($limit);
63 } elseif ($limit === 'all') {
64 $limit = count($bookmarks);
65 } else {
66 throw new ApiBadParametersException('Invalid limit');
67 }
68
69 // 'environment' is set by Slim and encapsulate $_SERVER.
70 $indexUrl = index_url($this->ci['environment']);
71
72 $out = [];
73 $index = 0;
74 foreach ($bookmarks as $bookmark) {
75 if (count($out) >= $limit) {
76 break;
77 }
78 if ($index++ >= $offset) {
79 $out[] = ApiUtils::formatLink($bookmark, $indexUrl);
80 }
81 }
82
83 return $response->withJson($out, 200, $this->jsonStyle);
84 }
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 {
99 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
100 if ($id === null || ! $this->bookmarkService->exists($id)) {
101 throw new ApiLinkNotFoundException();
102 }
103 $index = index_url($this->ci['environment']);
104 $out = ApiUtils::formatLink($this->bookmarkService->get($id), $index);
105
106 return $response->withJson($out, 200, $this->jsonStyle);
107 }
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 {
119 $data = (array) ($request->getParsedBody() ?? []);
120 $bookmark = ApiUtils::buildBookmarkFromRequest(
121 $data,
122 $this->conf->get('privacy.default_private_links'),
123 $this->conf->get('general.tags_separator', ' ')
124 );
125 // duplicate by URL, return 409 Conflict
126 if (
127 ! empty($bookmark->getUrl())
128 && ! empty($dup = $this->bookmarkService->findByUrl($bookmark->getUrl()))
129 ) {
130 return $response->withJson(
131 ApiUtils::formatLink($dup, index_url($this->ci['environment'])),
132 409,
133 $this->jsonStyle
134 );
135 }
136
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);
142 }
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 {
157 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
158 if ($id === null || !$this->bookmarkService->exists($id)) {
159 throw new ApiLinkNotFoundException();
160 }
161
162 $index = index_url($this->ci['environment']);
163 $data = $request->getParsedBody();
164
165 $requestBookmark = ApiUtils::buildBookmarkFromRequest(
166 $data,
167 $this->conf->get('privacy.default_private_links'),
168 $this->conf->get('general.tags_separator', ' ')
169 );
170 // duplicate URL on a different link, return 409 Conflict
171 if (
172 ! empty($requestBookmark->getUrl())
173 && ! empty($dup = $this->bookmarkService->findByUrl($requestBookmark->getUrl()))
174 && $dup->getId() != $id
175 ) {
176 return $response->withJson(
177 ApiUtils::formatLink($dup, $index),
178 409,
179 $this->jsonStyle
180 );
181 }
182
183 $responseBookmark = $this->bookmarkService->get($id);
184 $responseBookmark = ApiUtils::updateLink($responseBookmark, $requestBookmark);
185 $this->bookmarkService->set($responseBookmark);
186
187 $out = ApiUtils::formatLink($responseBookmark, $index);
188 return $response->withJson($out, 200, $this->jsonStyle);
189 }
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 {
204 $id = is_integer_mixed($args['id']) ? (int) $args['id'] : null;
205 if ($id === null || !$this->bookmarkService->exists($id)) {
206 throw new ApiLinkNotFoundException();
207 }
208 $bookmark = $this->bookmarkService->get($id);
209 $this->bookmarkService->remove($bookmark);
210
211 return $response->withStatus(204);
212 }
213}