SearchComposer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Search;
  8. use InvalidArgumentException;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OCP\IURLGenerator;
  11. use OCP\IUser;
  12. use OCP\Search\FilterDefinition;
  13. use OCP\Search\IFilter;
  14. use OCP\Search\IFilteringProvider;
  15. use OCP\Search\IInAppSearch;
  16. use OCP\Search\IProvider;
  17. use OCP\Search\ISearchQuery;
  18. use OCP\Search\SearchResult;
  19. use Psr\Container\ContainerExceptionInterface;
  20. use Psr\Container\ContainerInterface;
  21. use Psr\Log\LoggerInterface;
  22. use RuntimeException;
  23. use function array_map;
  24. /**
  25. * Queries individual \OCP\Search\IProvider implementations and composes a
  26. * unified search result for the user's search term
  27. *
  28. * The search process is generally split into two steps
  29. *
  30. * 1. Get a list of provider (`getProviders`)
  31. * 2. Get search results of each provider (`search`)
  32. *
  33. * The reasoning behind this is that the runtime complexity of a combined search
  34. * result would be O(n) and linearly grow with each provider added. This comes
  35. * from the nature of php where we can't concurrently fetch the search results.
  36. * So we offload the concurrency the client application (e.g. JavaScript in the
  37. * browser) and let it first get the list of providers to then fetch all results
  38. * concurrently. The client is free to decide whether all concurrent search
  39. * results are awaited or shown as they come in.
  40. *
  41. * @see IProvider::search() for the arguments of the individual search requests
  42. */
  43. class SearchComposer {
  44. /**
  45. * @var array<string, array{appId: string, provider: IProvider}>
  46. */
  47. private array $providers = [];
  48. private array $commonFilters;
  49. private array $customFilters = [];
  50. private array $handlers = [];
  51. public function __construct(
  52. private Coordinator $bootstrapCoordinator,
  53. private ContainerInterface $container,
  54. private IURLGenerator $urlGenerator,
  55. private LoggerInterface $logger
  56. ) {
  57. $this->commonFilters = [
  58. IFilter::BUILTIN_TERM => new FilterDefinition(IFilter::BUILTIN_TERM, FilterDefinition::TYPE_STRING),
  59. IFilter::BUILTIN_SINCE => new FilterDefinition(IFilter::BUILTIN_SINCE, FilterDefinition::TYPE_DATETIME),
  60. IFilter::BUILTIN_UNTIL => new FilterDefinition(IFilter::BUILTIN_UNTIL, FilterDefinition::TYPE_DATETIME),
  61. IFilter::BUILTIN_TITLE_ONLY => new FilterDefinition(IFilter::BUILTIN_TITLE_ONLY, FilterDefinition::TYPE_BOOL, false),
  62. IFilter::BUILTIN_PERSON => new FilterDefinition(IFilter::BUILTIN_PERSON, FilterDefinition::TYPE_PERSON),
  63. IFilter::BUILTIN_PLACES => new FilterDefinition(IFilter::BUILTIN_PLACES, FilterDefinition::TYPE_STRINGS, false),
  64. IFilter::BUILTIN_PROVIDER => new FilterDefinition(IFilter::BUILTIN_PROVIDER, FilterDefinition::TYPE_STRING, false),
  65. ];
  66. }
  67. /**
  68. * Load all providers dynamically that were registered through `registerProvider`
  69. *
  70. * If $targetProviderId is provided, only this provider is loaded
  71. * If a provider can't be loaded we log it but the operation continues nevertheless
  72. */
  73. private function loadLazyProviders(?string $targetProviderId = null): void {
  74. $context = $this->bootstrapCoordinator->getRegistrationContext();
  75. if ($context === null) {
  76. // Too early, nothing registered yet
  77. return;
  78. }
  79. $registrations = $context->getSearchProviders();
  80. foreach ($registrations as $registration) {
  81. try {
  82. /** @var IProvider $provider */
  83. $provider = $this->container->get($registration->getService());
  84. $providerId = $provider->getId();
  85. if ($targetProviderId !== null && $targetProviderId !== $providerId) {
  86. continue;
  87. }
  88. $this->providers[$providerId] = [
  89. 'appId' => $registration->getAppId(),
  90. 'provider' => $provider,
  91. ];
  92. $this->handlers[$providerId] = [$providerId];
  93. if ($targetProviderId !== null) {
  94. break;
  95. }
  96. } catch (ContainerExceptionInterface $e) {
  97. // Log an continue. We can be fault tolerant here.
  98. $this->logger->error('Could not load search provider dynamically: ' . $e->getMessage(), [
  99. 'exception' => $e,
  100. 'app' => $registration->getAppId(),
  101. ]);
  102. }
  103. }
  104. $this->loadFilters();
  105. }
  106. private function loadFilters(): void {
  107. foreach ($this->providers as $providerId => $providerData) {
  108. $appId = $providerData['appId'];
  109. $provider = $providerData['provider'];
  110. if (!$provider instanceof IFilteringProvider) {
  111. continue;
  112. }
  113. foreach ($provider->getCustomFilters() as $filter) {
  114. $this->registerCustomFilter($filter, $providerId);
  115. }
  116. foreach ($provider->getAlternateIds() as $alternateId) {
  117. $this->handlers[$alternateId][] = $providerId;
  118. }
  119. foreach ($provider->getSupportedFilters() as $filterName) {
  120. if ($this->getFilterDefinition($filterName, $providerId) === null) {
  121. throw new InvalidArgumentException('Invalid filter '. $filterName);
  122. }
  123. }
  124. }
  125. }
  126. private function registerCustomFilter(FilterDefinition $filter, string $providerId): void {
  127. $name = $filter->name();
  128. if (isset($this->commonFilters[$name])) {
  129. throw new InvalidArgumentException('Filter name is already used');
  130. }
  131. if (isset($this->customFilters[$providerId])) {
  132. $this->customFilters[$providerId][$name] = $filter;
  133. } else {
  134. $this->customFilters[$providerId] = [$name => $filter];
  135. }
  136. }
  137. /**
  138. * Get a list of all provider IDs & Names for the consecutive calls to `search`
  139. * Sort the list by the order property
  140. *
  141. * @param string $route the route the user is currently at
  142. * @param array $routeParameters the parameters of the route the user is currently at
  143. *
  144. * @return array
  145. */
  146. public function getProviders(string $route, array $routeParameters): array {
  147. $this->loadLazyProviders();
  148. $providers = array_map(
  149. function (array $providerData) use ($route, $routeParameters) {
  150. $appId = $providerData['appId'];
  151. $provider = $providerData['provider'];
  152. $order = $provider->getOrder($route, $routeParameters);
  153. if ($order === null) {
  154. return;
  155. }
  156. $triggers = [$provider->getId()];
  157. if ($provider instanceof IFilteringProvider) {
  158. $triggers += $provider->getAlternateIds();
  159. $filters = $provider->getSupportedFilters();
  160. } else {
  161. $filters = [IFilter::BUILTIN_TERM];
  162. }
  163. return [
  164. 'id' => $provider->getId(),
  165. 'appId' => $appId,
  166. 'name' => $provider->getName(),
  167. 'icon' => $this->fetchIcon($appId, $provider->getId()),
  168. 'order' => $order,
  169. 'triggers' => $triggers,
  170. 'filters' => $this->getFiltersType($filters, $provider->getId()),
  171. 'inAppSearch' => $provider instanceof IInAppSearch,
  172. ];
  173. },
  174. $this->providers,
  175. );
  176. $providers = array_filter($providers);
  177. // Sort providers by order and strip associative keys
  178. usort($providers, function ($provider1, $provider2) {
  179. return $provider1['order'] <=> $provider2['order'];
  180. });
  181. return $providers;
  182. }
  183. private function fetchIcon(string $appId, string $providerId): string {
  184. $icons = [
  185. [$providerId, $providerId.'.svg'],
  186. [$providerId, 'app.svg'],
  187. [$appId, $providerId.'.svg'],
  188. [$appId, $appId.'.svg'],
  189. [$appId, 'app.svg'],
  190. ['core', 'places/default-app-icon.svg'],
  191. ];
  192. if ($appId === 'settings' && $providerId === 'users') {
  193. // Conflict:
  194. // the file /apps/settings/users.svg is already used in black version by top right user menu
  195. // Override icon name here
  196. $icons = [['settings', 'users-white.svg']];
  197. }
  198. foreach ($icons as $i => $icon) {
  199. try {
  200. return $this->urlGenerator->imagePath(... $icon);
  201. } catch (RuntimeException $e) {
  202. // Ignore error
  203. }
  204. }
  205. return '';
  206. }
  207. /**
  208. * @param $filters string[]
  209. * @return array<string, string>
  210. */
  211. private function getFiltersType(array $filters, string $providerId): array {
  212. $filterList = [];
  213. foreach ($filters as $filter) {
  214. $filterList[$filter] = $this->getFilterDefinition($filter, $providerId)->type();
  215. }
  216. return $filterList;
  217. }
  218. private function getFilterDefinition(string $name, string $providerId): ?FilterDefinition {
  219. if (isset($this->commonFilters[$name])) {
  220. return $this->commonFilters[$name];
  221. }
  222. if (isset($this->customFilters[$providerId][$name])) {
  223. return $this->customFilters[$providerId][$name];
  224. }
  225. return null;
  226. }
  227. /**
  228. * @param array<string, string> $parameters
  229. */
  230. public function buildFilterList(string $providerId, array $parameters): FilterCollection {
  231. $this->loadLazyProviders($providerId);
  232. $list = [];
  233. foreach ($parameters as $name => $value) {
  234. $filter = $this->buildFilter($name, $value, $providerId);
  235. if ($filter === null) {
  236. continue;
  237. }
  238. $list[$name] = $filter;
  239. }
  240. return new FilterCollection(... $list);
  241. }
  242. private function buildFilter(string $name, string $value, string $providerId): ?IFilter {
  243. $filterDefinition = $this->getFilterDefinition($name, $providerId);
  244. if ($filterDefinition === null) {
  245. $this->logger->debug('Unable to find {name} definition', [
  246. 'name' => $name,
  247. 'value' => $value,
  248. ]);
  249. return null;
  250. }
  251. if (!$this->filterSupportedByProvider($filterDefinition, $providerId)) {
  252. // FIXME Use dedicated exception and handle it
  253. throw new UnsupportedFilter($name, $providerId);
  254. }
  255. return FilterFactory::get($filterDefinition->type(), $value);
  256. }
  257. private function filterSupportedByProvider(FilterDefinition $filterDefinition, string $providerId): bool {
  258. // Non exclusive filters can be ommited by apps
  259. if (!$filterDefinition->exclusive()) {
  260. return true;
  261. }
  262. $provider = $this->providers[$providerId]['provider'];
  263. $supportedFilters = $provider instanceof IFilteringProvider
  264. ? $provider->getSupportedFilters()
  265. : [IFilter::BUILTIN_TERM];
  266. return in_array($filterDefinition->name(), $supportedFilters, true);
  267. }
  268. /**
  269. * Query an individual search provider for results
  270. *
  271. * @param IUser $user
  272. * @param string $providerId one of the IDs received by `getProviders`
  273. * @param ISearchQuery $query
  274. *
  275. * @return SearchResult
  276. * @throws InvalidArgumentException when the $providerId does not correspond to a registered provider
  277. */
  278. public function search(
  279. IUser $user,
  280. string $providerId,
  281. ISearchQuery $query,
  282. ): SearchResult {
  283. $this->loadLazyProviders($providerId);
  284. $provider = $this->providers[$providerId]['provider'] ?? null;
  285. if ($provider === null) {
  286. throw new InvalidArgumentException("Provider $providerId is unknown");
  287. }
  288. return $provider->search($user, $query);
  289. }
  290. }