]> git.mxchange.org Git - friendica.git/blob - src/Model/Tag.php
7ef4a2d82e220266d27e3b8a20231ba3e8e3994b
[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\Logger;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\Util\Strings;
29
30 /**
31  * Class Tag
32  *
33  * This Model class handles tag table interactions.
34  * This tables stores relevant tags related to posts, like hashtags and mentions.
35  */
36 class Tag
37 {
38         const UNKNOWN  = 0;
39         const HASHTAG  = 1;
40         const MENTION  = 2;
41         const CATEGORY = 3;
42         const FILE     = 5;
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          */
67         public static function store(int $uriid, int $type, string $name, string $url = '')
68         {
69                 $name = trim($name, "\x00..\x20\xFF#!@");
70                 if (empty($name)) {
71                         return;
72                 }
73
74                 $cid = 0;
75                 $tagid = 0;
76
77                 if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) {
78                         if (empty($url)) {
79                                 // No mention without a contact url
80                                 return;
81                         }
82
83                         Logger::info('Get ID for contact', ['url' => $url]);
84
85                         $condition = ['nurl' => Strings::normaliseLink($url), 'uid' => 0, 'deleted' => false];
86                         $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
87                         if (DBA::isResult($contact)) {
88                                 $cid = $contact['id'];
89                         } else {
90                                 // The contact wasn't found in the system (most likely some dead account)
91                                 // We ensure that we only store a single entry by overwriting the previous name
92                                 Logger::info('Update tag', ['url' => $url, 'name' => $name]);
93                                 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
94                         }
95                 }
96
97                 if (empty($cid)) {
98                         $fields = ['name' => substr($name, 0, 96), 'url' => ''];
99
100                         if (($type != Tag::HASHTAG) && !empty($url) && ($url != $name)) {
101                                 $fields['url'] = strtolower($url);
102                         }
103         
104                         $tag = DBA::selectFirst('tag', ['id'], $fields);
105                         if (!DBA::isResult($tag)) {
106                                 DBA::insert('tag', $fields, true);
107                                 $tagid = DBA::lastInsertId();
108                         } else {
109                                 $tagid = $tag['id'];
110                         }
111         
112                         if (empty($tagid)) {
113                                 Logger::error('No tag id created', $fields);
114                                 return;
115                         }
116                 }
117
118                 DBA::insert('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid], true);
119
120                 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'callstack' => System::callstack(8)]);
121         }
122
123         /**
124          * Store tag/mention elements
125          *
126          * @param integer $uriid
127          * @param string $hash
128          * @param string $name
129          * @param string $url
130          */
131         public static function storeByHash(int $uriid, string $hash, string $name, string $url = '')
132         {
133                 $type = self::getTypeForHash($hash);
134                 if ($type == self::UNKNOWN) {
135                         return;
136                 }
137
138                 self::store($uriid, $type, $name, $url);
139         }
140
141         /**
142          * Store tags and mentions from the body
143          * 
144          * @param integer $uriid URI-Id
145          * @param string  $body   Body of the post
146          * @param string  $tags   Accepted tags
147          */
148         public static function storeFromBody(int $uriid, string $body, string $tags = null)
149         {
150                 if (is_null($tags)) {
151                         $tags =  self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
152                 }
153
154                 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
155
156                 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
157                         return;
158                 }
159
160                 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
161
162                 foreach ($result as $tag) {
163                         self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]);
164                 }
165         }
166
167         /**
168          * Store raw tags (not encapsulated in links) from the body
169          * This function is needed in the intermediate phase.
170          * Later we can call item::setHashtags in advance to have all tags converted.
171          * 
172          * @param integer $uriid URI-Id
173          * @param string  $body   Body of the post
174          */
175         public static function storeRawTagsFromBody(int $uriid, string $body)
176         {
177                 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
178
179                 $result = BBCode::getTags($body);
180                 if (empty($result)) {
181                         return;
182                 }
183
184                 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
185
186                 foreach ($result as $tag) {
187                         if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
188                                 continue;
189                         }
190                         self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
191                 }
192         }
193
194         /**
195          * Remove tag/mention
196          *
197          * @param integer $uriid
198          * @param integer $type
199          * @param string $name
200          * @param string $url
201          */
202         public static function remove(int $uriid, int $type, string $name, string $url = '')
203         {
204                 $tag = DBA::fetchFirst("SELECT `id` FROM `tag` INNER JOIN `post-tag` ON `post-tag`.`tid` = `tag`.`id`
205                         WHERE `uri-id` = ? AND `type` = ? AND `name` = ? AND `url` = ?", $uriid, $type, $name, $url);
206                 if (!DBA::isResult($tag)) {
207                         return;
208                 }
209                 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['id'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
210                 DBA::delete('post-tag', ['uri-id' => $uriid, 'tid' => $tag['id']]);
211         }
212
213         /**
214          * Remove tag/mention
215          *
216          * @param integer $uriid
217          * @param string $hash
218          * @param string $name
219          * @param string $url
220          */
221         public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
222         {
223                 $type = self::getTypeForHash($hash);
224                 if ($type == self::UNKNOWN) {
225                         return;
226                 }
227
228                 self::remove($uriid, $type, $name, $url);
229         }
230
231         /**
232          * Get the type for the given hash
233          *
234          * @param string $hash
235          * @return integer type
236          */
237         private static function getTypeForHash(string $hash)
238         {
239                 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
240                         return self::MENTION;
241                 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
242                         return self::EXCLUSIVE_MENTION;
243                 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
244                         return self::IMPLICIT_MENTION;
245                 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
246                         return self::HASHTAG;
247                 } else {
248                         return self::UNKNOWN;
249                 }
250
251         }
252 }