3 * @file src/Model/FileTag.php
6 namespace Friendica\Model;
8 use Friendica\Database\DBA;
12 * This class handles FileTag related functions
14 * post categories and "save to file" use the same item.file table for storage.
15 * We will differentiate the different uses by wrapping categories in angle brackets
16 * and save to file categories in square brackets.
17 * To do this we need to escape these characters if they appear in our tag.
22 * URL encode <, >, left and right brackets
24 * @param string $s String to be URL encoded.
26 * @return string The URL encoded string.
28 public static function encode($s)
30 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
34 * URL decode <, >, left and right brackets
36 * @param string $s The URL encoded string to be decoded
38 * @return string The decoded string.
40 public static function decode($s)
42 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
48 * @param string $table The table to be queired.
49 * @param string $s The search term
50 * @param string $type Optional file type.
52 * @return string Query string.
54 public static function fileQuery($table, $s, $type = 'file')
56 if ($type == 'file') {
57 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
59 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
62 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
66 * Get file tags from array
68 * ex. given [music,video] return <music><video> or [music][video]
70 * @param array $array A list of tags.
71 * @param string $type Optional file type.
73 * @return string A list of file tags.
75 public static function arrayToFile(array $array, string $type = 'file')
78 if ($type == 'file') {
86 foreach ($array as $item) {
88 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
96 * Get tag list from file tags
98 * ex. given <music><video>[friends], return [music,video] or [friends]
100 * @param string $file File tags
101 * @param string $type Optional file type.
103 * @return array List of tag names.
105 public static function fileToArray(string $file, string $type = 'file')
110 if ($type == 'file') {
111 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
113 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
117 foreach ($matches as $match) {
118 $return[] = self::decode($match[1]);
126 * Get file tags from list
128 * ex. given music,video return <music><video> or [music][video]
129 * @param string $list A comma delimited list of tags.
130 * @param string $type Optional file type.
132 * @return string A list of file tags.
133 * @deprecated since 2019.06 use arrayToFile() instead
135 public static function listToFile(string $list, string $type = 'file')
137 $list_array = explode(',', $list);
139 return self::arrayToFile($list_array, $type);
143 * Get list from file tags
145 * ex. given <music><video>[friends], return music,video or friends
146 * @param string $file File tags
147 * @param string $type Optional file type.
149 * @return string Comma delimited list of tag names.
150 * @deprecated since 2019.06 use fileToArray() instead
152 public static function fileToList(string $file, $type = 'file')
154 return implode(',', self::fileToArray($file, $type));
158 * Update file tags in PConfig
160 * @param int $uid Unique Identity.
161 * @param string $file_old Categories previously associated with an item
162 * @param string $file_new New list of categories for an item
163 * @param string $type Optional file type.
165 * @return boolean A value indicating success or failure.
168 public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
172 } elseif ($file_old == $file_new) {
176 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
178 if (strlen($saved)) {
179 if ($type == 'file') {
182 $termtype = TERM_FILE;
186 $termtype = TERM_CATEGORY;
189 $filetags_updated = $saved;
191 // check for new tags to be added as filetags in pconfig
193 foreach (self::fileToArray($file_new, $type) as $tag) {
194 if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
199 $filetags_updated .= self::arrayToFile($new_tags, $type);
201 // check for deleted tags to be removed from filetags in pconfig
203 foreach (self::fileToArray($file_old, $type) as $tag) {
204 if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
205 $deleted_tags[] = $tag;
209 foreach ($deleted_tags as $key => $tag) {
210 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
212 intval(Term::OBJECT_TYPE_POST),
216 if (DBA::isResult($r)) {
217 unset($deleted_tags[$key]);
219 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
223 if ($saved != $filetags_updated) {
224 DI::pConfig()->set($uid, 'system', 'filetags', $filetags_updated);
228 } elseif (strlen($file_new)) {
229 DI::pConfig()->set($uid, 'system', 'filetags', $file_new);
238 * @param int $uid Unique identity.
239 * @param int $item_id Item identity.
240 * @param string $file File tag.
242 * @return boolean A value indicating success or failure.
243 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
245 public static function saveFile($uid, $item_id, $file)
251 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
252 if (DBA::isResult($item)) {
253 if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
254 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
255 Item::update($fields, ['id' => $item_id]);
258 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
260 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
261 DI::pConfig()->set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
264 info(DI::l10n()->t('Item filed'));
271 * Remove tag from file
273 * @param int $uid Unique identity.
274 * @param int $item_id Item identity.
275 * @param string $file File tag.
276 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
278 * @return boolean A value indicating success or failure.
279 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
281 public static function unsaveFile($uid, $item_id, $file, $cat = false)
288 $pattern = '<' . self::encode($file) . '>';
289 $termtype = Term::CATEGORY;
291 $pattern = '[' . self::encode($file) . ']';
292 $termtype = Term::FILE;
295 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
297 if (!DBA::isResult($item)) {
301 $fields = ['file' => str_replace($pattern, '', $item['file'])];
303 Item::update($fields, ['id' => $item_id]);
305 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
307 intval(Term::OBJECT_TYPE_POST),
312 if (!DBA::isResult($r)) {
313 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
314 DI::pConfig()->set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));