3 * @file src/Model/FileTag.php
6 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\PConfig;
10 use Friendica\Database\DBA;
13 * @brief This class handles FileTag related functions
17 // post categories and "save to file" use the same item.file table for storage.
18 // We will differentiate the different uses by wrapping categories in angle brackets
19 // and save to file categories in square brackets.
20 // To do this we need to escape these characters if they appear in our tag.
23 * @brief URL encode <, >, left and right brackets
25 * @param string $s String to be URL encoded.
27 * @return string The URL encoded string.
29 public static function encode($s)
31 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
35 * @brief URL decode <, >, left and right brackets
37 * @param string $s The URL encoded string to be decoded
39 * @return string The decoded string.
41 public static function decode($s)
43 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
47 * @brief Query files for tag
49 * @param string $table The table to be queired.
50 * @param string $s The search term
51 * @param string $type Optional file type.
53 * @return string Query string.
55 public static function fileQuery($table, $s, $type = 'file')
57 if ($type == 'file') {
58 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
60 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
63 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
67 * @brief Get file tags from list
69 * ex. given music,video return <music><video> or [music][video]
70 * @param string $list A comma delimited list of tags.
71 * @param string $type Optional file type.
73 * @return string A list of file tags.
75 public static function listToFile($list, $type = 'file')
79 $list_array = explode(",", $list);
80 if ($type == 'file') {
88 foreach ($list_array as $item)
92 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
101 * @brief Get list from file tags
103 * ex. given <music><video>[friends], return music,video or friends
104 * @param string $file File tags
105 * @param string $type Optional file type.
107 * @return string Comma delimited list of tag names.
109 public static function fileToList($file, $type = 'file')
114 if ($type == 'file') {
115 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
117 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
122 foreach ($matches as $mtch)
129 $list .= self::decode($mtch[1]);
137 * @brief Update file tags in PConfig
139 * @param int $uid Unique Identity.
140 * @param string $file_old Categories previously associated with an item
141 * @param string $file_new New list of categories for an item
142 * @param string $type Optional file type.
144 * @return boolean A value indicating success or failure.
145 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
147 public static function updatePconfig($uid, $file_old, $file_new, $type = 'file')
151 } elseif ($file_old == $file_new) {
155 $saved = PConfig::get($uid, 'system', 'filetags');
159 if ($type == 'file') {
162 $termtype = TERM_FILE;
166 $termtype = TERM_CATEGORY;
169 $filetags_updated = $saved;
171 // check for new tags to be added as filetags in pconfig
173 $check_new_tags = explode(",", self::fileToList($file_new, $type));
175 foreach ($check_new_tags as $tag)
177 if (!stristr($saved,$lbracket . self::encode($tag) . $rbracket)) {
182 $filetags_updated .= self::listToFile(implode(",", $new_tags), $type);
184 // check for deleted tags to be removed from filetags in pconfig
186 $check_deleted_tags = explode(",", self::fileToList($file_old, $type));
188 foreach ($check_deleted_tags as $tag)
190 if (!stristr($file_new,$lbracket . self::encode($tag) . $rbracket)) {
191 $deleted_tags[] = $tag;
195 foreach ($deleted_tags as $key => $tag)
197 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
199 intval(TERM_OBJ_POST),
203 if (DBA::isResult($r)) {
204 unset($deleted_tags[$key]);
206 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
210 if ($saved != $filetags_updated)
212 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
216 } elseif (strlen($file_new)) {
217 PConfig::set($uid, 'system', 'filetags', $file_new);
224 * @brief Add tag to file
226 * @param int $uid Unique identity.
227 * @param int $item_id Item identity.
228 * @param string $file File tag.
230 * @return boolean A value indicating success or failure.
231 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
233 public static function saveFile($uid, $item_id, $file)
240 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
241 if (DBA::isResult($item))
243 if (!stristr($item['file'], '[' . self::encode($file) . ']'))
245 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
246 Item::update($fields, ['id' => $item_id]);
249 $saved = PConfig::get($uid, 'system', 'filetags');
251 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']'))
253 PConfig::set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
256 info(L10n::t('Item filed'));
263 * @brief Remove tag from file
265 * @param int $uid Unique identity.
266 * @param int $item_id Item identity.
267 * @param string $file File tag.
268 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
270 * @return boolean A value indicating success or failure.
271 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
273 public static function unsaveFile($uid, $item_id, $file, $cat = false)
281 $pattern = '<' . self::encode($file) . '>';
282 $termtype = TERM_CATEGORY;
284 $pattern = '[' . self::encode($file) . ']';
285 $termtype = TERM_FILE;
288 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
290 if (!DBA::isResult($item))
295 $fields = ['file' => str_replace($pattern, '', $item['file'])];
297 Item::update($fields, ['id' => $item_id]);
299 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
301 intval(TERM_OBJ_POST),
306 if (!DBA::isResult($r))
308 $saved = PConfig::get($uid, 'system', 'filetags');
309 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));