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
15 * post categories and "save to file" use the same item.file table for storage.
16 * We will differentiate the different uses by wrapping categories in angle brackets
17 * and save to file categories in square brackets.
18 * 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 * Get file tags from array
69 * ex. given [music,video] return <music><video> or [music][video]
71 * @param array $array A list of tags.
72 * @param string $type Optional file type.
74 * @return string A list of file tags.
76 public static function arrayToFile(array $array, string $type = 'file')
79 if ($type == 'file') {
87 foreach ($array as $item) {
89 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
97 * Get tag list from file tags
99 * ex. given <music><video>[friends], return [music,video] or [friends]
101 * @param string $file File tags
102 * @param string $type Optional file type.
104 * @return array List of tag names.
106 public static function fileToArray(string $file, string $type = 'file')
111 if ($type == 'file') {
112 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
114 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
118 foreach ($matches as $match) {
119 $return[] = self::decode($match[1]);
127 * @brief Get file tags from list
129 * ex. given music,video return <music><video> or [music][video]
130 * @param string $list A comma delimited list of tags.
131 * @param string $type Optional file type.
133 * @return string A list of file tags.
134 * @deprecated since 2019.06 use arrayToFile() instead
136 public static function listToFile(string $list, string $type = 'file')
138 $list_array = explode(',', $list);
140 return self::arrayToFile($list_array, $type);
144 * @brief Get list from file tags
146 * ex. given <music><video>[friends], return music,video or friends
147 * @param string $file File tags
148 * @param string $type Optional file type.
150 * @return string Comma delimited list of tag names.
151 * @deprecated since 2019.06 use fileToArray() instead
153 public static function fileToList(string $file, $type = 'file')
155 return implode(',', self::fileToArray($file, $type));
159 * @brief Update file tags in PConfig
161 * @param int $uid Unique Identity.
162 * @param string $file_old Categories previously associated with an item
163 * @param string $file_new New list of categories for an item
164 * @param string $type Optional file type.
166 * @return boolean A value indicating success or failure.
169 public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
173 } elseif ($file_old == $file_new) {
177 $saved = PConfig::get($uid, 'system', 'filetags');
179 if (strlen($saved)) {
180 if ($type == 'file') {
183 $termtype = TERM_FILE;
187 $termtype = TERM_CATEGORY;
190 $filetags_updated = $saved;
192 // check for new tags to be added as filetags in pconfig
194 foreach (self::fileToArray($file_new, $type) as $tag) {
195 if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
200 $filetags_updated .= self::arrayToFile($new_tags, $type);
202 // check for deleted tags to be removed from filetags in pconfig
204 foreach (self::fileToArray($file_old, $type) as $tag) {
205 if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
206 $deleted_tags[] = $tag;
210 foreach ($deleted_tags as $key => $tag) {
211 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
213 intval(Term::OBJECT_TYPE_POST),
217 if (DBA::isResult($r)) {
218 unset($deleted_tags[$key]);
220 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
224 if ($saved != $filetags_updated) {
225 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
229 } elseif (strlen($file_new)) {
230 PConfig::set($uid, 'system', 'filetags', $file_new);
237 * @brief Add tag to file
239 * @param int $uid Unique identity.
240 * @param int $item_id Item identity.
241 * @param string $file File tag.
243 * @return boolean A value indicating success or failure.
244 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
246 public static function saveFile($uid, $item_id, $file)
252 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
253 if (DBA::isResult($item)) {
254 if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
255 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
256 Item::update($fields, ['id' => $item_id]);
259 $saved = PConfig::get($uid, 'system', 'filetags');
261 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
262 PConfig::set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
265 info(L10n::t('Item filed'));
272 * @brief Remove tag from file
274 * @param int $uid Unique identity.
275 * @param int $item_id Item identity.
276 * @param string $file File tag.
277 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
279 * @return boolean A value indicating success or failure.
280 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
282 public static function unsaveFile($uid, $item_id, $file, $cat = false)
289 $pattern = '<' . self::encode($file) . '>';
290 $termtype = Term::CATEGORY;
292 $pattern = '[' . self::encode($file) . ']';
293 $termtype = Term::FILE;
296 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
298 if (!DBA::isResult($item)) {
302 $fields = ['file' => str_replace($pattern, '', $item['file'])];
304 Item::update($fields, ['id' => $item_id]);
306 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
308 intval(Term::OBJECT_TYPE_POST),
313 if (!DBA::isResult($r)) {
314 $saved = PConfig::get($uid, 'system', 'filetags');
315 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));