forked from Daniel-KM/Omeka-S-module-Search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
678 lines (616 loc) · 23.4 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?php
/*
* Copyright BibLibre, 2016-2017
* Copyright Daniel Berthereau, 2017-2018
*
* 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 Omeka\Entity\Resource;
use Omeka\Module\AbstractModule;
use Omeka\Mvc\Controller\Plugin\Messenger;
use Omeka\Settings\SettingsInterface;
use Omeka\Stdlib\Message;
use Search\Form\ConfigForm;
use Search\Indexer\IndexerInterface;
use Zend\EventManager\Event;
use Zend\EventManager\EventInterface;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\Controller\AbstractController;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Renderer\PhpRenderer;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$this->addAclRules();
$this->addRoutes();
}
public function init(ModuleManager $moduleManager)
{
$event = $moduleManager->getEvent();
$container = $event->getParam('ServiceManager');
$serviceListener = $container->get('ServiceListener');
$serviceListener->addServiceManager(
'Search\AdapterManager',
'search_adapters',
Feature\AdapterProviderInterface::class,
'getSearchAdapterConfig'
);
$serviceListener->addServiceManager(
'Search\FormAdapterManager',
'search_form_adapters',
Feature\FormAdapterProviderInterface::class,
'getSearchFormAdapterConfig'
);
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$this->setServiceLocator($serviceLocator);
$messenger = new Messenger;
$optionalModule = 'jQueryUI';
if (!$this->isModuleActive($optionalModule)) {
$messenger->addWarning('The module jQueryUI is required to customize the search pages.'); // @translate
}
$optionalModule = 'Reference';
if (!$this->isModuleActive($optionalModule)) {
$messenger->addWarning('The module Reference is required to use the facets with the default internal adapter.'); // @translate
}
$this->execSqlFromFile(__DIR__ . '/data/install/schema.sql');
$this->manageConfig('install');
$this->manageMainSettings('install');
$this->manageSiteSettings('install');
// TODO Move internal adapter in another module.
// Create the internal adapter.
$connection = $serviceLocator->get('Omeka\Connection');
$sql = <<<'SQL'
INSERT INTO `search_index`
(`name`, `adapter`, `settings`, `created`)
VALUES
('Internal', 'internal', ?, NOW());
SQL;
$sarchIndexSettings = ['resources' => ['items', 'item_sets']];
$connection->executeQuery($sql, [
json_encode($sarchIndexSettings, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$sql = <<<'SQL'
INSERT INTO `search_page`
(`index_id`, `name`, `path`, `form_adapter`, `settings`, `created`)
VALUES
('1', 'Internal', 'find', 'basic', ?, NOW());
SQL;
$sarchPageSettings = require __DIR__ . '/config/adapter_internal.php';
$connection->executeQuery($sql, [
json_encode($sarchPageSettings, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$messenger->addNotice('The internal search engine is available. Enable it in the main settings (for admin) and in site settings (for public).'); // @translate
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$this->setServiceLocator($serviceLocator);
$this->execSqlFromFile(__DIR__ . '/data/install/uninstall.sql');
$this->manageConfig('uninstall');
$this->manageMainSettings('uninstall');
$this->manageSiteSettings('uninstall');
}
public function upgrade($oldVersion, $newVersion,
ServiceLocatorInterface $serviceLocator)
{
$this->setServiceLocator($serviceLocator);
require_once 'data/scripts/upgrade.php';
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
// Hacked, because the admin layout doesn't use a partial or a trigger for the search engine.
'*',
'view.layout',
function (EventInterface $event) {
$view = $event->getTarget();
// TODO How to attach all admin events only?
if ($view->params()->fromRoute('__SITE__')) {
return;
}
$settings = $this->getServiceLocator()->get('Omeka\Settings');
$adminSearchPage = $settings->get('search_main_page');
if (empty($adminSearchPage)) {
return;
}
$view->headLink()->appendStylesheet($view->assetUrl('css/search-admin-search.css', 'Search'));
$view->headScript()->appendScript(sprintf('var searchUrl = %s;', json_encode($adminSearchPage, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
$view->headScript()->appendFile($view->assetUrl('js/search-admin-search.js', 'Search'));
}
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.delete.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemSetAdapter::class,
'api.create.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemSetAdapter::class,
'api.update.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemSetAdapter::class,
'api.delete.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.update.post',
[$this, 'updateSearchIndexMedia']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.delete.pre',
[$this, 'preUpdateSearchIndexMedia']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.delete.post',
[$this, 'updateSearchIndexMedia']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_input_filters',
[$this, 'handleMainSettingsFilters']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_elements',
[$this, 'handleSiteSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_input_filters',
[$this, 'handleSiteSettingsFilters']
);
}
public function getConfigForm(PhpRenderer $renderer)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$data = [];
$defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
foreach ($defaultSettings as $name => $value) {
$data[$name] = $settings->get($name, $value);
}
$form->init();
$form->setData($data);
$html = $renderer->formCollection($form);
return $html;
}
public function handleConfigForm(AbstractController $controller)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$space = strtolower(__NAMESPACE__);
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$params = $controller->getRequest()->getPost();
$form->init();
$form->setData($params);
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
$params = $form->getData();
$defaultSettings = $config[$space]['config'];
$params = array_intersect_key($params, $defaultSettings);
foreach ($params as $name => $value) {
$settings->set($name, $value);
}
}
protected function addAclRules()
{
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$acl->allow(
null,
[
\Search\Controller\IndexController::class,
\Search\Api\Adapter\SearchPageAdapter::class,
\Search\Api\Adapter\SearchIndexAdapter::class,
]
);
$acl->allow(
null,
[
\Search\Entity\SearchPage::class,
\Search\Entity\SearchIndex::class,
],
'read'
);
}
protected function addRoutes()
{
$services = $this->getServiceLocator();
$router = $services->get('Router');
if (!$router instanceof \Zend\Router\Http\TreeRouteStack) {
return;
}
$settings = $services->get('Omeka\Settings');
$adminSearchPages = $settings->get('search_pages', []);
$api = $services->get('Omeka\ApiManager');
$pages = $api->search('search_pages')->getContent();
foreach ($pages as $page) {
$pageId = $page->id();
$pagePath = $page->path();
$router->addRoute('search-page-' . $pageId, [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/s/:site-slug/' . $pagePath,
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__SITE__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'search',
'id' => $pageId,
],
],
]);
if (in_array($pageId, $adminSearchPages)) {
$router->addRoute('search-admin-page-' . $pageId, [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/admin/' . $pagePath,
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__ADMIN__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'search',
'id' => $pageId,
],
],
]);
}
}
}
public function preUpdateSearchIndexMedia(Event $event)
{
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$media = $api->read('media', $request->getId())->getContent();
$data = $request->getContent();
$data['itemId'] = $media->item()->id();
$request->setContent($data);
}
public function updateSearchIndex(Event $event)
{
$serviceLocator = $this->getServiceLocator();
$api = $serviceLocator->get('Omeka\ApiManager');
$request = $event->getParam('request');
$response = $event->getParam('response');
$requestResource = $request->getResource();
/** @var \Search\Api\Representation\SearchIndexRepresentation[] $searchIndexes */
$searchIndexes = $api->search('search_indexes')->getContent();
foreach ($searchIndexes as $searchIndex) {
$searchIndexSettings = $searchIndex->settings();
if (in_array($requestResource, $searchIndexSettings['resources'])) {
$indexer = $searchIndex->indexer();
if ($request->getOperation() == 'delete') {
$id = $request->getId();
$this->deleteIndexResource($indexer, $requestResource, $id);
} else {
$resource = $response->getContent();
$this->updateIndexResource($indexer, $resource);
}
}
}
}
public function updateSearchIndexMedia(Event $event)
{
$serviceLocator = $this->getServiceLocator();
$api = $serviceLocator->get('Omeka\ApiManager');
$request = $event->getParam('request');
$response = $event->getParam('response');
$itemId = $request->getValue('itemId');
$item = $itemId
? $api->read('items', $itemId, [], ['responseContent' => 'resource'])->getContent()
: $response->getContent()->getItem();
/** @var \Search\Api\Representation\SearchIndexRepresentation[] $searchIndexes */
$searchIndexes = $api->search('search_indexes')->getContent();
foreach ($searchIndexes as $searchIndex) {
$searchIndexSettings = $searchIndex->settings();
if (in_array('items', $searchIndexSettings['resources'])) {
$indexer = $searchIndex->indexer();
$this->updateIndexResource($indexer, $item);
}
}
}
/**
* Delete the search index for a resource.
*
* @param IndexerInterface $indexer
* @param string $resourceName
* @param int $id
*/
protected function deleteIndexResource(IndexerInterface $indexer, $resourceName, $id)
{
try {
$indexer->deleteResource($resourceName, $id);
} catch (\Exception $e) {
$services = $this->getServiceLocator();
$logger = $services->get('Omeka\Logger');
$logger->err(new Message('Unable to delete the search index for resource #%d: %s', // @translate
$id, $e->getMessage()));
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$messenger->addWarning(new Message('Unable to delete the search index for the deleted resource #%d: see log.', // @translate
$id));
}
}
/**
* Update the search index for a resource.
*
* @param IndexerInterface $indexer
* @param Resource $resource
*/
protected function updateIndexResource(IndexerInterface $indexer, Resource $resource)
{
try {
$indexer->indexResource($resource);
} catch (\Exception $e) {
$services = $this->getServiceLocator();
$logger = $services->get('Omeka\Logger');
$logger->err(new Message('Unable to index metadata of resource #%d for search: %s', // @translate
$resource->getId(), $e->getMessage()));
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$messenger->addWarning(new Message('Unable to update the search index for resource #%d: see log.', // @translate
$resource->getId()));
}
}
public function handleMainSettings(Event $event)
{
$this->handleAnySettings($event, 'settings');
}
public function handleSiteSettings(Event $event)
{
$this->handleAnySettings($event, 'site_settings');
}
public function handleMainSettingsFilters(Event $event)
{
$inputFilter = $event->getParam('inputFilter');
$searchFilter = $inputFilter->get('search');
$searchFilter->add([
'name' => 'search_pages',
'required' => false,
]);
$searchFilter->add([
'name' => 'search_main_page',
'required' => false,
]);
$searchFilter->add([
'name' => 'search_api_page',
'required' => false,
]);
}
public function handleSiteSettingsFilters(Event $event)
{
$inputFilter = $event->getParam('inputFilter');
$searchFilter = $inputFilter->get('search');
$searchFilter->add([
'name' => 'search_pages',
'required' => false,
]);
$searchFilter->add([
'name' => 'search_main_page',
'required' => false,
]);
}
/**
* Execute a sql from a file.
*
* @param string $filepath
* @return mixed
*/
protected function execSqlFromFile($filepath)
{
if (!file_exists($filepath) || !filesize($filepath) || !is_readable($filepath)) {
return;
}
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
$sql = file_get_contents($filepath);
return $connection->exec($sql);
}
/**
* Set or delete settings of the config of a module.
*
* @param string $process
*/
protected function manageConfig($process)
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$this->manageAnySettings($settings, 'config', $process);
}
/**
* Set or delete main settings.
*
* @param string $process
*/
protected function manageMainSettings($process)
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$this->manageAnySettings($settings, 'settings', $process);
}
/**
* Set or delete settings of all sites.
*
* @param string $process
*/
protected function manageSiteSettings($process)
{
$settingsType = 'site_settings';
$config = require __DIR__ . '/config/module.config.php';
$space = strtolower(__NAMESPACE__);
if (empty($config[$space][$settingsType])) {
return;
}
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings\Site');
$api = $services->get('Omeka\ApiManager');
$sites = $api->search('sites')->getContent();
foreach ($sites as $site) {
$settings->setTargetId($site->id());
$this->manageAnySettings($settings, $settingsType, $process);
}
}
/**
* Set or delete all settings of a specific type.
*
* @param SettingsInterface $settings
* @param string $settingsType
* @param string $process
*/
protected function manageAnySettings(SettingsInterface $settings, $settingsType, $process)
{
$config = require __DIR__ . '/config/module.config.php';
$space = strtolower(__NAMESPACE__);
if (empty($config[$space][$settingsType])) {
return;
}
$defaultSettings = $config[$space][$settingsType];
foreach ($defaultSettings as $name => $value) {
switch ($process) {
case 'install':
$settings->set($name, $value);
break;
case 'uninstall':
$settings->delete($name);
break;
}
}
}
/**
* Prepare a settings fieldset.
*
* @param Event $event
* @param string $settingsType
*/
protected function handleAnySettings(Event $event, $settingsType)
{
$services = $this->getServiceLocator();
$settingsTypes = [
// 'config' => 'Omeka\Settings',
'settings' => 'Omeka\Settings',
'site_settings' => 'Omeka\Settings\Site',
// 'user_settings' => 'Omeka\Settings\User',
];
if (!isset($settingsTypes[$settingsType])) {
return;
}
// TODO Check fieldsets in the config of the module.
$settingFieldsets = [
// 'config' => Form\ConfigForm::class,
'settings' => Form\SettingsFieldset::class,
'site_settings' => Form\SiteSettingsFieldset::class,
// 'user_settings' => Form\UserSettingsFieldset::class,
];
if (!isset($settingFieldsets[$settingsType])) {
return;
}
$settings = $services->get($settingsTypes[$settingsType]);
$data = $this->prepareDataToPopulate($settings, $settingsType);
if (empty($data)) {
return;
}
$space = strtolower(__NAMESPACE__);
$fieldset = $services->get('FormElementManager')->get($settingFieldsets[$settingsType]);
$fieldset->setName($space);
$form = $event->getTarget();
$form->add($fieldset);
$form->get($space)->populateValues($data);
}
/**
* Prepare data for a form or a fieldset.
*
* To be overridden by module for specific keys.
*
* @todo Use form methods to populate.
* @param SettingsInterface $settings
* @param string $settingsType
* @return array
*/
protected function prepareDataToPopulate(SettingsInterface $settings, $settingsType)
{
$services = $this->getServiceLocator();
$config = $services->get('Config');
$space = strtolower(__NAMESPACE__);
if (empty($config[$space][$settingsType])) {
return;
}
$defaultSettings = $config[$space][$settingsType];
$data = [];
foreach ($defaultSettings as $name => $value) {
$val = $settings->get($name, $value);
$data[$name] = $val;
}
return $data;
}
/**
* Check if a module is active.
*
* @param string $moduleClass
* @return bool
*/
protected function isModuleActive($moduleClass)
{
$services = $this->getServiceLocator();
$moduleManager = $services->get('Omeka\ModuleManager');
$module = $moduleManager->getModule($moduleClass);
return $module
&& $module->getState() === \Omeka\Module\Manager::STATE_ACTIVE;
}
}