]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/front/controller/admin/ShaarePublishController.php
Fix: synchronous metadata retrieval is failing in strict mode
[github/shaarli/Shaarli.git] / application / front / controller / admin / ShaarePublishController.php
index fd680ea0090ca1e72d3d3c5d3b1ff3add7c66317..fb9cacc22fae47b3e9bc57cfe054292f4511b87d 100644 (file)
@@ -6,6 +6,7 @@ namespace Shaarli\Front\Controller\Admin;
 
 use Shaarli\Bookmark\Bookmark;
 use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
+use Shaarli\Formatter\BookmarkFormatter;
 use Shaarli\Formatter\BookmarkMarkdownFormatter;
 use Shaarli\Render\TemplatePage;
 use Shaarli\Thumbnailer;
@@ -14,6 +15,16 @@ use Slim\Http\Response;
 
 class ShaarePublishController extends ShaarliAdminController
 {
+    /**
+     * @var BookmarkFormatter[] Statically cached instances of formatters
+     */
+    protected $formatters = [];
+
+    /**
+     * @var array Statically cached bookmark's tags counts
+     */
+    protected $tags;
+
     /**
      * GET /admin/shaare - Displays the bookmark form for creation.
      *                     Note that if the URL is found in existing bookmarks, then it will be in edit mode.
@@ -35,6 +46,9 @@ class ShaarePublishController extends ShaarliAdminController
 
         $links = [];
         foreach ($urls as $url) {
+            if (empty($url)) {
+                continue;
+            }
             $link = $this->buildLinkDataFromUrl($request, $url);
             $data = $this->buildFormData($link, $link['linkIsNew'], $request);
             $data['token'] = $this->container->sessionManager->generateToken();
@@ -72,7 +86,7 @@ class ShaarePublishController extends ShaarliAdminController
             return $this->redirect($response, '/');
         }
 
-        $formatter = $this->container->formatterFactory->getFormatter('raw');
+        $formatter = $this->getFormatter('raw');
         $link = $formatter->format($bookmark);
 
         return $this->displayForm($link, false, $request, $response);
@@ -99,9 +113,13 @@ class ShaarePublishController extends ShaarliAdminController
         $bookmark->setDescription($request->getParam('lf_description'));
         $bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', []));
         $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN));
-        $bookmark->setTagsString($request->getParam('lf_tags'));
+        $bookmark->setTagsString(
+            $request->getParam('lf_tags'),
+            $this->container->conf->get('general.tags_separator', ' ')
+        );
 
-        if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
+        if (
+            $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
             && true !== $this->container->conf->get('general.enable_async_metadata', true)
             && $bookmark->shouldUpdateThumbnail()
         ) {
@@ -110,11 +128,11 @@ class ShaarePublishController extends ShaarliAdminController
         $this->container->bookmarkService->addOrSet($bookmark, false);
 
         // To preserve backward compatibility with 3rd parties, plugins still use arrays
-        $formatter = $this->container->formatterFactory->getFormatter('raw');
+        $formatter = $this->getFormatter('raw');
         $data = $formatter->format($bookmark);
         $this->executePageHooks('save_link', $data);
 
-        $bookmark->fromArray($data);
+        $bookmark->fromArray($data, $this->container->conf->get('general.tags_separator', ' '));
         $this->container->bookmarkService->set($bookmark);
 
         // If we are called from the bookmarklet, we must close the popup:
@@ -125,13 +143,14 @@ class ShaarePublishController extends ShaarliAdminController
         }
 
         if (!empty($request->getParam('returnurl'))) {
-            $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl'));
+            $this->container->environment['HTTP_REFERER'] = $request->getParam('returnurl');
         }
 
         return $this->redirectFromReferer(
             $request,
             $response,
-            ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
+            ['/admin/add-shaare', '/admin/shaare'],
+            ['addlink', 'post', 'edit_link'],
             $bookmark->getShortUrl()
         );
     }
@@ -151,10 +170,10 @@ class ShaarePublishController extends ShaarliAdminController
             $this->assignView($key, $value);
         }
 
-        $editLabel = false === $isNew ? t('Edit') .' ' : '';
+        $editLabel = false === $isNew ? t('Edit') . ' ' : '';
         $this->assignView(
             'pagetitle',
-            $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli')
+            $editLabel . t('Shaare') . ' - ' . $this->container->conf->get('general.title', 'Shaarli')
         );
 
         return $response->write($this->render(TemplatePage::EDIT_LINK));
@@ -177,7 +196,8 @@ class ShaarePublishController extends ShaarliAdminController
 
             // If this is an HTTP(S) link, we try go get the page to extract
             // the title (otherwise we will to straight to the edit form.)
-            if (true !== $this->container->conf->get('general.enable_async_metadata', true)
+            if (
+                true !== $this->container->conf->get('general.enable_async_metadata', true)
                 && empty($title)
                 && strpos(get_url_scheme($url) ?: '', 'http') !== false
             ) {
@@ -198,7 +218,7 @@ class ShaarePublishController extends ShaarliAdminController
             ];
         }
 
-        $formatter = $this->container->formatterFactory->getFormatter('raw');
+        $formatter = $this->getFormatter('raw');
         $link = $formatter->format($bookmark);
         $link['linkIsNew'] = false;
 
@@ -207,20 +227,48 @@ class ShaarePublishController extends ShaarliAdminController
 
     protected function buildFormData(array $link, bool $isNew, Request $request): array
     {
-        $tags = $this->container->bookmarkService->bookmarksCountPerTag();
-        if ($this->container->conf->get('formatter') === 'markdown') {
-            $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
-        }
+        $link['tags'] = $link['tags'] !== null && strlen($link['tags']) > 0
+            ? $link['tags'] . $this->container->conf->get('general.tags_separator', ' ')
+            : $link['tags']
+        ;
 
         return escape([
             'link' => $link,
             'link_is_new' => $isNew,
             'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
             'source' => $request->getParam('source') ?? '',
-            'tags' => $tags,
+            'tags' => $this->getTags(),
             'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
             'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
             'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
         ]);
     }
+
+    /**
+     * Memoize formatterFactory->getFormatter() calls.
+     */
+    protected function getFormatter(string $type): BookmarkFormatter
+    {
+        if (!array_key_exists($type, $this->formatters) || $this->formatters[$type] === null) {
+            $this->formatters[$type] = $this->container->formatterFactory->getFormatter($type);
+        }
+
+        return $this->formatters[$type];
+    }
+
+    /**
+     * Memoize bookmarkService->bookmarksCountPerTag() calls.
+     */
+    protected function getTags(): array
+    {
+        if ($this->tags === null) {
+            $this->tags = $this->container->bookmarkService->bookmarksCountPerTag();
+
+            if ($this->container->conf->get('formatter') === 'markdown') {
+                $this->tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
+            }
+        }
+
+        return $this->tags;
+    }
 }