3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
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;
33 * This Model class handles tag table interactions.
34 * This tables stores relevant tags related to posts, like hashtags and mentions.
44 * An implicit mention is a mention in a comment body that is redundant with the threading information.
46 const IMPLICIT_MENTION = 8;
48 * An exclusive mention transfers the ownership of the post to the target account, usually a forum.
50 const EXCLUSIVE_MENTION = 9;
52 const TAG_CHARACTER = [
55 self::IMPLICIT_MENTION => '%',
56 self::EXCLUSIVE_MENTION => '!',
60 * Store tag/mention elements
62 * @param integer $uriid
63 * @param integer $type
66 * @param boolean $probing
68 public static function store(int $uriid, int $type, string $name, string $url = '', $probing = true)
70 $name = trim($name, "\x00..\x20\xFF#!@");
78 if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) {
80 // No mention without a contact url
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 Logger::info('Got id for contact url', ['cid' => $cid, 'url' => $url]);
93 $ssl_url = str_replace('http://', 'https://', $url);
94 $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, 0];
95 $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
96 if (DBA::isResult($contact)) {
97 $cid = $contact['id'];
98 Logger::info('Got id for contact alias', ['cid' => $cid, 'url' => $url]);
102 $cid = Contact::getIdForURL($url, 0, true);
103 Logger::info('Got id by probing', ['cid' => $cid, 'url' => $url]);
107 // The contact wasn't found in the system (most likely some dead account)
108 // We ensure that we only store a single entry by overwriting the previous name
109 Logger::info('Contact not found, updating tag', ['url' => $url, 'name' => $name]);
110 DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
115 $fields = ['name' => substr($name, 0, 96), 'url' => ''];
117 if (($type != Tag::HASHTAG) && !empty($url) && ($url != $name)) {
118 $fields['url'] = strtolower($url);
121 $tag = DBA::selectFirst('tag', ['id'], $fields);
122 if (!DBA::isResult($tag)) {
123 DBA::insert('tag', $fields, true);
124 $tagid = DBA::lastInsertId();
130 Logger::error('No tag id created', $fields);
135 $fields = ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid];
137 if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) {
138 $condition = $fields;
139 $condition['type'] = [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION];
140 if (DBA::exists('post-tag', $condition)) {
141 Logger::info('Tag already exists', $fields);
146 DBA::insert('post-tag', $fields, true);
148 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
152 * Store tag/mention elements
154 * @param integer $uriid
155 * @param string $hash
156 * @param string $name
158 * @param boolean $probing
160 public static function storeByHash(int $uriid, string $hash, string $name, string $url = '', $probing = true)
162 $type = self::getTypeForHash($hash);
163 if ($type == self::UNKNOWN) {
167 self::store($uriid, $type, $name, $url, $probing);
171 * Store tags and mentions from the body
173 * @param integer $uriid URI-Id
174 * @param string $body Body of the post
175 * @param string $tags Accepted tags
176 * @param boolean $probing Perform a probing for contacts, adding them if needed
178 public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
180 if (is_null($tags)) {
181 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
184 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
186 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
190 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
192 foreach ($result as $tag) {
193 self::storeByHash($uriid, $tag[1], $tag[3], $tag[2], $probing);
198 * Store raw tags (not encapsulated in links) from the body
199 * This function is needed in the intermediate phase.
200 * Later we can call item::setHashtags in advance to have all tags converted.
202 * @param integer $uriid URI-Id
203 * @param string $body Body of the post
205 public static function storeRawTagsFromBody(int $uriid, string $body)
207 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
209 $result = BBCode::getTags($body);
210 if (empty($result)) {
214 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
216 foreach ($result as $tag) {
217 if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
220 self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
227 * @param integer $uriid
228 * @param integer $type
229 * @param string $name
232 public static function remove(int $uriid, int $type, string $name, string $url = '')
234 $condition = ['uri-id' => $uriid, 'type' => $type, 'url' => $url];
235 if ($type == self::HASHTAG) {
236 $condition['name'] = $name;
239 $tag = DBA::selectFirst('tag-view', ['tid', 'cid'], $condition);
240 if (!DBA::isResult($tag)) {
244 Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['tid'], 'name' => $name, 'url' => $url, 'callstack' => System::callstack(8)]);
245 DBA::delete('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tag['tid'], 'cid' => $tag['cid']]);
251 * @param integer $uriid
252 * @param string $hash
253 * @param string $name
256 public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
258 $type = self::getTypeForHash($hash);
259 if ($type == self::UNKNOWN) {
263 self::remove($uriid, $type, $name, $url);
267 * Get the type for the given hash
269 * @param string $hash
270 * @return integer type
272 private static function getTypeForHash(string $hash)
274 if ($hash == self::TAG_CHARACTER[self::MENTION]) {
275 return self::MENTION;
276 } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) {
277 return self::EXCLUSIVE_MENTION;
278 } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) {
279 return self::IMPLICIT_MENTION;
280 } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) {
281 return self::HASHTAG;
283 return self::UNKNOWN;