aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controller/admin
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-23 12:29:52 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-27 20:11:30 +0100
commitc609944cb906a2f5002cd86a808aa36d8deb2afd (patch)
treebdac2c4ddffb524336582ba9a94f4fb0b2c952a0 /application/front/controller/admin
parent25e90d8d75382721ff7473fa1686090fcfeb46ff (diff)
downloadShaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.tar.gz
Shaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.tar.zst
Shaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.zip
Bulk creation: improve performances using memoization
Reduced additional processing time per links from ~40ms to ~5ms
Diffstat (limited to 'application/front/controller/admin')
-rw-r--r--application/front/controller/admin/ShaarePublishController.php52
1 files changed, 43 insertions, 9 deletions
diff --git a/application/front/controller/admin/ShaarePublishController.php b/application/front/controller/admin/ShaarePublishController.php
index fd680ea0..65fdcdee 100644
--- a/application/front/controller/admin/ShaarePublishController.php
+++ b/application/front/controller/admin/ShaarePublishController.php
@@ -6,6 +6,7 @@ namespace Shaarli\Front\Controller\Admin;
6 6
7use Shaarli\Bookmark\Bookmark; 7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException; 8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Formatter\BookmarkFormatter;
9use Shaarli\Formatter\BookmarkMarkdownFormatter; 10use Shaarli\Formatter\BookmarkMarkdownFormatter;
10use Shaarli\Render\TemplatePage; 11use Shaarli\Render\TemplatePage;
11use Shaarli\Thumbnailer; 12use Shaarli\Thumbnailer;
@@ -15,6 +16,16 @@ use Slim\Http\Response;
15class ShaarePublishController extends ShaarliAdminController 16class ShaarePublishController extends ShaarliAdminController
16{ 17{
17 /** 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
28 /**
18 * GET /admin/shaare - Displays the bookmark form for creation. 29 * GET /admin/shaare - Displays the bookmark form for creation.
19 * Note that if the URL is found in existing bookmarks, then it will be in edit mode. 30 * Note that if the URL is found in existing bookmarks, then it will be in edit mode.
20 */ 31 */
@@ -72,7 +83,7 @@ class ShaarePublishController extends ShaarliAdminController
72 return $this->redirect($response, '/'); 83 return $this->redirect($response, '/');
73 } 84 }
74 85
75 $formatter = $this->container->formatterFactory->getFormatter('raw'); 86 $formatter = $this->getFormatter('raw');
76 $link = $formatter->format($bookmark); 87 $link = $formatter->format($bookmark);
77 88
78 return $this->displayForm($link, false, $request, $response); 89 return $this->displayForm($link, false, $request, $response);
@@ -110,7 +121,7 @@ class ShaarePublishController extends ShaarliAdminController
110 $this->container->bookmarkService->addOrSet($bookmark, false); 121 $this->container->bookmarkService->addOrSet($bookmark, false);
111 122
112 // To preserve backward compatibility with 3rd parties, plugins still use arrays 123 // To preserve backward compatibility with 3rd parties, plugins still use arrays
113 $formatter = $this->container->formatterFactory->getFormatter('raw'); 124 $formatter = $this->getFormatter('raw');
114 $data = $formatter->format($bookmark); 125 $data = $formatter->format($bookmark);
115 $this->executePageHooks('save_link', $data); 126 $this->executePageHooks('save_link', $data);
116 127
@@ -198,7 +209,7 @@ class ShaarePublishController extends ShaarliAdminController
198 ]; 209 ];
199 } 210 }
200 211
201 $formatter = $this->container->formatterFactory->getFormatter('raw'); 212 $formatter = $this->getFormatter('raw');
202 $link = $formatter->format($bookmark); 213 $link = $formatter->format($bookmark);
203 $link['linkIsNew'] = false; 214 $link['linkIsNew'] = false;
204 215
@@ -207,20 +218,43 @@ class ShaarePublishController extends ShaarliAdminController
207 218
208 protected function buildFormData(array $link, bool $isNew, Request $request): array 219 protected function buildFormData(array $link, bool $isNew, Request $request): array
209 { 220 {
210 $tags = $this->container->bookmarkService->bookmarksCountPerTag();
211 if ($this->container->conf->get('formatter') === 'markdown') {
212 $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
213 }
214
215 return escape([ 221 return escape([
216 'link' => $link, 222 'link' => $link,
217 'link_is_new' => $isNew, 223 'link_is_new' => $isNew,
218 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', 224 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
219 'source' => $request->getParam('source') ?? '', 225 'source' => $request->getParam('source') ?? '',
220 'tags' => $tags, 226 'tags' => $this->getTags(),
221 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), 227 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
222 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), 228 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
223 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), 229 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
224 ]); 230 ]);
225 } 231 }
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 }
226} 260}