TagSearchProvider.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Marcel Klehr <mklehr@gmx.net>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\SystemTags\Search;
  30. use OC\Files\Search\SearchComparison;
  31. use OC\Files\Search\SearchOrder;
  32. use OC\Files\Search\SearchQuery;
  33. use OCP\Files\FileInfo;
  34. use OCP\Files\IMimeTypeDetector;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\Node;
  37. use OCP\Files\Search\ISearchComparison;
  38. use OCP\Files\Search\ISearchOrder;
  39. use OCP\IL10N;
  40. use OCP\IURLGenerator;
  41. use OCP\IUser;
  42. use OCP\Search\IProvider;
  43. use OCP\Search\ISearchQuery;
  44. use OCP\Search\SearchResult;
  45. use OCP\Search\SearchResultEntry;
  46. use OCP\SystemTag\ISystemTag;
  47. use OCP\SystemTag\ISystemTagManager;
  48. use OCP\SystemTag\ISystemTagObjectMapper;
  49. use RecursiveArrayIterator;
  50. use RecursiveIteratorIterator;
  51. class TagSearchProvider implements IProvider {
  52. /** @var IL10N */
  53. private $l10n;
  54. /** @var IURLGenerator */
  55. private $urlGenerator;
  56. /** @var IMimeTypeDetector */
  57. private $mimeTypeDetector;
  58. /** @var IRootFolder */
  59. private $rootFolder;
  60. private ISystemTagObjectMapper $objectMapper;
  61. private ISystemTagManager $tagManager;
  62. public function __construct(
  63. IL10N $l10n,
  64. IURLGenerator $urlGenerator,
  65. IMimeTypeDetector $mimeTypeDetector,
  66. IRootFolder $rootFolder,
  67. ISystemTagObjectMapper $objectMapper,
  68. ISystemTagManager $tagManager
  69. ) {
  70. $this->l10n = $l10n;
  71. $this->urlGenerator = $urlGenerator;
  72. $this->mimeTypeDetector = $mimeTypeDetector;
  73. $this->rootFolder = $rootFolder;
  74. $this->objectMapper = $objectMapper;
  75. $this->tagManager = $tagManager;
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function getId(): string {
  81. return 'systemtags';
  82. }
  83. /**
  84. * @inheritDoc
  85. */
  86. public function getName(): string {
  87. return $this->l10n->t('Tags');
  88. }
  89. /**
  90. * @inheritDoc
  91. */
  92. public function getOrder(string $route, array $routeParameters): int {
  93. if ($route === 'files.View.index') {
  94. return -4;
  95. }
  96. return 6;
  97. }
  98. /**
  99. * @inheritDoc
  100. */
  101. public function search(IUser $user, ISearchQuery $query): SearchResult {
  102. $matchingTags = $this->tagManager->getAllTags(true, $query->getTerm());
  103. if (count($matchingTags) === 0) {
  104. return SearchResult::complete($this->l10n->t('Tags'), []);
  105. }
  106. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  107. $fileQuery = new SearchQuery(
  108. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%' . $query->getTerm() . '%'),
  109. $query->getLimit(),
  110. (int)$query->getCursor(),
  111. $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
  112. new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
  113. ] : [],
  114. $user
  115. );
  116. // do search
  117. $searchResults = $userFolder->search($fileQuery);
  118. $resultIds = array_map(function (Node $node) {
  119. return $node->getId();
  120. }, $searchResults);
  121. $matchedTags = $this->objectMapper->getTagIdsForObjects($resultIds, 'files');
  122. // prepare direct tag results
  123. $tagResults = array_map(function (ISystemTag $tag) {
  124. $thumbnailUrl = '';
  125. $link = $this->urlGenerator->linkToRoute(
  126. 'files.view.index'
  127. ) . '?view=systemtagsfilter&tags='.$tag->getId();
  128. $searchResultEntry = new SearchResultEntry(
  129. $thumbnailUrl,
  130. $this->l10n->t('All tagged %s …', [$tag->getName()]),
  131. '',
  132. $this->urlGenerator->getAbsoluteURL($link),
  133. 'icon-tag'
  134. );
  135. return $searchResultEntry;
  136. }, $matchingTags);
  137. // prepare files results
  138. return SearchResult::paginated(
  139. $this->l10n->t('Tags'),
  140. array_map(function (Node $result) use ($userFolder, $matchedTags, $query) {
  141. // Generate thumbnail url
  142. $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
  143. $path = $userFolder->getRelativePath($result->getPath());
  144. // Use shortened link to centralize the various
  145. // files/folder url redirection in files.View.showFile
  146. $link = $this->urlGenerator->linkToRoute(
  147. 'files.View.showFile',
  148. ['fileid' => $result->getId()]
  149. );
  150. $searchResultEntry = new SearchResultEntry(
  151. $thumbnailUrl,
  152. $result->getName(),
  153. $this->formatSubline($query, $matchedTags[$result->getId()]),
  154. $this->urlGenerator->getAbsoluteURL($link),
  155. $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
  156. );
  157. $searchResultEntry->addAttribute('fileId', (string)$result->getId());
  158. $searchResultEntry->addAttribute('path', $path);
  159. return $searchResultEntry;
  160. }, $searchResults)
  161. + $tagResults,
  162. $query->getCursor() + $query->getLimit()
  163. );
  164. }
  165. /**
  166. * Format subline for tagged files: Show the first 3 tags
  167. *
  168. * @param $query
  169. * @param array $tagInfo
  170. * @return string
  171. */
  172. private function formatSubline(ISearchQuery $query, array $tagInfo): string {
  173. /**
  174. * @var ISystemTag[]
  175. */
  176. $tags = $this->tagManager->getTagsByIds($tagInfo);
  177. $tagNames = array_map(function ($tag) {
  178. return $tag->getName();
  179. }, array_filter($tags, function ($tag) {
  180. return $tag->isUserVisible();
  181. }));
  182. // show the tag that you have searched for first
  183. usort($tagNames, function ($tagName) use ($query) {
  184. return strpos($tagName, $query->getTerm()) !== false? -1 : 1;
  185. });
  186. return $this->l10n->t('tagged %s', [implode(', ', array_slice($tagNames, 0, 3))]);
  187. }
  188. private function flattenArray($array) {
  189. $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
  190. return iterator_to_array($it, true);
  191. }
  192. }