@@ -33,137 +33,137 @@ |
||
| 33 | 33 | |
| 34 | 34 | class FileSystemTags implements ICheck { |
| 35 | 35 | |
| 36 | - /** @var array */ |
|
| 37 | - protected $fileIds; |
|
| 38 | - |
|
| 39 | - /** @var array */ |
|
| 40 | - protected $fileSystemTags; |
|
| 41 | - |
|
| 42 | - /** @var IL10N */ |
|
| 43 | - protected $l; |
|
| 44 | - |
|
| 45 | - /** @var ISystemTagManager */ |
|
| 46 | - protected $systemTagManager; |
|
| 47 | - |
|
| 48 | - /** @var ISystemTagObjectMapper */ |
|
| 49 | - protected $systemTagObjectMapper; |
|
| 50 | - |
|
| 51 | - /** @var IStorage */ |
|
| 52 | - protected $storage; |
|
| 53 | - |
|
| 54 | - /** @var string */ |
|
| 55 | - protected $path; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param IL10N $l |
|
| 59 | - * @param ISystemTagManager $systemTagManager |
|
| 60 | - * @param ISystemTagObjectMapper $systemTagObjectMapper |
|
| 61 | - */ |
|
| 62 | - public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) { |
|
| 63 | - $this->l = $l; |
|
| 64 | - $this->systemTagManager = $systemTagManager; |
|
| 65 | - $this->systemTagObjectMapper = $systemTagObjectMapper; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param IStorage $storage |
|
| 70 | - * @param string $path |
|
| 71 | - */ |
|
| 72 | - public function setFileInfo(IStorage $storage, $path) { |
|
| 73 | - $this->storage = $storage; |
|
| 74 | - $this->path = $path; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $operator |
|
| 79 | - * @param string $value |
|
| 80 | - * @return bool |
|
| 81 | - */ |
|
| 82 | - public function executeCheck($operator, $value) { |
|
| 83 | - $systemTags = $this->getSystemTags(); |
|
| 84 | - return ($operator === 'is') === in_array($value, $systemTags); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param string $operator |
|
| 89 | - * @param string $value |
|
| 90 | - * @throws \UnexpectedValueException |
|
| 91 | - */ |
|
| 92 | - public function validateCheck($operator, $value) { |
|
| 93 | - if (!in_array($operator, ['is', '!is'])) { |
|
| 94 | - throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - try { |
|
| 98 | - $this->systemTagManager->getTagsByIds($value); |
|
| 99 | - } catch (TagNotFoundException $e) { |
|
| 100 | - throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2); |
|
| 101 | - } catch (\InvalidArgumentException $e) { |
|
| 102 | - throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get the ids of the assigned system tags |
|
| 108 | - * @return string[] |
|
| 109 | - */ |
|
| 110 | - protected function getSystemTags() { |
|
| 111 | - $cache = $this->storage->getCache(); |
|
| 112 | - $fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class)); |
|
| 113 | - |
|
| 114 | - $systemTags = []; |
|
| 115 | - foreach ($fileIds as $i => $fileId) { |
|
| 116 | - if (isset($this->fileSystemTags[$fileId])) { |
|
| 117 | - $systemTags[] = $this->fileSystemTags[$fileId]; |
|
| 118 | - unset($fileIds[$i]); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if (!empty($fileIds)) { |
|
| 123 | - $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files'); |
|
| 124 | - foreach ($mappedSystemTags as $fileId => $fileSystemTags) { |
|
| 125 | - $this->fileSystemTags[$fileId] = $fileSystemTags; |
|
| 126 | - $systemTags[] = $fileSystemTags; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $systemTags = call_user_func_array('array_merge', $systemTags); |
|
| 131 | - $systemTags = array_unique($systemTags); |
|
| 132 | - return $systemTags; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Get the file ids of the given path and its parents |
|
| 137 | - * @param ICache $cache |
|
| 138 | - * @param string $path |
|
| 139 | - * @param bool $isExternalStorage |
|
| 140 | - * @return int[] |
|
| 141 | - */ |
|
| 142 | - protected function getFileIds(ICache $cache, $path, $isExternalStorage) { |
|
| 143 | - $cacheId = $cache->getNumericStorageId(); |
|
| 144 | - if (isset($this->fileIds[$cacheId][$path])) { |
|
| 145 | - return $this->fileIds[$cacheId][$path]; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $parentIds = []; |
|
| 149 | - if ($path !== $this->dirname($path)) { |
|
| 150 | - $parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage); |
|
| 151 | - } else if (!$isExternalStorage) { |
|
| 152 | - return []; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $fileId = $cache->getId($path); |
|
| 156 | - if ($fileId !== -1) { |
|
| 157 | - $parentIds[] = $cache->getId($path); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $this->fileIds[$cacheId][$path] = $parentIds; |
|
| 161 | - |
|
| 162 | - return $parentIds; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - protected function dirname($path) { |
|
| 166 | - $dir = dirname($path); |
|
| 167 | - return $dir === '.' ? '' : $dir; |
|
| 168 | - } |
|
| 36 | + /** @var array */ |
|
| 37 | + protected $fileIds; |
|
| 38 | + |
|
| 39 | + /** @var array */ |
|
| 40 | + protected $fileSystemTags; |
|
| 41 | + |
|
| 42 | + /** @var IL10N */ |
|
| 43 | + protected $l; |
|
| 44 | + |
|
| 45 | + /** @var ISystemTagManager */ |
|
| 46 | + protected $systemTagManager; |
|
| 47 | + |
|
| 48 | + /** @var ISystemTagObjectMapper */ |
|
| 49 | + protected $systemTagObjectMapper; |
|
| 50 | + |
|
| 51 | + /** @var IStorage */ |
|
| 52 | + protected $storage; |
|
| 53 | + |
|
| 54 | + /** @var string */ |
|
| 55 | + protected $path; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param IL10N $l |
|
| 59 | + * @param ISystemTagManager $systemTagManager |
|
| 60 | + * @param ISystemTagObjectMapper $systemTagObjectMapper |
|
| 61 | + */ |
|
| 62 | + public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) { |
|
| 63 | + $this->l = $l; |
|
| 64 | + $this->systemTagManager = $systemTagManager; |
|
| 65 | + $this->systemTagObjectMapper = $systemTagObjectMapper; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param IStorage $storage |
|
| 70 | + * @param string $path |
|
| 71 | + */ |
|
| 72 | + public function setFileInfo(IStorage $storage, $path) { |
|
| 73 | + $this->storage = $storage; |
|
| 74 | + $this->path = $path; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $operator |
|
| 79 | + * @param string $value |
|
| 80 | + * @return bool |
|
| 81 | + */ |
|
| 82 | + public function executeCheck($operator, $value) { |
|
| 83 | + $systemTags = $this->getSystemTags(); |
|
| 84 | + return ($operator === 'is') === in_array($value, $systemTags); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param string $operator |
|
| 89 | + * @param string $value |
|
| 90 | + * @throws \UnexpectedValueException |
|
| 91 | + */ |
|
| 92 | + public function validateCheck($operator, $value) { |
|
| 93 | + if (!in_array($operator, ['is', '!is'])) { |
|
| 94 | + throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + try { |
|
| 98 | + $this->systemTagManager->getTagsByIds($value); |
|
| 99 | + } catch (TagNotFoundException $e) { |
|
| 100 | + throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2); |
|
| 101 | + } catch (\InvalidArgumentException $e) { |
|
| 102 | + throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get the ids of the assigned system tags |
|
| 108 | + * @return string[] |
|
| 109 | + */ |
|
| 110 | + protected function getSystemTags() { |
|
| 111 | + $cache = $this->storage->getCache(); |
|
| 112 | + $fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class)); |
|
| 113 | + |
|
| 114 | + $systemTags = []; |
|
| 115 | + foreach ($fileIds as $i => $fileId) { |
|
| 116 | + if (isset($this->fileSystemTags[$fileId])) { |
|
| 117 | + $systemTags[] = $this->fileSystemTags[$fileId]; |
|
| 118 | + unset($fileIds[$i]); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if (!empty($fileIds)) { |
|
| 123 | + $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files'); |
|
| 124 | + foreach ($mappedSystemTags as $fileId => $fileSystemTags) { |
|
| 125 | + $this->fileSystemTags[$fileId] = $fileSystemTags; |
|
| 126 | + $systemTags[] = $fileSystemTags; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $systemTags = call_user_func_array('array_merge', $systemTags); |
|
| 131 | + $systemTags = array_unique($systemTags); |
|
| 132 | + return $systemTags; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Get the file ids of the given path and its parents |
|
| 137 | + * @param ICache $cache |
|
| 138 | + * @param string $path |
|
| 139 | + * @param bool $isExternalStorage |
|
| 140 | + * @return int[] |
|
| 141 | + */ |
|
| 142 | + protected function getFileIds(ICache $cache, $path, $isExternalStorage) { |
|
| 143 | + $cacheId = $cache->getNumericStorageId(); |
|
| 144 | + if (isset($this->fileIds[$cacheId][$path])) { |
|
| 145 | + return $this->fileIds[$cacheId][$path]; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $parentIds = []; |
|
| 149 | + if ($path !== $this->dirname($path)) { |
|
| 150 | + $parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage); |
|
| 151 | + } else if (!$isExternalStorage) { |
|
| 152 | + return []; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $fileId = $cache->getId($path); |
|
| 156 | + if ($fileId !== -1) { |
|
| 157 | + $parentIds[] = $cache->getId($path); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $this->fileIds[$cacheId][$path] = $parentIds; |
|
| 161 | + |
|
| 162 | + return $parentIds; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + protected function dirname($path) { |
|
| 166 | + $dir = dirname($path); |
|
| 167 | + return $dir === '.' ? '' : $dir; |
|
| 168 | + } |
|
| 169 | 169 | } |