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.

TagSearchProvider.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\SearchBinaryOperator;
  31. use OC\Files\Search\SearchComparison;
  32. use OC\Files\Search\SearchOrder;
  33. use OC\Files\Search\SearchQuery;
  34. use OCP\SystemTag\ISystemTag;
  35. use OCP\SystemTag\ISystemTagManager;
  36. use OCP\SystemTag\ISystemTagObjectMapper;
  37. use OCP\Files\FileInfo;
  38. use OCP\Files\IMimeTypeDetector;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\Search\ISearchComparison;
  41. use OCP\Files\Node;
  42. use OCP\Files\Search\ISearchOrder;
  43. use OCP\IL10N;
  44. use OCP\IURLGenerator;
  45. use OCP\IUser;
  46. use OCP\Search\IProvider;
  47. use OCP\Search\ISearchQuery;
  48. use OCP\Search\SearchResult;
  49. use OCP\Search\SearchResultEntry;
  50. use RecursiveArrayIterator;
  51. use RecursiveIteratorIterator;
  52. class TagSearchProvider implements IProvider {
  53. /** @var IL10N */
  54. private $l10n;
  55. /** @var IURLGenerator */
  56. private $urlGenerator;
  57. /** @var IMimeTypeDetector */
  58. private $mimeTypeDetector;
  59. /** @var IRootFolder */
  60. private $rootFolder;
  61. private ISystemTagObjectMapper $objectMapper;
  62. private ISystemTagManager $tagManager;
  63. public function __construct(
  64. IL10N $l10n,
  65. IURLGenerator $urlGenerator,
  66. IMimeTypeDetector $mimeTypeDetector,
  67. IRootFolder $rootFolder,
  68. ISystemTagObjectMapper $objectMapper,
  69. ISystemTagManager $tagManager
  70. ) {
  71. $this->l10n = $l10n;
  72. $this->urlGenerator = $urlGenerator;
  73. $this->mimeTypeDetector = $mimeTypeDetector;
  74. $this->rootFolder = $rootFolder;
  75. $this->objectMapper = $objectMapper;
  76. $this->tagManager = $tagManager;
  77. }
  78. /**
  79. * @inheritDoc
  80. */
  81. public function getId(): string {
  82. return 'systemtags';
  83. }
  84. /**
  85. * @inheritDoc
  86. */
  87. public function getName(): string {
  88. return $this->l10n->t('Tags');
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. public function getOrder(string $route, array $routeParameters): int {
  94. if ($route === 'files.View.index') {
  95. return -4;
  96. }
  97. return 6;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function search(IUser $user, ISearchQuery $query): SearchResult {
  103. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  104. $fileQuery = new SearchQuery(
  105. new SearchBinaryOperator(SearchBinaryOperator::OPERATOR_OR, [
  106. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'tagname', '%' . $query->getTerm() . '%'),
  107. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%' . $query->getTerm() . '%'),
  108. ]),
  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. $relevantTags = $this->tagManager->getTagsByIds(array_unique($this->flattenArray($matchedTags)));
  123. // prepare direct tag results
  124. $tagResults = array_map(function(ISystemTag $tag) {
  125. $thumbnailUrl = '';
  126. $link = $this->urlGenerator->linkToRoute(
  127. 'files.view.index'
  128. ) . '?view=systemtagsfilter&tags='.$tag->getId();
  129. $searchResultEntry = new SearchResultEntry(
  130. $thumbnailUrl,
  131. $this->l10n->t('All tagged %s …', [$tag->getName()]),
  132. '',
  133. $this->urlGenerator->getAbsoluteURL($link),
  134. 'icon-tag'
  135. );
  136. return $searchResultEntry;
  137. }, array_filter($relevantTags, function($tag) use ($query) {
  138. return $tag->isUserVisible() && strpos($tag->getName(), $query->getTerm()) !== false;
  139. }));
  140. // prepare files results
  141. return SearchResult::paginated(
  142. $this->l10n->t('Tags'),
  143. array_map(function (Node $result) use ($userFolder, $matchedTags, $query) {
  144. // Generate thumbnail url
  145. $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
  146. $path = $userFolder->getRelativePath($result->getPath());
  147. // Use shortened link to centralize the various
  148. // files/folder url redirection in files.View.showFile
  149. $link = $this->urlGenerator->linkToRoute(
  150. 'files.View.showFile',
  151. ['fileid' => $result->getId()]
  152. );
  153. $searchResultEntry = new SearchResultEntry(
  154. $thumbnailUrl,
  155. $result->getName(),
  156. $this->formatSubline($query, $matchedTags[$result->getId()]),
  157. $this->urlGenerator->getAbsoluteURL($link),
  158. $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
  159. );
  160. $searchResultEntry->addAttribute('fileId', (string)$result->getId());
  161. $searchResultEntry->addAttribute('path', $path);
  162. return $searchResultEntry;
  163. }, $searchResults)
  164. + $tagResults,
  165. $query->getCursor() + $query->getLimit()
  166. );
  167. }
  168. /**
  169. * Format subline for tagged files: Show the first 3 tags
  170. *
  171. * @param $query
  172. * @param array $tagInfo
  173. * @return string
  174. */
  175. private function formatSubline(ISearchQuery $query, array $tagInfo): string {
  176. /**
  177. * @var ISystemTag[]
  178. */
  179. $tags = $this->tagManager->getTagsByIds($tagInfo);
  180. $tagNames = array_map(function($tag) {
  181. return $tag->getName();
  182. }, array_filter($tags, function($tag) {
  183. return $tag->isUserVisible();
  184. }));
  185. // show the tag that you have searched for first
  186. usort($tagNames, function($tagName) use($query) {
  187. return strpos($tagName, $query->getTerm()) !== false? -1 : 1;
  188. });
  189. return $this->l10n->t('tagged %s', [implode(', ', array_slice($tagNames, 0, 3))]);
  190. }
  191. private function flattenArray($array) {
  192. $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
  193. return iterator_to_array($it, true);
  194. }
  195. }