forked from Daniel-KM/Omeka-S-module-OaiPmhHarvester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
92 lines (83 loc) · 2.8 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
<?php declare(strict_types=1);
namespace OaiPmhHarvester;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Module\AbstractModule;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function install(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
$this->execSqlFromFile(__DIR__ . '/data/install/schema.sql');
}
public function uninstall(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
$this->execSqlFromFile(__DIR__ . '/data/install/uninstall.sql');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services): void
{
$serviceLocator = $services;
$this->setServiceLocator($serviceLocator);
require_once __DIR__ . '/data/scripts/upgrade.php';
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// Manage the deletion of an item.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.delete.pre',
[$this, 'handleBeforeDelete'],
);
}
/**
* Execute a sql from a file.
*
* @param string $filepath
* @return mixed
*/
protected function execSqlFromFile($filepath)
{
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
$sql = file_get_contents($filepath);
$sqls = array_filter(array_map('trim', explode(";\n", $sql)));
foreach ($sqls as $sql) {
$result = $connection->executeStatement($sql);
}
return $result;
}
public function handleBeforeDelete(Event $event): void
{
/**
* @var \Omeka\Api\Manager $api
* @var \Omeka\Api\Request $request
*/
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$resourceId = $request->getId();
$resourceName = $request->getResource();
try {
$api
->delete(
'oaipmhharvester_entities',
[
'entityId' => $resourceId,
'entityName' => $resourceName,
],
[],
[
// The flush is automatically done on main resource
// execution, or skipped when failing.
'flushEntityManager' => false,
]
);
} catch (\Omeka\Api\Exception\NotFoundException $e) {
}
}
}