]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/api/controllers/Tags.php
Handle pagination through BookmarkService
[github/shaarli/Shaarli.git] / application / api / controllers / Tags.php
CommitLineData
d3f42ca4
A
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5use Shaarli\Api\ApiUtils;
6use Shaarli\Api\Exceptions\ApiBadParametersException;
d3f42ca4 7use Shaarli\Api\Exceptions\ApiTagNotFoundException;
cf92b4dd 8use Shaarli\Bookmark\BookmarkFilter;
d3f42ca4
A
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class Tags
14 *
15 * REST API Controller: all services related to tags collection.
16 *
17 * @package Api\Controllers
18 */
19class Tags extends ApiController
20{
21 /**
cf92b4dd 22 * @var int Number of bookmarks returned if no limit is provided.
d3f42ca4
A
23 */
24 public static $DEFAULT_LIMIT = 'all';
25
26 /**
27 * Retrieve a list of tags, 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 getTags($request, $response)
37 {
38 $visibility = $request->getParam('visibility');
cf92b4dd 39 $tags = $this->bookmarkService->bookmarksCountPerTag([], $visibility);
d3f42ca4
A
40
41 // Return tags from the {offset}th tag, starting from 0.
42 $offset = $request->getParam('offset');
43 if (! empty($offset) && ! ctype_digit($offset)) {
44 throw new ApiBadParametersException('Invalid offset');
45 }
46 $offset = ! empty($offset) ? intval($offset) : 0;
47 if ($offset > count($tags)) {
48 return $response->withJson([], 200, $this->jsonStyle);
49 }
50
cf92b4dd 51 // limit parameter is either a number of bookmarks or 'all' for everything.
d3f42ca4
A
52 $limit = $request->getParam('limit');
53 if (empty($limit)) {
54 $limit = self::$DEFAULT_LIMIT;
55 }
56 if (ctype_digit($limit)) {
57 $limit = intval($limit);
58 } elseif ($limit === 'all') {
59 $limit = count($tags);
60 } else {
61 throw new ApiBadParametersException('Invalid limit');
62 }
63
64 $out = [];
65 $index = 0;
66 foreach ($tags as $tag => $occurrences) {
67 if (count($out) >= $limit) {
68 break;
69 }
70 if ($index++ >= $offset) {
71 $out[] = ApiUtils::formatTag($tag, $occurrences);
72 }
73 }
74
75 return $response->withJson($out, 200, $this->jsonStyle);
76 }
77
78 /**
79 * Return a single formatted tag by its name.
80 *
81 * @param Request $request Slim request.
82 * @param Response $response Slim response.
83 * @param array $args Path parameters. including the tag name.
84 *
85 * @return Response containing the link array.
86 *
87 * @throws ApiTagNotFoundException generating a 404 error.
88 */
89 public function getTag($request, $response, $args)
90 {
cf92b4dd 91 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
92 if (!isset($tags[$args['tagName']])) {
93 throw new ApiTagNotFoundException();
94 }
95 $out = ApiUtils::formatTag($args['tagName'], $tags[$args['tagName']]);
96
97 return $response->withJson($out, 200, $this->jsonStyle);
98 }
99
100 /**
101 * Rename a tag from the given name.
102 * If the new name provided matches an existing tag, they will be merged.
103 *
104 * @param Request $request Slim request.
105 * @param Response $response Slim response.
106 * @param array $args Path parameters. including the tag name.
107 *
108 * @return Response response.
109 *
110 * @throws ApiTagNotFoundException generating a 404 error.
111 * @throws ApiBadParametersException new tag name not provided
112 */
113 public function putTag($request, $response, $args)
114 {
cf92b4dd 115 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
116 if (! isset($tags[$args['tagName']])) {
117 throw new ApiTagNotFoundException();
118 }
119
120 $data = $request->getParsedBody();
121 if (empty($data['name'])) {
122 throw new ApiBadParametersException('New tag name is required in the request body');
123 }
124
9b8c0a45 125 $searchResult = $this->bookmarkService->search(
cf92b4dd
A
126 ['searchtags' => $args['tagName']],
127 BookmarkFilter::$ALL,
128 true
129 );
9b8c0a45 130 foreach ($searchResult->getBookmarks() as $bookmark) {
cf92b4dd
A
131 $bookmark->renameTag($args['tagName'], $data['name']);
132 $this->bookmarkService->set($bookmark, false);
133 $this->history->updateLink($bookmark);
d3f42ca4 134 }
cf92b4dd 135 $this->bookmarkService->save();
d3f42ca4 136
cf92b4dd 137 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
138 $out = ApiUtils::formatTag($data['name'], $tags[$data['name']]);
139 return $response->withJson($out, 200, $this->jsonStyle);
140 }
141
142 /**
143 * Delete an existing tag by its name.
144 *
145 * @param Request $request Slim request.
146 * @param Response $response Slim response.
147 * @param array $args Path parameters. including the tag name.
148 *
149 * @return Response response.
150 *
151 * @throws ApiTagNotFoundException generating a 404 error.
152 */
153 public function deleteTag($request, $response, $args)
154 {
cf92b4dd 155 $tags = $this->bookmarkService->bookmarksCountPerTag();
d3f42ca4
A
156 if (! isset($tags[$args['tagName']])) {
157 throw new ApiTagNotFoundException();
158 }
cf92b4dd 159
9b8c0a45 160 $searchResult = $this->bookmarkService->search(
cf92b4dd
A
161 ['searchtags' => $args['tagName']],
162 BookmarkFilter::$ALL,
163 true
164 );
9b8c0a45 165 foreach ($searchResult->getBookmarks() as $bookmark) {
cf92b4dd
A
166 $bookmark->deleteTag($args['tagName']);
167 $this->bookmarkService->set($bookmark, false);
168 $this->history->updateLink($bookmark);
d3f42ca4 169 }
cf92b4dd 170 $this->bookmarkService->save();
d3f42ca4
A
171
172 return $response->withStatus(204);
173 }
174}