FullTextSearchManager.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /** @var IProviderService */
  39. private $providerService;
  40. /** @var IIndexService */
  41. private $indexService;
  42. /** @var ISearchService */
  43. private $searchService;
  44. /**
  45. * @since 15.0.0
  46. *
  47. * @param IProviderService $providerService
  48. */
  49. public function registerProviderService(IProviderService $providerService) {
  50. $this->providerService = $providerService;
  51. }
  52. /**
  53. * @since 15.0.0
  54. *
  55. * @param IIndexService $indexService
  56. */
  57. public function registerIndexService(IIndexService $indexService) {
  58. $this->indexService = $indexService;
  59. }
  60. /**
  61. * @since 15.0.0
  62. *
  63. * @param ISearchService $searchService
  64. */
  65. public function registerSearchService(ISearchService $searchService) {
  66. $this->searchService = $searchService;
  67. }
  68. /**
  69. * @since 16.0.0
  70. *
  71. * @return bool
  72. */
  73. public function isAvailable(): bool {
  74. if ($this->indexService === null ||
  75. $this->providerService === null ||
  76. $this->searchService === null) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * @return IProviderService
  83. * @throws FullTextSearchAppNotAvailableException
  84. */
  85. private function getProviderService(): IProviderService {
  86. if ($this->providerService === null) {
  87. throw new FullTextSearchAppNotAvailableException('No IProviderService registered');
  88. }
  89. return $this->providerService;
  90. }
  91. /**
  92. * @return IIndexService
  93. * @throws FullTextSearchAppNotAvailableException
  94. */
  95. private function getIndexService(): IIndexService {
  96. if ($this->indexService === null) {
  97. throw new FullTextSearchAppNotAvailableException('No IIndexService registered');
  98. }
  99. return $this->indexService;
  100. }
  101. /**
  102. * @return ISearchService
  103. * @throws FullTextSearchAppNotAvailableException
  104. */
  105. private function getSearchService(): ISearchService {
  106. if ($this->searchService === null) {
  107. throw new FullTextSearchAppNotAvailableException('No ISearchService registered');
  108. }
  109. return $this->searchService;
  110. }
  111. /**
  112. * @throws FullTextSearchAppNotAvailableException
  113. */
  114. public function addJavascriptAPI() {
  115. $this->getProviderService()->addJavascriptAPI();
  116. }
  117. /**
  118. * @param string $providerId
  119. *
  120. * @return bool
  121. * @throws FullTextSearchAppNotAvailableException
  122. */
  123. public function isProviderIndexed(string $providerId): bool {
  124. return $this->getProviderService()->isProviderIndexed($providerId);
  125. }
  126. /**
  127. * @param string $providerId
  128. * @param string $documentId
  129. * @return IIndex
  130. * @throws FullTextSearchAppNotAvailableException
  131. */
  132. public function getIndex(string $providerId, string $documentId): IIndex {
  133. return $this->getIndexService()->getIndex($providerId, $documentId);
  134. }
  135. /**
  136. * @param string $providerId
  137. * @param string $documentId
  138. * @param string $userId
  139. * @param int $status
  140. *
  141. * @see IIndex for available value for $status.
  142. *
  143. * @return IIndex
  144. * @throws FullTextSearchAppNotAvailableException
  145. */
  146. public function createIndex(string $providerId, string $documentId, string $userId, int $status = 0): IIndex {
  147. return $this->getIndexService()->createIndex($providerId, $documentId, $userId, $status);
  148. }
  149. /**
  150. * @param string $providerId
  151. * @param string $documentId
  152. * @param int $status
  153. * @param bool $reset
  154. *
  155. * @see IIndex for available value for $status.
  156. *
  157. * @throws FullTextSearchAppNotAvailableException
  158. */
  159. public function updateIndexStatus(string $providerId, string $documentId, int $status, bool $reset = false) {
  160. $this->getIndexService()->updateIndexStatus($providerId, $documentId, $status, $reset);
  161. }
  162. /**
  163. * @param string $providerId
  164. * @param array $documentIds
  165. * @param int $status
  166. * @param bool $reset
  167. *
  168. * @see IIndex for available value for $status.
  169. *
  170. * @throws FullTextSearchAppNotAvailableException
  171. */
  172. public function updateIndexesStatus(string $providerId, array $documentIds, int $status, bool $reset = false) {
  173. $this->getIndexService()->updateIndexesStatus($providerId, $documentIds, $status, $reset);
  174. }
  175. /**
  176. * @param IIndex[] $indexes
  177. *
  178. * @throws FullTextSearchAppNotAvailableException
  179. */
  180. public function updateIndexes(array $indexes) {
  181. $this->getIndexService()->updateIndexes($indexes);
  182. }
  183. /**
  184. * @param array $request
  185. * @param string $userId
  186. *
  187. * @return ISearchResult[]
  188. * @throws FullTextSearchAppNotAvailableException
  189. */
  190. public function search(array $request, string $userId = ''): array {
  191. $searchRequest = $this->getSearchService()->generateSearchRequest($request);
  192. return $this->getSearchService()->search($userId, $searchRequest);
  193. }
  194. }