FullTextSearchManager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @since 16.0.0
  73. *
  74. * @return bool
  75. */
  76. public function isAvailable(): bool {
  77. if ($this->indexService === null ||
  78. $this->providerService === null ||
  79. $this->searchService === null) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * @return IProviderService
  86. * @throws FullTextSearchAppNotAvailableException
  87. */
  88. private function getProviderService(): IProviderService {
  89. if ($this->providerService === null) {
  90. throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
  91. }
  92. return $this->providerService;
  93. }
  94. /**
  95. * @return IIndexService
  96. * @throws FullTextSearchAppNotAvailableException
  97. */
  98. private function getIndexService(): IIndexService {
  99. if ($this->indexService === null) {
  100. throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
  101. }
  102. return $this->indexService;
  103. }
  104. /**
  105. * @return ISearchService
  106. * @throws FullTextSearchAppNotAvailableException
  107. */
  108. private function getSearchService(): ISearchService {
  109. if ($this->searchService === null) {
  110. throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
  111. }
  112. return $this->searchService;
  113. }
  114. /**
  115. * @throws FullTextSearchAppNotAvailableException
  116. */
  117. public function addJavascriptAPI() {
  118. $this->getProviderService()->addJavascriptAPI();
  119. }
  120. /**
  121. * @param string $providerId
  122. *
  123. * @return bool
  124. * @throws FullTextSearchAppNotAvailableException
  125. */
  126. public function isProviderIndexed(string $providerId): bool {
  127. return $this->getProviderService()->isProviderIndexed($providerId);
  128. }
  129. /**
  130. * @param string $providerId
  131. * @param string $documentId
  132. * @return IIndex
  133. * @throws FullTextSearchAppNotAvailableException
  134. */
  135. public function getIndex(string $providerId, string $documentId): IIndex {
  136. return $this->getIndexService()->getIndex($providerId, $documentId);
  137. }
  138. /**
  139. * @param string $providerId
  140. * @param string $documentId
  141. * @param string $userId
  142. * @param int $status
  143. *
  144. * @see IIndex for available value for $status.
  145. *
  146. * @return IIndex
  147. * @throws FullTextSearchAppNotAvailableException
  148. */
  149. public function createIndex(string $providerId, string $documentId, string $userId, int $status = 0): IIndex {
  150. return $this->getIndexService()->createIndex($providerId, $documentId, $userId, $status);
  151. }
  152. /**
  153. * @param string $providerId
  154. * @param string $documentId
  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 updateIndexStatus(string $providerId, string $documentId, int $status, bool $reset = false) {
  163. $this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
  164. }
  165. /**
  166. * @param string $providerId
  167. * @param array $documentIds
  168. * @param int $status
  169. * @param bool $reset
  170. *
  171. * @see IIndex for available value for $status.
  172. *
  173. * @throws FullTextSearchAppNotAvailableException
  174. */
  175. public function updateIndexesStatus(string $providerId, array $documentIds, int $status, bool $reset = false) {
  176. $this->getIndexService()->updateIndexesStatus($providerId, $documentIds, $status, $reset);
  177. }
  178. /**
  179. * @param IIndex[] $indexes
  180. *
  181. * @throws FullTextSearchAppNotAvailableException
  182. */
  183. public function updateIndexes(array $indexes) {
  184. $this->getIndexService()->updateIndexes($indexes);
  185. }
  186. /**
  187. * @param array $request
  188. * @param string $userId
  189. *
  190. * @return ISearchResult[]
  191. * @throws FullTextSearchAppNotAvailableException
  192. */
  193. public function search(array $request, string $userId = ''): array {
  194. $searchRequest = $this->getSearchService()->generateSearchRequest($request);
  195. return $this->getSearchService()->search($userId, $searchRequest);
  196. }
  197. }