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