]> git.mxchange.org Git - friendica.git/blob - src/Model/Tag.php
Merge pull request #8939 from MrPetovan/task/8906-frio-viewas-redesign
[friendica.git] / src / Model / Tag.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Duration;
26 use Friendica\Core\Logger;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Util\Strings;
31
32 /**
33  * Class Tag
34  *
35  * This Model class handles tag table interactions.
36  * This tables stores relevant tags related to posts, like hashtags and mentions.
37  */
38 class Tag
39 {
40         const UNKNOWN  = 0;
41         const HASHTAG  = 1;
42         const MENTION  = 2;
43         /**
44          * An implicit mention is a mention in a comment body that is redundant with the threading information.
45          */
46         const IMPLICIT_MENTION  = 8;
47         /**
48          * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
49          */
50         const EXCLUSIVE_MENTION = 9;
51
52         const TAG_CHARACTER = [
53                 self::HASHTAG           => '#',
54                 self::MENTION           => '@',
55                 self::IMPLICIT_MENTION  => '%',
56                 self::EXCLUSIVE_MENTION => '!',
57         ];
58
59         /**
60          * Store tag/mention elements
61          *
62          * @param integer $uriid
63          * @param integer $type
64          * @param string  $name
65          * @param string  $url
66          * @param boolean $probing
67          */
68         public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
69         {
70                 if ($type == self::HASHTAG) {
71                         // Remove some common "garbarge" from tags
72                         $name = trim($name, "\x00..\x20\xFF#!@,;.:'/?!^°$%".'"');
73
74                         $tags = explode(self::TAG_CHARACTER[self::HASHTAG], $name);
75                         if (count($tags) > 1) {
76                                 foreach ($tags as $tag) {
77                                         self::store($uriid, $type, $tag, $url, $probing);
78                                 }
79                                 return;
80                         }
81                 }
82
83                 if (empty($name)) {
84                         return;
85                 }
86
87                 $cid = 0;
88                 $tagid = 0;
89
90                 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
91                         if (empty($url)) {
92                                 // No mention without a contact url
93                                 return;
94                         }
95
96                         if (!$probing) {
97                                 $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false];
98                                 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
99                                 if (DBA::isResult($contact)) {
100                                         $cid = $contact['id'];
101                                         Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]);
102                                 }
103
104                                 if (empty($cid)) {
105                                         $ssl_url = str_replace('http://', 'https://', $url);
106                                         $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0];
107                                         $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
108                                         if (DBA::isResult($contact)) {
109                                                 $cid = $contact['id'];
110                                                 Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]);
111                                         }
112                                 }
113                         } else {
114                                 $cid = Contact::getIdForURL($url, 0, false);
115                                 Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
116                         }
117
118                         if (empty($cid)) {
119                                 // The contact wasn't found in the system (most likely some dead account)
120                                 // We ensure that we only store a single entry by overwriting the previous name
121                                 Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
122                                 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
123                         }
124                 }
125
126                 if (empty($cid)) {
127                         if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
128                                 $url = strtolower($url);
129                         } else {
130                                 $url = '';
131                         }
132
133                         $tagid = self::getID($name, $url);
134                         if (empty($tagid)) {
135                                 return;
136                         }
137                 }
138
139                 $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
140
141                 if (in_array($type, [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])) {
142                         $condition = $fields;
143                         $condition['type'] = [self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION];
144                         if (DBA::exists('post-tag', $condition)) {
145                                 Logger::info('Tag already exists', $fields);
146                                 return;
147                         }
148                 }
149
150                 DBA::insert('post-tag', $fields, true);
151
152                 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
153         }
154
155         /**
156          * Get a tag id for a given tag name and url
157          *
158          * @param string $name
159          * @param string $url
160          * @return void
161          */
162         public static function getID(string $name, string $url = '')
163         {
164                 $fields = ['name' => substr($name, 0, 96), 'url' => $url];
165
166                 $tag = DBA::selectFirst('tag', ['id'], $fields);
167                 if (DBA::isResult($tag)) {
168                         return $tag['id'];
169                 }
170
171                 DBA::insert('tag', $fields, true);
172                 $tid = DBA::lastInsertId();
173                 if (!empty($tid)) {
174                         return $tid;
175                 }
176
177                 Logger::error('No tag id created', $fields);
178                 return 0;
179         }
180
181         /**
182          * Store tag/mention elements
183          *
184          * @param integer $uriid
185          * @param string $hash
186          * @param string $name
187          * @param string $url
188          * @param boolean $probing
189          */
190         public static function storeByHash(int $uriid, string $hash, string $name, string $url = '', $probing = true)
191         {
192                 $type = self::getTypeForHash($hash);
193                 if ($type == self::UNKNOWN) {
194                         return;
195                 }
196
197                 self::store($uriid, $type, $name, $url, $probing);
198         }
199
200         /**
201          * Store tags and mentions from the body
202          * 
203          * @param integer $uriid   URI-Id
204          * @param string  $body    Body of the post
205          * @param string  $tags    Accepted tags
206          * @param boolean $probing Perform a probing for contacts, adding them if needed
207          */
208         public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
209         {
210                 if (is_null($tags)) {
211                         $tags =  self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
212                 }
213
214                 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
215
216                 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
217                         return;
218                 }
219
220                 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
221
222                 foreach ($result as $tag) {
223                         self::storeByHash($uriid, $tag[1], $tag[3], $tag[2], $probing);
224                 }
225         }
226
227         /**
228          * Store raw tags (not encapsulated in links) from the body
229          * This function is needed in the intermediate phase.
230          * Later we can call item::setHashtags in advance to have all tags converted.
231          * 
232          * @param integer $uriid URI-Id
233          * @param string  $body   Body of the post
234          */
235         public static function storeRawTagsFromBody(int $uriid, string $body)
236         {
237                 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
238
239                 $result = BBCode::getTags($body);
240                 if (empty($result)) {
241                         return;
242                 }
243
244                 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
245
246                 foreach ($result as $tag) {
247                         if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
248                                 continue;
249                         }
250                         self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
251                 }
252         }
253
254         /**
255          * Checks for stored hashtags and mentions for the given post
256          *
257          * @param integer $uriid
258          * @return bool
259          */
260         public static function existsForPost(int $uriid)
261         {
262                 return DBA::exists('post-tag', ['uri-id' => $uriid, 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION]]);
263         }
264
265         /**
266          * Remove tag/mention
267          *
268          * @param integer $uriid
269          * @param integer $type
270          * @param string $name
271          * @param string $url
272          */
273         public static function remove(int $uriid, int $type, string $name, string $url = '')
274         {
275                 $condition = ['uri-id' => $uriid, 'type' => $type, 'url' => $url];
276                 if ($type == self::HASHTAG) {
277                         $condition['name'] = $name;
278                 }
279
280                 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
281                 if (!DBA::isResult($tag)) {
282                         return;
283                 }
284
285                 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
286                 DBA::delete('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
287         }
288
289         /**
290          * Remove tag/mention
291          *
292          * @param integer $uriid
293          * @param string $hash
294          * @param string $name
295          * @param string $url
296          */
297         public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
298         {
299                 $type = self::getTypeForHash($hash);
300                 if ($type == self::UNKNOWN) {
301                         return;
302                 }
303
304                 self::remove($uriid, $type, $name, $url);
305         }
306
307         /**
308          * Get the type for the given hash
309          *
310          * @param string $hash
311          * @return integer type
312          */
313         private static function getTypeForHash(string $hash)
314         {
315                 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
316                         return self::MENTION;
317                 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
318                         return self::EXCLUSIVE_MENTION;
319                 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
320                         return self::IMPLICIT_MENTION;
321                 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
322                         return self::HASHTAG;
323                 } else {
324                         return self::UNKNOWN;
325                 }
326         }
327
328         /**
329          * Create implicit mentions for a given post
330          *
331          * @param integer $uri_id
332          * @param integer $parent_uri_id
333          */
334         public static function createImplicitMentions(int $uri_id, int $parent_uri_id)
335         {
336                 // Always mention the direct parent author
337                 $parent = Item::selectFirst(['author-link', 'author-name'], ['uri-id' => $parent_uri_id]);
338                 self::store($uri_id, self::IMPLICIT_MENTION, $parent['author-name'], $parent['author-link']);
339
340                 if (DI::config()->get('system', 'disable_implicit_mentions')) {
341                         return;
342                 }
343
344                 $tags = DBA::select('tag-view', ['name', 'url'], ['uri-id' => $parent_uri_id]);
345                 while ($tag = DBA::fetch($tags)) {
346                         self::store($uri_id, self::IMPLICIT_MENTION, $tag['name'], $tag['url']);
347                 }
348                 DBA::close($tags);
349         }
350
351         /**
352          * Retrieves the terms from the provided type(s) associated with the provided item ID.
353          *
354          * @param int       $item_id
355          * @param int|array $type
356          * @return array
357          * @throws \Exception
358          */
359         public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
360         {
361                 $condition = ['uri-id' => $uri_id, 'type' => $type];
362                 return DBA::selectToArray('tag-view', ['type', 'name', 'url'], $condition);
363         }
364
365         /**
366          * Return a string with all tags and mentions
367          *
368          * @param integer $uri_id
369          * @param array   $type
370          * @return string tags and mentions
371          * @throws \Exception
372          */
373         public static function getCSVByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION, self::EXCLUSIVE_MENTION])
374         {
375                 $tag_list = [];
376                 $tags = self::getByURIId($uri_id, $type);
377                 foreach ($tags as $tag) {
378                         $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['name'] . '[/url]';
379                 }
380
381                 return implode(',', $tag_list);
382         }
383
384         /**
385          * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
386          * provided item's body with them.
387          *
388          * @param array $item
389          * @return array
390          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
391          * @throws \ImagickException
392          */
393         public static function populateFromItem(&$item)
394         {
395                 $return = [
396                         'tags' => [],
397                         'hashtags' => [],
398                         'mentions' => [],
399                         'implicit_mentions' => [],
400                 ];
401
402                 $searchpath = DI::baseUrl() . "/search?tag=";
403
404                 $taglist = DBA::select('tag-view', ['type', 'name', 'url'],
405                         ['uri-id' => $item['uri-id'], 'type' => [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION]]);
406                 while ($tag = DBA::fetch($taglist)) {
407                         if ($tag['url'] == '') {
408                                 $tag['url'] = $searchpath . rawurlencode($tag['name']);
409                         }
410
411                         $orig_tag = $tag['url'];
412
413                         $prefix = self::TAG_CHARACTER[$tag['type']];
414                         switch($tag['type']) {
415                                 case self::HASHTAG:
416                                         if ($orig_tag != $tag['url']) {
417                                                 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
418                                         }
419
420                                         $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
421                                         $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
422                                         break;
423                                 case self::MENTION:
424                                 case self::EXCLUSIVE_MENTION:
425                                                 $tag['url'] = Contact::magicLink($tag['url']);
426                                         $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
427                                         $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['name']) . '</a>';
428                                         break;
429                                 case self::IMPLICIT_MENTION:
430                                         $return['implicit_mentions'][] = $prefix . $tag['name'];
431                                         break;
432                         }
433                 }
434                 DBA::close($taglist);
435
436                 return $return;
437         }
438
439         /**
440          * Search posts for given tag
441          *
442          * @param string $search
443          * @param integer $uid
444          * @param integer $start
445          * @param integer $limit
446          * @return array with URI-ID
447          */
448         public static function getURIIdListByTag(string $search, int $uid = 0, int $start = 0, int $limit = 100)
449         {
450                 $condition = ["`name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $search, $uid];
451                 $params = [
452                         'order' => ['uri-id' => true],
453                         'group_by' => ['uri-id'],
454                         'limit' => [$start, $limit]
455                 ];
456
457                 $tags = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
458
459                 $uriids = [];
460                 while ($tag = DBA::fetch($tags)) {
461                         $uriids[] = $tag['uri-id'];
462                 }
463                 DBA::close($tags);
464
465                 return $uriids;
466         }
467
468         /**
469          * Returns a list of the most frequent global hashtags over the given period
470          *
471          * @param int $period Period in hours to consider posts
472          * @return array
473          * @throws \Exception
474          */
475         public static function getGlobalTrendingHashtags(int $period, $limit = 10)
476         {
477                 $tags = DI::cache()->get('global_trending_tags');
478
479                 if (empty($tags)) {
480                         $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
481                                 FROM `tag-search-view`
482                                 WHERE `private` = ? AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
483                                 GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
484                                 Item::PUBLIC, $period, $limit);
485
486                         if (DBA::isResult($tagsStmt)) {
487                                 $tags = DBA::toArray($tagsStmt);
488                                 DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
489                         }
490                 }
491
492                 return $tags ?: [];
493         }
494
495         /**
496          * Returns a list of the most frequent local hashtags over the given period
497          *
498          * @param int $period Period in hours to consider posts
499          * @return array
500          * @throws \Exception
501          */
502         public static function getLocalTrendingHashtags(int $period, $limit = 10)
503         {
504                 $tags = DI::cache()->get('local_trending_tags');
505
506                 if (empty($tags)) {
507                         $tagsStmt = DBA::p("SELECT `name` AS `term`, COUNT(*) AS `score`
508                                 FROM `tag-search-view`
509                                 WHERE `private` = ? AND `wall` AND `origin` AND `received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
510                                 GROUP BY `term` ORDER BY `score` DESC LIMIT ?",
511                                 Item::PUBLIC, $period, $limit);
512
513                         if (DBA::isResult($tagsStmt)) {
514                                 $tags = DBA::toArray($tagsStmt);
515                                 DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
516                         }
517                 }
518
519                 return $tags ?: [];
520         }
521
522         /**
523          * Check if the provided tag is of one of the provided term types.
524          *
525          * @param string $tag
526          * @param int    ...$types
527          * @return bool
528          */
529         public static function isType($tag, ...$types)
530         {
531                 $tag_chars = [];
532                 foreach ($types as $type) {
533                         if (array_key_exists($type, self::TAG_CHARACTER)) {
534                                 $tag_chars[] = self::TAG_CHARACTER[$type];
535                         }
536                 }
537
538                 return Strings::startsWithChars($tag, $tag_chars);
539         }
540
541         /**
542          * Fetch user who subscribed to the given tag
543          *
544          * @param string $tag
545          * @return array User list
546          */
547         private static function getUIDListByTag(string $tag)
548         {
549                 $uids = [];
550                 $searches = DBA::select('search', ['uid'], ['term' => $tag]);
551                 while ($search = DBA::fetch($searches)) {
552                         $uids[] = $search['uid'];
553                 }
554                 DBA::close($searches);
555
556                 return $uids;
557         }
558
559         /**
560          * Fetch user who subscribed to the tags of the given item
561          *
562          * @param integer $uri_id
563          * @return array User list
564          */
565         public static function getUIDListByURIId(int $uri_id)
566         {
567                 $uids = [];
568                 $tags = self::getByURIId($uri_id, [self::HASHTAG]);
569
570                 foreach ($tags as $tag) {
571                         $uids = array_merge($uids, self::getUIDListByTag(self::TAG_CHARACTER[self::HASHTAG] . $tag['name']));
572                 }
573
574                 return array_unique($uids);
575         }
576 }