forked from biblibre/omeka-s-module-Search
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathModule.php
761 lines (688 loc) · 29.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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
<?php declare(strict_types=1);
/*
* Copyright BibLibre, 2016-2017
* Copyright Daniel Berthereau, 2017-2021
*
* 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;
if (!class_exists(\Generic\AbstractModule::class)) {
require file_exists(dirname(__DIR__) . '/Generic/AbstractModule.php')
? dirname(__DIR__) . '/Generic/AbstractModule.php'
: __DIR__ . '/src/Generic/AbstractModule.php';
}
use Generic\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\MvcEvent;
use Omeka\Entity\Resource;
use Omeka\Mvc\Controller\Plugin\Messenger;
use Omeka\Stdlib\Message;
use Search\Indexer\IndexerInterface;
class Module extends AbstractModule
{
const NAMESPACE = __NAMESPACE__;
/**
* @var bool
*/
protected $isBatchUpdate;
public function init(ModuleManager $moduleManager): void
{
/** @var \Laminas\ModuleManager\Listener\ServiceListenerInterface $serviceListerner */
$serviceListener = $moduleManager->getEvent()->getParam('ServiceManager')
->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 onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
$this->addAclRules();
$this->addRoutes();
}
protected function preInstall(): void
{
$messenger = new Messenger;
$message = new Message(
'This module is deprecated and has been superceded by %sAdvanced Search%s. The upgrade from it is automatic.', // @translate
'<a href="https://gitlab.com/Daniel-KM/Omeka-S-module-AdvancedSearch" target="_blank">',
'</a>'
);
$message->escapeHtml(false);
$messenger->addWarning($message);
}
protected function postInstall(): void
{
$messenger = new Messenger;
$optionalModule = 'Reference';
if (!$this->isModuleActive($optionalModule)) {
$messenger->addWarning('The module Reference is required to use the facets with the default internal adapter, but not for the Solr adapter.'); // @translate
}
$this->installResources();
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
'*',
'view.layout',
[$this, 'addHeaders']
);
// Listeners for the indexing for items, item sets and media.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.post',
[$this, 'updateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.batch_update.pre',
[$this, 'preBatchUpdateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.batch_update.post',
[$this, 'postBatchUpdateSearchIndex']
);
$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.batch_update.pre',
[$this, 'preBatchUpdateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemSetAdapter::class,
'api.batch_update.post',
[$this, 'postBatchUpdateSearchIndex']
);
$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.batch_update.pre',
[$this, 'preBatchUpdateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.batch_update.post',
[$this, 'postBatchUpdateSearchIndex']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.delete.pre',
[$this, 'preUpdateSearchIndexMedia']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.delete.post',
[$this, 'updateSearchIndexMedia']
);
// Listeners for sites.
$sharedEventManager->attach(
\Omeka\Api\Adapter\SiteAdapter::class,
'api.create.post',
[$this, 'addSearchPageToSite']
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
$sharedEventManager->attach(
\Omeka\Form\SiteSettingsForm::class,
'form.add_elements',
[$this, 'handleSiteSettings']
);
}
protected function addAclRules(): void
{
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$acl
// This first rule duplicates the second, but is needed for a site.
->allow(
null,
[
\Search\Controller\IndexController::class,
\Search\Api\Adapter\SearchPageAdapter::class,
\Search\Api\Adapter\SearchIndexAdapter::class,
],
['read', 'search']
)
->allow(
null,
[
\Search\Controller\IndexController::class,
\Search\Api\Adapter\SearchPageAdapter::class,
\Search\Api\Adapter\SearchIndexAdapter::class,
]
)
->allow(
null,
[
\Search\Entity\SearchPage::class,
\Search\Entity\SearchIndex::class,
],
['read']
);
}
protected function addRoutes(): void
{
$services = $this->getServiceLocator();
/** @var \Omeka\Mvc\Status $status */
$status = $services->get('Omeka\Status');
if ($status->isApiRequest()) {
return;
}
$router = $services->get('Router');
if (!$router instanceof \Laminas\Router\Http\TreeRouteStack) {
return;
}
$api = $services->get('Omeka\ApiManager');
/** @var \Search\Api\Representation\SearchPageRepresentation[] $searchPages */
$searchPages = $api->search('search_pages')->getContent();
$isAdminRequest = $status->isAdminRequest();
if ($isAdminRequest) {
$settings = $services->get('Omeka\Settings');
$adminSearchPages = $settings->get('search_pages', []);
foreach ($searchPages as $searchPage) {
$searchPageId = $searchPage->id();
if (in_array($searchPageId, $adminSearchPages)) {
$router->addRoute(
'search-admin-page-' . $searchPageId,
[
'type' => \Laminas\Router\Http\Segment::class,
'options' => [
'route' => '/admin/' . $searchPage->path(),
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__ADMIN__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'search',
'id' => $searchPageId,
],
],
'may_terminate' => true,
'child_routes' => [
'suggest' => [
'type' => \Laminas\Router\Http\Literal::class,
'options' => [
'route' => '/suggest',
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__ADMIN__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'suggest',
'id' => $searchPageId,
],
],
],
],
],
);
}
}
}
// Public search pages are required to manage them at site level.
foreach ($searchPages as $searchPage) {
$searchPageId = $searchPage->id();
$searchPageSlug = $searchPage->path();
$router->addRoute(
'search-page-' . $searchPageId,
[
'type' => \Laminas\Router\Http\Segment::class,
'options' => [
'route' => '/s/:site-slug/' . $searchPageSlug,
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__SITE__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'search',
'id' => $searchPageId,
// Store the page slug to simplify checks.
'page-slug' => $searchPageSlug,
],
],
'may_terminate' => true,
'child_routes' => [
'suggest' => [
'type' => \Laminas\Router\Http\Literal::class,
'options' => [
'route' => '/suggest',
'defaults' => [
'__NAMESPACE__' => 'Search\Controller',
'__SITE__' => true,
'controller' => \Search\Controller\IndexController::class,
'action' => 'suggest',
'id' => $searchPageId,
// Store the page slug to simplify checks.
'page-slug' => $searchPageSlug,
],
],
],
],
],
);
}
}
/**
* Prepare a batch update to process it one time only for performance.
*
* This process avoids a bug too.
* When there is a batch update, with modules SearchSolr and NumericDataTypes,
* a bug occurs on the second call to update when the process is done in
* admin ui via batch edit selected resources and when one of the selected
* resources has a resource template: a resource template property is
* created, but it must not exist, since the event is not related to the
* resource templates (only read them). The issue occurs when SearchSolr
* tries to read values from the representation (item values extraction),
* but only when the module NumericDataTypes is used. The new ResourceTemplateProperty
* is visible via the method \Omeka\Api\Adapter\AbstractEntityAdapter::detachAllNewEntities()
* after the first update.
* This issue doesn't occurs in background batch edit all (see \Omeka\Controller\Admin\itemController::batchEditAllAction()
* and \Omeka\Job\BatchUpdate::perform()). But, conversely, when this option
* is set, it doesn't work any more for a background process, so a check is
* done to check if this is a background event.
* @todo Find where the resource template property is created. This issue may disappear de facto in a future version.
*
* @todo Clean the process with the fix in Omeka 3.1.
*
* @param Event $event
*/
public function preBatchUpdateSearchIndex(Event $event): void
{
// This is a background job if there is no route match.
$routeMatch = $this->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch();
$this->isBatchUpdate = !empty($routeMatch);
}
public function postBatchUpdateSearchIndex(Event $event): void
{
if (!$this->isBatchUpdate) {
return;
}
$serviceLocator = $this->getServiceLocator();
$api = $serviceLocator->get('Omeka\ApiManager');
$request = $event->getParam('request');
$requestResource = $request->getResource();
$response = $event->getParam('response');
$resources = $response->getContent();
/** @var \Search\Api\Representation\SearchIndexRepresentation[] $searchIndexes */
$searchIndexes = $api->search('search_indexes')->getContent();
foreach ($searchIndexes as $searchIndex) {
if (in_array($requestResource, $searchIndex->setting('resources', []))) {
$indexer = $searchIndex->indexer();
try {
$indexer->indexResources($resources);
} catch (\Exception $e) {
$services = $this->getServiceLocator();
$logger = $services->get('Omeka\Logger');
$logger->err(new Message(
'Unable to batch index metadata for search index "%s": %s', // @translate
$searchIndex->name(), $e->getMessage()
));
$messenger = $services->get('ControllerPluginManager')->get('messenger');
$messenger->addWarning(new Message(
'Unable to batch update the search index "%s": see log.', // @translate
$searchIndex->name()
));
}
}
}
$this->isBatchUpdate = false;
}
public function preUpdateSearchIndexMedia(Event $event): void
{
$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): void
{
if ($this->isBatchUpdate) {
return;
}
$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) {
if (in_array($requestResource, $searchIndex->setting('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): void
{
$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) {
if (in_array('items', $searchIndex->setting('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): void
{
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): void
{
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 handleSiteSettings(Event $event): void
{
// This is an exception, because there is already a fieldset named
// "search" in the core, so it should be named "search_module".
$services = $this->getServiceLocator();
$settingsType = 'site_settings';
$settings = $services->get('Omeka\Settings\Site');
$site = $services->get('ControllerPluginManager')->get('currentSite');
$id = $site()->id();
$this->initDataToPopulate($settings, $settingsType, $id);
$data = $this->prepareDataToPopulate($settings, $settingsType);
if (is_null($data)) {
return;
}
$space = 'search_module';
$fieldset = $services->get('FormElementManager')->get(\Search\Form\SiteSettingsFieldset::class);
$fieldset->setName($space);
$form = $event->getTarget();
$form->add($fieldset);
$form->get($space)->populateValues($data);
}
/**
* Add the headers.
*
* @param Event $event
*/
public function addHeaders(Event $event): void
{
// The admin search field is added via a js hack, because the admin
// layout doesn't use a partial or a trigger for the sidebar.
$view = $event->getTarget();
$plugins = $view->getHelperPluginManager();
/** @var \Omeka\Mvc\Status $status */
$status = $plugins->get('status');
if ($status->isSiteRequest()) {
$params = $view->params()->fromRoute();
if ($params['controller'] === \Search\Controller\IndexController::class) {
$searchPage = @$params['id'];
} else {
$searchPage = $view->siteSetting('search_main_page');
}
} elseif ($status->isAdminRequest()) {
$searchPage = $view->setting('search_main_page');
} else {
return;
}
if (!$searchPage) {
return;
}
/** @var \Search\Api\Representation\SearchPageRepresentation $searchPage */
$searchPage = $plugins->get('api')->searchOne('search_pages', ['id' => $searchPage])->getContent();
if (!$searchPage) {
return;
}
$formAdapter = $searchPage->formAdapter();
$partialHeaders = $formAdapter ? $formAdapter->getFormPartialHeaders() : null;
if ($status->isAdminRequest()) {
$basePath = $plugins->get('basePath');
$assetUrl = $plugins->get('assetUrl');
$searchUrl = $basePath('admin/' . $searchPage->path());
if ($searchPage->subSetting('autosuggest', 'enable')) {
$autoSuggestUrl = $searchPage->subSetting('autosuggest', 'url') ?: $searchUrl . '/suggest';
}
$plugins->get('headLink')
->appendStylesheet($assetUrl('css/search-admin-search.css', 'Search'));
$plugins->get('headScript')
->appendScript(sprintf('var searchUrl = %s;', json_encode($searchUrl, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))
. (isset($autoSuggestUrl) ? sprintf("\nvar searchAutosuggestUrl=%s;", json_encode($autoSuggestUrl, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) : '')
)
->appendFile($assetUrl('js/search-admin-search.js', 'Search'), 'text/javascript', ['defer' => 'defer']);
}
if (!$partialHeaders) {
return;
}
// No echo: it should just be a preload.
$view->vars()->offsetSet('searchPage', $searchPage);
$view->partial($partialHeaders);
}
public function addSearchPageToSite(Event $event): void
{
/**
* @var \Omeka\Settings\Settings $settings
* @var \Omeka\Settings\SiteSettings $siteSettings
* @var \Omeka\Mvc\Controller\Plugin\Api $api
*
* @var \Omeka\Api\Representation\SiteRepresentation $site
* @var \Search\Api\Representation\SearchPageRepresentation $searchPage
*/
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$siteSettings = $services->get('Omeka\Settings\Site');
$api = $services->get('ControllerPluginManager')->get('api');
$site = null;
$searchPage = null;
// Take the search page of the default site or the first site, else the
// default search page.
$defaultSite = (int) $settings->get('default_site');
if ($defaultSite) {
$site = $api->searchOne('sites', ['id' => $defaultSite])->getContent();
}
if ($site) {
$siteSettings->setTargetId($site->id());
$searchPageId = (int) $siteSettings->get('search_main_page');
} else {
$searchPageId = (int) $settings->get('search_main_page');
}
if ($searchPageId) {
$searchPage = $api->searchOne('search_pages', ['id' => $searchPageId])->getContent();
}
if (!$searchPage) {
$searchPage = $api->searchOne('search_pages')->getContent();
}
if (!$searchPage) {
$searchPageId = $this->createDefaultSearchPage();
$searchPage = $api->searchOne('search_pages', ['id' => $searchPageId])->getContent();
}
/** @var \Omeka\Entity\Site $site */
$site = $event->getParam('response')->getContent();
$siteSettings->setTargetId($site->getId());
$siteSettings->set('search_main_page', $searchPage->id());
$siteSettings->set('search_pages', [$searchPage->id()]);
$siteSettings->set('search_redirect_itemset', true);
}
protected function installResources(): void
{
$this->createDefaultSearchPage();
}
protected function createDefaultSearchPage(): int
{
// 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 = new Messenger;
/** @var \Doctrine\DBAL\Connection $connection */
$connection = $services->get('Omeka\Connection');
// Check if the internal index exists.
$sqlSearchIndexId = <<<'SQL'
SELECT `id`
FROM `search_index`
WHERE `adapter` = "internal"
ORDER BY `id`;
SQL;
$searchIndexId = (int) $connection->fetchColumn($sqlSearchIndexId);
if (!$searchIndexId) {
// Create the internal adapter.
$sql = <<<'SQL'
INSERT INTO `search_index`
(`name`, `adapter`, `settings`, `created`)
VALUES
('Internal (sql)', 'internal', ?, NOW());
SQL;
$searchIndexSettings = ['resources' => ['items', 'item_sets']];
$connection->executeQuery($sql, [
json_encode($searchIndexSettings, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$searchIndexId = $connection->fetchColumn($sqlSearchIndexId);
$message = new Message(
'The internal search engine (sql) is available. Configure it in the %ssearch manager%s.', // @translate
// Don't use the url helper, the route is not available during install.
sprintf('<a href="%s">', $urlHelper('admin') . '/search-manager'),
'</a>'
);
$message->setEscapeHtml(false);
$messenger->addSuccess($message);
}
// Check if the default search page exists.
$sqlSearchPageId = <<<SQL
SELECT `id`
FROM `search_page`
WHERE `index_id` = $searchIndexId
ORDER BY `id`;
SQL;
$searchPageId = (int) $connection->fetchColumn($sqlSearchPageId);
if (!$searchPageId) {
$sql = <<<SQL
INSERT INTO `search_page`
(`index_id`, `name`, `path`, `form_adapter`, `settings`, `created`)
VALUES
($searchIndexId, 'Default', 'find', 'main', ?, NOW());
SQL;
$searchPageSettings = require __DIR__ . '/data/search_pages/default.php';
$connection->executeQuery($sql, [
json_encode($searchPageSettings, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
]);
$message = new Message(
'The default search page is available. Configure it in the %ssearch manager%s, in the main settings (for admin) and in site settings (for public).', // @translate
// Don't use the url helper, the route is not available during install.
sprintf('<a href="%s">', $urlHelper('admin') . '/search-manager'),
'</a>'
);
$message->setEscapeHtml(false);
$messenger->addSuccess($message);
}
return $searchPageId;
}
}