|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NodeGraph; |
| 4 | + |
| 5 | +use Omeka\Module\AbstractModule; |
| 6 | +use Laminas\Mvc\MvcEvent; |
| 7 | +use Laminas\EventManager\SharedEventManagerInterface; |
| 8 | +use Omeka\Entity\SitePage; |
| 9 | +use Omeka\Api\Representation\SitePageRepresentation; |
| 10 | +use Laminas\EventManager\Event; |
| 11 | + |
| 12 | + |
| 13 | +class Module extends AbstractModule |
| 14 | +{ |
| 15 | + |
| 16 | + public function getConfig(): array |
| 17 | + { |
| 18 | + return include __DIR__ . '/config/module.config.php'; |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | + public function onBootstrap(MvcEvent $event): void |
| 23 | + { |
| 24 | + // IMPORTANT: ensure base class runs (this is what normally calls attachListeners()). |
| 25 | + parent::onBootstrap($event); |
| 26 | + |
| 27 | + $services = $event->getApplication()->getServiceManager(); |
| 28 | + $viewHelperManager = $services->get('ViewHelperManager'); |
| 29 | + $headScript = $viewHelperManager->get('headScript'); |
| 30 | + $headLink = $viewHelperManager->get('headLink'); |
| 31 | + |
| 32 | + //Sigma js |
| 33 | + $headScript |
| 34 | + ->appendFile('https://cdnjs.cloudflare.com/ajax/libs/graphology/0.24.0/graphology.umd.min.js') |
| 35 | + ->appendFile('https://cdn.jsdelivr.net/npm/graphology-library@0.8.0/dist/graphology-library.min.js') |
| 36 | + ->appendFile('https://cdnjs.cloudflare.com/ajax/libs/sigma.js/3.0.2/sigma.min.js'); |
| 37 | + } |
| 38 | + |
| 39 | + public function attachListeners(SharedEventManagerInterface $sem): void |
| 40 | + { |
| 41 | + // After API save (create/update) of site pages, queue builds. |
| 42 | + $sem->attach('*', 'api.create.post', [$this, 'onPageSaved']); |
| 43 | + $sem->attach('*', 'api.update.post', [$this, 'onPageSaved']); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public function onPageSaved(Event $event): void |
| 48 | + { |
| 49 | + $services = $this->getServiceLocator(); |
| 50 | + $response = $event->getParam('response'); |
| 51 | + $content = $response ? $response->getContent() : null; |
| 52 | + |
| 53 | + $pages = is_array($content) ? $content : ($content ? [$content] : []); |
| 54 | + $adapter = $services->get('Omeka\ApiAdapterManager')->get('site_pages'); |
| 55 | + $dispatcher = $services->get(\Omeka\Job\Dispatcher::class); |
| 56 | + $conn = $services->get('Omeka\Connection'); |
| 57 | + |
| 58 | + foreach ($pages as $page) { |
| 59 | + if ($page instanceof SitePage) { |
| 60 | + $page = $adapter->getRepresentation($page); |
| 61 | + } |
| 62 | + if (!$page instanceof SitePageRepresentation) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($page->blocks() as $blockRep) { |
| 67 | + // Only Node graph block |
| 68 | + if ($blockRep->layout() !== 'Node Graph') { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $data = $blockRep->data(); |
| 73 | + $cacheEnabled = !empty($data['cache_result']); |
| 74 | + if (!$cacheEnabled) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // If caching is on, compute hash of inputs that affect output |
| 79 | + $hash = sha1(json_encode($data)); |
| 80 | + $blockId = (int) $blockRep->id(); |
| 81 | + |
| 82 | + // If cache already exist, delete and rebuild |
| 83 | + $conn->executeStatement( |
| 84 | + 'DELETE FROM nodegraph_cache WHERE block_id = ? AND hash = ?', |
| 85 | + [$blockId, $hash] |
| 86 | + ); |
| 87 | + |
| 88 | + // Queue job to (re)build the cache |
| 89 | + $dispatcher->dispatch(\NodeGraph\Job\BuildGraph::class, [ |
| 90 | + 'block_id' => $blockId, |
| 91 | + 'hash' => $hash, |
| 92 | + 'page_id' => $page->id(), |
| 93 | + ]); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public function install($serviceLocator) |
| 99 | + { |
| 100 | + $conn = $serviceLocator->get('Omeka\Connection'); |
| 101 | + $conn->executeStatement(' |
| 102 | + CREATE TABLE IF NOT EXISTS nodegraph_cache ( |
| 103 | + block_id INT NOT NULL PRIMARY KEY, |
| 104 | + hash VARCHAR(40) NOT NULL, |
| 105 | + payload LONGTEXT NOT NULL, |
| 106 | + updated DATETIME NOT NULL |
| 107 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 108 | + '); |
| 109 | + } |
| 110 | + |
| 111 | + public function uninstall($serviceLocator) |
| 112 | + { |
| 113 | + $conn = $serviceLocator->get('Omeka\Connection'); |
| 114 | + $conn->executeStatement('DROP TABLE IF EXISTS nodegraph_cache;'); |
| 115 | + } |
| 116 | +} |
0 commit comments