FullTextSearchManager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\FullTextSearch;
  8. use OCP\FullTextSearch\Exceptions\FullTextSearchAppNotAvailableException;
  9. use OCP\FullTextSearch\IFullTextSearchManager;
  10. use OCP\FullTextSearch\Model\IIndex;
  11. use OCP\FullTextSearch\Model\ISearchResult;
  12. use OCP\FullTextSearch\Service\IIndexService;
  13. use OCP\FullTextSearch\Service\IProviderService;
  14. use OCP\FullTextSearch\Service\ISearchService;
  15. /**
  16. * Class FullTextSearchManager
  17. *
  18. * @package OC\FullTextSearch
  19. */
  20. class FullTextSearchManager implements IFullTextSearchManager {
  21. private ?IProviderService $providerService = null;
  22. private ?IIndexService $indexService = null;
  23. private ?ISearchService $searchService = null;
  24. /**
  25. * @since 15.0.0
  26. */
  27. public function registerProviderService(IProviderService $providerService): void {
  28. $this->providerService = $providerService;
  29. }
  30. /**
  31. * @since 15.0.0
  32. */
  33. public function registerIndexService(IIndexService $indexService): void {
  34. $this->indexService = $indexService;
  35. }
  36. /**
  37. * @since 15.0.0
  38. */
  39. public function registerSearchService(ISearchService $searchService): void {
  40. $this->searchService = $searchService;
  41. }
  42. /**
  43. * @since 16.0.0
  44. */
  45. public function isAvailable(): bool {
  46. if ($this->indexService === null ||
  47. $this->providerService === null ||
  48. $this->searchService === null) {
  49. return false;
  50. }
  51. return true;
  52. }
  53. /**
  54. * @throws FullTextSearchAppNotAvailableException
  55. */
  56. private function getProviderService(): IProviderService {
  57. if ($this->providerService === null) {
  58. throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
  59. }
  60. return $this->providerService;
  61. }
  62. /**
  63. * @throws FullTextSearchAppNotAvailableException
  64. */
  65. private function getIndexService(): IIndexService {
  66. if ($this->indexService === null) {
  67. throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
  68. }
  69. return $this->indexService;
  70. }
  71. /**
  72. * @throws FullTextSearchAppNotAvailableException
  73. */
  74. private function getSearchService(): ISearchService {
  75. if ($this->searchService === null) {
  76. throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
  77. }
  78. return $this->searchService;
  79. }
  80. /**
  81. * @throws FullTextSearchAppNotAvailableException
  82. */
  83. public function addJavascriptAPI(): void {
  84. $this->getProviderService()->addJavascriptAPI();
  85. }
  86. /**
  87. * @throws FullTextSearchAppNotAvailableException
  88. */
  89. public function isProviderIndexed(string $providerId): bool {
  90. return $this->getProviderService()->isProviderIndexed($providerId);
  91. }
  92. /**
  93. * @throws FullTextSearchAppNotAvailableException
  94. */
  95. public function getIndex(string $providerId, string $documentId): IIndex {
  96. return $this->getIndexService()->getIndex($providerId, $documentId);
  97. }
  98. /**
  99. * @see IIndex for available value for $status.
  100. *
  101. * @throws FullTextSearchAppNotAvailableException
  102. */
  103. public function createIndex(
  104. string $providerId,
  105. string $documentId,
  106. string $userId,
  107. int $status = 0,
  108. ): IIndex {
  109. return $this->getIndexService()->createIndex($providerId, $documentId, $userId, $status);
  110. }
  111. /**
  112. * @see IIndex for available value for $status.
  113. *
  114. * @throws FullTextSearchAppNotAvailableException
  115. */
  116. public function updateIndexStatus(
  117. string $providerId,
  118. string $documentId,
  119. int $status,
  120. bool $reset = false,
  121. ): void {
  122. $this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
  123. }
  124. /**
  125. * @see IIndex for available value for $status.
  126. *
  127. * @throws FullTextSearchAppNotAvailableException
  128. */
  129. public function updateIndexesStatus(
  130. string $providerId,
  131. array $documentIds,
  132. int $status,
  133. bool $reset = false,
  134. ): void {
  135. $this->getIndexService()->updateIndexesStatus($providerId, $documentIds, $status, $reset);
  136. }
  137. /**
  138. * @param IIndex[] $indexes
  139. *
  140. * @throws FullTextSearchAppNotAvailableException
  141. */
  142. public function updateIndexes(array $indexes): void {
  143. $this->getIndexService()->updateIndexes($indexes);
  144. }
  145. /**
  146. * @return ISearchResult[]
  147. * @throws FullTextSearchAppNotAvailableException
  148. */
  149. public function search(array $request, string $userId = ''): array {
  150. $searchRequest = $this->getSearchService()->generateSearchRequest($request);
  151. return $this->getSearchService()->search($userId, $searchRequest);
  152. }
  153. }