1
0
Fork 0

File and category aren't using "term" anymore

This commit is contained in:
Michael 2020-05-03 15:13:40 +00:00
commit 875d6b36e9
10 changed files with 276 additions and 59 deletions

View file

@ -23,7 +23,7 @@ namespace Friendica\Model;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Term;
use Friendica\Model\Post\Category;
/**
* This class handles FileTag related functions
@ -196,11 +196,11 @@ class FileTag
if ($type == 'file') {
$lbracket = '[';
$rbracket = ']';
$termtype = Term::FILE;
$termtype = Category::FILE;
} else {
$lbracket = '<';
$rbracket = '>';
$termtype = Term::CATEGORY;
$termtype = Category::CATEGORY;
}
$filetags_updated = $saved;
@ -224,13 +224,7 @@ class FileTag
}
foreach ($deleted_tags as $key => $tag) {
$r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
DBA::escape($tag),
intval(Term::OBJECT_TYPE_POST),
intval($termtype),
intval($uid));
if (DBA::isResult($r)) {
if (DBA::exists('category-view', ['name' => $tag, 'type' => $termtype, 'uid' => $uid])) {
unset($deleted_tags[$key]);
} else {
$filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
@ -303,10 +297,10 @@ class FileTag
if ($cat == true) {
$pattern = '<' . self::encode($file) . '>';
$termtype = Term::CATEGORY;
$termtype = Category::CATEGORY;
} else {
$pattern = '[' . self::encode($file) . ']';
$termtype = Term::FILE;
$termtype = Category::FILE;
}
$item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
@ -319,14 +313,7 @@ class FileTag
Item::update($fields, ['id' => $item_id]);
$r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
DBA::escape($file),
intval(Term::OBJECT_TYPE_POST),
intval($termtype),
intval($uid)
);
if (!DBA::isResult($r)) {
if (!DBA::exists('category-view', ['name' => $file, 'type' => $termtype, 'uid' => $uid])) {
$saved = DI::pConfig()->get($uid, 'system', 'filetags');
DI::pConfig()->set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
}

View file

@ -32,6 +32,7 @@ use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Post\Category;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
@ -319,7 +320,7 @@ class Item
if (!array_key_exists('verb', $row) || in_array($row['verb'], ['', Activity::POST, Activity::SHARE])) {
// Build the file string out of the term entries
if (array_key_exists('file', $row) && empty($row['file'])) {
$row['file'] = Term::fileTextFromItemId($row['internal-iid']);
$row['file'] = Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
}
}
@ -345,7 +346,8 @@ class Item
// Remove internal fields
unset($row['internal-activity']);
unset($row['internal-network']);
unset($row['internal-iid']);
unset($row['internal-uri-id']);
unset($row['internal-uid']);
unset($row['internal-psid']);
unset($row['internal-iaid']);
unset($row['internal-user-ignored']);
@ -671,7 +673,8 @@ class Item
'resource-id', 'event-id', 'attach', 'post-type', 'file',
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global',
'id' => 'item_id', 'network', 'icid', 'iaid', 'id' => 'internal-iid',
'id' => 'item_id', 'network', 'icid', 'iaid',
'uri-id' => 'internal-uri-id', 'uid' => 'internal-uid',
'network' => 'internal-network', 'iaid' => 'internal-iaid', 'psid' => 'internal-psid'];
if ($usermode) {
@ -834,7 +837,7 @@ class Item
private static function constructSelectFields(array $fields, array $selected)
{
if (!empty($selected)) {
$selected = array_merge($selected, ['internal-iid', 'internal-psid', 'internal-iaid', 'internal-network']);
$selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-psid', 'internal-iaid', 'internal-network']);
}
if (in_array('verb', $selected)) {
@ -917,7 +920,7 @@ class Item
// We cannot simply expand the condition to check for origin entries
// The condition needn't to be a simple array but could be a complex condition.
// And we have to execute this query before the update to ensure to fetch the same data.
$items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'iaid', 'icid', 'file'], $condition);
$items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'iaid', 'icid', 'uid', 'file'], $condition);
$content_fields = [];
foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
@ -1013,7 +1016,7 @@ class Item
}
if (!is_null($files)) {
Term::insertFromFileFieldByItemId($item['id'], $files);
Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
if (!empty($item['file'])) {
DBA::update('item', ['file' => ''], ['id' => $item['id']]);
}
@ -1099,7 +1102,7 @@ class Item
{
Logger::info('Mark item for deletion by id', ['id' => $item_id, 'callstack' => System::callstack()]);
// locate item to be deleted
$fields = ['id', 'uri', 'uid', 'parent', 'parent-uri', 'origin',
$fields = ['id', 'uri', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin',
'deleted', 'file', 'resource-id', 'event-id', 'attach',
'verb', 'object-type', 'object', 'target', 'contact-id',
'icid', 'iaid', 'psid'];
@ -1172,7 +1175,7 @@ class Item
$item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
DBA::update('item', $item_fields, ['id' => $item['id']]);
Term::insertFromFileFieldByItemId($item['id'], '');
Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
self::deleteThread($item['id'], $item['parent-uri']);
if (!self::exists(["`uri` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri']])) {
@ -1949,7 +1952,7 @@ class Item
* This is not perfect - but a workable solution until we found the reason for the problem.
*/
if (!empty($files)) {
Term::insertFromFileFieldByItemId($current_post, $files);
Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
}
// In that function we check if this is a forum post. Additionally we delete the item under certain circumstances

