aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-12 12:44:48 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit03340c18ead651ef9e11f883745695f2edafbae3 (patch)
tree619822bfb909f68f78f4a266a89e7249644b9d4c /application
parent8e47af2b3620c920116ec056173277c039163ec1 (diff)
downloadShaarli-03340c18ead651ef9e11f883745695f2edafbae3.tar.gz
Shaarli-03340c18ead651ef9e11f883745695f2edafbae3.tar.zst
Shaarli-03340c18ead651ef9e11f883745695f2edafbae3.zip
Slim router: handle add tag route
Diffstat (limited to 'application')
-rw-r--r--application/bookmark/LinkUtils.php2
-rw-r--r--application/container/ShaarliContainer.php1
-rw-r--r--application/formatter/BookmarkMarkdownFormatter.php4
-rw-r--r--application/front/controllers/TagController.php74
4 files changed, 78 insertions, 3 deletions
diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php
index 88379430..98d9038a 100644
--- a/application/bookmark/LinkUtils.php
+++ b/application/bookmark/LinkUtils.php
@@ -220,7 +220,7 @@ function hashtag_autolink($description, $indexUrl = '')
220 * \p{Mn} - any non marking space (accents, umlauts, etc) 220 * \p{Mn} - any non marking space (accents, umlauts, etc)
221 */ 221 */
222 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; 222 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
223 $replacement = '$1<a href="'. $indexUrl .'?addtag=$2" title="Hashtag $2">#$2</a>'; 223 $replacement = '$1<a href="'. $indexUrl .'./add-tag/$2" title="Hashtag $2">#$2</a>';
224 return preg_replace($regex, $replacement, $description); 224 return preg_replace($regex, $replacement, $description);
225} 225}
226 226
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
index af62e574..3995f669 100644
--- a/application/container/ShaarliContainer.php
+++ b/application/container/ShaarliContainer.php
@@ -18,6 +18,7 @@ use Slim\Container;
18/** 18/**
19 * Extension of Slim container to document the injected objects. 19 * Extension of Slim container to document the injected objects.
20 * 20 *
21 * @property mixed[] $environment $_SERVER automatically injected by Slim
21 * @property ConfigManager $conf 22 * @property ConfigManager $conf
22 * @property SessionManager $sessionManager 23 * @property SessionManager $sessionManager
23 * @property LoginManager $loginManager 24 * @property LoginManager $loginManager
diff --git a/application/formatter/BookmarkMarkdownFormatter.php b/application/formatter/BookmarkMarkdownFormatter.php
index 077e5312..5d244d4c 100644
--- a/application/formatter/BookmarkMarkdownFormatter.php
+++ b/application/formatter/BookmarkMarkdownFormatter.php
@@ -114,7 +114,7 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter
114 114
115 /** 115 /**
116 * Replace hashtag in Markdown links format 116 * Replace hashtag in Markdown links format
117 * E.g. `#hashtag` becomes `[#hashtag](?addtag=hashtag)` 117 * E.g. `#hashtag` becomes `[#hashtag](./add-tag/hashtag)`
118 * It includes the index URL if specified. 118 * It includes the index URL if specified.
119 * 119 *
120 * @param string $description 120 * @param string $description
@@ -133,7 +133,7 @@ class BookmarkMarkdownFormatter extends BookmarkDefaultFormatter
133 * \p{Mn} - any non marking space (accents, umlauts, etc) 133 * \p{Mn} - any non marking space (accents, umlauts, etc)
134 */ 134 */
135 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui'; 135 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
136 $replacement = '$1[#$2]('. $indexUrl .'?addtag=$2)'; 136 $replacement = '$1[#$2]('. $indexUrl .'./add-tag/$2)';
137 137
138 $descriptionLines = explode(PHP_EOL, $description); 138 $descriptionLines = explode(PHP_EOL, $description);
139 $descriptionOut = ''; 139 $descriptionOut = '';
diff --git a/application/front/controllers/TagController.php b/application/front/controllers/TagController.php
new file mode 100644
index 00000000..598275b0
--- /dev/null
+++ b/application/front/controllers/TagController.php
@@ -0,0 +1,74 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagController
12 *
13 * Slim controller handle tags.
14 *
15 * @package Front\Controller
16 */
17class TagController extends ShaarliController
18{
19 /**
20 * Add another tag in the current search through an HTTP redirection.
21 *
22 * @param array $args Should contain `newTag` key as tag to add to current search
23 */
24 public function addTag(Request $request, Response $response, array $args): Response
25 {
26 $newTag = $args['newTag'] ?? null;
27 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
28
29 // In case browser does not send HTTP_REFERER, we search a single tag
30 if (null === $referer) {
31 if (null !== $newTag) {
32 return $response->withRedirect('./?searchtags='. urlencode($newTag));
33 }
34
35 return $response->withRedirect('./');
36 }
37
38 $currentUrl = parse_url($this->container->environment['HTTP_REFERER']);
39 parse_str($currentUrl['query'] ?? '', $params);
40
41 if (null === $newTag) {
42 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
43 }
44
45 // Prevent redirection loop
46 if (isset($params['addtag'])) {
47 unset($params['addtag']);
48 }
49
50 // Check if this tag is already in the search query and ignore it if it is.
51 // Each tag is always separated by a space
52 $currentTags = isset($params['searchtags']) ? explode(' ', $params['searchtags']) : [];
53
54 $addtag = true;
55 foreach ($currentTags as $value) {
56 if ($value === $newTag) {
57 $addtag = false;
58 break;
59 }
60 }
61
62 // Append the tag if necessary
63 if (true === $addtag) {
64 $currentTags[] = trim($newTag);
65 }
66
67 $params['searchtags'] = trim(implode(' ', $currentTags));
68
69 // We also remove page (keeping the same page has no sense, since the results are different)
70 unset($params['page']);
71
72 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
73 }
74}