-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
129 lines (117 loc) · 4.39 KB
/
Copy pathModule.php
File metadata and controls
129 lines (117 loc) · 4.39 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
<?php
namespace DemoData;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Api\Exception\NotFoundException;
use Omeka\Module\AbstractModule;
class Module extends AbstractModule
{
private const VOCAB_NS_URI = 'https://omeka.org/s/vocabs/demo-data#';
public function getConfig()
{
return include sprintf('%s/config/module.config.php', __DIR__);
}
public function install(ServiceLocatorInterface $services)
{
$api = $services->get('Omeka\ApiManager');
$existing = $api->search('vocabularies', [
'namespace_uri' => self::VOCAB_NS_URI,
'limit' => 0,
]);
if (0 === $existing->getTotalResults()) {
$services->get('Omeka\RdfImporter')->import(
'file',
[
'o:namespace_uri' => self::VOCAB_NS_URI,
'o:prefix' => 'demo-data',
'o:label' => 'Demo Data',
'o:comment' => 'Shared vocabulary for the Demo Data module.',
],
[
'file' => $this->vocabFile(),
'format' => 'turtle',
]
);
} else {
$this->syncVocab($services);
}
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services)
{
// syncVocab is safe to run on every upgrade: it adds new terms from the
// N3 file but never deletes existing ones, so it cannot break items
// that already use the vocabulary.
$this->syncVocab($services);
}
public function uninstall(ServiceLocatorInterface $services)
{
$api = $services->get('Omeka\ApiManager');
$settings = $services->get('Omeka\Settings');
$config = $services->get('Config');
$jobDispatcher = $services->get('Omeka\JobDispatcher');
// Without cleanup, imported resources become orphaned on reinstall and
// block re-import without manual deletion.
$allItemIds = [];
$allItemSetIds = [];
$allTemplateIds = [];
foreach (array_keys($config['demo_data']['datasets']) as $dataset) {
$tracking = $settings->get("demo_data_imported_{$dataset}");
if ($tracking) {
$allItemIds = array_merge($allItemIds, $tracking['items'] ?? []);
$allItemSetIds = array_merge($allItemSetIds, array_values($tracking['item_sets'] ?? []));
if (!empty($tracking['resource_template'])) {
$allTemplateIds[] = $tracking['resource_template'];
}
}
}
if ($allItemIds) {
$jobDispatcher->dispatch(\Omeka\Job\BatchDelete::class, [
'resource' => 'items',
'query' => ['id' => $allItemIds],
]);
}
if ($allItemSetIds) {
$jobDispatcher->dispatch(\Omeka\Job\BatchDelete::class, [
'resource' => 'item_sets',
'query' => ['id' => $allItemSetIds],
]);
}
foreach ($allTemplateIds as $templateId) {
try {
$api->delete('resource_templates', $templateId);
} catch (NotFoundException $e) {
// Already gone.
}
}
$vocabs = $api->search('vocabularies', [
'namespace_uri' => self::VOCAB_NS_URI,
'limit' => 1,
])->getContent();
if ($vocabs) {
$api->delete('vocabularies', $vocabs[0]->id());
}
foreach (array_keys($config['demo_data']['datasets']) as $dataset) {
$settings->delete("demo_data_imported_{$dataset}");
$settings->delete("demo_data_job_{$dataset}");
}
}
private function syncVocab(ServiceLocatorInterface $services): void
{
$rdfImporter = $services->get('Omeka\RdfImporter');
$vocabs = $services->get('Omeka\ApiManager')->search('vocabularies', [
'namespace_uri' => self::VOCAB_NS_URI,
'limit' => 1,
])->getContent();
if (!$vocabs) {
return;
}
$diff = $rdfImporter->getDiff('file', self::VOCAB_NS_URI, [
'file' => $this->vocabFile(),
'format' => 'turtle',
]);
$rdfImporter->update($vocabs[0]->id(), $diff);
}
private function vocabFile(): string
{
return sprintf('%s/vocabs/demo-data.n3', __DIR__);
}
}