1
0

FullTextSearchManager.php 5.7 KB

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