-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
140 lines (125 loc) · 4.3 KB
/
Module.php
File metadata and controls
140 lines (125 loc) · 4.3 KB
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
<?php
namespace NodeGraph;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Api\Representation\SitePageRepresentation;
use Omeka\Entity\SitePage;
use Omeka\Module\AbstractModule;
/**
* Node Graph module for Omeka S.
*
* Provides interactive sigma.js-based graph visualizations as site page blocks.
*/
class Module extends AbstractModule
{
/**
* @return array
*/
public function getConfig(): array
{
return include __DIR__ . '/config/module.config.php';
}
/**
* Attach event listeners for cache invalidation on page save.
*
* @param SharedEventManagerInterface $sem
* @return void
*/
public function attachListeners(SharedEventManagerInterface $sem): void
{
$sem->attach('*', 'api.create.post', [$this, 'onPageSaved']);
$sem->attach('*', 'api.update.post', [$this, 'onPageSaved']);
}
/**
* Handle page save events to rebuild graph cache when needed.
*
* @param Event $event
* @return void
*/
public function onPageSaved(Event $event): void
{
$services = $this->getServiceLocator();
$response = $event->getParam('response');
$content = $response ? $response->getContent() : null;
$pages = is_array($content) ? $content : ($content ? [$content] : []);
$adapter = $services->get('Omeka\ApiAdapterManager')->get('site_pages');
$dispatcher = $services->get(\Omeka\Job\Dispatcher::class);
$conn = $services->get('Omeka\Connection');
foreach ($pages as $page) {
if ($page instanceof SitePage) {
$page = $adapter->getRepresentation($page);
}
if (!$page instanceof SitePageRepresentation) {
continue;
}
foreach ($page->blocks() as $blockRep) {
if ($blockRep->layout() !== 'Node Graph') {
continue;
}
$data = $blockRep->data();
$cacheEnabled = !empty($data['cache_result']);
if (!$cacheEnabled) {
continue;
}
$hash = sha1(json_encode($data));
$blockId = (int) $blockRep->id();
$conn->executeStatement(
'DELETE FROM nodegraph_cache WHERE block_id = ? AND hash = ?',
[$blockId, $hash],
);
$dispatcher->dispatch(\NodeGraph\Job\BuildGraph::class, [
'block_id' => $blockId,
'hash' => $hash,
'page_id' => $page->id(),
]);
}
}
}
/**
* Create the module's database schema.
*
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function install(ServiceLocatorInterface $serviceLocator): void
{
$conn = $serviceLocator->get('Omeka\Connection');
$conn->executeStatement('
CREATE TABLE IF NOT EXISTS nodegraph_cache (
block_id INT NOT NULL PRIMARY KEY,
hash VARCHAR(40) NOT NULL,
payload LONGTEXT NOT NULL,
updated DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
');
}
/**
* Remove the module's database schema.
*
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function uninstall(ServiceLocatorInterface $serviceLocator): void
{
$conn = $serviceLocator->get('Omeka\Connection');
$conn->executeStatement('DROP TABLE IF EXISTS nodegraph_cache;');
}
/**
* Handle version upgrades with any required schema migrations.
*
* @param string $oldVersion
* @param string $newVersion
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator): void
{
// Future migrations go here, keyed by version comparison.
// Example:
// if (version_compare($oldVersion, '1.1.0', '<')) {
// $conn = $serviceLocator->get('Omeka\Connection');
// $conn->executeStatement('ALTER TABLE ...');
// }
}
}