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
67 public static function store(int $uriid, int $type, string $name, string $url = '')
69 $name = trim($name, "\x00..\x20\xFF#!@");
77 if (in_array($type, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION])) {
79 // No mention without a contact url
83 Logger::info('Get ID for contact', ['url' => $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'];
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]);
98 $fields = ['name' => substr($name, 0, 96), 'url' => ''];
100 if (($type != Tag::HASHTAG) && !empty($url) && ($url != $name)) {
101 $fields['url'] = strtolower($url);
104 $tag = DBA::selectFirst('tag', ['id'], $fields);
105 if (!DBA::isResult($tag)) {
106 DBA::insert('tag', $fields, true);
107 $tagid = DBA::lastInsertId();
113 Logger::error('No tag id created', $fields);
118 DBA::insert('post-tag', ['uri-id' => $uriid, 'type' => $type, 'tid' => $tagid, 'cid' => $cid], true);
120 Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'callstack' => System::callstack(8)]);
124 * Store tag/mention elements
126 * @param integer $uriid
127 * @param string $hash
128 * @param string $name
131 public static function storeByHash(int $uriid, string $hash, string $name, string $url = '')
133 $type = self::getTypeForHash($hash);
134 if ($type == self::UNKNOWN) {
138 self::store($uriid, $type, $name, $url);
142 * Store tags and mentions from the body
144 * @param integer $uriid URI-Id
145 * @param string $body Body of the post
146 * @param string $tags Accepted tags
148 public static function storeFromBody(int $uriid, string $body, string $tags = null)
150 if (is_null($tags)) {
151 $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
154 Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
156 if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
160 Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
162 foreach ($result as $tag) {
163 self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]);
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.
172 * @param integer $uriid URI-Id
173 * @param string $body Body of the post
175 public static function storeRawTagsFromBody(int $uriid, string $body)
177 Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
179 $result = BBCode::getTags($body);
180 if (empty($result)) {
184 Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
186 foreach ($result as $tag) {
187 if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
190 self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
197 * @param integer $uriid
198 * @param integer $type
199 * @param string $name
202 public static function remove(int $uriid, int $type, string $name, string $url = '')
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)) {
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']]);
216 * @param integer $uriid
217 * @param string $hash
218 * @param string $name
221 public static function removeByHash(int $uriid, string $hash, string $name, string $url = '')
223 $type = self::getTypeForHash($hash);
224 if ($type == self::UNKNOWN) {
228 self::remove($uriid, $type, $name, $url);
232 * Get the type for the given hash
234 * @param string $hash
235 * @return integer type
237 private static function getTypeForHash(string $hash)
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;
248 return self::UNKNOWN;