-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathItemAdapter.php
263 lines (239 loc) · 9.75 KB
/
ItemAdapter.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
<?php
class ApiImport_ResponseAdapter_Omeka_ItemAdapter extends ApiImport_ResponseAdapter_AbstractRecordAdapter
{
protected $recordType = 'Item';
protected $service;
public function import()
{
//grab the data needed for using update_item or insert_item
$elementTexts = $this->elementTexts();
$itemMetadata = $this->itemMetadata();
//avoid accidental duplications
if($this->record && $this->record->exists()) {
$itemMetadata['overwriteElementTexts'] = true;
update_item($this->record, $itemMetadata, $elementTexts);
$this->updateItemOwner($this->record);
} else {
$this->record = insert_item($itemMetadata, $elementTexts);
//dig up the correct owner information, importing the user if needed
$this->updateItemOwner($this->record);
$this->addOmekaApiImportRecordIdMap();
}
//import files after the item is there, so the file has an item id to use
//we're also keeping track of the correspondences between local and remote
//file ids, so we have to introduce this little inefficiency of not passing
//the file data
$this->importFiles($this->record);
}
public function externalId()
{
return $this->responseData['id'];
}
/**
* Try to map a correspondence between the local and remote owners. Requires that a key with
* sufficient permission to the API is given
*
* @param Item $item
*/
protected function updateItemOwner($item)
{
if ($this->importUsers == 0) {
$item->owner_id = current_user()->id;
$item->save();
return;
}
$ownerId = $this->responseData['owner']['id'];
if (! $ownerId) {
$item->owner_id = null;
$item->save();
return;
}
$owner = $this->db->getTable('OmekaApiImportRecordIdMap')->localRecord('User', $ownerId, $this->endpointUri);
if($owner) {
$item->owner_id = $owner->id;
} else {
$response = $this->service->users->get($ownerId);
if($response->getStatus() == 200) {
$responseData = json_decode($response->getBody(), true);
$adapter = new ApiImport_ResponseAdapter_Omeka_UserAdapter($responseData, $this->endpointUri, null, $this->importUsers);
$adapter->import();
$item->owner_id = $adapter->record->id;
} else {
_log(__("Attempting User import") . " " . $response->getStatus() . ": " . $response->getMessage(), Zend_Log::INFO);
}
}
$item->save();
}
/**
* Process the element text data
* @param array $responseData
*/
protected function elementTexts($responseData = null)
{
$elementTexts = array();
if(!$responseData) {
$responseData = $this->responseData;
}
//need to work around a previous bug in contribution, which would store User Profile
//elements on the Item as well. This made item elements from API also include
//UP elements, which failed the lookup.
$db = get_db();
$elementTable = $db->getTable('Element');
$sql = "
SELECT DISTINCT external_id FROM `$db->ElementSet`
JOIN `$db->OmekaApiImportRecordIdMap` ON {$db->ElementSet}.id = local_id
WHERE {$db->OmekaApiImportRecordIdMap}.record_type = 'ElementSet'
AND {$db->ElementSet}.record_type = 'UserProfilesType'
";
$userProfilesElementSetIdsMap = $db->fetchCol($sql);
foreach($responseData['element_texts'] as $elTextData) {
if (in_array($elTextData['element_set']['id'], $userProfilesElementSetIdsMap)) {
continue;
}
//work around some possible oddities from .net with element data being null
if (is_null($elTextData['element_set']['id'])) {
continue;
}
if (is_null($elTextData['element']['id'])) {
continue;
}
$elName = $elTextData['element']['name'];
$elSet = $elTextData['element_set']['name'];
$elTextInsertArray = array('text' => $elTextData['text'],
'html' => $elTextData['html']
);
if (is_null($elTextInsertArray['text'])) {
$elTextInsertArray['text'] = '';
}
$elementTexts[$elSet][$elName][] = $elTextInsertArray;
}
return $elementTexts;
}
/**
* Parse out the item metadata for import/update_item
*/
protected function itemMetadata()
{
$metadata = array();
$metadata['public'] = $this->responseData['public'];
$metadata['featured'] = $this->responseData['featured'];
//external vs internal collection ids could be different
if (isset($this->responseData['collection']['id'])) {
$collectionExternalId = $this->responseData['collection']['id'];
} else {
$collectionExternalId = null;
}
$collectionId = null;
if (!is_null($collectionExternalId)) {
$collection = $this->localRecord('Collection', $collectionExternalId);
if($collection) {
$collectionId = $collection->id;
} else {
//import the collection
//older version of API exposed non-public collection info on the item
$collection = $this->importCollection($collectionExternalId);
if ($collection) {
$collectionId = $collection->id;
}
}
}
$metadata['collection_id'] = $collectionId;
$itemType = $this->responseData['item_type'] ? $this->localRecord('ItemType', $this->responseData['item_type']['id']) : null;
if($itemType) {
$itemTypeId = $itemType->id;
} else {
$itemTypeId = null;
}
$metadata['item_type_id'] = $itemTypeId;
$tagsArray = array();
foreach($this->responseData['tags'] as $tagData) {
$tagsArray[] = $tagData['name'];
}
$metadata['tags'] = implode(',', $tagsArray);
return $metadata;
}
protected function importFiles($item)
{
$ingester = Omeka_File_Ingest_AbstractIngest::factory(
'Url',
$item,
array()
);
$this->_addIngestValidators($ingester);
$files = $this->files();
//have to step through one by one so we can save the id map for each $fileRecord and $fileData
foreach($files as $fileData)
{
try {
$fileRecords = $ingester->ingest(array($fileData));
} catch (Exception $e) {
_log($e);
continue;
}
$item->saveFiles();
$fileRecord = array_pop($fileRecords);
$map = new OmekaApiImportRecordIdMap();
$map->record_type = 'File';
$map->local_id = $fileRecord->id;
$map->external_id = $fileData['externalId'];
$map->endpoint_uri = $this->endpointUri;
$map->save();
}
}
/**
* Parse out and query the data about files for Omeka_File_Ingest_AbstractIngest::factory
*
* @return array File data for the file ingester
*/
protected function files()
{
$files = array();
$response = $this->service->files->get(null, array('item' => $this->externalId()));
if($response->getStatus() == 200) {
$responseData = json_decode($response->getBody(), true);
} else {
_log($response->getMessage());
}
$externalIds = $this->db->getTable('OmekaApiImportRecordIdMap')
->findExternalIdsByParams(array('record_type' =>'File',
'endpoint_uri' => $this->endpointUri
));
$ids = array_keys($externalIds);
foreach($responseData as $fileData) {
if(! in_array($fileData['id'], $ids)) {
$files[] = array('source' => $fileData['file_urls']['original'],
'name' => $fileData['original_filename'],
'metadata' => $this->elementTexts($fileData),
//add the external id so we can produce the map
'externalId' => $fileData['id']
);
}
}
return $files;
}
protected function importCollection($collectionId)
{
$response = $this->service->collections->get($collectionId);
if($response->getStatus() == 200) {
$responseData = json_decode($response->getBody(), true);
$adapter = new ApiImport_ResponseAdapter_Omeka_CollectionAdapter($responseData, $this->endpointUri);
$adapter->setService($this->service);
$adapter->import();
return $adapter->getRecord();
}
return false;
}
protected function _addIngestValidators(Omeka_File_Ingest_AbstractIngest $ingester)
{
$validators = get_option(File::DISABLE_DEFAULT_VALIDATION_OPTION)
? array()
: array(
'extension whitelist' => new Omeka_Validate_File_Extension,
'MIME type whitelist' => new Omeka_Validate_File_MimeType);
$validators = apply_filters('file_ingest_validators', $validators);
// Build the default validators.
foreach ($validators as $validator) {
$ingester->addValidator($validator);
}
}
}