diff --git a/application/Utils.php b/application/Utils.php index 4b7fc546..4e97cdda 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -87,7 +87,7 @@ function endsWith($haystack, $needle, $case = true) * * @param mixed $input Data to escape: a single string or an array of strings. * - * @return string escaped. + * @return string|array escaped. */ function escape($input) { diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php new file mode 100644 index 00000000..b6f4a0ce --- /dev/null +++ b/application/front/controllers/TagCloudController.php @@ -0,0 +1,89 @@ +container->loginManager->isLoggedIn() === true) { + $visibility = $this->container->sessionManager->getSessionParameter('visibility'); + } + + $searchTags = $request->getQueryParam('searchtags'); + $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : []; + + $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null); + + // We sort tags alphabetically, then choose a font size according to count. + // First, find max value. + $maxCount = 0; + foreach ($tags as $count) { + $maxCount = max($maxCount, $count); + } + + alphabetical_sort($tags, false, true); + + $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1; + $tagList = []; + foreach ($tags as $key => $value) { + if (in_array($key, $filteringTags)) { + continue; + } + // Tag font size scaling: + // default 15 and 30 logarithm bases affect scaling, + // 2.2 and 0.8 are arbitrary font sizes in em. + $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; + $tagList[$key] = [ + 'count' => $value, + 'size' => number_format($size, 2, '.', ''), + ]; + } + + $searchTags = implode(' ', escape($filteringTags)); + $data = [ + 'search_tags' => $searchTags, + 'tags' => $tagList, + ]; + $data = $this->executeHooks($data); + foreach ($data as $key => $value) { + $this->assignView($key, $value); + } + + $searchTags = !empty($searchTags) ? $searchTags .' - ' : ''; + $this->assignView( + 'pagetitle', + $searchTags. t('Tag cloud') .' - '. $this->container->conf->get('general.title', 'Shaarli') + ); + + return $response->write($this->render('tag.cloud')); + } + + /** + * @param mixed[] $data Template data + * + * @return mixed[] Template data after active plugins render_picwall hook execution. + */ + protected function executeHooks(array $data): array + { + $this->container->pluginManager->executeHooks( + 'render_tagcloud', + $data, + ['loggedin' => $this->container->loginManager->isLoggedIn()] + ); + + return $data; + } +} diff --git a/application/security/SessionManager.php b/application/security/SessionManager.php index 994fcbe5..4ae99168 100644 --- a/application/security/SessionManager.php +++ b/application/security/SessionManager.php @@ -202,4 +202,14 @@ class SessionManager { return $this->session; } + + /** + * @param mixed $default value which will be returned if the $key is undefined + * + * @return mixed Content stored in session + */ + public function getSessionParameter(string $key, $default = null) + { + return $this->session[$key] ?? $default; + } } diff --git a/doc/md/Translations.md b/doc/md/Translations.md index dfdd021e..b8b7053f 100644 --- a/doc/md/Translations.md +++ b/doc/md/Translations.md @@ -44,7 +44,7 @@ http:///?do=import http:///login http:///picture-wall http:///?do=pluginadmin -http:///?do=tagcloud +http:///tag-cloud http:///?do=taglist ``` diff --git a/index.php b/index.php index a42c844a..83f1264f 100644 --- a/index.php +++ b/index.php @@ -616,49 +616,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM // -------- Tag cloud if ($targetPage == Router::$PAGE_TAGCLOUD) { - $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; - $filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : []; - $tags = $bookmarkService->bookmarksCountPerTag($filteringTags, $visibility); - - // We sort tags alphabetically, then choose a font size according to count. - // First, find max value. - $maxcount = 0; - foreach ($tags as $value) { - $maxcount = max($maxcount, $value); - } - - alphabetical_sort($tags, false, true); - - $logMaxCount = $maxcount > 1 ? log($maxcount, 30) : 1; - $tagList = array(); - foreach ($tags as $key => $value) { - if (in_array($key, $filteringTags)) { - continue; - } - // Tag font size scaling: - // default 15 and 30 logarithm bases affect scaling, - // 2.2 and 0.8 are arbitrary font sizes in em. - $size = log($value, 15) / $logMaxCount * 2.2 + 0.8; - $tagList[$key] = array( - 'count' => $value, - 'size' => number_format($size, 2, '.', ''), - ); - } - - $searchTags = implode(' ', escape($filteringTags)); - $data = array( - 'search_tags' => $searchTags, - 'tags' => $tagList, - ); - $pluginManager->executeHooks('render_tagcloud', $data, array('loggedin' => $loginManager->isLoggedIn())); - - foreach ($data as $key => $value) { - $PAGE->assign($key, $value); - } - - $searchTags = ! empty($searchTags) ? $searchTags .' - ' : ''; - $PAGE->assign('pagetitle', $searchTags. t('Tag cloud') .' - '. $conf->get('general.title', 'Shaarli')); - $PAGE->renderPage('tag.cloud'); + header('Location: ./tag-cloud'); exit; } @@ -1916,6 +1874,7 @@ $app->group('', function () { $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login'); $this->get('/logout', '\Shaarli\Front\Controller\LogoutController:index')->setName('logout'); $this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall'); + $this->get('/tag-cloud', '\Shaarli\Front\Controller\TagCloudController:index')->setName('tagcloud'); $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\TagController:addTag')->setName('add-tag'); })->add('\Shaarli\Front\ShaarliMiddleware'); diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html index 2086aeb0..ea89a209 100644 --- a/tpl/default/page.header.html +++ b/tpl/default/page.header.html @@ -30,7 +30,7 @@ {/if}
  • - {'Tag cloud'|t} + {'Tag cloud'|t}
  • {if="$thumbnails_enabled"}
  • diff --git a/tpl/default/tag.cloud.html b/tpl/default/tag.cloud.html index 80dcebf5..547d5018 100644 --- a/tpl/default/tag.cloud.html +++ b/tpl/default/tag.cloud.html @@ -15,7 +15,7 @@

    {'Tag cloud'|t} - {$countTags} {'tags'|t}

    {if="!empty($search_tags)"}

    - + {'List all links with those tags'|t}

    diff --git a/tpl/default/tag.sort.html b/tpl/default/tag.sort.html index 7af4723d..b7aa7d80 100644 --- a/tpl/default/tag.sort.html +++ b/tpl/default/tag.sort.html @@ -1,7 +1,7 @@
    diff --git a/tpl/vintage/page.header.html b/tpl/vintage/page.header.html index 8b9db353..971fac9a 100644 --- a/tpl/vintage/page.header.html +++ b/tpl/vintage/page.header.html @@ -31,7 +31,7 @@ {if="$showatom"}
  • ATOM Feed
  • {/if} -
  • Tag cloud
  • +
  • Tag cloud
  • Picture wall
  • Daily
  • {loop="$plugins_header.buttons_toolbar"}