119
src/Model/Post/Category.php Normal file
View file

@ -0,0 +1,119 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Model\Post;
use Friendica\Database\DBA;
use Friendica\Model\Item;
use Friendica\Model\Tag;
/**
* Class Category
*
* This Model class handles category table interactions.
* This tables stores user-applied categories related to posts.
*/
class Category
{
const UNKNOWN = 0;
const CATEGORY = 3;
const FILE = 5;
/**
* Generates the legacy item.file field string from an item ID.
* Includes only file and category terms.
*
* @param int $item_id
* @return string
* @throws \Exception
*/
public static function getTextByURIId(int $uri_id, int $uid)
{
$file_text = '';
$tags = DBA::selectToArray('post-category', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid]);
foreach ($tags as $tag) {
if ($tag['type'] == self::CATEGORY) {
$file_text .= '<' . $tag['term'] . '>';
} else {
$file_text .= '[' . $tag['term'] . ']';
}
}
return $file_text;
}
/**
* Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
* Deletes all previous file terms for the same item ID.
*
* @param integer $item_id item id
* @param $files
* @return void
* @throws \Exception
*/
public static function storeTextByURIId(int $uri_id, int $uid, string $files)
{
$message = Item::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
if (!DBA::isResult($message)) {
return;
}
// Clean up all tags
DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
if ($message['deleted']) {
return;
}
if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
foreach ($result[1] as $file) {
$tagid = Tag::getID($file);
if (empty($tagid)) {
continue;
}
DBA::insert('post-category', [
'uri-id' => $uri_id,
'uid' => $uid,
'type' => self::FILE,
'tid' => $tagid
]);
}
}
if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
foreach ($result[1] as $file) {
$tagid = Tag::getID($file);
if (empty($tagid)) {
continue;
}
DBA::insert('post-category', [
'uri-id' => $uri_id,
'uid' => $uid,
'type' => self::CATEGORY,
'tid' => $tagid
]);
}
}
}
}

View file

@ -124,22 +124,14 @@ class Tag
}
if (empty($cid)) {
$fields = ['name' => substr($name, 0, 96), 'url' => ''];
if (($type != self::HASHTAG) && !empty($url) && ($url != $name)) {
$fields['url'] = strtolower($url);
}
$tag = DBA::selectFirst('tag', ['id'], $fields);
if (!DBA::isResult($tag)) {
DBA::insert('tag', $fields, true);
$tagid = DBA::lastInsertId();
$url = strtolower($url);
} else {
$tagid = $tag['id'];
$url = '';
}
$tagid = self::getID($name, $url);
if (empty($tagid)) {
Logger::error('No tag id created', $fields);
return;
}
}
@ -160,6 +152,32 @@ class Tag
Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
}
/**
* Get a tag id for a given tag name and url
*
* @param string $name
* @param string $url
* @return void
*/
public static function getID(string $name, string $url = '')
{
$fields = ['name' => substr($name, 0, 96), 'url' => $url];
$tag = DBA::selectFirst('tag', ['id'], $fields);
if (DBA::isResult($tag)) {
return $tag['id'];
}
DBA::insert('tag', $fields, true);
$tid = DBA::lastInsertId();
if (!empty($tid)) {
return $tid;
}
Logger::error('No tag id created', $fields);
return 0;
}
/**
* Store tag/mention elements
*