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