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

Test.php « Command « lib - github.com/nextcloud/fulltextsearch.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca44d947640decc721357b6438c1f0fa0fde0cab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php

/**
 * Nextcloud - nextant
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Maxence Lange <maxence@pontapreta.net>
 * @copyright Maxence Lange 2017
 * @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\Nextant\Command;

use OC\Core\Command\Base;
use \OCA\Nextant\Service\SolrAdminService;
use \OCA\Nextant\Service\SolrToolsService;
use \OCA\Nextant\Service\SolrService;
use \OCA\Nextant\Service\TestService;
use \OCA\Nextant\Service\IndexService;
use \OCA\Nextant\Service\ConfigService;
use \OCA\Nextant\Controller\SettingsController;
use \OCA\Nextant\Items\ItemDocument;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Test extends Base {

	private $configService;

	private $solrService;

	private $solrTools;

	private $solrAdmin;

	private $settingsController;

	public function __construct(
		ConfigService $configService, SolrService $solrService, SolrToolsService $solrTools,
		SolrAdminService $solrAdmin, IndexService $indexService,
		SettingsController $settingsController
	) {
		parent::__construct();
		$this->configService = $configService;
		$this->solrService = $solrService;
		$this->solrTools = $solrTools;
		$this->solrAdmin = $solrAdmin;
		$this->indexService = $indexService;
		$this->settingsController = $settingsController;
	}

	protected function configure() {
		parent::configure();
		$this->setName('nextant:test')
			 ->setDescription('test your Nextant configuration')
			 ->addArgument('address', InputArgument::REQUIRED, 'address of the solr to test')
			 ->addArgument('core', InputArgument::REQUIRED, 'core to test')
			 ->addOption(
				 'save', 's', InputOption::VALUE_NONE, 'Save configuration if test is successful'
			 );
	}

	protected function execute(InputInterface $input, OutputInterface $output) {

		if (!$address = $input->getArgument('address')) {
			$output->writeln('You need to specify the address to test');

			return;
		}

		if (!$core = $input->getArgument('core')) {
			$output->writeln('You need to specify the core');

			return;
		}

		$tmpConfig = array(
			'solr_url'     => $address,
			'solr_core'    => $core,
			'solr_timeout' => 30
		);

		if (!$this->solrService->setClient($tmpConfig)) {
			$output->writeln('Address is invalid');

			return;
		}

		// test ping
		if (!$this->test_ping($output)) {
			return;
		}

		// test schema
		if (!$this->test_schema($output)) {
			return;
		}

		// test extract
		if (!$this->test_extract($output)) {
			return;
		}

		// test update
		if (!$this->test_update($output)) {
			return;
		}

		// test search
		if (!$this->test_search($output)) {
			return;
		}

		// test delete
		if (!$this->test_delete($output)) {
			return;
		}

		$output->writeln('');
		$output->writeln('All test results were fine.');

		if ($input->getOption("save")) {
			$output->writeln('Saving configuration.');

			$this->configService->setAppValue('solr_url', $address);
			$this->configService->setAppValue('solr_core', $core);
			$this->configService->setAppValue('solr_timeout', 30);

			if ($this->configService->getAppValue('configured') !== '1') {
				$this->configService->setAppValue('configured', '2');
			}

			if ($this->configService->getAppValue('configured') === '2')
			{
				$output->writeln('You will need a first index to finish installation of Nextant.');
			}
		}
	}

	private function test_ping($output) {
		$output->write(' - Pinging Solr: ');
		if (!$this->solrAdmin->ping($ierror)) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		$output->writeln('<info>ok</info>');

		return true;
	}

	private function test_schema($output) {
		$output->write(' - Checking Solr schema: ');
		$ierror = null;
		if (!$this->solrAdmin->checkSchema(true, $ierror)) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		$output->writeln('<info>ok</info>');

		return true;
	}

	private function test_extract($output) {
		$ierror = null;
		$output->write(' - Extracting test document: ');
		$doc = TestService::generateTestDocument(
			1, '_nextant_test', __DIR__ . '/../../LICENSE', '/LICENSE'
		);
		$data = array(
			$doc
		);
		$solrDocs = null;
		$this->indexService->extract(
			ItemDocument::TYPE_TEST, '_nextant_test', $data, $solrDocs, true, $ierror
		);

		if (!$doc->isProcessed()) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		$output->writeln('<info>ok</info>');

		return true;
	}

	private function test_update($output) {
		$ierror = null;
		$output->write(' - Updating test document: ');
		$doc = TestService::generateTestDocument(
			1, '_nextant_test', __DIR__ . '/../../LICENSE', '/LICENSE2'
		);
		$asource =
			$this->indexService->getDocuments(ItemDocument::TYPE_TEST, '_nextant_test', 1, $ierror);

		if ($asource === false || sizeof($asource) != 1 || (!key_exists('test_1', $asource))) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		$source = $asource['test_1'];

		$doc->setPath('/LICENSE2');
		$doc->setShare(
			array(
				'nextant_test_share'
			)
		);
		$doc->setShareGroup(
			array(
				'nextant_test_share_group'
			)
		);
		$doc->deleted(false);

		$data = array(
			$doc
		);
		$this->indexService->updateDocuments(
			ItemDocument::TYPE_TEST, '_nextant_test', $data, $asource, $ierror
		);

		if (!$this->solrTools->commit(false, $ierror)) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		if (!$source->isUpdated()) {
			$output->writeln(
				'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
			);

			return false;
		}

		$output->writeln('<info>ok</info>');

		return true;
	}

	private function test_search($output) {
		$output->write(' - Searching test document: ');
		$ierror = null;
		$keyword = 'LICENSE';
		$this->solrService->setOwner('_nextant_test');
		if ($result = $this->solrService->search($keyword, array(), $ierror)) {
			if (sizeof($result) > 0) {

				foreach ($result as $doc) {
					if ($doc->getType() === ItemDocument::TYPE_TEST && $doc->getId() === 1) {
						$output->writeln('<info>ok</info>');

						return true;
					}
				}

				// CHECK ID DOCUMENT
				$output->writeln('<error>fail</error>');

				return false;
			}

			$output->writeln('<error>fail</error>');

			return false;
		}

		$output->writeln(
			'<error>fail</error> - ' . (($ierror === null) ? "0" : $ierror->getCode())
		);

		return false;
	}

	private function test_delete($output) {
		$ierror = null;
		$output->write(' - Deleting test document: ');

		$doc = new ItemDocument(ItemDocument::TYPE_TEST, 1);
		$data = array(
			$doc
		);
		$this->indexService->removeDocuments($data, $ierror);
		if ($doc->isRemoved()) {
			$output->writeln('<info>ok</info>');

			return true;
		}

		$output->writeln('<error>fail</error> - ' . ($ierror === null) ? "0" : $ierror->getCode());

		return false;
	}
}