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