forked from Numerizen/OaiPmhHarvester
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.php
More file actions
300 lines (267 loc) · 10.3 KB
/
Copy pathModule.php
File metadata and controls
300 lines (267 loc) · 10.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
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
<?php declare(strict_types=1);
namespace OaiPmhHarvester;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Omeka\Module\AbstractModule;
use Omeka\Stdlib\Message;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event): void
{
parent::onBootstrap($event);
/** @var \Omeka\Permissions\Acl $acl */
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$roles = $acl->getRoles();
$acl
->allow(
$roles,
[
\OaiPmhHarvester\Api\Adapter\HarvestAdapter::class,
],
['read', 'search']
)
;
}
public function install(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
$plugins = $services->get('ControllerPluginManager');
$messenger = $plugins->get('messenger');
$settings = $services->get('Omeka\Settings');
$this->execSqlFromFile(__DIR__ . '/data/install/schema.sql');
$config = $services->get('Config');
$basePath = $config['file_store']['local']['base_path'] ?: (OMEKA_PATH . '/files');
if (!is_dir($basePath) || !is_readable($basePath) || !is_writeable($basePath)) {
$message = new Message(
'The directory "%s" is not writeable, so the oai-pmh xml responses won’t be storable.', // @translate
$basePath
);
$messenger->addWarning($message);
}
$dir = $basePath . '/oai-pmh-harvest';
if (!file_exists($dir)) {
mkdir($dir);
}
$searchFields = $settings->get('advancedsearch_search_fields');
if ($searchFields !== null) {
$searchFields[] = 'common/advanced-search/harvests';
$settings->set('advancedsearch_search_fields', $searchFields);
}
}
public function uninstall(ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
$this->execSqlFromFile(__DIR__ . '/data/install/uninstall.sql');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services): void
{
$this->setServiceLocator($services);
require_once __DIR__ . '/data/scripts/upgrade.php';
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager): void
{
// Manage the deletion of an item.
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.delete.post',
[$this, 'handleDeletePost'],
);
// Manage search items with harvests.
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.search.query',
[$this, 'handleApiSearchQuery']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.advanced_search',
[$this, 'handleViewAdvancedSearch']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.search.filters',
[$this, 'handleSearchFilters']
);
// Display the harvest in item views.
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.sidebar',
[$this, 'handleViewShowAfterAdmin']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.details',
[$this, 'handleViewShowAfterAdmin']
);
}
/**
* Execute a sql from a file.
*
* @param string $filepath
* @return mixed
*/
protected function execSqlFromFile($filepath)
{
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
$sql = file_get_contents($filepath);
$sqls = array_filter(array_map('trim', explode(";\n", $sql)));
foreach ($sqls as $sql) {
$result = $connection->executeStatement($sql);
}
return $result;
}
public function handleDeletePost(Event $event): void
{
/**
* @var \Omeka\Api\Manager $api
* @var \Omeka\Api\Request $request
*/
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$request = $event->getParam('request');
$resourceId = $request->getId();
$resourceName = $request->getResource();
if ($resourceId && $resourceName) {
$response = $api->search('oaipmhharvester_entities', [
'entity_id' => $resourceId,
'entity_name' => $resourceName,
]);
foreach ($response->getContent() as $entity) {
try {
$api->delete('oaipmhharvester_entities', $entity->id());
} catch (\Throwable $e) {
}
}
}
}
/**
* Helper to build search queries.
*/
public function handleApiSearchQuery(Event $event): void
{
/**
* @var \Doctrine\ORM\QueryBuilder $qb
* @var \Omeka\Api\Adapter\AbstractResourceEntityAdapter $adapter
* @var \Omeka\Api\Request $request
* @var array $query
*/
$request = $event->getParam('request');
$query = $request->getContent();
if (array_key_exists('harvest_id', $query)
&& $query['harvest_id'] !== ''
&& $query['harvest_id'] !== []
) {
$adapter = $event->getTarget();
$qb = $event->getParam('queryBuilder');
$expr = $qb->expr();
// TODO Why use a dynamic alias instead of a fixed alias?
$entityAlias = $adapter->createAlias();
if (empty($query['harvest_id']) || $query['harvest_id'] === [0] || $query['harvest_id'] === ['0']) {
// TODO Optimize query to find items without harvest.
$qb
->leftJoin(
\OaiPmhHarvester\Entity\Entity::class,
$entityAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
"$entityAlias.entityId = omeka_root.id"
)
->andWhere($expr->isNull("$entityAlias.entityId"));
} else {
$ids = is_array($query['harvest_id']) ? $query['harvest_id'] : [$query['harvest_id']];
$ids = array_filter(array_map('intval', $ids));
if ($ids) {
$qb
->innerJoin(
\OaiPmhHarvester\Entity\Entity::class,
$entityAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
"$entityAlias.harvest IN(:harvest_ids) AND $entityAlias.entityId = omeka_root.id"
)
->setParameter('harvest_ids', $ids, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
} else {
// The harvest is set, but invalid (not integer).
$qb
->innerJoin(
\OaiPmhHarvester\Entity\Entity::class,
$entityAlias,
\Doctrine\ORM\Query\Expr\Join::WITH,
"$entityAlias.harvest = 0"
);
}
}
}
}
public function handleViewAdvancedSearch(Event $event): void
{
$partials = $event->getParam('partials');
$partials[] = 'common/advanced-search/harvests';
$event->setParam('partials', $partials);
}
/**
* Complete the list of search filters for the browse page.
*/
public function handleSearchFilters(Event $event): void
{
$filters = $event->getParam('filters');
$query = $event->getParam('query', []);
if (array_key_exists('harvest_id', $query)
&& $query['harvest_id'] !== ''
&& $query['harvest_id'] !== []
) {
$services = $this->getServiceLocator();
$translator = $services->get('MvcTranslator');
$values = is_array($query['harvest_id']) ? $query['harvest_id'] : [$query['harvest_id']];
$values = array_filter(array_map('intval', $values));
$filterLabel = $translator->translate('OAI-PMH harvest'); // @translate
if ($values && $values !== [0] && $values['0']) {
$filters[$filterLabel] = $values;
} else {
$filters[$filterLabel][] = $translator->translate('None'); // @translate
}
$event->setParam('filters', $filters);
}
}
public function handleViewShowAfterAdmin(Event $event): void
{
/**
* @var \Omeka\Api\Manager $api
* @var \Omeka\Permissions\Acl $acl
*/
$services = $this->getServiceLocator();
$acl = $this->getServiceLocator()->get('Omeka\Acl');
// TODO Check rights? Useless: the ids are a list of allowed ids.
$user = $services->get('Omeka\AuthenticationService')->getIdentity();
if (!$user || !$acl->isAdminRole($user->getRole())) {
return;
}
$view = $event->getTarget();
$vars = $view->vars();
/** @var \Omeka\Api\Representation\AbstractResourceEntityRepresentation $resource */
$resource = $vars->offsetGet('resource');
if (!$resource) {
return;
}
// Get the harvests for the current resource.
/** @var \Omeka\Api\Manager $api */
$api = $services->get('Omeka\ApiManager');
$harvestIds = $api->search(
'oaipmhharvester_entities',
['entity_id' => $resource->id(), 'entity_name' => $resource->resourceName()],
['returnScalar' => 'harvest']
)->getContent();
if (!count($harvestIds)) {
return;
}
$harvestIds = array_values(array_unique($harvestIds));
$vars->offsetSet('heading', $view->translate('OAI-PMH harvests')); // @translate
$vars->offsetSet('resourceName', 'oaipmhharvester_harvests');
$vars->offsetSet('ids', $harvestIds);
echo $view->partial('common/harvests-sidebar');
}
}