diff options
Diffstat (limited to 'application/LinkFilter.php')
-rw-r--r-- | application/LinkFilter.php | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/application/LinkFilter.php b/application/LinkFilter.php index e693b284..daa6d9cc 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php | |||
@@ -28,12 +28,17 @@ class LinkFilter | |||
28 | public static $FILTER_DAY = 'FILTER_DAY'; | 28 | public static $FILTER_DAY = 'FILTER_DAY'; |
29 | 29 | ||
30 | /** | 30 | /** |
31 | * @var array all available links. | 31 | * @var string Allowed characters for hashtags (regex syntax). |
32 | */ | ||
33 | public static $HASHTAG_CHARS = '\p{Pc}\p{N}\p{L}\p{Mn}'; | ||
34 | |||
35 | /** | ||
36 | * @var LinkDB all available links. | ||
32 | */ | 37 | */ |
33 | private $links; | 38 | private $links; |
34 | 39 | ||
35 | /** | 40 | /** |
36 | * @param array $links initialization. | 41 | * @param LinkDB $links initialization. |
37 | */ | 42 | */ |
38 | public function __construct($links) | 43 | public function __construct($links) |
39 | { | 44 | { |
@@ -89,18 +94,16 @@ class LinkFilter | |||
89 | private function noFilter($privateonly = false) | 94 | private function noFilter($privateonly = false) |
90 | { | 95 | { |
91 | if (! $privateonly) { | 96 | if (! $privateonly) { |
92 | krsort($this->links); | ||
93 | return $this->links; | 97 | return $this->links; |
94 | } | 98 | } |
95 | 99 | ||
96 | $out = array(); | 100 | $out = array(); |
97 | foreach ($this->links as $value) { | 101 | foreach ($this->links as $key => $value) { |
98 | if ($value['private']) { | 102 | if ($value['private']) { |
99 | $out[$value['linkdate']] = $value; | 103 | $out[$key] = $value; |
100 | } | 104 | } |
101 | } | 105 | } |
102 | 106 | ||
103 | krsort($out); | ||
104 | return $out; | 107 | return $out; |
105 | } | 108 | } |
106 | 109 | ||
@@ -116,10 +119,10 @@ class LinkFilter | |||
116 | private function filterSmallHash($smallHash) | 119 | private function filterSmallHash($smallHash) |
117 | { | 120 | { |
118 | $filtered = array(); | 121 | $filtered = array(); |
119 | foreach ($this->links as $l) { | 122 | foreach ($this->links as $key => $l) { |
120 | if ($smallHash == smallHash($l['linkdate'])) { | 123 | if ($smallHash == $l['shorturl']) { |
121 | // Yes, this is ugly and slow | 124 | // Yes, this is ugly and slow |
122 | $filtered[$l['linkdate']] = $l; | 125 | $filtered[$key] = $l; |
123 | return $filtered; | 126 | return $filtered; |
124 | } | 127 | } |
125 | } | 128 | } |
@@ -183,7 +186,7 @@ class LinkFilter | |||
183 | $keys = array('title', 'description', 'url', 'tags'); | 186 | $keys = array('title', 'description', 'url', 'tags'); |
184 | 187 | ||
185 | // Iterate over every stored link. | 188 | // Iterate over every stored link. |
186 | foreach ($this->links as $link) { | 189 | foreach ($this->links as $id => $link) { |
187 | 190 | ||
188 | // ignore non private links when 'privatonly' is on. | 191 | // ignore non private links when 'privatonly' is on. |
189 | if (! $link['private'] && $privateonly === true) { | 192 | if (! $link['private'] && $privateonly === true) { |
@@ -217,11 +220,10 @@ class LinkFilter | |||
217 | } | 220 | } |
218 | 221 | ||
219 | if ($found) { | 222 | if ($found) { |
220 | $filtered[$link['linkdate']] = $link; | 223 | $filtered[$id] = $link; |
221 | } | 224 | } |
222 | } | 225 | } |
223 | 226 | ||
224 | krsort($filtered); | ||
225 | return $filtered; | 227 | return $filtered; |
226 | } | 228 | } |
227 | 229 | ||
@@ -251,7 +253,7 @@ class LinkFilter | |||
251 | return $filtered; | 253 | return $filtered; |
252 | } | 254 | } |
253 | 255 | ||
254 | foreach ($this->links as $link) { | 256 | foreach ($this->links as $key => $link) { |
255 | // ignore non private links when 'privatonly' is on. | 257 | // ignore non private links when 'privatonly' is on. |
256 | if (! $link['private'] && $privateonly === true) { | 258 | if (! $link['private'] && $privateonly === true) { |
257 | continue; | 259 | continue; |
@@ -263,18 +265,19 @@ class LinkFilter | |||
263 | for ($i = 0 ; $i < count($searchtags) && $found; $i++) { | 265 | for ($i = 0 ; $i < count($searchtags) && $found; $i++) { |
264 | // Exclusive search, quit if tag found. | 266 | // Exclusive search, quit if tag found. |
265 | // Or, tag not found in the link, quit. | 267 | // Or, tag not found in the link, quit. |
266 | if (($searchtags[$i][0] == '-' && in_array(substr($searchtags[$i], 1), $linktags)) | 268 | if (($searchtags[$i][0] == '-' |
267 | || ($searchtags[$i][0] != '-') && ! in_array($searchtags[$i], $linktags) | 269 | && $this->searchTagAndHashTag(substr($searchtags[$i], 1), $linktags, $link['description'])) |
270 | || ($searchtags[$i][0] != '-') | ||
271 | && ! $this->searchTagAndHashTag($searchtags[$i], $linktags, $link['description']) | ||
268 | ) { | 272 | ) { |
269 | $found = false; | 273 | $found = false; |
270 | } | 274 | } |
271 | } | 275 | } |
272 | 276 | ||
273 | if ($found) { | 277 | if ($found) { |
274 | $filtered[$link['linkdate']] = $link; | 278 | $filtered[$key] = $link; |
275 | } | 279 | } |
276 | } | 280 | } |
277 | krsort($filtered); | ||
278 | return $filtered; | 281 | return $filtered; |
279 | } | 282 | } |
280 | 283 | ||
@@ -297,13 +300,36 @@ class LinkFilter | |||
297 | } | 300 | } |
298 | 301 | ||
299 | $filtered = array(); | 302 | $filtered = array(); |
300 | foreach ($this->links as $l) { | 303 | foreach ($this->links as $key => $l) { |
301 | if (startsWith($l['linkdate'], $day)) { | 304 | if ($l['created']->format('Ymd') == $day) { |
302 | $filtered[$l['linkdate']] = $l; | 305 | $filtered[$key] = $l; |
303 | } | 306 | } |
304 | } | 307 | } |
305 | ksort($filtered); | 308 | |
306 | return $filtered; | 309 | // sort by date ASC |
310 | return array_reverse($filtered, true); | ||
311 | } | ||
312 | |||
313 | /** | ||
314 | * Check if a tag is found in the taglist, or as an hashtag in the link description. | ||
315 | * | ||
316 | * @param string $tag Tag to search. | ||
317 | * @param array $taglist List of tags for the current link. | ||
318 | * @param string $description Link description. | ||
319 | * | ||
320 | * @return bool True if found, false otherwise. | ||
321 | */ | ||
322 | protected function searchTagAndHashTag($tag, $taglist, $description) | ||
323 | { | ||
324 | if (in_array($tag, $taglist)) { | ||
325 | return true; | ||
326 | } | ||
327 | |||
328 | if (preg_match('/(^| )#'. $tag .'([^'. self::$HASHTAG_CHARS .']|$)/mui', $description) > 0) { | ||
329 | return true; | ||
330 | } | ||
331 | |||
332 | return false; | ||
307 | } | 333 | } |
308 | 334 | ||
309 | /** | 335 | /** |