]> git.mxchange.org Git - friendica.git/blob - src/Model/Term.php
Merge pull request #8558 from annando/corrected-view
[friendica.git] / src / Model / Term.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\Core\Cache\Duration;
25 use Friendica\Core\Logger;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Util\Strings;
29
30 /**
31  * Class Term
32  *
33  * This Model class handles term table interactions.
34  * This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
35  * user-applied categories.
36  */
37 class Term
38 {
39     const UNKNOWN           = 0;
40     const HASHTAG           = 1;
41     const MENTION           = 2;
42     const CATEGORY          = 3;
43     const FILE              = 5;
44         /**
45          * An implicit mention is a mention in a comment body that is redundant with the threading information.
46          */
47     const IMPLICIT_MENTION  = 8;
48         /**
49          * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
50          */
51     const EXCLUSIVE_MENTION = 9;
52
53     const TAG_CHARACTER = [
54         self::HASHTAG           => '#',
55         self::MENTION           => '@',
56         self::IMPLICIT_MENTION  => '%',
57         self::EXCLUSIVE_MENTION => '!',
58     ];
59
60     const OBJECT_TYPE_POST  = 1;
61     const OBJECT_TYPE_PHOTO = 2;
62
63         /**
64          * Returns a list of the most frequent global hashtags over the given period
65          *
66          * @param int $period Period in hours to consider posts
67          * @return array
68          * @throws \Exception
69          */
70         public static function getGlobalTrendingHashtags(int $period, $limit = 10)
71         {
72                 $tags = DI::cache()->get('global_trending_tags');
73
74                 if (!$tags) {
75                         $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
76                                 FROM `term` t
77                                  JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
78                                  JOIN `thread` ON `thread`.`iid` = i.`id`
79                                 WHERE `thread`.`visible`
80                                   AND NOT `thread`.`deleted`
81                                   AND NOT `thread`.`moderated`
82                                   AND `thread`.`private` = ?
83                                   AND t.`uid` = 0
84                                   AND t.`otype` = ?
85                                   AND t.`type` = ?
86                                   AND t.`term` != ''
87                                   AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
88                                 GROUP BY `term`
89                                 ORDER BY `score` DESC
90                                 LIMIT ?",
91                                 Item::PUBLIC,
92                                 Term::OBJECT_TYPE_POST,
93                                 Term::HASHTAG,
94                                 $period,
95                                 $limit
96                         );
97
98                         if (DBA::isResult($tagsStmt)) {
99                                 $tags = DBA::toArray($tagsStmt);
100                                 DI::cache()->set('global_trending_tags', $tags, Duration::HOUR);
101                         }
102                 }
103
104                 return $tags ?: [];
105         }
106
107         /**
108          * Returns a list of the most frequent local hashtags over the given period
109          *
110          * @param int $period Period in hours to consider posts
111          * @return array
112          * @throws \Exception
113          */
114         public static function getLocalTrendingHashtags(int $period, $limit = 10)
115         {
116                 $tags = DI::cache()->get('local_trending_tags');
117
118                 if (!$tags) {
119                         $tagsStmt = DBA::p("SELECT t.`term`, COUNT(*) AS `score`
120                                 FROM `term` t
121                                 JOIN `item` i ON i.`id` = t.`oid` AND i.`uid` = t.`uid`
122                                 JOIN `thread` ON `thread`.`iid` = i.`id`
123                                 WHERE `thread`.`visible`
124                                   AND NOT `thread`.`deleted`
125                                   AND NOT `thread`.`moderated`
126                                   AND `thread`.`private` = ?
127                                   AND `thread`.`wall`
128                                   AND `thread`.`origin`
129                                   AND t.`otype` = ?
130                                   AND t.`type` = ?
131                                   AND t.`term` != ''
132                                   AND i.`received` > DATE_SUB(NOW(), INTERVAL ? HOUR)
133                                 GROUP BY `term`
134                                 ORDER BY `score` DESC
135                                 LIMIT ?",
136                                 Item::PUBLIC,
137                                 Term::OBJECT_TYPE_POST,
138                                 Term::HASHTAG,
139                                 $period,
140                                 $limit
141                         );
142
143                         if (DBA::isResult($tagsStmt)) {
144                                 $tags = DBA::toArray($tagsStmt);
145                                 DI::cache()->set('local_trending_tags', $tags, Duration::HOUR);
146                         }
147                 }
148
149                 return $tags ?: [];
150         }
151
152         /**
153          * Generates the legacy item.tag field comma-separated BBCode string from an item ID.
154          * Includes only hashtags, implicit and explicit mentions.
155          *
156          * @param int $item_id
157          * @return string
158          * @throws \Exception
159          */
160         public static function tagTextFromItemId($item_id)
161         {
162                 $tag_list = [];
163                 $tags = self::tagArrayFromItemId($item_id, [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]);
164                 foreach ($tags as $tag) {
165                         $tag_list[] = self::TAG_CHARACTER[$tag['type']] . '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
166                 }
167
168                 return implode(',', $tag_list);
169         }
170
171         /**
172          * Retrieves the terms from the provided type(s) associated with the provided item ID.
173          *
174          * @param int       $item_id
175          * @param int|array $type
176          * @return array
177          * @throws \Exception
178          */
179         public static function tagArrayFromItemId($item_id, $type = [self::HASHTAG, self::MENTION])
180         {
181                 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type];
182                 $tags = DBA::select('term', ['type', 'term', 'url'], $condition);
183                 if (!DBA::isResult($tags)) {
184                         return [];
185                 }
186
187                 return DBA::toArray($tags);
188         }
189
190         /**
191          * Generates the legacy item.file field string from an item ID.
192          * Includes only file and category terms.
193          *
194          * @param int $item_id
195          * @return string
196          * @throws \Exception
197          */
198         public static function fileTextFromItemId($item_id)
199         {
200                 $file_text = '';
201                 $tags = self::tagArrayFromItemId($item_id, [self::FILE, self::CATEGORY]);
202                 foreach ($tags as $tag) {
203                         if ($tag['type'] == self::CATEGORY) {
204                                 $file_text .= '<' . $tag['term'] . '>';
205                         } else {
206                                 $file_text .= '[' . $tag['term'] . ']';
207                         }
208                 }
209
210                 return $file_text;
211         }
212
213         /**
214          * Inserts new terms for the provided item ID based on the legacy item.tag field BBCode content.
215          * Deletes all previous tag terms for the same item ID.
216          * Sets both the item.mention and thread.mentions field flags if a mention concerning the item UID is found.
217          *
218          * @param int    $item_id
219          * @param string $tag_str
220          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
221          */
222         public static function insertFromTagFieldByItemId($item_id, $tag_str)
223         {
224                 $profile_base = DI::baseUrl();
225                 $profile_data = parse_url($profile_base);
226                 $profile_path = $profile_data['path'] ?? '';
227                 $profile_base_friendica = $profile_data['host'] . $profile_path . '/profile/';
228                 $profile_base_diaspora = $profile_data['host'] . $profile_path . '/u/';
229
230                 $fields = ['guid', 'uid', 'id', 'edited', 'deleted', 'created', 'received', 'title', 'body', 'parent'];
231                 $item = Item::selectFirst($fields, ['id' => $item_id]);
232                 if (!DBA::isResult($item)) {
233                         return;
234                 }
235
236                 $item['tag'] = $tag_str;
237
238                 // Clean up all tags
239                 self::deleteByItemId($item_id);
240
241                 if ($item['deleted']) {
242                         return;
243                 }
244
245                 $taglist = explode(',', $item['tag']);
246
247                 $tags_string = '';
248                 foreach ($taglist as $tag) {
249                         if (Strings::startsWith($tag, self::TAG_CHARACTER)) {
250                                 $tags_string .= ' ' . trim($tag);
251                         } else {
252                                 $tags_string .= ' #' . trim($tag);
253                         }
254                 }
255
256                 $data = ' ' . $item['title'] . ' ' . $item['body'] . ' ' . $tags_string . ' ';
257
258                 // ignore anything in a code block
259                 $data = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $data);
260
261                 $tags = [];
262
263                 $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
264                 if (preg_match_all($pattern, $data, $matches)) {
265                         foreach ($matches[1] as $match) {
266                                 $tags['#' . $match] = '';
267                         }
268                 }
269
270                 $pattern = '/\W([\#@!%])\[url\=(.*?)\](.*?)\[\/url\]/ism';
271                 if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
272                         foreach ($matches as $match) {
273
274                                 if (in_array($match[1], [
275                                         self::TAG_CHARACTER[self::MENTION],
276                                         self::TAG_CHARACTER[self::IMPLICIT_MENTION],
277                                         self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]
278                                 ])) {
279                                         $contact = Contact::getDetailsByURL($match[2], 0);
280                                         if (!empty($contact['addr'])) {
281                                                 $match[3] = $contact['addr'];
282                                         }
283
284                                         if (!empty($contact['url'])) {
285                                                 $match[2] = $contact['url'];
286                                         }
287                                 }
288
289                                 $tags[$match[2]] = $match[1] . trim($match[3], ',.:;[]/\"?!');
290                         }
291                 }
292
293                 foreach ($tags as $link => $tag) {
294                         if (self::isType($tag, self::HASHTAG)) {
295                                 // try to ignore #039 or #1 or anything like that
296                                 if (ctype_digit(substr(trim($tag), 1))) {
297                                         continue;
298                                 }
299
300                                 // try to ignore html hex escapes, e.g. #x2317
301                                 if ((substr(trim($tag), 1, 1) == 'x' || substr(trim($tag), 1, 1) == 'X') && ctype_digit(substr(trim($tag), 2))) {
302                                         continue;
303                                 }
304
305                                 $type = self::HASHTAG;
306                                 $term = substr($tag, 1);
307                                 $link = '';
308                         } elseif (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION)) {
309                                 if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)) {
310                                         $type = self::MENTION;
311                                 } else {
312                                         $type = self::IMPLICIT_MENTION;
313                                 }
314
315                                 $contact = Contact::getDetailsByURL($link, 0);
316                                 if (!empty($contact['name'])) {
317                                         $term = $contact['name'];
318                                 } else {
319                                         $term = substr($tag, 1);
320                                 }
321                         } else { // This shouldn't happen
322                                 $type = self::HASHTAG;
323                                 $term = $tag;
324                                 $link = '';
325
326                                 Logger::notice('Unknown term type', ['tag' => $tag]);
327                         }
328
329                         if (DBA::exists('term', ['uid' => $item['uid'], 'otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'term' => $term, 'type' => $type])) {
330                                 continue;
331                         }
332
333                         if (empty($term)) {
334                                 continue;
335                         }
336
337                         if ($item['uid'] == 0) {
338                                 $global = true;
339                                 DBA::update('term', ['global' => true], ['otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
340                         } else {
341                                 $global = DBA::exists('term', ['uid' => 0, 'otype' => self::OBJECT_TYPE_POST, 'guid' => $item['guid']]);
342                         }
343
344                         DBA::insert('term', [
345                                 'uid'      => $item['uid'],
346                                 'oid'      => $item_id,
347                                 'otype'    => self::OBJECT_TYPE_POST,
348                                 'type'     => $type,
349                                 'term'     => substr($term, 0, 255),
350                                 'url'      => $link,
351                                 'guid'     => $item['guid'],
352                                 'created'  => $item['created'],
353                                 'received' => $item['received'],
354                                 'global'   => $global
355                         ]);
356
357                         // Search for mentions
358                         if (self::isType($tag, self::MENTION, self::EXCLUSIVE_MENTION)
359                                 && (
360                                         strpos($link, $profile_base_friendica) !== false
361                                         || strpos($link, $profile_base_diaspora) !== false
362                                 )
363                         ) {
364                                 $users_stmt = DBA::p("SELECT `uid` FROM `contact` WHERE self AND (`url` = ? OR `nurl` = ?)", $link, $link);
365                                 $users = DBA::toArray($users_stmt);
366                                 foreach ($users AS $user) {
367                                         if ($user['uid'] == $item['uid']) {
368                                                 /// @todo This function is called from Item::update - so we mustn't call that function here
369                                                 DBA::update('item', ['mention' => true], ['id' => $item_id]);
370                                                 DBA::update('thread', ['mention' => true], ['iid' => $item['parent']]);
371                                         }
372                                 }
373                         }
374                 }
375         }
376
377         /**
378          * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
379          * Deletes all previous file terms for the same item ID.
380          *
381          * @param integer $item_id item id
382          * @param         $files
383          * @return void
384          * @throws \Exception
385          */
386         public static function insertFromFileFieldByItemId($item_id, $files)
387         {
388                 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
389                 if (!DBA::isResult($message)) {
390                         return;
391                 }
392
393                 // Clean up all tags
394                 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
395
396                 if ($message["deleted"]) {
397                         return;
398                 }
399
400                 $message['file'] = $files;
401
402                 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
403                         foreach ($files[1] as $file) {
404                                 DBA::insert('term', [
405                                         'uid' => $message["uid"],
406                                         'oid' => $item_id,
407                                         'otype' => self::OBJECT_TYPE_POST,
408                                         'type' => self::FILE,
409                                         'term' => $file
410                                 ]);
411                         }
412                 }
413
414                 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
415                         foreach ($files[1] as $file) {
416                                 DBA::insert('term', [
417                                         'uid' => $message["uid"],
418                                         'oid' => $item_id,
419                                         'otype' => self::OBJECT_TYPE_POST,
420                                         'type' => self::CATEGORY,
421                                         'term' => $file
422                                 ]);
423                         }
424                 }
425         }
426
427         /**
428          * Sorts an item's tags into mentions, hashtags and other tags. Generate personalized URLs by user and modify the
429          * provided item's body with them.
430          *
431          * @param array $item
432          * @return array
433          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
434          * @throws \ImagickException
435          */
436         public static function populateTagsFromItem(&$item)
437         {
438                 $return = [
439                         'tags' => [],
440                         'hashtags' => [],
441                         'mentions' => [],
442                         'implicit_mentions' => [],
443                 ];
444
445                 $searchpath = DI::baseUrl() . "/search?tag=";
446
447                 $taglist = DBA::select(
448                         'term',
449                         ['type', 'term', 'url'],
450                         ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item['id'], 'type' => [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION]],
451                         ['order' => ['tid']]
452                 );
453                 while ($tag = DBA::fetch($taglist)) {
454                         if ($tag['url'] == '') {
455                                 $tag['url'] = $searchpath . rawurlencode($tag['term']);
456                         }
457
458                         $orig_tag = $tag['url'];
459
460                         $prefix = self::TAG_CHARACTER[$tag['type']];
461                         switch($tag['type']) {
462                                 case self::HASHTAG:
463                                         if ($orig_tag != $tag['url']) {
464                                                 $item['body'] = str_replace($orig_tag, $tag['url'], $item['body']);
465                                         }
466
467                                         $return['hashtags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
468                                         $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
469                                         break;
470                                 case self::MENTION:
471                                         $tag['url'] = Contact::magicLink($tag['url']);
472                                         $return['mentions'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
473                                         $return['tags'][] = $prefix . '<a href="' . $tag['url'] . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($tag['term']) . '</a>';
474                                         break;
475                                 case self::IMPLICIT_MENTION:
476                                         $return['implicit_mentions'][] = $prefix . $tag['term'];
477                                         break;
478                         }
479                 }
480                 DBA::close($taglist);
481
482                 return $return;
483         }
484
485         /**
486          * Delete tags of the specific type(s) from an item
487          *
488          * @param int       $item_id
489          * @param int|array $type
490          * @throws \Exception
491          */
492         public static function deleteByItemId($item_id, $type = [self::HASHTAG, self::MENTION, self::IMPLICIT_MENTION])
493         {
494                 if (empty($item_id)) {
495                         return;
496                 }
497
498                 // Clean up all tags
499                 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => $type]);
500         }
501
502         /**
503          * Check if the provided tag is of one of the provided term types.
504          *
505          * @param string $tag
506          * @param int    ...$types
507          * @return bool
508          */
509         public static function isType($tag, ...$types)
510         {
511                 $tag_chars = [];
512                 foreach ($types as $type) {
513                         if (array_key_exists($type, self::TAG_CHARACTER)) {
514                                 $tag_chars[] = self::TAG_CHARACTER[$type];
515                         }
516                 }
517
518                 return Strings::startsWith($tag, $tag_chars);
519         }
520 }