-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
189 lines (168 loc) · 7.75 KB
/
Module.php
File metadata and controls
189 lines (168 loc) · 7.75 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
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
<?php
namespace WebArchive;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Form\Element\Select;
use Laminas\Form\Element\Url;
use Laminas\Mvc\Controller\AbstractController;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Renderer\PhpRenderer;
use Omeka\Module\AbstractModule;
use WebArchive\Form\ConfigForm;
class Module extends AbstractModule
{
const MEDIA_TYPES = ['application/wacz', 'application/warc'];
const EMBED_MODES = [
'default' => 'Default', // @translate
'full' => 'Full', // @translate
'replayonly' => 'Replay only', // @translate
'replay-with-info' => 'Replay with info', // @translate
];
public function getConfig()
{
return include sprintf('%s/config/module.config.php', __DIR__);
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$settings = $serviceLocator->get('Omeka\Settings');
$extensionWhitelist = $settings->get('extension_whitelist', []);
// "gz" is needed to allow .warc.gz uploads (Omeka resolves the extension from the last dot only)
foreach (['wacz', 'warc', 'gz'] as $ext) {
if (!in_array($ext, $extensionWhitelist)) {
$extensionWhitelist[] = $ext;
}
}
$settings->set('extension_whitelist', $extensionWhitelist);
$mediaTypeWhitelist = $settings->get('media_type_whitelist', []);
// "application/gzip" is needed because WARC files may be gzip-compressed
foreach (['application/wacz', 'application/warc', 'application/gzip'] as $type) {
if (!in_array($type, $mediaTypeWhitelist)) {
$mediaTypeWhitelist[] = $type;
}
}
$settings->set('media_type_whitelist', $mediaTypeWhitelist);
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$settings = $serviceLocator->get('Omeka\Settings');
$settings->delete('webarchive_embed_mode');
}
public function getConfigForm(PhpRenderer $renderer)
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$form->setData([
'webarchive_embed_mode' => $settings->get('webarchive_embed_mode', 'default'),
]);
return $renderer->formCollection($form, false);
}
public function handleConfigForm(AbstractController $controller)
{
$services = $this->getServiceLocator();
$settings = $services->get('Omeka\Settings');
$form = $services->get('FormElementManager')->get(ConfigForm::class);
$form->setData($controller->params()->fromPost());
if (!$form->isValid()) {
$controller->messenger()->addErrors($form->getMessages());
return false;
}
$formData = $form->getData();
$settings->set('webarchive_embed_mode', $formData['webarchive_embed_mode']);
return true;
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
'Omeka\Api\Adapter\MediaAdapter',
'api.hydrate.post',
[$this, 'handleWebArchiveHydration']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Media',
'view.edit.form.advanced',
[$this, 'addMediaFields']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Media',
'view.show.after',
[$this, 'showMediaFields']
);
}
public function handleWebArchiveHydration(Event $event)
{
$entity = $event->getParam('entity');
$request = $event->getParam('request');
// Correct MIME types on CREATE: finfo misdetects these formats, and the file
// validator runs before this event, so we accept broad types and correct here.
if ($request->getOperation() === 'create') {
// WACZ is ZIP-based; finfo may detect it as application/zip or other types. Correct by extension.
if ($entity->getExtension() === 'wacz'
&& $entity->getMediaType() !== 'application/wacz'
) {
$entity->setMediaType('application/wacz');
}
// Modern libmagic detects uncompressed WARCs as application/warc; gzip-compressed WARCs
// as application/gzip. Older libmagic may return application/octet-stream. Correct by extension.
if ($entity->getExtension() === 'warc'
&& $entity->getMediaType() !== 'application/warc'
) {
$entity->setMediaType('application/warc');
}
// .warc.gz files have extension 'gz'; detect by source filename
if ($entity->getMediaType() === 'application/gzip'
&& str_ends_with($entity->getSource(), '.warc.gz')
) {
$entity->setMediaType('application/warc');
}
}
// Persist per-media fields on UPDATE
if ($request->getOperation() === 'update' && in_array($entity->getMediaType(), self::MEDIA_TYPES)) {
$content = $request->getContent();
$data = $entity->getData() ?? [];
if (array_key_exists('webarchive_start_url', $content)) {
$data['start_url'] = $content['webarchive_start_url'] ?: null;
}
if (array_key_exists('webarchive_embed_mode', $content)) {
$data['embed_mode'] = $content['webarchive_embed_mode'] ?: null;
}
$entity->setData($data);
}
}
public function addMediaFields(Event $event)
{
$view = $event->getTarget();
$media = $view->resource;
if (!$media || !in_array($media->mediaType(), self::MEDIA_TYPES)) {
return;
}
$mediaData = $media->mediaData() ?? [];
$startUrl = new Url('webarchive_start_url');
$startUrl->setLabel('Starting URL') // @translate
->setOption('info', 'Enter the original URL of the page to open first. Leave blank to show the archive\'s pages list, where viewers can browse all captured pages. Required if embed mode is set to "Replay only".') // @translate
->setAttribute('id', 'web-archive-start-url')
->setValue($mediaData['start_url'] ?? '');
$embedMode = new Select('webarchive_embed_mode');
$embedMode->setLabel('Embed mode') // @translate
->setOption('info', 'Controls what the player shows around the archived content. "Default" and "Full" both show the full interface with navigation; "Full" differs only in sizing behavior. "Replay only" shows just the archived page with no controls, and requires a starting URL to be meaningful. "Replay with info" shows the archived page alongside a metadata panel with title, date, and source URL. Leave blank to use the site default.') // @translate
->setAttribute('id', 'web-archive-embed-mode')
->setEmptyOption('[Site default]') // @translate
->setValueOptions(self::EMBED_MODES)
->setValue($mediaData['embed_mode'] ?? '');
echo $view->partial('common/media-fields-edit', ['elements' => [$startUrl, $embedMode]]);
}
public function showMediaFields(Event $event)
{
$view = $event->getTarget();
$media = $view->media;
if (!$media || !in_array($media->mediaType(), self::MEDIA_TYPES)) {
return;
}
$mediaData = $media->mediaData() ?? [];
echo $view->partial('common/media-fields-show', [
'startUrl' => $mediaData['start_url'] ?? null,
'embedMode' => $mediaData['embed_mode'] ?? null,
'embedModes' => self::EMBED_MODES,
]);
}
}