FileSystemTags.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Check;
  22. use OCP\Files\Cache\ICache;
  23. use OCP\Files\IHomeStorage;
  24. use OCP\Files\Storage\IStorage;
  25. use OCP\IL10N;
  26. use OCP\SystemTag\ISystemTagManager;
  27. use OCP\SystemTag\ISystemTagObjectMapper;
  28. use OCP\SystemTag\TagNotFoundException;
  29. use OCP\WorkflowEngine\ICheck;
  30. class FileSystemTags implements ICheck {
  31. /** @var array */
  32. protected $fileIds;
  33. /** @var array */
  34. protected $fileSystemTags;
  35. /** @var IL10N */
  36. protected $l;
  37. /** @var ISystemTagManager */
  38. protected $systemTagManager;
  39. /** @var ISystemTagObjectMapper */
  40. protected $systemTagObjectMapper;
  41. /** @var IStorage */
  42. protected $storage;
  43. /** @var string */
  44. protected $path;
  45. /**
  46. * @param IL10N $l
  47. * @param ISystemTagManager $systemTagManager
  48. * @param ISystemTagObjectMapper $systemTagObjectMapper
  49. */
  50. public function __construct(IL10N $l, ISystemTagManager $systemTagManager, ISystemTagObjectMapper $systemTagObjectMapper) {
  51. $this->l = $l;
  52. $this->systemTagManager = $systemTagManager;
  53. $this->systemTagObjectMapper = $systemTagObjectMapper;
  54. }
  55. /**
  56. * @param IStorage $storage
  57. * @param string $path
  58. */
  59. public function setFileInfo(IStorage $storage, $path) {
  60. $this->storage = $storage;
  61. $this->path = $path;
  62. }
  63. /**
  64. * @param string $operator
  65. * @param string $value
  66. * @return bool
  67. */
  68. public function executeCheck($operator, $value) {
  69. $systemTags = $this->getSystemTags();
  70. return ($operator === 'is') === in_array($value, $systemTags);
  71. }
  72. /**
  73. * @param string $operator
  74. * @param string $value
  75. * @throws \UnexpectedValueException
  76. */
  77. public function validateCheck($operator, $value) {
  78. if (!in_array($operator, ['is', '!is'])) {
  79. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  80. }
  81. try {
  82. $this->systemTagManager->getTagsByIds($value);
  83. } catch (TagNotFoundException $e) {
  84. throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2);
  85. } catch (\InvalidArgumentException $e) {
  86. throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3);
  87. }
  88. }
  89. /**
  90. * Get the ids of the assigned system tags
  91. * @return string[]
  92. */
  93. protected function getSystemTags() {
  94. $cache = $this->storage->getCache();
  95. $fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class));
  96. $systemTags = [];
  97. foreach ($fileIds as $i => $fileId) {
  98. if (isset($this->fileSystemTags[$fileId])) {
  99. $systemTags[] = $this->fileSystemTags[$fileId];
  100. unset($fileIds[$i]);
  101. }
  102. }
  103. if (!empty($fileIds)) {
  104. $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files');
  105. foreach ($mappedSystemTags as $fileId => $fileSystemTags) {
  106. $this->fileSystemTags[$fileId] = $fileSystemTags;
  107. $systemTags[] = $fileSystemTags;
  108. }
  109. }
  110. $systemTags = call_user_func_array('array_merge', $systemTags);
  111. $systemTags = array_unique($systemTags);
  112. return $systemTags;
  113. }
  114. /**
  115. * Get the file ids of the given path and its parents
  116. * @param ICache $cache
  117. * @param string $path
  118. * @param bool $isExternalStorage
  119. * @return int[]
  120. */
  121. protected function getFileIds(ICache $cache, $path, $isExternalStorage) {
  122. $cacheId = $cache->getNumericStorageId();
  123. if (isset($this->fileIds[$cacheId][$path])) {
  124. return $this->fileIds[$cacheId][$path];
  125. }
  126. $parentIds = [];
  127. if ($path !== $this->dirname($path)) {
  128. $parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage);
  129. } else if (!$isExternalStorage) {
  130. return [];
  131. }
  132. $fileId = $cache->getId($path);
  133. if ($fileId !== -1) {
  134. $parentIds[] = $cache->getId($path);
  135. }
  136. $this->fileIds[$cacheId][$path] = $parentIds;
  137. return $parentIds;
  138. }
  139. protected function dirname($path) {
  140. $dir = dirname($path);
  141. return $dir === '.' ? '' : $dir;
  142. }
  143. }