]> git.mxchange.org Git - friendica.git/blob - src/Model/Tag.php
Use rawContent for Special Options to avoid a protected options() method
[friendica.git] / src / Model / Tag.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
30 use Friendica\Database\DBA;
31 use Friendica\DI;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\Strings;
34
35 /**
36  * Class Tag
37  *
38  * This Model class handles tag table interactions.
39  * This tables stores relevant tags related to posts, like hashtags and mentions.
40  */
41 class Tag
42 {
43         const UNKNOWN  = 0;
44         const HASHTAG  = 1;
45         const MENTION  = 2;
46         /**
47          * An implicit mention is a mention in a comment body that is redundant with the threading information.
48          */
49         const IMPLICIT_MENTION  = 8;
50         /**
51          * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
52          */
53         const EXCLUSIVE_MENTION = 9;
54
55         const TAG_CHARACTER = [
56                 self::HASHTAG           => '#',
57                 self::MENTION           => '@',
58                 self::IMPLICIT_MENTION  => '%',
59                 self::EXCLUSIVE_MENTION => '!',
60         ];
61
62         /**
63          * Store tag/mention elements
64          *
65          * @param integer $uriid
66          * @param integer $type
67          * @param string  $name
68          * @param string  $url
69          * @param boolean $probing
70          */
71         public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
72         {
73                 if ($type == self::HASHTAG) {
74                         // Trim Unicode non-word characters
75                         $name = preg_replace('/(^\W+)|(\W+$)/us', '', $name);
76
77                         $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
78                         if (count($tags) > 1) {
79                                 foreach ($tags as $tag) {
80                                         self::store($uriid, $type, $tag, $url, $probing);
81                                 }
82                                 return;
83                         }
84                 }
85
86                 if (empty($name)) {
87                         return;
88                 }
89
90                 $cid = 0;
91                 $tagid = 0;
92
93                 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
94                         if (empty($url)) {
95                                 // No mention without a contact url
96                                 return;
97                         }
98
99                         if ((substr($url, 0, 7) == 'https//') || (substr($url, 0, 6) == 'http//')) {
100                                 Logger::notice('Wrong scheme in url', ['url' => $url, 'callstack' => System::callstack(20)]);
101                         }
102
103                         if (!$probing) {
104                                 $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false];
105                                 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
106                                 if (DBA::isResult($contact)) {
107                                         $cid = $contact['id'];
108                                         Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]);
109                                 }
110
111                                 if (empty($cid)) {
112                                         $ssl_url = str_replace('http://', 'https://', $url);
113                                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0];
114                                         $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
115                                         if (DBA::isResult($contact)) {
116                                                 $cid = $contact['id'];
117                                                 Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]);
118                                         }
119                                 }
120                         } else {
121                                 $cid = Contact::getIdForURL($url, 0, false);
122                                 Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
123                         }
124
125                         if (empty($cid)) {
126                                 // The contact wasn't found in the system (most likely some dead account)
127                                 // We ensure that we only store a single entry by overwriting the previous name
128                                 Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
129                                 if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) {
130                                         DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
131                                 }
132                         }
133                 }
134
135                 if (empty($cid)) {
136                         if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
137                                 $url = strtolower($url);
138                         } else {
139                                 $url = '';
140                         }
141
142                         $tagid = self::getID($name, $url);
143                         if (empty($tagid)) {
144                                 return;
145                         }
146                 }
147
148                 $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
149
150                 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
151                         $condition = $fields;
152                         $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION];
153                         if (DBA::exists('post-tag', $condition)) {
154                                 Logger::info('Tag already exists', $fields);
155                                 return;
156                         }
157                 }
158
159                 DBA::insert('post-tag', $fields, Database::INSERT_IGNORE);
160
161                 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
162         }
163
164         /**
165          * Get a tag id for a given tag name and url
166          *
167          * @param string $name
168          * @param string $url
169          * @return void
170          */
171         public static function getID(string $name, string $url = '')
172         {
173                 $fields = ['name' => substr($name, 0, 96), 'url' => $url];
174
175                 $tag = DBA::selectFirst('tag', ['id'], $fields);
176                 if (DBA::isResult($tag)) {
177                         return $tag['id'];
178                 }
179
180                 DBA::insert('tag', $fields, Database::INSERT_IGNORE);
181                 $tid = DBA::lastInsertId();
182                 if (!empty($tid)) {
183                         return $tid;
184                 }
185
186                 Logger::error('No tag id created', $fields);
187                 return 0;
188         }
189
190         /**
191          * Store tag/mention elements
192          *
193          * @param integer $uriid
194          * @param string $hash
195          * @param string $name
196          * @param string $url
197          * @param boolean $probing
198          */
199         public static function storeByHash(int $uriid, string $hash, string $name, string $url = '', $probing = true)
200         {
201                 $type = self::getTypeForHash($hash);
202                 if ($type == self::UNKNOWN) {
203                         return;
204                 }
205
206                 self::store($uriid, $type, $name, $url, $probing);
207         }
208
209         /**
210          * Store tags and mentions from the body
211          * 
212          * @param integer $uriid   URI-Id
213          * @param string  $body    Body of the post
214          * @param string  $tags    Accepted tags
215          * @param boolean $probing Perform a probing for contacts, adding them if needed
216          */
217         public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
218         {
219                 if (is_null($tags)) {
220                         $tags =  self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
221                 }
222
223                 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
224
225                 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
226                         return;
227                 }
228
229                 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
230
231                 foreach ($result as $tag) {
232                         self::storeByHash($uriid, $tag[1], $tag[3], $tag[2], $probing);
233                 }
234         }
235
236         /**
237          * Store raw tags (not encapsulated in links) from the body
238          * This function is needed in the intermediate phase.
239          * Later we can call item::setHashtags in advance to have all tags converted.
240          * 
241          * @param integer $uriid URI-Id
242          * @param string  $body   Body of the post
243          */
244         public static function storeRawTagsFromBody(int $uriid, string $body)
245         {
246                 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
247
248                 $result = BBCode::getTags($body);
249                 if (empty($result)) {
250                         return;
251                 }
252
253                 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
254
255                 foreach ($result as $tag) {
256                         if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
257                                 continue;
258                         }
259                         self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
260                 }
261         }
262
263         /**
264          * Checks for stored hashtags and mentions for the given post
265          *
266          * @param integer $uriid
267          * @return bool
268          */
269         public static function existsForPost(int $uriid)
270         {
271                 return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]]);
272         }
273
274         /**
275          * Remove tag/mention
276          *
277          * @param integer $uriid
278          * @param integer $type
279          * @param string $name
280          * @param string $url
281          */
282         public static function remove(int $uriid, int $type, string $name, string $url = '')
283         {
284                 $condition = ['uri-id' => $uriid, 'type' => $type, 'url' => $url];
285                 if ($type == self::HASHTAG) {
286                         $condition['name'] = $name;
287                 }
288
289                 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
290                 if (!DBA::isResult($tag)) {
291                         return;
292                 }
293
294                 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
295                 DBA::delete('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
296         }
297
298         /**
299          * Remove tag/mention
300          *
301          * @param integer $uriid
302          * @param string $hash
303          * @param string $name
304          * @param string $url
305          */
306         public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
307         {
308                 $type = self::getTypeForHash($hash);
309                 if ($type == self::UNKNOWN) {
310                         return;
311                 }
312
313                 self::remove($uriid, $type, $name, $url);
314         }
315
316         /**
317          * Get the type for the given hash
318          *
319          * @param string $hash
320          * @return integer type
321          */
322         private static function getTypeForHash(string $hash)
323         {
324                 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
325                         return self::MENTION;
326                 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
327                         return self::EXCLUSIVE_MENTION;
328                 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
329                         return self::IMPLICIT_MENTION;
330                 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
331                         return self::HASHTAG;
332                 } else {
333                         return self::UNKNOWN;
334                 }
335         }
336
337         /**
338          * Create implicit mentions for a given post
339          *
340          * @param integer $uri_id
341          * @param integer $parent_uri_id
342          */
343         public static function createImplicitMentions(int $uri_id, int $parent_uri_id)
344         {
345                 // Always mention the direct parent author
346                 $parent = Post::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
347                 self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
348
349                 if (DI::config()->get('system', 'disable_implicit_mentions')) {
350                         return;
351                 }
352
353                 $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id]);
354                 while ($tag = DBA::fetch($tags)) {
355                         self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']);
356                 }
357                 DBA::close($tags);
358         }
359
360         /**
361          * Retrieves the terms from the provided type(s) associated with the provided item ID.
362          *
363          * @param int       $item_id
364          * @param int|array $type
365          * @return array
366          * @throws \Exception
367          */
368         public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
369         {
370                 $condition = ['uri-id' => $uri_id, 'type' => $type];
371                 return DBA::selectToArray('tag-view', ['type', 'name', 'url'], $condition);
372         }
373
374         /**
375          * Return a string with all tags and mentions
376          *
377          * @param integer $uri_id
378          * @param array   $type
379          * @return string tags and mentions
380          * @throws \Exception
381          */
382         public static function getCSVByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
383         {
384                 $tag_list = [];
385                 $tags = self::getByURIId($uri_id, $type);
386                 foreach ($tags as $tag) {
387                         $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['name'] . '[/url]';
388                 }
389
390                 return implode(',', $tag_list);
391         }
392
393         /**
394          * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
395          * provided item's body with them.
396          *
397          * @param array $item
398          * @return array
399          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
400          * @throws \ImagickException
401          */
402         public static function populateFromItem(&$item)
403         {
404                 $return = [
405                         'tags' => [],
406                         'hashtags' => [],
407                         'mentions' => [],
408                         'implicit_mentions' => [],
409                 ];
410
411                 $searchpath = DI::baseUrl() . "/search?tag=";
412
413                 $taglist = DBA::select('tag-view', ['type', 'name', 'url', 'cid'],
414                         ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
415                 while ($tag = DBA::fetch($taglist)) {
416                         if ($tag['url'] == '') {
417                                 $tag['url'] = $searchpath . rawurlencode($tag['name']);
418                         }
419
420                         $orig_tag = $tag['url'];
421
422                         $prefix = self::TAG_CHARACTER[$tag['type']];
423                         switch($tag['type']) {
424                                 case self::HASHTAG:
425                                         if ($orig_tag != $tag['url']) {
426                                                 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
427                                         }
428
429                                         $return['hashtags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
430                                         $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
431                                         break;
432                                 case self::MENTION:
433                                 case self::EXCLUSIVE_MENTION:
434                                         if (!empty($tag['cid'])) {
435                                                 $tag['url'] = Contact::magicLinkById($tag['cid']);
436                                         } else {
437                                                 $tag['url'] = Contact::magicLink($tag['url']);
438                                         }
439                                         $return['mentions'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
440                                         $return['tags'][] = '<bdi>' . $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a></bdi>';
441                                         break;
442                                 case self::IMPLICIT_MENTION:
443                                         $return['implicit_mentions'][] = $prefix . $tag['name'];
444                                         break;
445                         }
446                 }
447                 DBA::close($taglist);
448
449                 return $return;
450         }
451
452         /**
453          * Counts posts for given tag
454          *
455          * @param string $search
456          * @param integer $uid
457          * @return integer number of posts
458          */
459         public static function countByTag(string $search, int $uid = 0)
460         {
461                 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
462                         AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
463                         $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
464
465                 return DBA::count('tag-search-view', $condition);
466         }
467
468         /**
469          * Search posts for given tag
470          *
471          * @param string $search
472          * @param integer $uid
473          * @param integer $start
474          * @param integer $limit
475          * @param integer $last_uriid
476          * @return array with URI-ID
477          */
478         public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
479         {
480                 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
481                         AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
482                         $search, 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
483
484                 if (!empty($last_uriid)) {
485                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
486                 }
487
488                 $params = [
489                         'order' => ['uri-id' => true],
490                         'limit' => [$start, $limit]
491                 ];
492
493                 $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
494
495                 $uriids = [];
496                 while ($tag = DBA::fetch($tags)) {
497                         $uriids[] = $tag['uri-id'];
498                 }
499                 DBA::close($tags);
500
501                 return $uriids;
502         }
503
504         /**
505          * Returns a list of the most frequent global hashtags over the given period
506          *
507          * @param int $period Period in hours to consider posts
508          * @param int $limit  Number of returned tags
509          * @return array
510          * @throws \Exception
511          */
512         public static function getGlobalTrendingHashtags(int $period, $limit = 10)
513         {
514                 $tags = DI::cache()->get('global_trending_tags-' . $period . '-' . $limit);
515                 if (!empty($tags)) {
516                         return $tags;
517                 } else {
518                         return self::setGlobalTrendingHashtags($period, $limit);
519                 }
520         }
521
522         /**
523          * Fetch the blocked tags as SQL
524          *
525          * @return string 
526          */
527         private static function getBlockedSQL()
528         {
529                 $blocked_txt = DI::config()->get('system', 'blocked_tags');
530                 if (empty($blocked_txt)) {
531                         return '';
532                 }
533
534                 $blocked = explode(',', $blocked_txt);
535                 array_walk($blocked, function(&$value) { $value = "'" . DBA::escape(trim($value)) . "'";});
536                 return " AND NOT `name` IN (" . implode(',', $blocked) . ")";
537         }
538
539         /**
540          * Creates a list of the most frequent global hashtags over the given period
541          *
542          * @param int $period Period in hours to consider posts
543          * @param int $limit  Number of returned tags
544          * @return array
545          * @throws \Exception
546          */
547         public static function setGlobalTrendingHashtags(int $period, int $limit = 10)
548         {
549                 // Get a uri-id that is at least X hours old.
550                 // We use the uri-id in the query for the hash tags since this is much faster
551                 $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
552                         ['order' => ['received' => true]]);
553                 if (empty($post['uri-id'])) {
554                         return [];
555                 }
556
557                 $block_sql = self::getBlockedSQL();
558
559                 $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
560                         FROM `tag-search-view`
561                         WHERE `private` = ? AND `uid` = ? AND `uri-id` > ? $block_sql
562                         GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
563                         Item::PUBLIC, 0, $post['uri-id'], $limit);
564
565                 if (DBA::isResult($tagsStmt)) {
566                         $tags = DBA::toArray($tagsStmt);
567                         DI::cache()->set('global_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
568                         return $tags;
569                 }
570
571                 return [];
572         }
573
574         /**
575          * Returns a list of the most frequent local hashtags over the given period
576          *
577          * @param int $period Period in hours to consider posts
578          * @param int $limit  Number of returned tags
579          * @return array
580          * @throws \Exception
581          */
582         public static function getLocalTrendingHashtags(int $period, $limit = 10)
583         {
584                 $tags = DI::cache()->get('local_trending_tags-' . $period . '-' . $limit);
585                 if (!empty($tags)) {
586                         return $tags;
587                 } else {
588                         return self::setLocalTrendingHashtags($period, $limit);
589                 }
590         }
591
592         /**
593          * Returns a list of the most frequent local hashtags over the given period
594          *
595          * @param int $period Period in hours to consider posts
596          * @param int $limit  Number of returned tags
597          * @return array
598          * @throws \Exception
599          */
600         public static function setLocalTrendingHashtags(int $period, int $limit = 10)
601         {
602                 // Get a uri-id that is at least X hours old.
603                 // We use the uri-id in the query for the hash tags since this is much faster
604                 $post = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - ' . $period . ' hour')],
605                         ['order' => ['received' => true]]);
606                 if (empty($post['uri-id'])) {
607                         return [];
608                 }
609
610                 $block_sql = self::getBlockedSQL();
611
612                 $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`, COUNT(DISTINCT(`author-id`)) as `authors`
613                         FROM `tag-search-view`
614                         WHERE `private` = ? AND `wall` AND `origin` AND `uri-id` > ? $block_sql
615                         GROUP BY `term` ORDER BY `authors` DESC, `score` DESC LIMIT ?",
616                         Item::PUBLIC, $post['uri-id'], $limit);
617
618                 if (DBA::isResult($tagsStmt)) {
619                         $tags = DBA::toArray($tagsStmt);
620                         DI::cache()->set('local_trending_tags-' . $period . '-' . $limit, $tags, Duration::DAY);
621                         return $tags;
622                 }
623
624                 return [];
625         }
626
627         /**
628          * Check if the provided tag is of one of the provided term types.
629          *
630          * @param string $tag
631          * @param int    ...$types
632          * @return bool
633          */
634         public static function isType($tag, ...$types)
635         {
636                 $tag_chars = [];
637                 foreach ($types as $type) {
638                         if (array_key_exists($type, self::TAG_CHARACTER)) {
639                                 $tag_chars[] = self::TAG_CHARACTER[$type];
640                         }
641                 }
642
643                 return Strings::startsWithChars($tag, $tag_chars);
644         }
645
646         /**
647          * Fetch user who subscribed to the given tag
648          *
649          * @param string $tag
650          * @return array User list
651          */
652         private static function getUIDListByTag(string $tag)
653         {
654                 $uids = [];
655                 $searches = DBA::select('search', ['uid'], ['term' => $tag]);
656                 while ($search = DBA::fetch($searches)) {
657                         $uids[] = $search['uid'];
658                 }
659                 DBA::close($searches);
660
661                 return $uids;
662         }
663
664         /**
665          * Fetch user who subscribed to the tags of the given item
666          *
667          * @param integer $uri_id
668          * @return array User list
669          */
670         public static function getUIDListByURIId(int $uri_id)
671         {
672                 $uids = [];
673                 $tags = self::getByURIId($uri_id, [self::HASHTAG]);
674
675                 foreach ($tags as $tag) {
676                         $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
677                 }
678
679                 return array_unique($uids);
680         }
681 }