-
Notifications
You must be signed in to change notification settings - Fork 9
/
Module.php
429 lines (384 loc) · 18.3 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
<?php
/*
* Copyright BibLibre, 2016-2017
*
* 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 Search;
use Laminas\ModuleManager\ModuleManager;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\EventManager\Event;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Module\AbstractModule;
use Composer\Semver\Comparator;
class Module extends AbstractModule
{
protected $resourcesBeingUpdatedInBatch = [];
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$acl->allow(null, 'Search\Api\Adapter\SearchPageAdapter');
$acl->allow(null, 'Search\Api\Adapter\SearchIndexAdapter');
$acl->allow(null, 'Search\Api\Adapter\SavedQueryAdapter');
$acl->allow(null, 'Search\Entity\SearchPage', 'read');
$acl->allow(null, 'Search\Entity\SearchIndex', 'read');
$acl->allow(null, 'Search\Entity\SavedQuery', 'create');
$acl->allow(null, 'Search\Entity\SavedQuery', 'delete');
$acl->allow(null, 'Search\Controller\Index');
$acl->allow(null, 'Search\Controller\SavedQuery');
$this->addRoutes();
}
public function init(ModuleManager $moduleManager)
{
$event = $moduleManager->getEvent();
$container = $event->getParam('ServiceManager');
$serviceListener = $container->get('ServiceListener');
$serviceListener->addServiceManager(
'Search\AdapterManager',
'search_adapters',
'Search\Feature\AdapterProviderInterface',
'getSearchAdapterConfig'
);
$serviceListener->addServiceManager(
'Search\FormAdapterManager',
'search_form_adapters',
'Search\Feature\FormAdapterProviderInterface',
'getSearchFormAdapterConfig'
);
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$sql = '
CREATE TABLE IF NOT EXISTS `search_index` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`adapter` varchar(255) NOT NULL,
`settings` text,
`created` datetime NOT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
';
$connection->exec($sql);
$sql = '
CREATE TABLE IF NOT EXISTS `search_page` (
id INT AUTO_INCREMENT NOT NULL,
`name` varchar(255) NOT NULL,
`path` varchar(255) NOT NULL,
`index_id` int(11) unsigned NOT NULL,
`form_adapter` varchar(255) NOT NULL,
`settings` text,
`created` datetime NOT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (id),
FOREIGN KEY (`index_id`) REFERENCES `search_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
';
$connection->exec($sql);
$connection->exec('CREATE TABLE saved_query (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, site_id INT DEFAULT NULL, search_page_id INT DEFAULT NULL, query_string LONGTEXT NOT NULL, query_title VARCHAR(255) NOT NULL, query_description LONGTEXT DEFAULT NULL, INDEX IDX_496E6EF2A76ED395 (user_id), INDEX IDX_496E6EF2F6BD1646 (site_id), INDEX IDX_496E6EF281978C7E (search_page_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF2F6BD1646 FOREIGN KEY (site_id) REFERENCES site (id) ON DELETE CASCADE');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF281978C7E FOREIGN KEY (search_page_id) REFERENCES search_page (id) ON DELETE CASCADE');
}
public function upgrade($oldVersion, $newVersion,
ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
if (version_compare($oldVersion, '0.1.1', '<')) {
$connection->exec('
ALTER TABLE search_page
CHANGE `form` `form_adapter` varchar(255) NOT NULL
');
}
if (version_compare($oldVersion, '0.10.0', '<')) {
$pages = $connection->fetchAll('SELECT id, settings FROM search_page WHERE form_adapter = ?', ['standard']);
foreach ($pages as $page) {
$settings = json_decode($page['settings'], true);
$search_fields = [];
if (isset($settings['form']['search_fields'])) {
foreach ($settings['form']['search_fields'] as $i => $fieldName) {
$search_fields[$fieldName] = [
'enabled' => '1',
'weight' => $i,
];
}
$settings['form']['search_fields'] = $search_fields;
$connection->update('search_page', ['settings' => json_encode($settings)], ['id' => $page['id']]);
}
}
}
if (Comparator::lessThan($oldVersion, '0.11.0')) {
$connection->exec('ALTER TABLE search_page MODIFY id INT AUTO_INCREMENT NOT NULL');
$connection->exec('CREATE TABLE saved_query (id INT AUTO_INCREMENT NOT NULL, user_id INT DEFAULT NULL, site_id INT DEFAULT NULL, search_page_id INT DEFAULT NULL, query_string LONGTEXT NOT NULL, query_title VARCHAR(255) NOT NULL, query_description LONGTEXT DEFAULT NULL, INDEX IDX_496E6EF2A76ED395 (user_id), INDEX IDX_496E6EF2F6BD1646 (site_id), INDEX IDX_496E6EF281978C7E (search_page_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF2A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF2F6BD1646 FOREIGN KEY (site_id) REFERENCES site (id) ON DELETE CASCADE');
$connection->exec('ALTER TABLE saved_query ADD CONSTRAINT FK_496E6EF281978C7E FOREIGN KEY (search_page_id) REFERENCES search_page (id) ON DELETE CASCADE');
}
if (Comparator::lessThan($oldVersion, '0.14.0')) {
$pages = $connection->executeQuery('SELECT id, settings, form_adapter FROM search_page')->fetchAll();
foreach ($pages as $page) {
$settings = json_decode($page['settings'], true);
$enabled_facets = array_filter($settings['facets'] ?? [], fn ($a) => $a['enabled'] ?? false);
uasort($enabled_facets, fn ($a, $b) => $a['weight'] - $b['weight']);
$settings['facets'] = [];
foreach ($enabled_facets as $fieldName => $facetData) {
$settings['facets'][] = [
'name' => $fieldName,
'label' => $facetData['display']['label'] ?? '',
];
}
$enabled_sort_fields = array_filter($settings['sort_fields'] ?? [], fn ($a) => $a['enabled'] ?? false);
uasort($enabled_sort_fields, fn ($a, $b) => $a['weight'] - $b['weight']);
$settings['sort_fields'] = [];
foreach ($enabled_sort_fields as $fieldName => $sortFieldData) {
$settings['sort_fields'][] = [
'name' => $fieldName,
'label' => $sortFieldData['display']['label'] ?? '',
];
}
if ($page['form_adapter'] === 'standard') {
$enabled_search_fields = array_filter($settings['form']['search_fields'] ?? [], fn ($a) => $a['enabled'] ?? false);
uasort($enabled_search_fields, fn ($a, $b) => $a['weight'] - $b['weight']);
$settings['form'] ??= [];
$settings['form']['search_fields'] = [];
foreach ($enabled_search_fields as $fieldName => $searchFieldData) {
$settings['form']['search_fields'][] = [
'name' => $fieldName,
];
}
}
$connection->update('search_page', ['settings' => json_encode($settings)], ['id' => $page['id']]);
}
}
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->exec('DROP TABLE IF EXISTS saved_query');
$connection->exec('DROP TABLE IF EXISTS search_page');
$connection->exec('DROP TABLE IF EXISTS search_index');
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$identifiers = ['Omeka\Api\Adapter\ItemAdapter', 'Omeka\Api\Adapter\ItemSetAdapter'];
foreach ($identifiers as $identifier) {
$sharedEventManager->attach(
$identifier,
'api.batch_update.pre',
[$this, 'onResourceBatchUpdatePre']
);
$sharedEventManager->attach(
$identifier,
'api.batch_update.post',
[$this, 'onResourceBatchUpdatePost']
);
$sharedEventManager->attach(
$identifier,
'api.update.post',
[$this, 'onResourceUpdatePost']
);
$sharedEventManager->attach(
$identifier,
'api.batch_create.pre',
[$this, 'onResourceBatchCreatePre']
);
$sharedEventManager->attach(
$identifier,
'api.batch_create.post',
[$this, 'onResourceBatchCreatePost']
);
$sharedEventManager->attach(
$identifier,
'api.create.post',
[$this, 'onResourceCreatePost']
);
$sharedEventManager->attach(
$identifier,
'api.delete.post',
[$this, 'onResourceDeletePost']
);
}
}
public function onResourceBatchUpdatePre(Event $event)
{
$request = $event->getParam('request');
$ids = $request->getIds();
foreach ($ids as $id) {
$key = sprintf('%s:%s', $request->getResource(), $id);
$this->resourcesBeingUpdatedInBatch[$key] = true;
}
}
public function onResourceBatchUpdatePost(Event $event)
{
$response = $event->getParam('response');
$resources = $response->getContent();
$this->indexResources($resources);
foreach ($resources as $resource) {
$key = sprintf('%s:%s', $resource->getResourceName(), $resource->getId());
unset($this->resourcesBeingUpdatedInBatch[$key]);
}
}
public function onResourceUpdatePost(Event $event)
{
$request = $event->getParam('request');
$key = sprintf('%s:%s', $request->getResource(), $request->getId());
if (array_key_exists($key, $this->resourcesBeingUpdatedInBatch)) {
// Do nothing. These resources will be indexed in
// api.batch_update.post listener
return;
}
$response = $event->getParam('response');
$resource = $response->getContent();
$this->indexResources([$resource]);
}
public function onResourceBatchCreatePre(Event $event)
{
$request = $event->getParam('request');
$content = $request->getContent();
foreach ($content as &$c) {
// This is used in api.create.post listener to avoid indexing the
// same resources twice
$c['o-module-search:batch-create'] = true;
}
$request->setContent($content);
}
public function onResourceBatchCreatePost(Event $event)
{
$response = $event->getParam('response');
$resources = $response->getContent();
$this->indexResources($resources);
}
public function onResourceCreatePost(Event $event)
{
$request = $event->getParam('request');
$content = $request->getContent();
if (array_key_exists('o-module-search:batch-create', $content)) {
// Do nothing. These resources will be indexed in
// api.batch_create.post listener
return;
}
$response = $event->getParam('response');
$resource = $response->getContent();
$this->indexResources([$resource]);
}
public function onResourceDeletePost(Event $event)
{
$serviceLocator = $this->getServiceLocator();
$api = $serviceLocator->get('Omeka\ApiManager');
$logger = $serviceLocator->get('Omeka\Logger');
$request = $event->getParam('request');
$searchIndexes = $api->search('search_indexes')->getContent();
foreach ($searchIndexes as $searchIndex) {
$searchIndexSettings = $searchIndex->settings();
if (!in_array($request->getResource(), $searchIndexSettings['resources'])) {
continue;
}
try {
$indexer = $searchIndex->indexer();
$indexer->deleteResource($request->getResource(), $request->getId());
} catch (\Exception $e) {
$logger->err(sprintf('Search: failed to delete resource: %s', $e));
}
}
}
protected function indexResources(array $resources)
{
if (empty($resources)) {
return;
}
$serviceLocator = $this->getServiceLocator();
$logger = $serviceLocator->get('Omeka\Logger');
$jobDispatcher = $serviceLocator->get(\Omeka\Job\Dispatcher::class);
// If we are in a background job or a script executed from the command
// line, we do not have a time limit so we can index resources
// immediately.
// Otherwise, index resources in a background job in order to not slow
// down the request
if (PHP_SAPI === 'cli') {
$api = $serviceLocator->get('Omeka\ApiManager');
$em = $serviceLocator->get('Omeka\EntityManager');
$originalIdentityMap = $em->getUnitOfWork()->getIdentityMap();
$searchIndexes = $api->search('search_indexes')->getContent();
foreach ($searchIndexes as $searchIndex) {
$searchIndexSettings = $searchIndex->settings();
$filteredResources = array_filter($resources, fn ($resource) => in_array($resource->getResourceName(), $searchIndexSettings['resources']));
if (empty($filteredResources)) {
continue;
}
try {
$indexer = $searchIndex->indexer();
$indexer->indexResources($filteredResources);
} catch (\Exception $e) {
$logger->err(sprintf('Search: failed to index resources: %s', $e));
}
}
$identityMap = $em->getUnitOfWork()->getIdentityMap();
foreach ($identityMap as $entityClass => $entities) {
foreach ($entities as $idHash => $entity) {
if (!isset($originalIdentityMap[$entityClass][$idHash])) {
$em->detach($entity);
}
}
}
} else {
$ids = array_map(fn ($resource) => $resource->getId(), $resources);
$jobDispatcher->dispatch(Job\UpdateIndex::class, ['ids' => $ids]);
}
}
protected function addRoutes()
{
$serviceLocator = $this->getServiceLocator();
$settings = $serviceLocator->get('Omeka\Settings');
$router = $serviceLocator->get('Router');
$api = $serviceLocator->get('Omeka\ApiManager');
if (!$router instanceof \Laminas\Router\Http\TreeRouteStack) {
return;
}
$pages = $api->search('search_pages')->getContent();
foreach ($pages as $page) {
$path = $page->path();
$router->addRoute('search-page-' . $page->id(), [
'type' => 'segment',
'options' => [
'route' => '/s/:site-slug/' . $path,
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__SITE__' => true,
'__SEARCH_PAGE__' => true,
'controller' => 'Index',
'action' => 'search',
'id' => $page->id(),
],
],
]);
}
}
}