Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/fulltextsearch.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2018-07-24 22:02:59 +0300
committerMaxence Lange <maxence@artificial-owl.com>2018-07-24 22:02:59 +0300
commit5e779d9876a9429d7abbf83f7d090ec9fe9a9329 (patch)
tree2f2058e3100907a73d2bf1058b8259f9d4451ef7 /lib
parentc91a0477b3bbbda71fd7bc6b1e9a19b076074f07 (diff)
new command ./occ full:test
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Command/Test.php339
-rw-r--r--lib/Provider/TestProvider.php243
-rw-r--r--lib/Service/TestService.php70
3 files changed, 652 insertions, 0 deletions
diff --git a/lib/Command/Test.php b/lib/Command/Test.php
new file mode 100644
index 0000000..bfea923
--- /dev/null
+++ b/lib/Command/Test.php
@@ -0,0 +1,339 @@
+<?php
+/**
+ * FullTextSearch - Full text search framework for Nextcloud
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\FullTextSearch\Command;
+
+use Exception;
+use OCA\FullTextSearch\Exceptions\ProviderIsNotCompatibleException;
+use OCA\FullTextSearch\IFullTextSearchProvider;
+use OCA\FullTextSearch\Model\ExtendedBase;
+use OCA\FullTextSearch\Model\IndexDocument;
+use OCA\FullTextSearch\Model\IndexOptions;
+use OCA\FullTextSearch\Model\Runner;
+use OCA\FullTextSearch\Provider\TestProvider;
+use OCA\FullTextSearch\Service\IndexService;
+use OCA\FullTextSearch\Service\MiscService;
+use OCA\FullTextSearch\Service\PlatformService;
+use OCA\FullTextSearch\Service\RunningService;
+use OCP\AppFramework\QueryException;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+
+class Test extends ExtendedBase {
+
+ /** @var RunningService */
+ private $runningService;
+
+ /** @var IndexService */
+ private $indexService;
+
+ /** @var PlatformService */
+ private $platformService;
+
+ /** @var MiscService */
+ private $miscService;
+
+
+ /** @var Runner */
+ private $runner;
+
+
+ /** @var boolean */
+ private $isJson = false;
+
+ /**
+ * Index constructor.
+ *
+ * @param RunningService $runningService
+ * @param IndexService $indexService
+ * @param PlatformService $platformService
+ * @param MiscService $miscService
+ */
+ public function __construct(
+ RunningService $runningService, IndexService $indexService,
+ PlatformService $platformService, MiscService $miscService
+ ) {
+ parent::__construct();
+
+ $this->indexService = $indexService;
+ $this->runningService = $runningService;
+ $this->platformService = $platformService;
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ *
+ */
+ protected function configure() {
+ parent::configure();
+ $this->setName('fulltextsearch:test')
+ ->setDescription('Testing the platform setup')
+ ->addOption('json', 'j', InputOption::VALUE_NONE, 'return result as JSON');
+ }
+
+
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ *
+ * @return int|null|void
+ * @throws Exception
+ */
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->isJson = ($input->getOption('json') === true);
+
+ $this->output($output, '.Testing your current setup:');
+
+ try {
+ $this->output($output, 'Creating mocked content provider.');
+ $testProvider = $this->generateMockProvider();
+ $this->output($output, true);
+
+ $this->output($output, 'Testing mocked provider: get indexable documents.');
+ $indexableDocuments = $testProvider->generateIndexableDocuments('user');
+ $this->output($output, '(' . sizeof($indexableDocuments) . ' items)', false);
+ $this->output($output, true);
+
+ } catch (Exception $e) {
+ $this->output($output, false);
+ throw $e;
+ }
+
+ try {
+ $this->output($output, 'Loading search platform.');
+ $testPlatform = $this->platformService->getPlatform();
+ $this->output($output, '(' . $testPlatform->getName() . ')', false);
+ $this->output($output, true);
+
+ $this->output($output, 'Testing search platform.');
+ $testPlatform->testPlatform();
+ $this->output($output, true);
+
+ $this->output($output, 'Locking process');
+ $this->runner = new Runner($this->runningService, 'test');
+ $this->runner->start(true);
+ $this->indexService->setRunner($this->runner);
+ $testPlatform->setRunner($this->runner);
+ $testProvider->setRunner($this->runner);
+ $this->output($output, true);
+
+ $this->output($output, 'Indexing search platform.');
+ $options = new IndexOptions(
+ [
+ 'provider' => TestProvider::TEST_PROVIDER_ID
+ ]
+ );
+ $this->indexService->indexProviderContentFromUser(
+ $testPlatform, $testProvider, 'user', $options
+ );
+ $this->output($output, true);
+
+
+ $this->output($output, 'Unlocking process');
+ $this->runner->stop();
+ $this->output($output, true);
+
+
+ } catch (Exception $e) {
+ $this->output($output, false);
+ throw $e;
+ }
+
+
+// $options = $this->generateIndexOptions($input);
+//
+// try {
+// $this->runner->sourceIsCommandLine($this, $output);
+// $this->runner->start();
+//
+// $providers = $this->providerService->getProviders();
+// foreach ($providers as $provider) {
+//
+// if (!$this->isIncludedProvider($options, $provider->getId())) {
+// continue;
+// }
+//
+// $this->runner->output('indexing ' . $provider->getName() . '.');
+// $provider->setRunner($this->runner);
+// $provider->setIndexOptions($options);
+// $this->indexProvider($provider, $options);
+// }
+//
+// } catch (Exception $e) {
+// $this->runner->exception($e->getMessage(), true);
+// throw $e;
+// }
+//
+// $this->runner->stop();
+
+ $this->output($output, '', true);
+ }
+
+
+ /**
+ * @return IFullTextSearchProvider
+ * @throws QueryException
+ * @throws ProviderIsNotCompatibleException
+ */
+ private function generateMockProvider() {
+ $providerId = 'OCA\FullTextSearch\Provider\TestProvider';
+ $provider = \OC::$server->query((string)$providerId);
+ if (!($provider instanceof IFullTextSearchProvider)) {
+ throw new ProviderIsNotCompatibleException(
+ 'TestProvider is not a compatible IFullTextSearchProvider'
+ );
+ }
+
+ return $provider;
+ }
+
+
+ /**
+ * @param OutputInterface $output
+ * @param string|bool $line
+ * @param bool $isNewLine
+ */
+ private function output(OutputInterface $output, $line, $isNewLine = true) {
+ $line = $this->convertBoolToLine($line, $isNewLine);
+ if ($isNewLine) {
+ $output->write(' ', true);
+ }
+
+ $output->write($line . ' ', false);
+ }
+
+
+ /**
+ * @param string|bool $line
+ * @param $isNewLine
+ *
+ * @return string
+ */
+ private function convertBoolToLine($line, &$isNewLine) {
+ if (!is_bool($line)) {
+ return $line;
+ }
+
+ $isNewLine = false;
+ if ($line === false) {
+ return '<error>fail</error>';
+ }
+
+ return '<info>ok</info>';
+ }
+
+// /**
+// * @param IFullTextSearchProvider $provider
+// * @param IndexOptions $options
+// *
+// * @throws Exception
+// */
+// private function indexProvider(IFullTextSearchProvider $provider, IndexOptions $options) {
+// $platform = $this->platformService->getPlatform();
+// $platform->initializeIndex();
+// $provider->onInitializingIndex($platform);
+//
+// $platform->setRunner($this->runner);
+//
+// $users = $this->generateUserList($options);
+// foreach ($users as $user) {
+// if ($user === null) {
+// continue;
+// }
+//
+// $this->runner->output(' USER: ' . $user->getUID());
+// $this->indexService->indexProviderContentFromUser(
+// $platform, $provider, $user->getUID(), $options
+// );
+// }
+//
+// $this->providerService->setProviderAsIndexed($provider, true);
+//
+// }
+//
+//
+// /**
+// * @param InputInterface $input
+// *
+// * @return IndexOptions
+// */
+// private function generateIndexOptions(InputInterface $input) {
+// $jsonOptions = $input->getArgument('options');
+// $options = json_decode($jsonOptions, true);
+//
+// if (!is_array($options)) {
+// $options = [];
+// }
+//
+// return new IndexOptions($options);
+// }
+//
+//
+// /**
+// * @param IndexOptions $options
+// * @param string $providerId
+// *
+// * @return bool
+// */
+// private function isIncludedProvider(IndexOptions $options, $providerId) {
+// if ($options->getOption('provider', '') !== ''
+// && $options->getOption('provider') !== $providerId) {
+// return false;
+// }
+//
+// if ($options->getOption('providers', null) !== null
+// && is_array($options->getOption('providers'))) {
+// return (in_array($providerId, $options->getOption('providers')));
+// }
+//
+// return true;
+// }
+//
+//
+// /**
+// * @param IndexOptions $options
+// *
+// * @return array
+// */
+// private function generateUserList(IndexOptions $options) {
+// if ($options->getOption('user', '') !== '') {
+// return [$this->userManager->get($options->getOption('user'))];
+// }
+//
+// if ($options->getOption('users', null) !== null
+// && is_array($options->getOption('users'))) {
+// return array_map([$this->userManager, 'get'], $options->getOption('users'));
+// }
+//
+// return $this->userManager->search('');
+// }
+}
+
+
+
diff --git a/lib/Provider/TestProvider.php b/lib/Provider/TestProvider.php
new file mode 100644
index 0000000..097b214
--- /dev/null
+++ b/lib/Provider/TestProvider.php
@@ -0,0 +1,243 @@
+<?php
+/**
+ * FullTextSearch - Full text search framework for Nextcloud
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\FullTextSearch\Provider;
+
+use OCA\FullTextSearch\AppInfo\Application;
+use OCA\FullTextSearch\IFullTextSearchPlatform;
+use OCA\FullTextSearch\IFullTextSearchProvider;
+use OCA\FullTextSearch\Model\Index;
+use OCA\FullTextSearch\Model\IndexDocument;
+use OCA\FullTextSearch\Model\IndexOptions;
+use OCA\FullTextSearch\Model\Runner;
+use OCA\FullTextSearch\Model\SearchRequest;
+use OCA\FullTextSearch\Model\SearchResult;
+use OCA\FullTextSearch\Service\ConfigService;
+use OCA\FullTextSearch\Service\MiscService;
+use OCA\FullTextSearch\Service\TestService;
+
+class TestProvider implements IFullTextSearchProvider {
+
+
+ const TEST_PROVIDER_ID = 'test_provider';
+
+ /** @var ConfigService */
+ private $configService;
+
+ /** @var TestService */
+ private $testService;
+
+ /** @var MiscService */
+ private $miscService;
+
+ /** @var Runner */
+ private $runner;
+
+ /** @var IndexOptions */
+ private $indexOptions = [];
+
+
+ /**
+ * TestProvider constructor.
+ *
+ * @param ConfigService $configService
+ * @param TestService $testService
+ * @param MiscService $miscService
+ */
+ public function __construct(
+ ConfigService $configService, TestService $testService, MiscService $miscService
+ ) {
+ $this->configService = $configService;
+ $this->testService = $testService;
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ * return unique id of the provider
+ */
+ public function getId() {
+ return self::TEST_PROVIDER_ID;
+ }
+
+
+ /**
+ * return name of the provider
+ */
+ public function getName() {
+ return 'Test Provider';
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getVersion() {
+ return $this->configService->getAppValue('installed_version');
+ }
+
+
+ /**
+ * @return array
+ */
+ public function getConfiguration() {
+ return $this->configService->getConfig();
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getAppId() {
+ return Application::APP_NAME;
+ }
+
+
+ public function setRunner(Runner $runner) {
+ $this->runner = $runner;
+ }
+
+
+ /**
+ * @param IndexOptions $options
+ */
+ public function setIndexOptions($options) {
+ $this->indexOptions = $options;
+ }
+
+
+ /**
+ * @return array
+ */
+ public function getOptionsTemplate() {
+ return [];
+ }
+
+
+ /**
+ * called when loading all providers.
+ *
+ * Loading some containers.
+ */
+ public function loadProvider() {
+ }
+
+
+ /**
+ * returns all indexable document for a user.
+ * There is no need to fill the document with content at this point.
+ *
+ * $platform is provided if the mapping needs to be changed.
+ *
+ * @param string $userId
+ *
+ * @return IndexDocument[]
+ */
+ public function generateIndexableDocuments($userId) {
+ $result = [];
+
+ $result[] = $this->testService->generateIndexDocuments(TestService::DOCUMENT_TEST_INDEX1);
+ $result[] = $this->testService->generateIndexDocuments(TestService::DOCUMENT_TEST_INDEX2);
+ $result[] = $this->testService->generateIndexDocuments(TestService::DOCUMENT_TEST_INDEX3);
+
+ return $result;
+ }
+
+
+ /**
+ * generate documents prior to the indexing.
+ * throw NoResultException if no more result
+ *
+ * @param IndexDocument[] $chunk
+ *
+ * @return IndexDocument[]
+ */
+ public function fillIndexDocuments($chunk) {
+ return $chunk;
+ }
+
+
+ /**
+ * @param IndexDocument $document
+ *
+ * @return bool
+ */
+ public function isDocumentUpToDate($document) {
+ return false;
+ }
+
+
+ /**
+ * @param Index $index
+ *
+ * @return IndexDocument|null
+ */
+ public function updateDocument(Index $index) {
+ return null;
+ }
+
+
+ /**
+ * @param IFullTextSearchPlatform $platform
+ */
+ public function onInitializingIndex(IFullTextSearchPlatform $platform) {
+ }
+
+
+ /**
+ * @param IFullTextSearchPlatform $platform
+ */
+ public function onResettingIndex(IFullTextSearchPlatform $platform) {
+ }
+
+
+ /**
+ * not used yet
+ */
+ public function unloadProvider() {
+ }
+
+
+ /**
+ * before a search, improve the request
+ *
+ * @param SearchRequest $request
+ */
+ public function improveSearchRequest(SearchRequest $request) {
+ }
+
+
+ /**
+ * after a search, improve results
+ *
+ * @param SearchResult $searchResult
+ */
+ public function improveSearchResult(SearchResult $searchResult) {
+ }
+
+
+} \ No newline at end of file
diff --git a/lib/Service/TestService.php b/lib/Service/TestService.php
new file mode 100644
index 0000000..9d93a1d
--- /dev/null
+++ b/lib/Service/TestService.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * FullTextSearch - Full text search framework for Nextcloud
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\FullTextSearch\Service;
+
+use OCA\FullTextSearch\AppInfo\Application;
+use OCA\FullTextSearch\Exceptions\ProviderOptionsDoesNotExistException;
+use OCA\FullTextSearch\Model\DocumentAccess;
+use OCA\FullTextSearch\Model\IndexDocument;
+use OCP\IConfig;
+use OCP\PreConditionNotMetException;
+use OCP\Util;
+
+class TestService {
+
+ const DOCUMENT_TEST_INDEX1 = 1;
+ const DOCUMENT_TEST_INDEX2 = 2;
+ const DOCUMENT_TEST_INDEX3 = 3;
+
+ /** @var MiscService */
+ private $miscService;
+
+
+ /**
+ * TestService constructor.
+ *
+ * @param MiscService $miscService
+ */
+ public function __construct(MiscService $miscService) {
+ $this->miscService = $miscService;
+ }
+
+
+ /**
+ * @param int $documentType
+ *
+ * @return IndexDocument
+ */
+ public function generateIndexDocuments($documentType) {
+ $indexDocument = new IndexDocument('test', 'test_' . $documentType);
+ $indexDocument->setContent('This is the content');
+
+ $access = new DocumentAccess('user');
+ $indexDocument->setAccess($access);
+ return $indexDocument;
+ }
+}