]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/admin/ShaarePublishController.php
Bulk creation: improve performances using memoization
[github/shaarli/Shaarli.git] / application / front / controller / admin / ShaarePublishController.php
CommitLineData
5d8de758
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
c609944c 9use Shaarli\Formatter\BookmarkFormatter;
5d8de758
A
10use Shaarli\Formatter\BookmarkMarkdownFormatter;
11use Shaarli\Render\TemplatePage;
12use Shaarli\Thumbnailer;
13use Slim\Http\Request;
14use Slim\Http\Response;
15
16class ShaarePublishController extends ShaarliAdminController
17{
c609944c
A
18 /**
19 * @var BookmarkFormatter[] Statically cached instances of formatters
20 */
21 protected $formatters = [];
22
23 /**
24 * @var array Statically cached bookmark's tags counts
25 */
26 protected $tags;
27
5d8de758
A
28 /**
29 * GET /admin/shaare - Displays the bookmark form for creation.
30 * Note that if the URL is found in existing bookmarks, then it will be in edit mode.
31 */
32 public function displayCreateForm(Request $request, Response $response): Response
33 {
34 $url = cleanup_url($request->getParam('post'));
35 $link = $this->buildLinkDataFromUrl($request, $url);
36
37 return $this->displayForm($link, $link['linkIsNew'], $request, $response);
38 }
39
40 /**
41 * POST /admin/shaare-batch - Displays multiple creation/edit forms from bulk add in add-link page.
42 */
43 public function displayCreateBatchForms(Request $request, Response $response): Response
44 {
45 $urls = array_map('cleanup_url', explode(PHP_EOL, $request->getParam('urls')));
46
47 $links = [];
48 foreach ($urls as $url) {
49 $link = $this->buildLinkDataFromUrl($request, $url);
50 $data = $this->buildFormData($link, $link['linkIsNew'], $request);
51 $data['token'] = $this->container->sessionManager->generateToken();
52 $data['source'] = 'batch';
53
54 $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
55
56 $links[] = $data;
57 }
58
59 $this->assignView('links', $links);
60 $this->assignView('batch_mode', true);
61 $this->assignView('async_metadata', $this->container->conf->get('general.enable_async_metadata', true));
62
63 return $response->write($this->render(TemplatePage::EDIT_LINK_BATCH));
64 }
65
66 /**
67 * GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
68 */
69 public function displayEditForm(Request $request, Response $response, array $args): Response
70 {
71 $id = $args['id'] ?? '';
72 try {
73 if (false === ctype_digit($id)) {
74 throw new BookmarkNotFoundException();
75 }
76 $bookmark = $this->container->bookmarkService->get((int) $id); // Read database
77 } catch (BookmarkNotFoundException $e) {
78 $this->saveErrorMessage(sprintf(
79 t('Bookmark with identifier %s could not be found.'),
80 $id
81 ));
82
83 return $this->redirect($response, '/');
84 }
85
c609944c 86 $formatter = $this->getFormatter('raw');
5d8de758
A
87 $link = $formatter->format($bookmark);
88
89 return $this->displayForm($link, false, $request, $response);
90 }
91
92 /**
93 * POST /admin/shaare
94 */
95 public function save(Request $request, Response $response): Response
96 {
97 $this->checkToken($request);
98
99 // lf_id should only be present if the link exists.
100 $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null;
101 if (null !== $id && true === $this->container->bookmarkService->exists($id)) {
102 // Edit
103 $bookmark = $this->container->bookmarkService->get($id);
104 } else {
105 // New link
106 $bookmark = new Bookmark();
107 }
108
109 $bookmark->setTitle($request->getParam('lf_title'));
110 $bookmark->setDescription($request->getParam('lf_description'));
111 $bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', []));
112 $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN));
113 $bookmark->setTagsString($request->getParam('lf_tags'));
114
115 if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
116 && true !== $this->container->conf->get('general.enable_async_metadata', true)
117 && $bookmark->shouldUpdateThumbnail()
118 ) {
119 $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
120 }
121 $this->container->bookmarkService->addOrSet($bookmark, false);
122
123 // To preserve backward compatibility with 3rd parties, plugins still use arrays
c609944c 124 $formatter = $this->getFormatter('raw');
5d8de758
A
125 $data = $formatter->format($bookmark);
126 $this->executePageHooks('save_link', $data);
127
128 $bookmark->fromArray($data);
129 $this->container->bookmarkService->set($bookmark);
130
131 // If we are called from the bookmarklet, we must close the popup:
132 if ($request->getParam('source') === 'bookmarklet') {
133 return $response->write('<script>self.close();</script>');
134 } elseif ($request->getParam('source') === 'batch') {
135 return $response;
136 }
137
138 if (!empty($request->getParam('returnurl'))) {
139 $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl'));
140 }
141
142 return $this->redirectFromReferer(
143 $request,
144 $response,
145 ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
146 $bookmark->getShortUrl()
147 );
148 }
149
150 /**
151 * Helper function used to display the shaare form whether it's a new or existing bookmark.
152 *
153 * @param array $link data used in template, either from parameters or from the data store
154 */
155 protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response
156 {
157 $data = $this->buildFormData($link, $isNew, $request);
158
159 $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
160
161 foreach ($data as $key => $value) {
162 $this->assignView($key, $value);
163 }
164
165 $editLabel = false === $isNew ? t('Edit') .' ' : '';
166 $this->assignView(
167 'pagetitle',
168 $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli')
169 );
170
171 return $response->write($this->render(TemplatePage::EDIT_LINK));
172 }
173
174 protected function buildLinkDataFromUrl(Request $request, string $url): array
175 {
176 // Check if URL is not already in database (in this case, we will edit the existing link)
177 $bookmark = $this->container->bookmarkService->findByUrl($url);
178 if (null === $bookmark) {
179 // Get shaare data if it was provided in URL (e.g.: by the bookmarklet).
180 $title = $request->getParam('title');
181 $description = $request->getParam('description');
182 $tags = $request->getParam('tags');
25e90d8d
A
183 if ($request->getParam('private') !== null) {
184 $private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN);
185 } else {
186 $private = $this->container->conf->get('privacy.default_private_links', false);
187 }
5d8de758
A
188
189 // If this is an HTTP(S) link, we try go get the page to extract
190 // the title (otherwise we will to straight to the edit form.)
191 if (true !== $this->container->conf->get('general.enable_async_metadata', true)
192 && empty($title)
193 && strpos(get_url_scheme($url) ?: '', 'http') !== false
194 ) {
195 $metadata = $this->container->metadataRetriever->retrieve($url);
196 }
197
198 if (empty($url)) {
199 $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: '));
200 }
201
202 return [
203 'title' => $title ?? $metadata['title'] ?? '',
204 'url' => $url ?? '',
205 'description' => $description ?? $metadata['description'] ?? '',
206 'tags' => $tags ?? $metadata['tags'] ?? '',
207 'private' => $private,
208 'linkIsNew' => true,
209 ];
210 }
211
c609944c 212 $formatter = $this->getFormatter('raw');
5d8de758
A
213 $link = $formatter->format($bookmark);
214 $link['linkIsNew'] = false;
215
216 return $link;
217 }
218
219 protected function buildFormData(array $link, bool $isNew, Request $request): array
220 {
5d8de758
A
221 return escape([
222 'link' => $link,
223 'link_is_new' => $isNew,
224 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
225 'source' => $request->getParam('source') ?? '',
c609944c 226 'tags' => $this->getTags(),
5d8de758
A
227 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
228 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
229 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
230 ]);
231 }
c609944c
A
232
233 /**
234 * Memoize formatterFactory->getFormatter() calls.
235 */
236 protected function getFormatter(string $type): BookmarkFormatter
237 {
238 if (!array_key_exists($type, $this->formatters) || $this->formatters[$type] === null) {
239 $this->formatters[$type] = $this->container->formatterFactory->getFormatter($type);
240 }
241
242 return $this->formatters[$type];
243 }
244
245 /**
246 * Memoize bookmarkService->bookmarksCountPerTag() calls.
247 */
248 protected function getTags(): array
249 {
250 if ($this->tags === null) {
251 $this->tags = $this->container->bookmarkService->bookmarksCountPerTag();
252
253 if ($this->container->conf->get('formatter') === 'markdown') {
254 $this->tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
255 }
256 }
257
258 return $this->tags;
259 }
5d8de758 260}