You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.8 KiB

3 years ago
  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. }