-
Notifications
You must be signed in to change notification settings - Fork 0
/
coalliance_foxml_migration.drush.inc
245 lines (209 loc) · 7.78 KB
/
coalliance_foxml_migration.drush.inc
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
<?php
/**
* @file
* A drush script for FoxML collection migration.
* heavily borrowed from https://github.com/UCLALibrary/ucla_migration
*/
/**
* Implements hook_drush_command().
*/
function coalliance_foxml_migration_drush_command() {
$items = array();
$items['foxml_save_pids_for_namespace'] = array(
'callback' => 'coalliance_foxml_migration_save_pids_for_namespace',
'description' => 'Gets all PIDS for the given namespace and saves them to a file.',
'arguments' => array(
'fedora_namespace' => "The Fedora namespace whose PIDS you're looking for.",
),
'options' => array(),
'examples' => array(
'simple example' => 'drush -u 1 -l http://example.com foxml_save_pids_for_namespace your_namespace',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['migrate_foxml2'] = array(
'callback' => 'coalliance_migration_from_foxml2',
'description' => 'Ingest content from foxml file for a single record.',
'arguments' => array(
'file_path' => 'The file path to the foxml file.',
'fedora_namespace' => "The Fedora namespace to ingest the record under.",
'dmdid' => "The record ID provided in the MODS for this particular record.",
),
'options' => array(),
'examples' => array(
'simple example' => 'drush -u 1 -l http://example.com migrate_foxml /home/dgiuser/21198-zz00294nxr.xml codearl 21198-zz002bzvvs',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['migrate_foxml3'] = array(
'callback' => 'coalliance_migration_from_foxml3',
'description' => 'Ingest content from foxml file for a single record.',
'arguments' => array(
'file_path' => 'The file path to the foxml file.',
'fedora_namespace' => "The Fedora namespace to ingest the record under.",
'dmdid' => "The record ID provided in the MODS for this particular record.",
),
'options' => array(),
'examples' => array(
'simple example' => 'drush -u 1 -l http://example.com migrate_foxml /home/dgiuser/21198-zz00294nxr.xml codearl 21198-zz002bzvvs',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['migrate_foxml'] = array(
'callback' => 'coalliance_migration_from_foxml',
'description' => 'Ingest content from foxml file for a single record.',
'arguments' => array(
'file_path' => 'The file path to the foxml file.',
'fedora_namespace' => "The Fedora namespace to ingest the record under.",
'dmdid' => "The record ID provided in the MODS for this particular record.",
),
'options' => array(),
'examples' => array(
'simple example' => 'drush -u 1 -l http://example.com migrate_foxml /home/dgiuser/21198-zz00294nxr.xml codearl 21198-zz002bzvvs',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
return $items;
}
/**
* Saves all pids in a Fedora namespace to a file.
*
* Output file is written to the coalliance_foxml_migration module folder. It is given
* the name of the fedora namespace and an extension of '.pid'.
*
* @param string $fedora_namespace
* The Fedora namespace of PIDS
*/
function coalliance_foxml_migration_save_pids_for_namespace($fedora_namespace) {
// Get a connection to Fedora.
$connection = islandora_get_tuque_connection();
$repo = $connection->repository;
// SPARQL query to get all objects in a given namespace.
$query =
"
SELECT ?obj
FROM <#ri>
WHERE {
?obj <info:fedora/fedora-system:def/model#hasModel> ?model .
FILTER(regex(str(?obj), '^info:fedora/$fedora_namespace:'))
}
";
// Execute the query.
$results = $repo->ri->sparqlQuery($query);
// Put all the pids in an array.
$pids = array();
foreach ($results as $result) {
array_push($pids, $result['obj']['value']);
}
// JSON encode the array and write it to a file.
$path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'coalliance_foxml_migration') . "/$fedora_namespace.pids";
$fp = fopen($path, 'w');
if (!$fp) {
drush_set_error("Can't open file to write",
"Cannot open file $path to write. Check your permissions for the coalliance_foxml_migration module folder or run this script using sudo.",
"Error: "
);
return;
}
if (!fwrite($fp, json_encode($pids))) {
drush_set_error("Can't write pids to file",
"Cannot write pids to $path. Check your permissions for the coalliance_foxml_migration module folder or run this script using sudo.",
"Error: "
);
fclose($fp);
return;
}
fclose($fp);
return;
}
function coalliance_migration_from_foxml2($file_path, $fedora_namespace, $dmdid) {
// Check to make sure the path to the foxml file has been provided.
if (!isset($file_path)) {
drush_set_error('File path not provided',
'Path to FoxML file not provided as command line argument',
'Error: ');
return;
}
// Attempt to grab the foxml file.
$foxml_file = file_get_contents($file_path);
if (!$foxml_file) {
drush_set_error('File not found',
"Could not locate foxml file $file_path",
"Error: ");
return;
}
#print_r($foxml_file);
#####$fedora_object->ingestDatastream($foxml_file);
$fedora_object = $foxml_file;
#$repository->ingestObject($fedora_object);
$my_islandora_tuque = new IslandoraTuque();
$repository = $my_islandora_tuque->repository;
$api_a = $repository->api->a; // For an Access API.
$api_m = $repository->api->m; // For a Management API.
#$datastreams = $api_a->listDatastreams('islandora:1');
#print_r($datastreams);
$test_pid = "wyu:9307";
$objectFormat = "info:fedora/fedora-system:FOXML-1.1";
$ingestMessage = array('string' => $foxml_file,
'label' => 'Fedora Label',
'format' => 'info:fedora/fedora-system:FOXML-1.1',
'encoding' => 'UTF-8',
'namespace' => 'islandora',
'ownerID' => 'adrAdmin',
'logMessage' => 'Log message from ingest');
#$result_pid = $api_m->ingest('$test_pid',array('string' => '$file_path','format' => 'info:fedora/fedora-system:FOXML-1.1') );
$result_pid = $api_m->ingest($ingestMessage) ;
print_r($result_pid);
// $foxml is a string containing the requested XML.
#$pid = "islandora:1";
#$foxml = $api_m->export($pid,
#array('format' => 'info:fedora/fedora-system:FOXML-1.1',
# 'context' => 'migrate',
# 'encoding' => 'UTF-8')
#);
#print_r($foxml);
# from https://github.com/Islandora/islandora/wiki/Build,-Access,-Modify-and-Delete-Fedora-objects-with-the-Tuque-interface
// $foxml is a string containing the requested XML.
#$pid = "wyu:9307";
#$foxml = $repository->api->m->export($pid,
#array('format' => 'info:fedora/fedora-system:FOXML-1.1',
# 'context' => 'migrate',
# 'encoding' => 'UTF-8')
#);
}
function coalliance_migration_from_foxml3($file_path, $fedora_namespace, $dmdid) {
// Check to make sure the path to the foxml file has been provided.
if (!isset($file_path)) {
drush_set_error('File path not provided',
'Path to mets file not provided as command line argument',
'Error: ');
return;
}
// Attempt to grab the foxml file.
$foxml_file = file_get_contents($file_path);
if (!$foxml_file) {
drush_set_error('File not found',
"Could not locate foxml file $file_path",
"Error: ");
return;
}
$fedora_object = $foxml_file;
/*
$my_islandora_tuque = new IslandoraTuque();
$repository = $my_islandora_tuque->repository;
$api_a = $repository->api->a; // For an Access API.
$api_m = $repository->api->m; // For a Management API.
*/
$connection = islandora_get_tugue_connection();
$repository = $connection->repository;
$object = $repository->constructObject($fedora_namespace);
$result_pid = $object; //test 1 to see if the object loads
//$result_pid = $repository->ingestObject($object); // test 2 to see if it ingests
/*
$test_pid = "wyu:9307";
$objectFormat = "info:fedora/fedora-system:FOXML-1.1";
$ingestMessage = "message";
$result_pid = $api_m->ingest($ingestMessage) ;
*/
print_r($result_pid); //print the result
}