-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
144 lines (127 loc) · 4.85 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
<?php
declare(strict_types=1);
namespace UniqueProperties;
if (!class_exists(\Generic\AbstractModule::class)) {
require file_exists(dirname(__DIR__) . '/Generic/AbstractModule.php')
? dirname(__DIR__) . '/Generic/AbstractModule.php'
: __DIR__ . '/src/Generic/AbstractModule.php';
}
use Generic\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Omeka\Stdlib\Message;
use Omeka\Mvc\Controller\Plugin\Messenger;
use Omeka\Api\Exception\ValidationException;
class Module extends AbstractModule
{
const NAMESPACE = __NAMESPACE__;
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
// add listeners for add/update
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.create.pre',
[$this, 'dedupAction']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.update.pre',
[$this, 'dedupAction'],
);
}
public function dedupAction(Event $event): void
{
$request = $event->getParam('request');
// Ignore other requests
if ($request->getOperation() != 'create' && $request->getOperation() != 'update') {
return;
}
$serviceLocator = $this->getServiceLocator();
$settings = $serviceLocator->get('Omeka\Settings');
$messenger = new Messenger;
$itemId = (int) $request->getId();
$data = $request->getContent();
$propIds = $settings->get('unique_properties');
$allowedProps = array();
foreach ($propIds as $propId) {
$allowedProps[$propId] = true;
}
$itemPropIDs = array();
// Fetch the property values from this request.
$propValues = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
if (isset($innerValue['property_id']) && !empty($innerValue['@value'])) {
// ignore property ids that are not matching
if (!$allowedProps[$innerValue['property_id']]) {
continue;
}
array_push($propValues, $innerValue['@value']);
array_push($itemPropIDs, $innerValue['property_id']);
}
}
}
}
// Convert property-ids, property-values into json
// This allows us to load this into a derived table in MySQL
$jsonPairs = json_encode(array_map(function ($value, $property_id) {
return ['value' => $value, 'property_id' => $property_id];
}, $propValues, $itemPropIDs), JSON_UNESCAPED_UNICODE);
$services = $this->getServiceLocator();
$connection = $services->get('Omeka\Connection');
// Check if same property-id, property-value exists in any other items.
$sql = <<<'SQL'
SELECT
CONCAT(
`vocabulary`.`label`,
' - ',
`property`.`label`
) as `field`,
`value`.`value` as `data`,
`value`.`resource_id` as `resource_id`
FROM
`value`
LEFT JOIN `resource` ON `resource`.`id` = `value`.`resource_id`
LEFT JOIN `property` ON `property`.`id` = `value`.`property_id`
LEFT JOIN `vocabulary` ON `vocabulary`.`id` = `property`.`vocabulary_id`
WHERE
`value`.`value` IN (:property_values)
AND `value`.`resource_id` <> :id
AND `value`.`property_id` IN (:property_ids)
AND `resource`.`resource_type` = 'Omeka\\Entity\\Item'
AND EXISTS (
SELECT 1
FROM JSON_TABLE(
:json_pairs,
"$[*]" COLUMNS (
value_prop VARCHAR(255) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' PATH "$.value",
property_id INT PATH "$.property_id"
)
) derived
WHERE
derived.value_prop = value.value
AND derived.property_id = value.property_id
)
SQL;
$stmt = $connection->executeQuery($sql, [
'id' => $itemId,
'property_ids' => $itemPropIDs,
'property_values' => $propValues,
'json_pairs' => $jsonPairs,
], [
'property_ids' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'property_values' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY,
'json_pairs' => \PDO::PARAM_STR,
]);
$results = $stmt->fetchAll();
foreach ($results as $result) {
$messenger->addError(new Message('Duplicate property value `%s` found for `%s` in #%d', $result['data'], $result['field'], $result['resource_id']));
throw new ValidationException('');
}
}
}