-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule.php
477 lines (429 loc) · 17.9 KB
/
Module.php
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php declare(strict_types=1);
/*
* Copyright BibLibre, 2016-2017
* Copyright Daniel Berthereau, 2017-2024
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
* redistribute the software under the terms of the CeCILL license as circulated
* by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify
* and redistribute granted by the license, users are provided only with a
* limited warranty and the software's author, the holder of the economic
* rights, and the successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with
* loading, using, modifying and/or developing or reproducing the software by
* the user in light of its specific status of free software, that may mean that
* it is complicated to manipulate, and that also therefore means that it is
* reserved for developers and experienced professionals having in-depth
* computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling
* the security of their systems and/or data to be ensured and, more generally,
* to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
namespace SearchSolr;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use AdvancedSearch\Api\Representation\SearchConfigRepresentation;
use AdvancedSearch\Api\Representation\SearchEngineRepresentation;
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\MvcEvent;
use Omeka\Module\AbstractModule;
use Omeka\Module\Exception\ModuleCannotInstallException;
/**
* SearchSolr
*
* Use search engine Solr with Omeka.
*
* @copyright Daniel Berthereau, 2017-2025
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
protected $dependencies = [
'AdvancedSearch',
];
public function init(ModuleManager $moduleManager): void
{
require_once __DIR__ . '/vendor/autoload.php';
// No need to check the dependency upon Search here.
// Once disabled via onBootstrap(), thiis method is no more called.
$event = $moduleManager->getEvent();
$container = $event->getParam('ServiceManager');
$serviceListener = $container->get('ServiceListener');
$serviceListener->addServiceManager(
'SearchSolr\ValueExtractorManager',
'searchsolr_value_extractors',
Feature\ValueExtractorProviderInterface::class,
'getSolrValueExtractorConfig'
);
$serviceListener->addServiceManager(
'SearchSolr\ValueFormatterManager',
'searchsolr_value_formatters',
Feature\ValueFormatterProviderInterface::class,
'getSolrValueFormatterConfig'
);
}
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
// Manage the dependency upon Search, in particular when upgrading.
// Once disabled, this current method and other ones are no more called.
if (!$this->isModuleActive('AdvancedSearch')) {
$this->disableModule(__NAMESPACE__);
return;
}
$this->addAclRules();
}
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
$translator = $services->get('MvcTranslator');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.65')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.65'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
if (!file_exists(__DIR__ . '/vendor/solarium/solarium/src/Client.php')) {
$message = new PsrMessage(
'The composer library "{library}" is not installed. See readme.', // @translate
['library' => 'Solarium']
);
throw new ModuleCannotInstallException((string) $message->setTransalor($translator));
}
if (!$this->checkModuleActiveVersion('AdvancedSearch', '3.4.38')) {
$message = new PsrMessage(
$translator->translate('This module requires module "{module}" version "{version}" or greater.'), // @translate
['module' => 'Advanced Search', 'version' => '3.4.38']
);
throw new ModuleCannotInstallException((string) $message);
}
}
protected function postInstall(): void
{
$this->installResources();
}
protected function postUninstall(): void
{
$services = $this->getServiceLocator();
$moduleManager = $services->get('Omeka\ModuleManager');
$module = $moduleManager->getModule('AdvancedSearch');
if ($module && in_array($module->getState(), [
\Omeka\Module\Manager::STATE_ACTIVE,
\Omeka\Module\Manager::STATE_NOT_ACTIVE,
])) {
$sql = <<<'SQL'
DELETE FROM `search_engine` WHERE `adapter` = 'solarium';
SQL;
$connection = $services->get('Omeka\Connection');
$connection->executeStatement($sql);
}
}
/**
* Add ACL rules for this module.
*/
protected function addAclRules(): void
{
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$acl->allow(null, [
\SearchSolr\Api\Adapter\SolrCoreAdapter::class,
\SearchSolr\Api\Adapter\SolrMapAdapter::class,
]);
$acl->allow(null, \SearchSolr\Entity\SolrCore::class, 'read');
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
\AdvancedSearch\Controller\Admin\IndexController::class,
'view.browse.after',
[$this, 'appendBrowseCores']
);
$sharedEventManager->attach(
Api\Adapter\SolrCoreAdapter::class,
'api.delete.post',
[$this, 'deletePostSolrCore']
);
$sharedEventManager->attach(
Api\Adapter\SolrMapAdapter::class,
'api.update.pre',
[$this, 'preSolrMap']
);
$sharedEventManager->attach(
Api\Adapter\SolrMapAdapter::class,
'api.delete.pre',
[$this, 'preSolrMap']
);
$sharedEventManager->attach(
Api\Adapter\SolrMapAdapter::class,
'api.update.post',
[$this, 'updatePostSolrMap']
);
$sharedEventManager->attach(
Api\Adapter\SolrMapAdapter::class,
'api.delete.post',
[$this, 'deletePostSolrMap']
);
$sharedEventManager->attach(
\SearchSolr\Api\Representation\SolrCoreRepresentation::class,
'rep.resource.json',
[$this, 'filterJsonLd']
);
}
/**
* Add a check for rights for api requests.
*
* @see https://github.com/Daniel-KM/Omeka-S-module-SearchSolr/issues/17
* @todo Protect via acl or route, not via checks here.
*/
public function filterJsonLd(Event $event)
{
/**
* @var \Omeka\Mvc\Status $status
* @var \Omeka\Entity\User|null $user
* @var \Omeka\Permissions\Acl $acl
*/
$services = $this->getServiceLocator();
$status = $services->get('Omeka\Status');
if (!$status->isApiRequest()
&& !$status->isKeyauthRequest()
) {
return;
}
$user = $services->get('Omeka\AuthenticationService')->getIdentity();
$hasRights = $user
&& $services->get('Omeka\Acl')->isAdminRole($user->getRole());
if ($hasRights) {
return;
}
$jsonLd = $event->getParam('jsonLd');
$jsonLd['o:settings'] = [];
$event->setParam('jsonLd', $jsonLd);
}
public function appendBrowseCores(Event $event): void
{
$view = $event->getTarget();
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$response = $api->search('solr_cores');
$cores = $response->getContent();
echo $view->partial('search-solr/admin/core/browse-table', [
'cores' => $cores,
]);
}
public function deletePostSolrCore(Event $event): void
{
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$id = $request->getId();
$searchEngines = $this->searchSearchEnginesByCoreId($id);
if (empty($searchEngines)) {
return;
}
$api->batchDelete('search_engines', array_keys($searchEngines), [], ['continueOnError' => true]);
}
public function preSolrMap(Event $event): void
{
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$solrMap = $api->read('solr_maps', $request->getId())->getContent();
$data = $request->getContent();
$data['solrMap'] = [
'solr_core_id' => $solrMap->solrCore()->id(),
'resource_name' => $solrMap->resourceName(),
'field_name' => $solrMap->fieldName(),
'source' => $solrMap->source(),
'settings' => $solrMap->settings(),
];
$request->setContent($data);
}
public function updatePostSolrMap(Event $event): void
{
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$response = $event->getParam('response');
$solrMap = $response->getContent();
$oldSolrMapValues = $request->getValue('solrMap');
// Quick check if the Solr field name is unchanged.
$fieldName = $solrMap->getFieldName();
$oldFieldName = $oldSolrMapValues['field_name'];
if ($fieldName === $oldFieldName) {
return;
}
$searchConfigs = $this->searchSearchConfigsByCoreId($solrMap->getSolrCore()->getId());
if (empty($searchConfigs)) {
return;
}
foreach ($searchConfigs as $searchConfig) {
$searchConfigSettings = $searchConfig->settings();
foreach ($searchConfigSettings as $key => $value) {
if (is_array($value)) {
if (isset($searchConfigSettings[$key][$oldFieldName])) {
$searchConfigSettings[$key][$fieldName] = $searchConfigSettings[$key][$oldFieldName];
unset($searchConfigSettings[$key][$oldFieldName]);
}
if (isset($searchConfigSettings[$key][$oldFieldName . ' asc'])) {
$searchConfigSettings[$key][$fieldName . ' asc'] = $searchConfigSettings[$key][$oldFieldName . ' asc'];
unset($searchConfigSettings[$key][$oldFieldName]);
}
if (isset($searchConfigSettings[$key][$oldFieldName . ' desc'])) {
$searchConfigSettings[$key][$fieldName . ' desc'] = $searchConfigSettings[$key][$oldFieldName . ' desc'];
unset($searchConfigSettings[$key][$oldFieldName]);
}
}
}
$api->update(
'search_configs',
$searchConfig->id(),
['o:settings' => $searchConfigSettings],
[],
['isPartial' => true]
);
}
}
public function deletePostSolrMap(Event $event): void
{
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$solrMapValues = $request->getValue('solrMap');
$searchConfigs = $this->searchSearchConfigsByCoreId($solrMapValues['solr_core_id']);
if (empty($searchConfigs)) {
return;
}
$fieldName = $solrMapValues['field_name'];
foreach ($searchConfigs as $searchConfig) {
$searchConfigSettings = $searchConfig->settings();
foreach ($searchConfigSettings as $key => $value) {
if (is_array($value)) {
unset($searchConfigSettings[$key][$fieldName]);
unset($searchConfigSettings[$key][$fieldName . ' asc']);
unset($searchConfigSettings[$key][$fieldName . ' desc']);
}
}
$api->update(
'search_configs',
$searchConfig->id(),
['o:settings' => $searchConfigSettings],
[],
['isPartial' => true]
);
}
}
/**
* Find all search indexes related to a specific solr core.
*
* @todo Factorize searchSearchEngines() from core with CoreController.
* @param int $solrCoreId
* @return SearchEngineRepresentation[] Result is indexed by id.
*/
protected function searchSearchEnginesByCoreId($solrCoreId)
{
$result = [];
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
/** @var \AdvancedSearch\Api\Representation\SearchEngineRepresentation[] $searchEngines */
$searchEngines = $api->search('search_engines', ['adapter' => 'solarium'])->getContent();
foreach ($searchEngines as $searchEngine) {
if ($searchEngine->settingEngineAdapter('solr_core_id') == $solrCoreId) {
$result[$searchEngine->id()] = $searchEngine;
}
}
return $result;
}
/**
* Find all search pages that use a specific solr core id.
*
* @todo Factorize searchSearchConfigs() from core with CoreController.
* @param int $solrCoreId
* @return SearchConfigRepresentation[] Result is indexed by id.
*/
protected function searchSearchConfigsByCoreId($solrCoreId)
{
// TODO Use entity manager to simplify search of pages from core.
$result = [];
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$searchEngines = $this->searchSearchEnginesByCoreId($solrCoreId);
foreach ($searchEngines as $searchEngine) {
$searchConfigs = $api->search('search_configs', ['engine_id' => $searchEngine->id()])->getContent();
foreach ($searchConfigs as $searchConfig) {
$result[$searchConfig->id()] = $searchConfig;
}
}
return $result;
}
protected function installResources(): void
{
$this->createDefaultSolrConfig();
}
protected function createDefaultSolrConfig(): void
{
// Note: during installation or upgrade, the api may not be available
// for the search api adapters, so use direct sql queries.
$services = $this->getServiceLocator();
$urlHelper = $services->get('ViewHelperManager')->get('url');
$messenger = $services->get('ControllerPluginManager')->get('messenger');
/** @var \Doctrine\DBAL\Connection $connection */
$connection = $services->get('Omeka\Connection');
// Check if the internal index exists.
$sqlSolrCoreId = <<<'SQL'
SELECT `id`
FROM `solr_core`
ORDER BY `id` ASC
SQL;
$solrCoreId = (int) $connection->fetchColumn($sqlSolrCoreId);
if ($solrCoreId) {
return;
}
// Set the default server id, used in some cases (shared core with Drupal).
$settings = $services->get('Omeka\Settings');
$serverId = strtolower(substr(str_replace(['+', '/', '='], ['', '', ''], base64_encode(random_bytes(128))), 0, 6));
$settings->set('searchsolr_server_id', $serverId);
// Install a default config.
$sql = <<<'SQL'
INSERT INTO `solr_core` (`name`, `settings`)
VALUES (?, ?);
SQL;
$solrCoreData = require __DIR__ . '/data/solr_cores/default.php';
$connection->executeStatement($sql, [
$solrCoreData['o:name'],
json_encode($solrCoreData['o:settings'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$solrCoreId = (int) $connection->fetchColumn($sqlSolrCoreId);
// Install a default mapping.
$sql = <<<'SQL'
INSERT INTO `solr_map` (`solr_core_id`, `resource_name`, `field_name`, `source`, `pool`, `settings`)
VALUES (?, ?, ?, ?, ?, ?);
SQL;
$defaultMaps = require __DIR__ . '/config/default_mappings.php';
foreach ($defaultMaps as $map) {
$connection->executeStatement($sql, [
$solrCoreId,
$map['resource_name'],
$map['field_name'],
$map['source'],
json_encode($map['pool'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
json_encode($map['settings'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
}
$message = new \Omeka\Stdlib\Message(
'The default core can be configured in the %1$ssearch manager%2$s.', // @translate
// Don't use the url helper, the route is not available during install.
sprintf('<a href="%s">', $urlHelper('admin') . '/search-manager/solr/core/' . $solrCoreId . '/edit'),
'</a>'
);
$message->setEscapeHtml(false);
$messenger->addSuccess($message);
}
}