Create FileTag class
create class and move functions
This commit is contained in:
parent
2f28c2ebbf
commit
3a604029eb
2 changed files with 271 additions and 197 deletions
202
include/text.php
202
include/text.php
|
@ -24,6 +24,7 @@ use Friendica\Util\Map;
|
|||
use Friendica\Util\Proxy as ProxyUtils;
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Model\FileTag;
|
||||
|
||||
require_once "include/conversation.php";
|
||||
|
||||
|
@ -1099,9 +1100,9 @@ function get_cats_and_terms($item)
|
|||
if ($cnt) {
|
||||
foreach ($matches as $mtch) {
|
||||
$categories[] = [
|
||||
'name' => xmlify(file_tag_decode($mtch[1])),
|
||||
'name' => xmlify(FileTag::decode($mtch[1])),
|
||||
'url' => "#",
|
||||
'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])):""),
|
||||
'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . xmlify(FileTag::decode($mtch[1])):""),
|
||||
'first' => $first,
|
||||
'last' => false
|
||||
];
|
||||
|
@ -1120,9 +1121,9 @@ function get_cats_and_terms($item)
|
|||
if ($cnt) {
|
||||
foreach ($matches as $mtch) {
|
||||
$folders[] = [
|
||||
'name' => xmlify(file_tag_decode($mtch[1])),
|
||||
'name' => xmlify(FileTag::decode($mtch[1])),
|
||||
'url' => "#",
|
||||
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . xmlify(file_tag_decode($mtch[1])) : ""),
|
||||
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . xmlify(FileTag::decode($mtch[1])) : ""),
|
||||
'first' => $first,
|
||||
'last' => false
|
||||
];
|
||||
|
@ -1360,199 +1361,6 @@ function item_post_type($item) {
|
|||
return L10n::t('post');
|
||||
}
|
||||
|
||||
// post categories and "save to file" use the same item.file table for storage.
|
||||
// We will differentiate the different uses by wrapping categories in angle brackets
|
||||
// and save to file categories in square brackets.
|
||||
// To do this we need to escape these characters if they appear in our tag.
|
||||
|
||||
function file_tag_encode($s) {
|
||||
return str_replace(['<','>','[',']'],['%3c','%3e','%5b','%5d'],$s);
|
||||
}
|
||||
|
||||
function file_tag_decode($s) {
|
||||
return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
|
||||
}
|
||||
|
||||
function file_tag_file_query($table,$s,$type = 'file') {
|
||||
|
||||
if ($type == 'file') {
|
||||
$str = preg_quote('[' . str_replace('%', '%%', file_tag_encode($s)) . ']');
|
||||
} else {
|
||||
$str = preg_quote('<' . str_replace('%', '%%', file_tag_encode($s)) . '>');
|
||||
}
|
||||
return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
|
||||
}
|
||||
|
||||
// ex. given music,video return <music><video> or [music][video]
|
||||
function file_tag_list_to_file($list, $type = 'file') {
|
||||
$tag_list = '';
|
||||
if (strlen($list)) {
|
||||
$list_array = explode(",",$list);
|
||||
if ($type == 'file') {
|
||||
$lbracket = '[';
|
||||
$rbracket = ']';
|
||||
} else {
|
||||
$lbracket = '<';
|
||||
$rbracket = '>';
|
||||
}
|
||||
|
||||
foreach ($list_array as $item) {
|
||||
if (strlen($item)) {
|
||||
$tag_list .= $lbracket . file_tag_encode(trim($item)) . $rbracket;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $tag_list;
|
||||
}
|
||||
|
||||
// ex. given <music><video>[friends], return music,video or friends
|
||||
function file_tag_file_to_list($file, $type = 'file') {
|
||||
$matches = false;
|
||||
$list = '';
|
||||
if ($type == 'file') {
|
||||
$cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
|
||||
} else {
|
||||
$cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
|
||||
}
|
||||
if ($cnt) {
|
||||
foreach ($matches as $mtch) {
|
||||
if (strlen($list)) {
|
||||
$list .= ',';
|
||||
}
|
||||
$list .= file_tag_decode($mtch[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
|
||||
// $file_old - categories previously associated with an item
|
||||
// $file_new - new list of categories for an item
|
||||
|
||||
if (!intval($uid)) {
|
||||
return false;
|
||||
} elseif ($file_old == $file_new) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$saved = PConfig::get($uid, 'system', 'filetags');
|
||||
if (strlen($saved)) {
|
||||
if ($type == 'file') {
|
||||
$lbracket = '[';
|
||||
$rbracket = ']';
|
||||
$termtype = TERM_FILE;
|
||||
} else {
|
||||
$lbracket = '<';
|
||||
$rbracket = '>';
|
||||
$termtype = TERM_CATEGORY;
|
||||
}
|
||||
|
||||
$filetags_updated = $saved;
|
||||
|
||||
// check for new tags to be added as filetags in pconfig
|
||||
$new_tags = [];
|
||||
$check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
|
||||
|
||||
foreach ($check_new_tags as $tag) {
|
||||
if (!stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket)) {
|
||||
$new_tags[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
$filetags_updated .= file_tag_list_to_file(implode(",",$new_tags),$type);
|
||||
|
||||
// check for deleted tags to be removed from filetags in pconfig
|
||||
$deleted_tags = [];
|
||||
$check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
|
||||
|
||||
foreach ($check_deleted_tags as $tag) {
|
||||
if (!stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket)) {
|
||||
$deleted_tags[] = $tag;
|
||||
}
|
||||
}
|
||||
|
||||
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_OBJ_POST),
|
||||
intval($termtype),
|
||||
intval($uid));
|
||||
|
||||
if (DBA::isResult($r)) {
|
||||
unset($deleted_tags[$key]);
|
||||
} else {
|
||||
$filetags_updated = str_replace($lbracket . file_tag_encode($tag) . $rbracket,'',$filetags_updated);
|
||||
}
|
||||
}
|
||||
|
||||
if ($saved != $filetags_updated) {
|
||||
PConfig::set($uid, 'system', 'filetags', $filetags_updated);
|
||||
}
|
||||
return true;
|
||||
} elseif (strlen($file_new)) {
|
||||
PConfig::set($uid, 'system', 'filetags', $file_new);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function file_tag_save_file($uid, $item_id, $file)
|
||||
{
|
||||
if (!intval($uid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
|
||||
if (DBA::isResult($item)) {
|
||||
if (!stristr($item['file'],'[' . file_tag_encode($file) . ']')) {
|
||||
$fields = ['file' => $item['file'] . '[' . file_tag_encode($file) . ']'];
|
||||
Item::update($fields, ['id' => $item_id]);
|
||||
}
|
||||
$saved = PConfig::get($uid, 'system', 'filetags');
|
||||
if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) {
|
||||
PConfig::set($uid, 'system', 'filetags', $saved . '[' . file_tag_encode($file) . ']');
|
||||
}
|
||||
info(L10n::t('Item filed'));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function file_tag_unsave_file($uid, $item_id, $file, $cat = false)
|
||||
{
|
||||
if (!intval($uid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cat == true) {
|
||||
$pattern = '<' . file_tag_encode($file) . '>' ;
|
||||
$termtype = TERM_CATEGORY;
|
||||
} else {
|
||||
$pattern = '[' . file_tag_encode($file) . ']' ;
|
||||
$termtype = TERM_FILE;
|
||||
}
|
||||
|
||||
$item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
|
||||
if (!DBA::isResult($item)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fields = ['file' => str_replace($pattern,'',$item['file'])];
|
||||
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_OBJ_POST),
|
||||
intval($termtype),
|
||||
intval($uid)
|
||||
);
|
||||
if (!DBA::isResult($r)) {
|
||||
$saved = PConfig::get($uid, 'system', 'filetags');
|
||||
PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function normalise_openid($s) {
|
||||
return trim(str_replace(['http://', 'https://'], ['', ''], $s), '/');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue