You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TagsPlugin.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Sergio Bertolín <sbertolin@solidgear.es>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Connector\Sabre;
  29. /**
  30. * ownCloud
  31. *
  32. * @author Vincent Petry
  33. * @copyright 2014 Vincent Petry <pvince81@owncloud.com>
  34. *
  35. * This library is free software; you can redistribute it and/or
  36. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  37. * License as published by the Free Software Foundation; either
  38. * version 3 of the License, or any later version.
  39. *
  40. * This library is distributed in the hope that it will be useful,
  41. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  44. *
  45. * You should have received a copy of the GNU Affero General Public
  46. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  47. *
  48. */
  49. use Sabre\DAV\PropFind;
  50. use Sabre\DAV\PropPatch;
  51. class TagsPlugin extends \Sabre\DAV\ServerPlugin {
  52. // namespace
  53. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  54. public const TAGS_PROPERTYNAME = '{http://owncloud.org/ns}tags';
  55. public const FAVORITE_PROPERTYNAME = '{http://owncloud.org/ns}favorite';
  56. public const TAG_FAVORITE = '_$!<Favorite>!$_';
  57. /**
  58. * Reference to main server object
  59. *
  60. * @var \Sabre\DAV\Server
  61. */
  62. private $server;
  63. /**
  64. * @var \OCP\ITagManager
  65. */
  66. private $tagManager;
  67. /**
  68. * @var \OCP\ITags
  69. */
  70. private $tagger;
  71. /**
  72. * Array of file id to tags array
  73. * The null value means the cache wasn't initialized.
  74. *
  75. * @var array
  76. */
  77. private $cachedTags;
  78. /**
  79. * @var \Sabre\DAV\Tree
  80. */
  81. private $tree;
  82. /**
  83. * @param \Sabre\DAV\Tree $tree tree
  84. * @param \OCP\ITagManager $tagManager tag manager
  85. */
  86. public function __construct(\Sabre\DAV\Tree $tree, \OCP\ITagManager $tagManager) {
  87. $this->tree = $tree;
  88. $this->tagManager = $tagManager;
  89. $this->tagger = null;
  90. $this->cachedTags = [];
  91. }
  92. /**
  93. * This initializes the plugin.
  94. *
  95. * This function is called by \Sabre\DAV\Server, after
  96. * addPlugin is called.
  97. *
  98. * This method should set up the required event subscriptions.
  99. *
  100. * @param \Sabre\DAV\Server $server
  101. * @return void
  102. */
  103. public function initialize(\Sabre\DAV\Server $server) {
  104. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  105. $server->xml->elementMap[self::TAGS_PROPERTYNAME] = TagList::class;
  106. $this->server = $server;
  107. $this->server->on('propFind', [$this, 'handleGetProperties']);
  108. $this->server->on('propPatch', [$this, 'handleUpdateProperties']);
  109. }
  110. /**
  111. * Returns the tagger
  112. *
  113. * @return \OCP\ITags tagger
  114. */
  115. private function getTagger() {
  116. if (!$this->tagger) {
  117. $this->tagger = $this->tagManager->load('files');
  118. }
  119. return $this->tagger;
  120. }
  121. /**
  122. * Returns tags and favorites.
  123. *
  124. * @param integer $fileId file id
  125. * @return array list($tags, $favorite) with $tags as tag array
  126. * and $favorite is a boolean whether the file was favorited
  127. */
  128. private function getTagsAndFav($fileId) {
  129. $isFav = false;
  130. $tags = $this->getTags($fileId);
  131. if ($tags) {
  132. $favPos = array_search(self::TAG_FAVORITE, $tags);
  133. if ($favPos !== false) {
  134. $isFav = true;
  135. unset($tags[$favPos]);
  136. }
  137. }
  138. return [$tags, $isFav];
  139. }
  140. /**
  141. * Returns tags for the given file id
  142. *
  143. * @param integer $fileId file id
  144. * @return array list of tags for that file
  145. */
  146. private function getTags($fileId) {
  147. if (isset($this->cachedTags[$fileId])) {
  148. return $this->cachedTags[$fileId];
  149. } else {
  150. $tags = $this->getTagger()->getTagsForObjects([$fileId]);
  151. if ($tags !== false) {
  152. if (empty($tags)) {
  153. return [];
  154. }
  155. return current($tags);
  156. }
  157. }
  158. return null;
  159. }
  160. /**
  161. * Updates the tags of the given file id
  162. *
  163. * @param int $fileId
  164. * @param array $tags array of tag strings
  165. */
  166. private function updateTags($fileId, $tags) {
  167. $tagger = $this->getTagger();
  168. $currentTags = $this->getTags($fileId);
  169. $newTags = array_diff($tags, $currentTags);
  170. foreach ($newTags as $tag) {
  171. if ($tag === self::TAG_FAVORITE) {
  172. continue;
  173. }
  174. $tagger->tagAs($fileId, $tag);
  175. }
  176. $deletedTags = array_diff($currentTags, $tags);
  177. foreach ($deletedTags as $tag) {
  178. if ($tag === self::TAG_FAVORITE) {
  179. continue;
  180. }
  181. $tagger->unTag($fileId, $tag);
  182. }
  183. }
  184. /**
  185. * Adds tags and favorites properties to the response,
  186. * if requested.
  187. *
  188. * @param PropFind $propFind
  189. * @param \Sabre\DAV\INode $node
  190. * @return void
  191. */
  192. public function handleGetProperties(
  193. PropFind $propFind,
  194. \Sabre\DAV\INode $node
  195. ) {
  196. if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
  197. return;
  198. }
  199. // need prefetch ?
  200. if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
  201. && $propFind->getDepth() !== 0
  202. && (!is_null($propFind->getStatus(self::TAGS_PROPERTYNAME))
  203. || !is_null($propFind->getStatus(self::FAVORITE_PROPERTYNAME))
  204. )) {
  205. // note: pre-fetching only supported for depth <= 1
  206. $folderContent = $node->getChildren();
  207. $fileIds[] = (int)$node->getId();
  208. foreach ($folderContent as $info) {
  209. $fileIds[] = (int)$info->getId();
  210. }
  211. $tags = $this->getTagger()->getTagsForObjects($fileIds);
  212. if ($tags === false) {
  213. // the tags API returns false on error...
  214. $tags = [];
  215. }
  216. $this->cachedTags = $this->cachedTags + $tags;
  217. $emptyFileIds = array_diff($fileIds, array_keys($tags));
  218. // also cache the ones that were not found
  219. foreach ($emptyFileIds as $fileId) {
  220. $this->cachedTags[$fileId] = [];
  221. }
  222. }
  223. $isFav = null;
  224. $propFind->handle(self::TAGS_PROPERTYNAME, function () use (&$isFav, $node) {
  225. [$tags, $isFav] = $this->getTagsAndFav($node->getId());
  226. return new TagList($tags);
  227. });
  228. $propFind->handle(self::FAVORITE_PROPERTYNAME, function () use ($isFav, $node) {
  229. if (is_null($isFav)) {
  230. [, $isFav] = $this->getTagsAndFav($node->getId());
  231. }
  232. if ($isFav) {
  233. return 1;
  234. } else {
  235. return 0;
  236. }
  237. });
  238. }
  239. /**
  240. * Updates tags and favorites properties, if applicable.
  241. *
  242. * @param string $path
  243. * @param PropPatch $propPatch
  244. *
  245. * @return void
  246. */
  247. public function handleUpdateProperties($path, PropPatch $propPatch) {
  248. $node = $this->tree->getNodeForPath($path);
  249. if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
  250. return;
  251. }
  252. $propPatch->handle(self::TAGS_PROPERTYNAME, function ($tagList) use ($node) {
  253. $this->updateTags($node->getId(), $tagList->getTags());
  254. return true;
  255. });
  256. $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node) {
  257. if ((int)$favState === 1 || $favState === 'true') {
  258. $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE);
  259. } else {
  260. $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE);
  261. }
  262. if (is_null($favState)) {
  263. // confirm deletion
  264. return 204;
  265. }
  266. return 200;
  267. });
  268. }
  269. }