|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Files_FullTextSearch - Index the content of your files |
|
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\Files_FullTextSearch\Service; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use OCA\Files_FullTextSearch\AppInfo\Application; |
|
35
|
|
|
use OCA\Files_FullTextSearch\Model\FilesDocument; |
|
36
|
|
|
use OCP\FullTextSearch\Model\IIndex; |
|
37
|
|
|
use OCP\IConfig; |
|
38
|
|
|
use OCP\PreConditionNotMetException; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Class ConfigService |
|
43
|
|
|
* |
|
44
|
|
|
* @package OCA\Files_FullTextSearch\Service |
|
45
|
|
|
*/ |
|
46
|
|
|
class ConfigService { |
|
47
|
|
|
|
|
48
|
|
|
const FILES_LOCAL = 'files_local'; |
|
49
|
|
|
const FILES_EXTERNAL = 'files_external'; |
|
50
|
|
|
const FILES_GROUP_FOLDERS = 'files_group_folders'; |
|
51
|
|
|
const FILES_ENCRYPTED = 'files_encrypted'; |
|
52
|
|
|
const FILES_FEDERATED = 'files_federated'; |
|
53
|
|
|
const FILES_SIZE = 'files_size'; |
|
54
|
|
|
const FILES_OFFICE = 'files_office'; |
|
55
|
|
|
const FILES_PDF = 'files_pdf'; |
|
56
|
|
|
const FILES_IMAGE = 'files_image'; |
|
57
|
|
|
const FILES_AUDIO = 'files_audio'; |
|
58
|
|
|
|
|
59
|
|
|
public $defaults = [ |
|
60
|
|
|
self::FILES_LOCAL => '1', |
|
61
|
|
|
self::FILES_EXTERNAL => '0', |
|
62
|
|
|
self::FILES_GROUP_FOLDERS => '0', |
|
63
|
|
|
self::FILES_ENCRYPTED => '0', |
|
64
|
|
|
self::FILES_FEDERATED => '0', |
|
65
|
|
|
self::FILES_SIZE => '20', |
|
66
|
|
|
self::FILES_PDF => '1', |
|
67
|
|
|
self::FILES_OFFICE => '1', |
|
68
|
|
|
self::FILES_IMAGE => '0', |
|
69
|
|
|
self::FILES_AUDIO => '0' |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** @var IConfig */ |
|
74
|
|
|
private $config; |
|
75
|
|
|
|
|
76
|
|
|
/** @var string */ |
|
77
|
|
|
private $userId; |
|
78
|
|
|
|
|
79
|
|
|
/** @var MiscService */ |
|
80
|
|
|
private $miscService; |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* ConfigService constructor. |
|
85
|
|
|
* |
|
86
|
|
|
* @param IConfig $config |
|
87
|
|
|
* @param string $userId |
|
88
|
|
|
* @param MiscService $miscService |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct(IConfig $config, $userId, MiscService $miscService) { |
|
91
|
|
|
$this->config = $config; |
|
92
|
|
|
$this->userId = $userId; |
|
93
|
|
|
$this->miscService = $miscService; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getConfig(): array { |
|
101
|
|
|
$keys = array_keys($this->defaults); |
|
102
|
|
|
$data = []; |
|
103
|
|
|
|
|
104
|
|
|
foreach ($keys as $k) { |
|
105
|
|
|
$data[$k] = $this->getAppValue($k); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array $save |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setConfig(array $save) { |
|
116
|
|
|
$keys = array_keys($this->defaults); |
|
117
|
|
|
|
|
118
|
|
|
foreach ($keys as $k) { |
|
119
|
|
|
if (array_key_exists($k, $save)) { |
|
120
|
|
|
$this->setAppValue($k, $save[$k]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Get a value by key |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $key |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
public function getAppValue(string $key): string { |
|
|
|
|
|
|
134
|
|
|
$defaultValue = null; |
|
135
|
|
|
if (array_key_exists($key, $this->defaults)) { |
|
136
|
|
|
$defaultValue = $this->defaults[$key]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return (string) $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set a value by key |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $key |
|
146
|
|
|
* @param string $value |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setAppValue(string $key, string $value) { |
|
149
|
|
|
$this->config->setAppValue(Application::APP_NAME, $key, $value); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* remove a key |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $key |
|
156
|
|
|
*/ |
|
157
|
|
|
public function deleteAppValue(string $key) { |
|
158
|
|
|
$this->config->deleteAppValue(Application::APP_NAME, $key); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* return if option is enabled. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $key |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
public function optionIsSelected(string $key): bool { |
|
170
|
|
|
return ($this->getAppValue($key) === '1'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get a user value by key |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $key |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
View Code Duplication |
public function getUserValue(string $key): string { |
|
|
|
|
|
|
182
|
|
|
$defaultValue = null; |
|
183
|
|
|
if (array_key_exists($key, $this->defaults)) { |
|
184
|
|
|
$defaultValue = $this->defaults[$key]; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this->config->getUserValue( |
|
188
|
|
|
$this->userId, Application::APP_NAME, $key, $defaultValue |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Get a user value by key and user |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $userId |
|
197
|
|
|
* @param string $key |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getValueForUser(string $userId, string $key): string { |
|
202
|
|
|
return $this->config->getUserValue($userId, Application::APP_NAME, $key); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Set a user value by key |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $userId |
|
209
|
|
|
* @param string $key |
|
210
|
|
|
* @param string $value |
|
211
|
|
|
* |
|
212
|
|
|
* @throws PreConditionNotMetException |
|
213
|
|
|
*/ |
|
214
|
|
|
public function setValueForUser(string $userId, string $key, string $value) { |
|
215
|
|
|
$this->config->setUserValue($userId, Application::APP_NAME, $key, $value); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $key |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $default |
|
223
|
|
|
* |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getSystemValue(string $key, string $default = ''): string { |
|
227
|
|
|
return $this->config->getSystemValue($key, $default); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param FilesDocument $document |
|
233
|
|
|
* @param string $option |
|
234
|
|
|
*/ |
|
235
|
|
|
public function setDocumentIndexOption(FilesDocument $document, string $option) { |
|
236
|
|
|
$document->getIndex() |
|
237
|
|
|
->addOption('_' . $option, (string)$this->getAppValue($option)); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param IIndex $index |
|
243
|
|
|
* |
|
244
|
|
|
* @return bool |
|
245
|
|
|
*/ |
|
246
|
|
|
public function compareIndexOptions(IIndex $index): bool { |
|
247
|
|
|
$options = $index->getOptions(); |
|
248
|
|
|
|
|
249
|
|
|
$ak = array_keys($options); |
|
250
|
|
|
foreach ($ak as $k) { |
|
251
|
|
|
if (substr($k, 0, 1) === '_' |
|
252
|
|
|
&& $options[$k] !== $this->getAppValue(substr($k, 1))) { |
|
253
|
|
|
return false; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return true; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.