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;
11 use Friendica\Model\Item;
14 * @brief This class handles FileTag related functions
18 // post categories and "save to file" use the same item.file table for storage.
19 // We will differentiate the different uses by wrapping categories in angle brackets
20 // and save to file categories in square brackets.
21 // To do this we need to escape these characters if they appear in our tag.
24 * @brief URL encode <, >, left and right brackets
26 * @param string $s String to be URL encoded.
28 * @return string The URL encoded string.
30 public static function encode($s)
32 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
36 * @brief URL decode <, >, left and right brackets
38 * @param string $s The URL encoded string to be decoded
40 * @return string The decoded string.
42 public static function decode($s)
44 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
48 * @brief Query files for tag
50 * @param string $table The table to be queired.
51 * @param string $s The search term
52 * @param string $type Optional file type.
54 * @return string Query string.
56 public static function fileQuery($table, $s, $type = 'file')
58 if ($type == 'file') {
59 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
61 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
64 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
68 * @brief Get file tags from list
70 * ex. given music,video return <music><video> or [music][video]
71 * @param string $list A comma delimited list of tags.
72 * @param string $type Optional file type.
74 * @return string A list of file tags.
76 public static function listToFile($list, $type = 'file')
80 $list_array = explode(",", $list);
81 if ($type == 'file') {
89 foreach ($list_array as $item)
93 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
102 * @brief Get list from file tags
104 * ex. given <music><video>[friends], return music,video or friends
105 * @param string $file File tags
106 * @param string $type Optional file type.
108 * @return string Comma delimited list of tag names.
110 public static function fileToList($file, $type = 'file')
115 if ($type == 'file') {
116 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
118 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
123 foreach ($matches as $mtch)
130 $list .= self::decode($mtch[1]);
138 * @brief Update file tags in PConfig
140 * @param int $uid Unique Identity.
141 * @param string $file_old Categories previously associated with an item
142 * @param string $file_new New list of categories for an item
143 * @param string $type Optional file type.
145 * @return boolean A value indicating success or failure.
146 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
148 public static function updatePconfig($uid, $file_old, $file_new, $type = 'file')
152 } elseif ($file_old == $file_new) {
156 $saved = PConfig::get($uid, 'system', 'filetags');
160 if ($type == 'file') {
163 $termtype = TERM_FILE;
167 $termtype = TERM_CATEGORY;
170 $filetags_updated = $saved;
172 // check for new tags to be added as filetags in pconfig
174 $check_new_tags = explode(",", self::fileToList($file_new, $type));
176 foreach ($check_new_tags as $tag)
178 if (!stristr($saved,$lbracket . self::encode($tag) . $rbracket)) {
183 $filetags_updated .= self::listToFile(implode(",", $new_tags), $type);
185 // check for deleted tags to be removed from filetags in pconfig
187 $check_deleted_tags = explode(",", self::fileToList($file_old, $type));
189 foreach ($check_deleted_tags as $tag)
191 if (!stristr($file_new,$lbracket . self::encode($tag) . $rbracket)) {
192 $deleted_tags[] = $tag;
196 foreach ($deleted_tags as $key => $tag)
198 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
200 intval(TERM_OBJ_POST),
204 if (DBA::isResult($r)) {
205 unset($deleted_tags[$key]);
207 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
211 if ($saved != $filetags_updated)
213 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
217 } elseif (strlen($file_new)) {
218 PConfig::set($uid, 'system', 'filetags', $file_new);
225 * @brief Add tag to file
227 * @param int $uid Unique identity.
228 * @param int $item_id Item identity.
229 * @param string $file File tag.
231 * @return boolean A value indicating success or failure.
232 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
234 public static function saveFile($uid, $item_id, $file)
241 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
242 if (DBA::isResult($item))
244 if (!stristr($item['file'], '[' . self::encode($file) . ']'))
246 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
247 Item::update($fields, ['id' => $item_id]);
250 $saved = PConfig::get($uid, 'system', 'filetags');
252 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']'))
254 PConfig::set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
257 info(L10n::t('Item filed'));
264 * @brief Remove tag from file
266 * @param int $uid Unique identity.
267 * @param int $item_id Item identity.
268 * @param string $file File tag.
269 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
271 * @return boolean A value indicating success or failure.
272 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
274 public static function unsaveFile($uid, $item_id, $file, $cat = false)
282 $pattern = '<' . self::encode($file) . '>';
283 $termtype = TERM_CATEGORY;
285 $pattern = '[' . self::encode($file) . ']';
286 $termtype = TERM_FILE;
289 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
291 if (!DBA::isResult($item))
296 $fields = ['file' => str_replace($pattern, '', $item['file'])];
298 Item::update($fields, ['id' => $item_id]);
300 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
302 intval(TERM_OBJ_POST),
307 if (!DBA::isResult($r))
309 $saved = PConfig::get($uid, 'system', 'filetags');
310 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));