|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* FullTextSearch - Full text search framework for Nextcloud |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
9
|
|
|
* later. See the COPYING file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Maxence Lange <[email protected]> |
|
12
|
|
|
* @copyright 2018 |
|
13
|
|
|
* @license GNU AGPL version 3 or any later version |
|
14
|
|
|
* |
|
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
18
|
|
|
* License, or (at your option) any later version. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
namespace OCA\FullTextSearch\Command; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use Exception; |
|
35
|
|
|
use OC\Core\Command\Base; |
|
36
|
|
|
use OCA\FullTextSearch\Model\Index; |
|
|
|
|
|
|
37
|
|
|
use OCA\FullTextSearch\Service\MiscService; |
|
38
|
|
|
use OCA\FullTextSearch\Service\ProviderService; |
|
39
|
|
|
use OCP\FullTextSearch\Model\IIndexDocument; |
|
40
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
41
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
42
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
43
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Class DocumentProvider |
|
48
|
|
|
* |
|
49
|
|
|
* @package OCA\FullTextSearch\Command |
|
50
|
|
|
*/ |
|
51
|
|
|
class DocumentProvider extends Base { |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** @var ProviderService */ |
|
55
|
|
|
private $providerService; |
|
56
|
|
|
|
|
57
|
|
|
/** @var MiscService */ |
|
58
|
|
|
private $miscService; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Index constructor. |
|
63
|
|
|
* |
|
64
|
|
|
* @param ProviderService $providerService |
|
65
|
|
|
* @param MiscService $miscService |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct(ProviderService $providerService, MiscService $miscService |
|
68
|
|
|
) { |
|
69
|
|
|
parent::__construct(); |
|
70
|
|
|
|
|
71
|
|
|
$this->providerService = $providerService; |
|
72
|
|
|
$this->miscService = $miscService; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
protected function configure() { |
|
|
|
|
|
|
80
|
|
|
parent::configure(); |
|
81
|
|
|
$this->setName('fulltextsearch:document:provider') |
|
82
|
|
|
->setDescription('Get document from index') |
|
83
|
|
|
->addArgument('userId', InputArgument::REQUIRED, 'userId') |
|
84
|
|
|
->addArgument('providerId', InputArgument::REQUIRED, 'providerId') |
|
85
|
|
|
->addArgument('documentId', InputArgument::REQUIRED, 'documentId') |
|
86
|
|
|
->addOption('content', 'c', InputOption::VALUE_NONE, 'return some content'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param InputInterface $input |
|
92
|
|
|
* @param OutputInterface $output |
|
93
|
|
|
* |
|
94
|
|
|
* @throws Exception |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
97
|
|
|
$providerId = $input->getArgument('providerId'); |
|
98
|
|
|
$documentId = $input->getArgument('documentId'); |
|
99
|
|
|
$userId = $input->getArgument('userId'); |
|
100
|
|
|
|
|
101
|
|
|
$providerWrapper = $this->providerService->getProvider($providerId); |
|
102
|
|
|
$provider = $providerWrapper->getProvider(); |
|
103
|
|
|
|
|
104
|
|
|
$index = new Index($providerId, $documentId); |
|
105
|
|
|
$index->setOwnerId($userId); |
|
106
|
|
|
$index->setStatus(Index::INDEX_FULL); |
|
107
|
|
|
$indexDocument = $provider->updateDocument($index); |
|
108
|
|
|
if ($indexDocument->getIndex() |
|
109
|
|
|
->isStatus(Index::INDEX_REMOVE)) { |
|
110
|
|
|
throw new Exception('Unknown document'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$output->writeln('Document: '); |
|
114
|
|
|
$output->writeln(json_encode($indexDocument, JSON_PRETTY_PRINT)); |
|
115
|
|
|
|
|
116
|
|
|
if ($input->getOption('content') !== true) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$output->writeln('Content: '); |
|
121
|
|
|
$content = $indexDocument->getContent(); |
|
122
|
|
|
if ($indexDocument->isContentEncoded() === IIndexDocument::ENCODED_BASE64) { |
|
123
|
|
|
$content = base64_decode($content, true); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$output->writeln(substr($content, 0, 80)); |
|
127
|
|
|
|
|
128
|
|
|
$parts = $indexDocument->getParts(); |
|
129
|
|
|
$output->writeln(sizeof($parts) . ' Part(s)'); |
|
130
|
|
|
foreach (array_keys($parts) as $part) { |
|
131
|
|
|
$output->writeln( |
|
132
|
|
|
"'" . $part . "' " . substr($parts[$part], 0, 80) . ' (size: ' . strlen( |
|
133
|
|
|
$parts[$part] |
|
134
|
|
|
) . ')' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: