mirror of
https://github.com/friendica/friendica
synced 2025-04-27 21:10:11 +00:00
Replace legacy file/category handling
This commit is contained in:
parent
0668b2dfd5
commit
d2ea3eabfb
14 changed files with 103 additions and 253 deletions
|
@ -22,8 +22,6 @@
|
|||
namespace Friendica\Model;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Post\Category;
|
||||
|
||||
/**
|
||||
* This class handles FileTag related functions
|
||||
|
@ -42,7 +40,7 @@ class FileTag
|
|||
*
|
||||
* @return string The URL encoded string.
|
||||
*/
|
||||
public static function encode($s)
|
||||
private static function encode($s)
|
||||
{
|
||||
return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
|
||||
}
|
||||
|
@ -54,31 +52,11 @@ class FileTag
|
|||
*
|
||||
* @return string The decoded string.
|
||||
*/
|
||||
public static function decode($s)
|
||||
private static function decode($s)
|
||||
{
|
||||
return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query files for tag
|
||||
*
|
||||
* @param string $table The table to be queired.
|
||||
* @param string $s The search term
|
||||
* @param string $type Optional file type.
|
||||
*
|
||||
* @return string Query string.
|
||||
*/
|
||||
public static function fileQuery($table, $s, $type = 'file')
|
||||
{
|
||||
if ($type == 'file') {
|
||||
$str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
|
||||
} else {
|
||||
$str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
|
||||
}
|
||||
|
||||
return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file tags from array
|
||||
*
|
||||
|
@ -156,93 +134,6 @@ class FileTag
|
|||
return self::arrayToFile($list_array, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list from file tags
|
||||
*
|
||||
* ex. given <music><video>[friends], return music,video or friends
|
||||
* @param string $file File tags
|
||||
* @param string $type Optional file type.
|
||||
*
|
||||
* @return string Comma delimited list of tag names.
|
||||
* @deprecated since 2019.06 use fileToArray() instead
|
||||
*/
|
||||
public static function fileToList(string $file, $type = 'file')
|
||||
{
|
||||
return implode(',', self::fileToArray($file, $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update file tags in PConfig
|
||||
*
|
||||
* @param int $uid Unique Identity.
|
||||
* @param string $file_old Categories previously associated with an item
|
||||
* @param string $file_new New list of categories for an item
|
||||
* @param string $type Optional file type.
|
||||
*
|
||||
* @return boolean A value indicating success or failure.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
|
||||
{
|
||||
if (!intval($uid)) {
|
||||
return false;
|
||||
} elseif ($file_old == $file_new) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$saved = DI::pConfig()->get($uid, 'system', 'filetags');
|
||||
|
||||
if (strlen($saved)) {
|
||||
if ($type == 'file') {
|
||||
$lbracket = '[';
|
||||
$rbracket = ']';
|
||||
$termtype = Category::FILE;
|
||||
} else {
|
||||
$lbracket = '<';
|
||||
$rbracket = '>';
|
||||
$termtype = Category::CATEGORY;
|
||||
}
|
||||
|
||||
$filetags_updated = $saved;
|
||||
|
||||
// check for new tags to be added as filetags in pconfig
|
||||
$new_tags = [];
|
||||
foreach (self::fileToArray($file_new, $type) as $tag) {
|
||||
if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
|
||||
$new_tags[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
$filetags_updated .= self::arrayToFile($new_tags, $type);
|
||||
|
||||
// check for deleted tags to be removed from filetags in pconfig
|
||||
$deleted_tags = [];
|
||||
foreach (self::fileToArray($file_old, $type) as $tag) {
|
||||
if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
|
||||
$deleted_tags[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($deleted_tags as $key => $tag) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if ($saved != $filetags_updated) {
|
||||
DI::pConfig()->set($uid, 'system', 'filetags', $filetags_updated);
|
||||
}
|
||||
|
||||
return true;
|
||||
} elseif (strlen($file_new)) {
|
||||
DI::pConfig()->set($uid, 'system', 'filetags', $file_new);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add tag to file
|
||||
*
|
||||
|
@ -259,17 +150,12 @@ class FileTag
|
|||
return false;
|
||||
}
|
||||
|
||||
$item = Post::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
|
||||
$item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $uid]);
|
||||
if (DBA::isResult($item)) {
|
||||
if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
|
||||
$fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
|
||||
Item::update($fields, ['id' => $item_id]);
|
||||
}
|
||||
$stored_file = Post\Category::getTextByURIId($item['uri-id'], $uid);
|
||||
|
||||
$saved = DI::pConfig()->get($uid, 'system', 'filetags');
|
||||
|
||||
if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
|
||||
DI::pConfig()->set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
|
||||
if (!stristr($stored_file, '[' . self::encode($file) . ']')) {
|
||||
Post\Category::storeTextByURIId($item['uri-id'], $uid, $stored_file . '[' . self::encode($file) . ']');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -295,26 +181,18 @@ class FileTag
|
|||
|
||||
if ($cat == true) {
|
||||
$pattern = '<' . self::encode($file) . '>';
|
||||
$termtype = Category::CATEGORY;
|
||||
} else {
|
||||
$pattern = '[' . self::encode($file) . ']';
|
||||
$termtype = Category::FILE;
|
||||
}
|
||||
|
||||
$item = Post::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
|
||||
|
||||
$item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => $uid]);
|
||||
if (!DBA::isResult($item)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fields = ['file' => str_replace($pattern, '', $item['file'])];
|
||||
$file = Post\Category::getTextByURIId($item['uri-id'], $uid);
|
||||
|
||||
Item::update($fields, ['id' => $item_id]);
|
||||
|
||||
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));
|
||||
}
|
||||
Post\Category::storeTextByURIId($item['uri-id'], $uid, str_replace($pattern, '', $file));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ class Item
|
|||
const DISPLAY_FIELDLIST = [
|
||||
'uid', 'id', 'parent', 'uri-id', 'uri', 'thr-parent', 'parent-uri', 'guid', 'network', 'gravity',
|
||||
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
|
||||
'wall', 'private', 'starred', 'origin', 'title', 'body', 'file', 'language',
|
||||
'wall', 'private', 'starred', 'origin', 'title', 'body', 'language',
|
||||
'content-warning', 'location', 'coord', 'app', 'rendered-hash', 'rendered-html', 'object',
|
||||
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
|
||||
'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
|
||||
|
@ -112,7 +112,7 @@ class Item
|
|||
'contact-id', 'type', 'wall', 'gravity', 'extid', 'psid',
|
||||
'created', 'edited', 'commented', 'received', 'changed', 'verb',
|
||||
'postopts', 'plink', 'resource-id', 'event-id', 'inform',
|
||||
'file', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
|
||||
'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'post-type',
|
||||
'private', 'pubmail', 'moderated', 'visible', 'starred', 'bookmark',
|
||||
'unseen', 'deleted', 'origin', 'forum_mode', 'mention', 'global', 'network',
|
||||
'title', 'content-warning', 'body', 'location', 'coord', 'app',
|
||||
|
@ -203,17 +203,13 @@ 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', 'uid', 'file'], $condition);
|
||||
$items = DBA::select('item', ['id', 'origin', 'uri', 'uri-id', 'uid'], $condition);
|
||||
|
||||
$content_fields = [];
|
||||
foreach (array_merge(self::CONTENT_FIELDLIST, self::MIXED_CONTENT_FIELDLIST) as $field) {
|
||||
if (isset($fields[$field])) {
|
||||
$content_fields[$field] = $fields[$field];
|
||||
if (in_array($field, self::CONTENT_FIELDLIST)) {
|
||||
unset($fields[$field]);
|
||||
} else {
|
||||
$fields[$field] = null;
|
||||
}
|
||||
unset($fields[$field]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -221,14 +217,12 @@ class Item
|
|||
|
||||
$clear_fields = ['bookmark', 'type', 'author-name', 'author-avatar', 'author-link', 'owner-name', 'owner-avatar', 'owner-link', 'postopts', 'inform'];
|
||||
foreach ($clear_fields as $field) {
|
||||
if (array_key_exists($field, $fields)) {
|
||||
$fields[$field] = null;
|
||||
}
|
||||
unset($fields[$field]);
|
||||
}
|
||||
|
||||
if (array_key_exists('file', $fields)) {
|
||||
$files = $fields['file'];
|
||||
$fields['file'] = null;
|
||||
unset($fields['file']);
|
||||
} else {
|
||||
$files = null;
|
||||
}
|
||||
|
@ -269,9 +263,6 @@ class Item
|
|||
|
||||
if (!is_null($files)) {
|
||||
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], $files);
|
||||
if (!empty($item['file'])) {
|
||||
DBA::update('item', ['file' => ''], ['id' => $item['id']]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($fields['attach'])) {
|
||||
|
@ -358,7 +349,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', 'uri-id', 'uid', 'parent', 'parent-uri', 'origin',
|
||||
'deleted', 'file', 'resource-id', 'event-id',
|
||||
'deleted', 'resource-id', 'event-id',
|
||||
'verb', 'object-type', 'object', 'target', 'contact-id', 'psid', 'gravity'];
|
||||
$item = Post::selectFirst($fields, ['id' => $item_id]);
|
||||
if (!DBA::isResult($item)) {
|
||||
|
@ -377,25 +368,7 @@ class Item
|
|||
}
|
||||
|
||||
// clean up categories and tags so they don't end up as orphans
|
||||
|
||||
$matches = [];
|
||||
$cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
|
||||
|
||||
if ($cnt) {
|
||||
foreach ($matches as $mtch) {
|
||||
FileTag::unsaveFile($item['uid'], $item['id'], $mtch[1],true);
|
||||
}
|
||||
}
|
||||
|
||||
$matches = [];
|
||||
|
||||
$cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
|
||||
|
||||
if ($cnt) {
|
||||
foreach ($matches as $mtch) {
|
||||
FileTag::unsaveFile($item['uid'], $item['id'], $mtch[1],false);
|
||||
}
|
||||
}
|
||||
Post\Category::deleteByURIId($item['uri-id'], $item['uid']);
|
||||
|
||||
/*
|
||||
* If item is a link to a photo resource, nuke all the associated photos
|
||||
|
@ -2363,7 +2336,7 @@ class Item
|
|||
$condition[0] .= " AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY";
|
||||
$condition[] = $days;
|
||||
|
||||
$items = Post::select(['file', 'resource-id', 'starred', 'type', 'id', 'post-type'], $condition);
|
||||
$items = Post::select(['resource-id', 'starred', 'type', 'id', 'post-type', 'uid', 'uri-id'], $condition);
|
||||
|
||||
if (!DBA::isResult($items)) {
|
||||
return;
|
||||
|
@ -2386,8 +2359,7 @@ class Item
|
|||
|
||||
while ($item = Post::fetch($items)) {
|
||||
// don't expire filed items
|
||||
|
||||
if (strpos($item['file'], '[') !== false) {
|
||||
if (DBA::exists('post-category', ['uri-id' => $item['uri-id'], 'uid' => $item['uid'], 'type' => Post\Category::FILE])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -63,22 +63,6 @@ class Post
|
|||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
if ($row['internal-file-count'] > 0) {
|
||||
$row['file'] = Post\Category::getTextByURIId($row['internal-uri-id'], $row['internal-uid']);
|
||||
} else {
|
||||
$row['file'] = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove internal fields
|
||||
unset($row['internal-file-count']);
|
||||
unset($row['internal-uri-id']);
|
||||
unset($row['internal-uid']);
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
|
@ -213,7 +197,6 @@ class Post
|
|||
}
|
||||
}
|
||||
|
||||
$selected = array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']);
|
||||
$selected = array_unique($selected);
|
||||
|
||||
return DBA::select($view, $selected, $condition, $params);
|
||||
|
@ -267,8 +250,6 @@ class Post
|
|||
$selected = Item::DISPLAY_FIELDLIST;
|
||||
}
|
||||
|
||||
$selected = array_unique(array_merge($selected, ['internal-uri-id', 'internal-uid', 'internal-file-count']));
|
||||
|
||||
$condition = DBA::mergeConditions($condition,
|
||||
["`visible` AND NOT `deleted` AND NOT `moderated`
|
||||
AND NOT `author-blocked` AND NOT `owner-blocked`
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
namespace Friendica\Model\Post;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Model\Item;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Model\Tag;
|
||||
|
||||
|
@ -38,11 +37,25 @@ class Category
|
|||
const CATEGORY = 3;
|
||||
const FILE = 5;
|
||||
|
||||
/**
|
||||
* Delete all categories and files from a given uri-id and user
|
||||
*
|
||||
* @param int $uri_id
|
||||
* @param int $uid
|
||||
* @return boolean success
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function deleteByURIId(int $uri_id, int $uid)
|
||||
{
|
||||
return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the legacy item.file field string from an item ID.
|
||||
* Includes only file and category terms.
|
||||
*
|
||||
* @param int $item_id
|
||||
* @param int $uri_id
|
||||
* @param int $uid
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
@ -62,6 +75,58 @@ class Category
|
|||
return $file_text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array of files or categories of a given uri-id
|
||||
*
|
||||
* @param int $uid
|
||||
* @param int $type
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function getArray(int $uid, int $type)
|
||||
{
|
||||
$tags = DBA::selectToArray('category-view', ['name'], ['uid' => $uid, 'type' => $type],
|
||||
['group_by' => ['name'], 'order' => ['name']]);
|
||||
if (empty($tags)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_column($tags, 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array of files or categories of a given uri-id
|
||||
*
|
||||
* @param int $uri_id
|
||||
* @param int $uid
|
||||
* @param int $type
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
|
||||
{
|
||||
$tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
|
||||
if (empty($tags)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_column($tags, 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a comma separated list of files or categories
|
||||
*
|
||||
* @param int $uri_id
|
||||
* @param int $uid
|
||||
* @param int $type
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function getCSVByURIId(int $uri_id, int $uid, int $type)
|
||||
{
|
||||
return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue