FullTextSearchManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\FullTextSearch;
  25. use OCP\FullTextSearch\Exceptions\FullTextSearchAppNotAvailableException;
  26. use OCP\FullTextSearch\IFullTextSearchManager;
  27. use OCP\FullTextSearch\Model\IIndex;
  28. use OCP\FullTextSearch\Model\ISearchResult;
  29. use OCP\FullTextSearch\Service\IIndexService;
  30. use OCP\FullTextSearch\Service\IProviderService;
  31. use OCP\FullTextSearch\Service\ISearchService;
  32. /**
  33. * Class FullTextSearchManager
  34. *
  35. * @package OC\FullTextSearch
  36. */
  37. class FullTextSearchManager implements IFullTextSearchManager {
  38. private ?IProviderService $providerService = null;
  39. private ?IIndexService $indexService = null;
  40. private ?ISearchService $searchService = null;
  41. /**
  42. * @since 15.0.0
  43. */
  44. public function registerProviderService(IProviderService $providerService): void {
  45. $this->providerService = $providerService;
  46. }
  47. /**
  48. * @since 15.0.0
  49. */
  50. public function registerIndexService(IIndexService $indexService): void {
  51. $this->indexService = $indexService;
  52. }
  53. /**
  54. * @since 15.0.0
  55. */
  56. public function registerSearchService(ISearchService $searchService): void {
  57. $this->searchService = $searchService;
  58. }
  59. /**
  60. * @since 16.0.0
  61. */
  62. public function isAvailable(): bool {
  63. if ($this->indexService === null ||
  64. $this->providerService === null ||
  65. $this->searchService === null) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * @throws FullTextSearchAppNotAvailableException
  72. */
  73. private function getProviderService(): IProviderService {
  74. if ($this->providerService === null) {
  75. throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
  76. }
  77. return $this->providerService;
  78. }
  79. /**
  80. * @throws FullTextSearchAppNotAvailableException
  81. */
  82. private function getIndexService(): IIndexService {
  83. if ($this->indexService === null) {
  84. throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
  85. }
  86. return $this->indexService;
  87. }
  88. /**
  89. * @throws FullTextSearchAppNotAvailableException
  90. */
  91. private function getSearchService(): ISearchService {
  92. if ($this->searchService === null) {
  93. throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
  94. }
  95. return $this->searchService;
  96. }
  97. /**
  98. * @throws FullTextSearchAppNotAvailableException
  99. */
  100. public function addJavascriptAPI(): void {
  101. $this->getProviderService()->addJavascriptAPI();
  102. }
  103. /**
  104. * @throws FullTextSearchAppNotAvailableException
  105. */
  106. public function isProviderIndexed(string $providerId): bool {
  107. return $this->getProviderService()->isProviderIndexed($providerId);
  108. }
  109. /**
  110. * @throws FullTextSearchAppNotAvailableException
  111. */
  112. public function getIndex(string $providerId, string $documentId): IIndex {
  113. return $this->getIndexService()->getIndex($providerId, $documentId);
  114. }
  115. /**
  116. * @see IIndex for available value for $status.
  117. *
  118. * @throws FullTextSearchAppNotAvailableException
  119. */
  120. public function createIndex(
  121. string $providerId,
  122. string $documentId,
  123. string $userId,
  124. int $status = 0,
  125. ): IIndex {
  126. return $this->getIndexService()->createIndex($providerId, $documentId, $userId, $status);
  127. }
  128. /**
  129. * @see IIndex for available value for $status.
  130. *
  131. * @throws FullTextSearchAppNotAvailableException
  132. */
  133. public function updateIndexStatus(
  134. string $providerId,
  135. string $documentId,
  136. int $status,
  137. bool $reset = false,
  138. ): void {
  139. $this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
  140. }
  141. /**
  142. * @see IIndex for available value for $status.
  143. *
  144. * @throws FullTextSearchAppNotAvailableException
  145. */
  146. public function updateIndexesStatus(
  147. string $providerId,
  148. array $documentIds,
  149. int $status,
  150. bool $reset = false,
  151. ): void {
  152. $this->getIndexService()->updateIndexesStatus($providerId, $documentIds, $status, $reset);
  153. }
  154. /**
  155. * @param IIndex[] $indexes
  156. *
  157. * @throws FullTextSearchAppNotAvailableException
  158. */
  159. public function updateIndexes(array $indexes): void {
  160. $this->getIndexService()->updateIndexes($indexes);
  161. }
  162. /**
  163. * @return ISearchResult[]
  164. * @throws FullTextSearchAppNotAvailableException
  165. */
  166. public function search(array $request, string $userId = ''): array {
  167. $searchRequest = $this->getSearchService()->generateSearchRequest($request);
  168. return $this->getSearchService()->search($userId, $searchRequest);
  169. }
  170. }