/
evld_create_entities.php
266 lines (247 loc) · 9.28 KB
/
evld_create_entities.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
<?php
/**
* Imports entities for EVLD's past perfect export
*
* @category islandora_import
* @author Mark Noble <mark@marmot.org>
* Date: 12/21/2015
* Time: 2:50 PM
*/
header( 'Content-type: text/html; charset=utf-8' );
define ('ROOT_DIR', __DIR__);
date_default_timezone_set('America/Denver');
ini_set('display_errors', true);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
session_write_close();
for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);
for($i = 0; $i < 40000; $i++)
{
echo ' '; // extra spaces
}
echo("<html><body>This page will import all entities into Islandora. You must first configure it using config.ini in the same path as this file. <br />
The page will show output while running. <br />
You can configure the page to import
<ul>
<li>People</li>
<li>Places</li>
<li>Events</li>
</ul></body>\r\n");
echo("Importing entities from EVLD Past Perfect Export<br/>\r\n");
flush();
$config = parse_ini_file(ROOT_DIR . '/config.ini');
global $solrUrl, $fedoraUser, $fedoraPassword;
$sourceXMLFile = $config['sourceXMLFile'];
$baseImageLocation = $config['baseImageLocation'];
$fedoraPassword = $config['fedoraPassword'];
$fedoraUser = $config['fedoraUser'];
$fedoraUrl = $config['fedoraUrl'];
$solrUrl = $config['solrUrl'];
$maxRecordsToProcess = $config['maxRecordsToProcess'];
$loadPeople = $config['loadPeople'];
$maxPeopleToLoad = isset($config['maxPeopleToLoad']) ? $config['maxPeopleToLoad'] : -1;
$loadEvents = $config['loadEvents'];
$loadPlaces = $config['loadPlaces'];
$updateModsForExistingEntities = $config['updateModsForExistingEntities'];
//Read the XML File
$xml = simplexml_load_file($sourceXMLFile);
if (!$xml){
echo("Failed to read XML, boo");
}else{
//Include code we need to use Tuque without Drupal
require_once(ROOT_DIR . '/tuque/Cache.php');
require_once(ROOT_DIR . '/tuque/FedoraApi.php');
require_once(ROOT_DIR . '/tuque/FedoraApiSerializer.php');
require_once(ROOT_DIR . '/tuque/Object.php');
require_once(ROOT_DIR . '/tuque/HttpConnection.php');
require_once(ROOT_DIR . '/tuque/Repository.php');
require_once(ROOT_DIR . '/tuque/RepositoryConnection.php');
require_once(ROOT_DIR . '/utils.php');
//Connect to tuque
try {
$serializer = new FedoraApiSerializer();
$cache = new SimpleCache();
$connection = new RepositoryConnection($fedoraUrl, $fedoraUser, $fedoraPassword);
$connection->verifyPeer = false;
$api = new FedoraApi($connection, $serializer);
$repository = new FedoraRepository($api, $cache);
echo("Connected to Tuque OK<br/>\r\n");
ob_flush();
flush();
usleep(100);
}catch (Exception $e){
echo("We could not connect to the fedora repository.");
die;
}
//Process each record
$i = 0;
$numPeopleLoaded = 0;
$numRead = 0;
global $existingEntities;
/** @var SimpleXMLElement $exportedItem */
foreach ($xml->export as $exportedItem){
$numRead++;
echo('.');
if ($loadPeople){
//Find each person within the export & add to Islandora
$people=preg_split('/\r\n|\r|\n/', $exportedItem->people);
foreach($people as $person) {
$person = trim($person);
if (strlen($person) == 0){
continue;
}
if ($numPeopleLoaded < $maxPeopleToLoad || $maxPeopleToLoad == -1){
//Check to see if the entity exists already
$new = false;
$existingPID = doesEntityExist($person);
if ($existingPID != false) {
if (!$updateModsForExistingEntities){
continue;
}
//Load the object
$entity = $repository->getObject($existingPID);
} else {
//Create an entity within Islandora
$entity = $repository->constructObject('person');
$entity->models = array('islandora:personCModel');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'marmot:people');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:entity_collection');
$new = true;
}
echo("$i) Processing Person $person <br/>");
$entity->label = $person;
//Add MADS data
if ($entity->getDatastream('MODS') == null) {
$modsDatastream = $entity->constructDatastream('MODS');
} else {
$modsDatastream = $entity->getDatastream('MODS');
}
$modsDatastream->label = 'MODS Record';
$modsDatastream->mimetype = 'text/xml';
$nameParts = explode(",", $person);
$lastName = trim($nameParts[0]);
$modsDatastream->setContentFromString("<?xml version=\"1.0\"?><mods xmlns=\"http://www.loc.gov/mods/v3\" xmlns:marmot=\"http://marmot.org/local_mods_extension\" xmlns:mods=\"http://www.loc.gov/mods/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><mods:titleInfo><mods:title>{$person}</mods:title></mods:titleInfo><mods:extension><marmot:marmotLocal><marmot:familyName>{$lastName}</marmot:familyName></marmot:marmotLocal></mods:extension></mods>");
$entity->ingestDatastream($modsDatastream);
if ($new) {
try {
$repository->ingestObject($entity);
$existingEntities[$person] = $entity->id;
} catch (Exception $e) {
echo("error ingesting object $e</br>\r\n");
}
}
$numPeopleLoaded++;
$i++;
}
}
}
//Find each event within the export & add to Islandora
if ($loadEvents){
$events=preg_split('/\r\n|\r|\n/', $exportedItem->event);
foreach($events as $event) {
$event = trim($event);
if (strlen($event) == 0){
continue;
}
//Check to see if the entity exists already
$new = false;
$existingPID = doesEntityExist($event);
if ($existingPID != false) {
if (!$updateModsForExistingEntities){
continue;
}
//Load the object
$entity = $repository->getObject($existingPID);
} else {
//Create an entity within Islandora
$entity = $repository->constructObject('event');
$entity->models = array('islandora:eventCModel');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'marmot:events');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:entity_collection');
$new = true;
}
echo("$i) Processing Event $event <br/>");
$entity->label = $event;
//Add MODS data
if ($entity->getDatastream('MODS') == null) {
$modsEventDatastream = $entity->constructDatastream('MODS');
} else {
$modsEventDatastream = $entity->getDatastream('MODS');
}
$modsEventDatastream->label = 'MODS Record';
$modsEventDatastream->mimetype = 'text/xml';
$modsEventDatastream->setContentFromString("<?xml version=\"1.0\"?><mods xmlns=\"http://www.loc.gov/mods/v3\" xmlns:marmot=\"http://marmot.org/local_mods_extension\" xmlns:mods=\"http://www.loc.gov/mods/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><mods:titleInfo><mods:title>{$event}</mods:title></mods:titleInfo></mods>");
$entity->ingestDatastream($modsEventDatastream);
if ($new) {
try {
$repository->ingestObject($entity);
$existingEntities[$event] = $entity->id;
} catch (Exception $e) {
echo("error ingesting object $e</br>\r\n");
}
}
$i++;
}
}
//Find each place within the export & add to Islandora
if ($loadPlaces){
$places=preg_split('/\r\n|\r|\n/', $exportedItem->place);
foreach($places as $place) {
$place = trim($place);
if (strlen($place) == 0){
continue;
}
//Check to see if the entity exists already
$new = false;
$existingPID = doesEntityExist($place);
if ($existingPID != false) {
if (!$updateModsForExistingEntities){
continue;
}
//Load the object
$entity = $repository->getObject($existingPID);
} else {
//Create an entity within Islandora
$entity = $repository->constructObject('place');
$entity->models = array('islandora:placeCModel');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'marmot:places');
$entity->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:entity_collection');
$new = true;
}
echo("$i) Processing Place $place <br/>");
$entity->label = $place;
//Add MODS data
if ($entity->getDatastream('MODS') == null) {
$modsPlaceDatastream = $entity->constructDatastream('MODS');
} else {
$modsPlaceDatastream = $entity->getDatastream('MODS');
}
$modsPlaceDatastream->label = 'MODS Record';
$modsPlaceDatastream->mimetype = 'text/xml';
$modsPlaceDatastream->setContentFromString("<?xml version=\"1.0\"?><mods xmlns=\"http://www.loc.gov/mods/v3\" xmlns:marmot=\"http://marmot.org/local_mods_extension\" xmlns:mods=\"http://www.loc.gov/mods/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><mods:titleInfo><mods:title>{$place}</mods:title></mods:titleInfo></mods>");
$entity->ingestDatastream($modsPlaceDatastream);
if ($new) {
try {
$repository->ingestObject($entity);
$existingEntities[$place] = $entity->id;
} catch (Exception $e) {
echo("error ingesting object $e</br>\r\n");
}
}
$i++;
}
}
//End processing this record
if ($numRead % 100 == 0){
echo("Processed $numRead Records\r\n");
flush();
set_time_limit(120);
}
flush();
}
echo('Done<br/></html>\r\n');
flush();
}