]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/formatter/BookmarkDefaultFormatter.php
d58a5e39dde46ca5f5f0c71ac80e1f60fde8e55b
[github/shaarli/Shaarli.git] / application / formatter / BookmarkDefaultFormatter.php
1 <?php
2
3 namespace Shaarli\Formatter;
4
5 /**
6 * Class BookmarkDefaultFormatter
7 *
8 * Default bookmark formatter.
9 * Escape values for HTML display and automatically add link to URL and hashtags.
10 *
11 * @package Shaarli\Formatter
12 */
13 class BookmarkDefaultFormatter extends BookmarkFormatter
14 {
15 const SEARCH_HIGHLIGHT_OPEN = '|@@HIGHLIGHT';
16 const SEARCH_HIGHLIGHT_CLOSE = 'HIGHLIGHT@@|';
17
18 /**
19 * @inheritdoc
20 */
21 protected function formatTitle($bookmark)
22 {
23 return escape($bookmark->getTitle());
24 }
25
26 /**
27 * @inheritdoc
28 */
29 protected function formatTitleHtml($bookmark)
30 {
31 $title = $this->tokenizeSearchHighlightField(
32 $bookmark->getTitle() ?? '',
33 $bookmark->getAdditionalContentEntry('search_highlight')['title'] ?? []
34 );
35
36 return $this->replaceTokens(escape($title));
37 }
38
39 /**
40 * @inheritdoc
41 */
42 protected function formatDescription($bookmark)
43 {
44 $indexUrl = ! empty($this->contextData['index_url']) ? $this->contextData['index_url'] : '';
45 $description = $this->tokenizeSearchHighlightField(
46 $bookmark->getDescription() ?? '',
47 $bookmark->getAdditionalContentEntry('search_highlight')['description'] ?? []
48 );
49
50 return $this->replaceTokens(format_description(escape($description), $indexUrl));
51 }
52
53 /**
54 * @inheritdoc
55 */
56 protected function formatTagList($bookmark)
57 {
58 return escape(parent::formatTagList($bookmark));
59 }
60
61 /**
62 * @inheritdoc
63 */
64 protected function formatTagListHtml($bookmark)
65 {
66 if (empty($bookmark->getAdditionalContentEntry('search_highlight')['tags'])) {
67 return $this->formatTagList($bookmark);
68 }
69
70 $tags = $this->tokenizeSearchHighlightField(
71 $bookmark->getTagsString(),
72 $bookmark->getAdditionalContentEntry('search_highlight')['tags']
73 );
74 $tags = $this->filterTagList(explode(' ', $tags));
75 $tags = escape($tags);
76 $tags = $this->replaceTokensArray($tags);
77
78 return $tags;
79 }
80
81 /**
82 * @inheritdoc
83 */
84 protected function formatTagString($bookmark)
85 {
86 return implode(' ', $this->formatTagList($bookmark));
87 }
88
89 /**
90 * @inheritdoc
91 */
92 protected function formatUrl($bookmark)
93 {
94 if ($bookmark->isNote() && isset($this->contextData['index_url'])) {
95 return rtrim($this->contextData['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/'));
96 }
97
98 return escape($bookmark->getUrl());
99 }
100
101 /**
102 * @inheritdoc
103 */
104 protected function formatRealUrl($bookmark)
105 {
106 if ($bookmark->isNote()) {
107 if (isset($this->contextData['index_url'])) {
108 $prefix = rtrim($this->contextData['index_url'], '/') . '/';
109 }
110
111 if (isset($this->contextData['base_path'])) {
112 $prefix = rtrim($this->contextData['base_path'], '/') . '/';
113 }
114
115 return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/'));
116 }
117
118 return escape($bookmark->getUrl());
119 }
120
121 /**
122 * @inheritdoc
123 */
124 protected function formatUrlHtml($bookmark)
125 {
126 $url = $this->tokenizeSearchHighlightField(
127 $bookmark->getUrl() ?? '',
128 $bookmark->getAdditionalContentEntry('search_highlight')['url'] ?? []
129 );
130
131 return $this->replaceTokens(escape($url));
132 }
133
134 /**
135 * @inheritdoc
136 */
137 protected function formatThumbnail($bookmark)
138 {
139 return escape($bookmark->getThumbnail());
140 }
141
142 /**
143 * Insert search highlight token in provided field content based on a list of search result positions
144 *
145 * @param string $fieldContent
146 * @param array|null $positions List of of search results with 'start' and 'end' positions.
147 *
148 * @return string Updated $fieldContent.
149 */
150 protected function tokenizeSearchHighlightField(string $fieldContent, ?array $positions): string
151 {
152 if (empty($positions)) {
153 return $fieldContent;
154 }
155
156 $insertedTokens = 0;
157 $tokenLength = strlen(static::SEARCH_HIGHLIGHT_OPEN);
158 foreach ($positions as $position) {
159 $position = [
160 'start' => $position['start'] + ($insertedTokens * $tokenLength),
161 'end' => $position['end'] + ($insertedTokens * $tokenLength),
162 ];
163
164 $content = mb_substr($fieldContent, 0, $position['start']);
165 $content .= static::SEARCH_HIGHLIGHT_OPEN;
166 $content .= mb_substr($fieldContent, $position['start'], $position['end'] - $position['start']);
167 $content .= static::SEARCH_HIGHLIGHT_CLOSE;
168 $content .= mb_substr($fieldContent, $position['end']);
169
170 $fieldContent = $content;
171
172 $insertedTokens += 2;
173 }
174
175 return $fieldContent;
176 }
177
178 /**
179 * Replace search highlight tokens with HTML highlighted span.
180 *
181 * @param string $fieldContent
182 *
183 * @return string updated content.
184 */
185 protected function replaceTokens(string $fieldContent): string
186 {
187 return str_replace(
188 [static::SEARCH_HIGHLIGHT_OPEN, static::SEARCH_HIGHLIGHT_CLOSE],
189 ['<span class="search-highlight">', '</span>'],
190 $fieldContent
191 );
192 }
193
194 /**
195 * Apply replaceTokens to an array of content strings.
196 *
197 * @param string[] $fieldContents
198 *
199 * @return array
200 */
201 protected function replaceTokensArray(array $fieldContents): array
202 {
203 foreach ($fieldContents as &$entry) {
204 $entry = $this->replaceTokens($entry);
205 }
206
207 return $fieldContents;
208 }
209 }