-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.php
279 lines (199 loc) · 8.75 KB
/
test.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
#!/usr/bin/env drush
#<?php
# include all Tuque php files
$tuquePath = libraries_get_path('tuque') . '/*.php';
foreach ( glob($tuquePath) as $filename) {
require_once($filename);
}
# repository connection parameters
$url = 'localhost:8080/fedora';
$username = 'fedoraAdmin';
$password = 'fedoraAdmin';
# set up connection and repository variables
$connection = new RepositoryConnection($url, $username, $password);
$api = new FedoraApi($connection);
$repository = new FedoraRepository($api, new SimpleCache());
$pid = "islandora:3";
try {
//drush_print("Attempting to access $objectPID from repository");
$object = $repository->getObject($pid);
}
catch (Exception $e) {
drush_print("\n\n**********####### ERROR #######*********");
drush_print("***Could not get object $pid from repo***\n\n");
return;
}
$modsDatastream = islandora_datastream_load('MODS', $object);
$modsxml = $modsDatastream->content;
$domdoc = new DOMDocument();
$domdoc->preserveWhiteSpace = false;
$domdoc->formatOutput = true;
$domdoc->loadXML($modsDatastream->content);
$xpath = new DOMXPath($domdoc);
$xpath->registerNameSpace('mods', 'http://www.loc.gov/mods/v3');
$xml_out = $domdoc->saveXML($domdoc->documentElement);
echo $xml_out . "\n\n";
$topicArray = array();
$domElemsToRemove = array();
// loop through all <subject> nodess
foreach ($xpath->query('//mods:subject') as $node) {
// find all the <topic> nodes which are children of each <subject> node
foreach($xpath->query('mods:topic', $node) as $topicNode) {
print "$topicNode->nodeValue\n";
$topicArray[] = $topicNode->nodeValue;
$newSubjectNode = $domdoc->createElement('subject');
$newTopicNode = $domdoc->createElement('topic', $topicNode->nodeValue);
$newNode = $node->parentNode->insertBefore($newSubjectNode, $node);
$newNode->appendChild($newTopicNode);
$domElemsToRemove[] = $node;
}
}
foreach( $domElemsToRemove as $toBeRemoved) {
$toBeRemoved->parentNode->removeChild($toBeRemoved);
}
// if (trim($xpath->query('mods:role', $node)->item(0)->nodeValue) == "Thesis advisor") {
// $roleNode = $xpath->query('mods:role', $node)->item(0);
// $namePartNode = $xpath->query('mods:namePart', $node)->item(0);
// $fullname = $xpath->query('mods:namePart', $node)->item(0)->nodeValue . "\n";
// $fullNameArray = explode(' ', $fullname);
// $givenName = '';
// for ($i=0;$i < count($fullNameArray)-1;$i++) {
// $givenName .= trim($fullNameArray[$i]) . " ";
// }
// $givenName = trim($givenName);
// $familyName = trim($fullNameArray[count($fullNameArray)-1]);
// echo "Thesis advisor is ";
// echo "$givenName $familyName\n";
// $newNodeGivenName = $domdoc->createElement('namePart', $givenName);
// $newNodeGivenName->setAttribute('type', 'given');
// $newNodeFamilyName = $domdoc->createElement('namePart', $familyName);
// $newNodeFamilyName->setAttribute('type', 'family');
// $node->insertBefore($newNodeGivenName, $roleNode);
// $node->insertBefore($newNodeFamilyName, $roleNode);
// $mods = $domdoc->documentElement;
// $namePartNode->parentNode->removeChild($namePartNode);
// }
// get original values
//$originalGivenName = trim($xpath->query('mods:namePart[@type="given"]', $node)->item(0)->nodeValue);
//$originalFamilyName = trim($xpath->query('mods:namePart[@type="family"]', $node)->item(0)->nodeValue);
// swap values
// $xpath->query('mods:namePart[@type="given"]', $node)->item(0)->nodeValue = $originalFamilyName;
// $xpath->query('mods:namePart[@type="family"]', $node)->item(0)->nodeValue = $originalGivenName;
// }
// foreach ($xpath->query('//mods:name[@type="personal"]') as $node) {
// foreach($other_nodes as $fooNode) {
// echo $fooNode->nodeValue;
// }
// foreach ($node->childNodes as $nodeChild) {
// if ((string)$nodeChild->localName == 'namePart') {
// $aNamePart = $nodeChild->attributes->item(0)->nodeValue;
// if ($aNamePart == 'given') {
// $nodeChild->attributes->item(0)->nodeValue == 'family';
// //$givenNameOriginal = $nodeChild->attributes->item(0)->nodeValue;
// }
// else if ($aNamePart == 'family') {
// $nodeChild->attributes->item(0)->nodeValue == 'given';
// //$familyNameOriginal = $nodeChild->attributes->item(0)->nodeValue;
// }
// }
// if ($nodeChild->localName == 'namePart') {
// foreach($nodeChild->attributes as $attr) {
// echo $attr->nodeName; // type
// echo $attr->nodeValue; // given/family
// if ($attr->nodeName == 'type' && $attr->nodeValue=='given') {
// //$attr->nodeValue = !FamilyName!;
// echo "found given name\n";
// $originalGivenName = $attr->nodeValue;
// }
// else if ($attr->nodeName == 'type' && $attr->nodeValue=='family') {
// //$attr->nodeValue = !givenName!;
// echo "found family name\n";
// $originalFamilyName = $attr->nodeValue;
// }
// }
// }
//echo (string)$nodePart->localName;
// }
// }
// foreach($xpath->query('//fakeprefix:namePart[@type="given"]') as $rowNode) {
// echo $rowNode->nodeValue . "\n";
// }
// $givenName = $xpath->query('//mods:namePart[@type="given"]')->item(0)->nodeValue;
// $familyName = $xpath->query('//mods:namePart[@type="family"]')->item(0)->nodeValue;
// echo "\nValues from XML:\n";
// echo "Given name: $givenName\n";
// echo "Family name: $familyName\n";
// echo "\n";
// // switch the values of given name and family name
// $xpath->query('//mods:namePart[@type="given"]')->item(0)->nodeValue = $familyName;
// $xpath->query('//mods:namePart[@type="family"]')->item(0)->nodeValue = $givenName;
// echo "\n\n" . $domdoc->saveXML($domdoc->documentElement);
// $nodeList = $domdoc->getElementsByTagName('namePart');
// foreach ($domdoc->getElementsByTagName('namePart') as $entry)
// {
// $entryType = $entry->getAttribute('type');
// print $entry->nodeValue . " " . $entry->getAttribute('type') . "\n";
// }
print $domdoc->saveXML($domdoc->documentElement);
// $modsDatastream->setContentFromString($domdoc);
return;
$url = 'localhost:8080/fedora';
$username = 'fedoraAdmin';
$password = 'fedoraAdmin';
# set up connection and repository variables
$connection = new RepositoryConnection($url, $username, $password);
$api = new FedoraApi($connection);
$repository = new FedoraRepository($api, new SimpleCache());
# what is the pid you want?
$pid = 'islandora:3';
# what to change the name to?
$givenName = "Mickey";
$familyName = "Mouse";
$wholeName = $givenName . ' ' . $familyName;
# try to fetch PID from repo
try {
$object = $repository->getObject($pid);
}
catch (Exception $e) {
drush_print('****************ERROR***************');
exit;
}
# grab the data streams
$dublinCoreDS = $object['DC'];
$modsDS = $object['MODS'];
// drush_print('******Dublin Core********');
// drush_print(@$dublinCoreDS->getContent());
// drush_print();
# parse the datastreams into simplexml objects
$modsXML = simplexml_load_string(@$modsDS->getContent());
$dcXML = simplexml_load_string(@$dublinCoreDS->getContent());
//print_r($dcXML->getDocNamespaces());
// foreach ($dcXML->children("dc", TRUE) as $entry)
// {
// if ($entry->contributor)
// {
// $entry->contributor = 'Mickey Mouse';
// }
// //drush_print($entry);
// }
# change the MODS record
$swapVariable = $mods->name['personal']->namePart['given'];
$mods->name['personal']->namePart['given'] = $mods->name['personal']->namePart['family'];
$mods->name['personal']->namePart['given'] = $swapVariable;
# change the contributor in the Dublin Core datastream
//$dcXML->children("dc", TRUE)->contributor[0] = $wholeName . ' (Author)';
// drush_print();
// drush_print('******EDITED Dublin Core********');
// drush_print($dcXML->asXML());
// drush_print();
# set content of datastreams to newly edited content
$modsDS->setContentFromString($mods);
//$dublinCoreDS->setContentFromString($dcXML->asXML());
# ingest datastream into repo
$object->ingestDatastream($modsDS);
//$object->ingestDatastream($dublinCoreDS);
// $dd = new DOMDocument;
// $dd->loadXML($dcXML->asXML());
// foreach ($dd->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', '*') as $element) {
// echo $element->localName . ', ' . $element->nodeValue . "\n";
// }