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\Database\DBA;
29 * This Model class handles term table interactions.
30 * This tables stores relevant terms related to posts, photos and searches, like hashtags, mentions and
31 * user-applied categories.
39 const OBJECT_TYPE_POST = 1;
42 * Generates the legacy item.file field string from an item ID.
43 * Includes only file and category terms.
49 public static function fileTextFromItemId($item_id)
53 $condition = ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]];
54 $tags = DBA::selectToArray('term', ['type', 'term', 'url'], $condition);
55 foreach ($tags as $tag) {
56 if ($tag['type'] == self::CATEGORY) {
57 $file_text .= '<' . $tag['term'] . '>';
59 $file_text .= '[' . $tag['term'] . ']';
67 * Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
68 * Deletes all previous file terms for the same item ID.
70 * @param integer $item_id item id
75 public static function insertFromFileFieldByItemId($item_id, $files)
77 $message = Item::selectFirst(['uid', 'deleted'], ['id' => $item_id]);
78 if (!DBA::isResult($message)) {
83 DBA::delete('term', ['otype' => self::OBJECT_TYPE_POST, 'oid' => $item_id, 'type' => [self::FILE, self::CATEGORY]]);
85 if ($message["deleted"]) {
89 $message['file'] = $files;
91 if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) {
92 foreach ($files[1] as $file) {
94 'uid' => $message["uid"],
96 'otype' => self::OBJECT_TYPE_POST,
103 if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) {
104 foreach ($files[1] as $file) {
105 DBA::insert('term', [
106 'uid' => $message["uid"],
108 'otype' => self::OBJECT_TYPE_POST,
109 'type' => self::CATEGORY,