SearchComposer.php 11 KB

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