-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.php
379 lines (340 loc) · 14.1 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php declare(strict_types=1);
namespace BulkImport;
if (!class_exists(\Common\TraitModule::class)) {
require_once dirname(__DIR__) . '/Common/TraitModule.php';
}
use Common\Stdlib\PsrMessage;
use Common\TraitModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ModuleManager\ModuleManager;
use Omeka\Entity\Media;
use Omeka\Module\AbstractModule;
/**
* Bulk Import
*
* @copyright Daniel Berthereau, 2018-2024
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*/
class Module extends AbstractModule
{
use TraitModule;
const NAMESPACE = __NAMESPACE__;
protected $dependencies = [
'Common',
'Log',
];
public function init(ModuleManager $moduleManager): void
{
require_once __DIR__ . '/vendor/autoload.php';
}
protected function preInstall(): void
{
$services = $this->getServiceLocator();
$translate = $services->get('ControllerPluginManager')->get('translate');
$translator = $services->get('MvcTranslator');
if (!method_exists($this, 'checkModuleActiveVersion') || !$this->checkModuleActiveVersion('Common', '3.4.60')) {
$message = new \Omeka\Stdlib\Message(
$translate('The module %1$s should be upgraded to version %2$s or later.'), // @translate
'Common', '3.4.60'
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message);
}
$config = $services->get('Config');
$basePath = $config['file_store']['local']['base_path'] ?: (OMEKA_PATH . '/files');
if (!$this->checkDestinationDir($basePath . '/xsl')) {
$message = new PsrMessage(
'The directory "{directory}" is not writeable.', // @translate
['directory' => $basePath . '/xsl']
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message->setTranslator($translator));
}
if (!$this->checkDestinationDir($basePath . '/bulk_import')) {
$message = new PsrMessage(
'The directory "{directory}" is not writeable.', // @translate
['directory' => $basePath . '/bulk_import']
);
throw new \Omeka\Module\Exception\ModuleCannotInstallException((string) $message->setTranslator($translator));
}
}
protected function postInstall(): void
{
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$user = $services->get('Omeka\AuthenticationService')->getIdentity();
// The module api is not available during install/upgrade.
require_once __DIR__ . '/src/Entity/Import.php';
require_once __DIR__ . '/src/Entity/Importer.php';
$directory = new \RecursiveDirectoryIterator(__DIR__ . '/data/importers', \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($directory);
$inflector = \Doctrine\Inflector\InflectorFactory::create()->build();
foreach (array_keys(iterator_to_array($iterator)) as $filepath) {
$data = include $filepath;
$data['o:owner'] = $user;
$entity = new \BulkImport\Entity\Importer();
foreach ($data as $key => $value) {
$posColon = strpos($key, ':');
$keyName = $posColon === false ? $key : substr($key, $posColon + 1);
$method = 'set' . ucfirst($inflector->camelize($keyName));
if (method_exists($entity, $method)) {
$entity->$method($value);
}
}
$entityManager->persist($entity);
}
$entityManager->flush();
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
$sharedEventManager->attach(
\Omeka\Media\Ingester\Manager::class,
'service.registered_names',
[$this, 'handleMediaIngesterRegisteredNames']
);
// Manage the extraction of medata from medias.
// The process should be done only for new medias, so keep the list
// of existing medias before processing.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.pre',
[$this, 'handleBeforeSaveItem'],
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.post',
[$this, 'handleAfterSaveItem'],
-10
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.post',
[$this, 'handleAfterSaveItem'],
-10
);
// TODO Check if the listener "api.create.post" for media is still needed.
$sharedEventManager->attach(
\Omeka\Api\Adapter\MediaAdapter::class,
'api.create.post',
[$this, 'handleAfterCreateMedia'],
-10
);
$sharedEventManager->attach(
\Omeka\Form\SettingForm::class,
'form.add_elements',
[$this, 'handleMainSettings']
);
}
/**
* Avoid to display ingester in item edit, because it's an internal one.
*/
public function handleMediaIngesterRegisteredNames(Event $event): void
{
$names = $event->getParam('registered_names');
$key = array_search('bulk', $names);
unset($names[$key]);
$event->setParam('registered_names', $names);
}
/**
* Store ids of existing medias to avoid to process them twice.
*/
public function handleBeforeSaveItem(Event $event): void
{
/**
* @var \Omeka\Api\Request $request
*/
$request = $event->getParam('request');
$itemId = (int) $request->getId();
if (!$itemId) {
return;
}
/** @var \Omeka\Api\Manager $api */
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
try {
/** @var \Omeka\Api\Representation\ItemRepresentation $item */
$item = $api->read('items', $itemId, [], ['initialize' => false, 'finalize' => false])->getContent();
} catch (\Exception $e) {
return;
}
$mediaIds = [];
foreach ($item->getMedia() as $media) {
$mediaId = (int) $media->getId() ?? null;;
if ($mediaId) {
$mediaIds[$mediaId] = $mediaId;
}
}
$this->storeExistingItemMediaIds($itemId, $mediaIds);
}
public function handleAfterSaveItem(Event $event): void
{
// Process extraction of metadata only when there is an original file.
$hasFile = false;
/**
* @var \Omeka\Entity\Item $item
* @var \Omeka\Entity\Media $media
*/
$item = $event->getParam('response')->getContent();
foreach ($item->getMedia() as $media) {
if (!$media->getMediaType()) {
continue;
}
$hasFile = true;
}
$services = $this->getServiceLocator();
if ($hasFile
&& $services->get('Omeka\Settings')->get('bulkimport_extract_metadata', false)
) {
$itemId = $item->getId();
// Run a job for item to avoid the 30 seconds issue with many files.
$args = [
'item_id' => $itemId,
'skip_media_ids' => $this->storeExistingItemMediaIds($itemId),
];
// FIXME Use a plugin, not a fake job. Or strategy "sync", but there is a doctrine exception on owner of the job.
// Of course, it is useless for a background job.
// $strategy = $this->isBackgroundProcess() ? $services->get(\Omeka\Job\DispatchStrategy\Synchronous::class) : null;
$strategy = null;
if ($this->isBackgroundProcess()) {
$job = new \Omeka\Entity\Job();
$job->setPid(null);
$job->setStatus(\Omeka\Entity\Job::STATUS_IN_PROGRESS);
$job->setClass(\BulkImport\Job\ExtractMediaMetadata::class);
$job->setArgs($args);
$job->setOwner($services->get('Omeka\AuthenticationService')->getIdentity());
$job->setStarted(new \DateTime('now'));
$jobClass = new \BulkImport\Job\ExtractMediaMetadata($job, $services);
$jobClass->perform();
} else {
/** @var \Omeka\Job\Dispatcher $dispatcher */
$dispatcher = $services->get(\Omeka\Job\Dispatcher::class);
$dispatcher->dispatch(\BulkImport\Job\ExtractMediaMetadata::class, $args, $strategy);
}
}
}
/**
* Create metadata for file.
*
* Normally, this method is never processed directly, only via item.
*
* @param \Laminas\EventManager\Event $event
*/
public function handleAfterCreateMedia(Event $event): void
{
/** @var \Omeka\Entity\Media $media */
$media = $event->getParam('response')->getContent();
if ($media->getMediaType()) {
$this->afterSaveMedia($media, true);
}
}
/**
* @param Media $media Media with a media type and not already processed.
*/
protected function afterSaveMedia(Media $media): void
{
static $processedMedia = [];
$mediaId = $media->getId();
if (!$mediaId || isset($processedMedia[$mediaId])) {
return;
}
$processedMedia[$mediaId] = true;
$itemId = (int) $media->getItem()->getId();
if (!$itemId) {
return;
}
/**
* @var \Doctrine\ORM\EntityManager $entityManager
* @var \Omeka\Settings\Settings $settings
* @var \Common\Stdlib\EasyMeta $easyMeta
* @var \BulkImport\Mvc\Controller\Plugin\ExtractMediaMetadata $extractMediaMetadata
*
* @see \BulkImport\Job\ExtractMediaMetadata
*/
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$settings = $services->get('Omeka\Settings');
if ($settings->get('bulkimport_extract_metadata', false)) {
$plugins = $services->get('ControllerPluginManager');
$easyMeta = $plugins->get('$easyMeta');
$extractMediaMetadata = $plugins->get('extractMediaMetadata');
$extractedData = $extractMediaMetadata->__invoke($media);
if ($extractedData) {
/** TODO Remove for Omeka v4. */
if (!function_exists('array_key_last')) {
function array_key_last(array $array)
{
return empty($array) ? null : key(array_slice($array, -1, 1, true));
}
}
// Convert the extracted metadata into properties and resource.
// TODO Move ResourceProcessor process into a separated Filler to be able to use it anywhere.
// For now, just manage resource class, template and properties without check neither linked resource.
$data = [];
foreach ($extractedData as $dest => $values) {
// TODO Reconvert dest.
$field = strtok($dest, ' ');
if ($field === 'o:resource_class') {
$value = array_key_last($values);
$id = $easyMeta->resourceClassId($value);
$data['o:resource_class'] = $id ? ['o:id' => $id] : null;
} elseif ($field === 'o:resource_template') {
$value = array_key_last($values);
$id = $easyMeta->resourceTemplateId($value);
$data['o:resource_template'] = $id ? ['o:id' => $id] : null;
} elseif (isset($propertyIds[$field])) {
$data[$field] = [];
$values = array_unique($values);
foreach ($values as $value) {
$data[$field][] = [
'type' => 'literal',
'property_id' => $propertyIds[$field],
'is_public' => true,
'@value' => $value,
];
}
}
}
if ($data) {
try {
$services->get('Omeka\ApiManager')->update('media', ['id' => $mediaId], $data, [], ['isPartial' => true]);
$services->get('Omeka\Logger')->notice(
'Data extracted for media #{media_id}.', // @translate
['media_id' => $mediaId]
);
} catch (\Exception $e) {
$services->get('Omeka\Logger')->err(
'Media #{media_id}: an issue occurred during update: {exception}.', // @translate
['media_id' => $mediaId, 'exception' => $e]
);
}
}
}
}
}
/**
* Check if the current process is a background one.
*
* The library to get status manages only admin, site or api requests.
* A background process is none of them.
*/
protected function isBackgroundProcess(): bool
{
// Warning: there is a matched route ("site") for backend processes.
/** @var \Omeka\Mvc\Status $status */
$status = $this->getServiceLocator()->get('Omeka\Status');
return !$status->isApiRequest()
&& !$status->isAdminRequest()
&& !$status->isSiteRequest()
&& (!method_exists($status, 'isKeyauthRequest') || !$status->isKeyauthRequest());
}
protected function storeExistingItemMediaIds(?int $itemId = null, ?array $mediaIds = null): ?array
{
static $store = [];
if (!$itemId) {
return $store;
}
if (is_null($mediaIds)) {
return $store[$itemId] ?? [];
}
$store[$itemId] = $mediaIds;
return null;
}
}