-
Notifications
You must be signed in to change notification settings - Fork 6
/
archidora.drush.inc
117 lines (110 loc) · 3.36 KB
/
archidora.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
<?php
/**
* @file
* Sample Drush script to iterate through a collection.
*/
/**
* Implemenets hook_drush_command().
*/
function archidora_drush_command() {
$commands = array();
$commands['archidora-send-collection-to-archivematica'] = array(
'aliases' => array('asca'),
'description' => dt('Sends the members of a collection to Archivematica. Note: This only works on a flat level and is only intended as an example. Use at your own risk.'),
'drupal dependencies' => array(
'islandora',
'archidora',
),
'examples' => array(
'drush -u 1 archidora-send-collection-to-archivematica',
),
'options' => array(
'target' => array(
'description' => 'The root pid of where to begin our search.',
'required' => TRUE,
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
return $commands;
}
/**
* Sends a collection of objects to Archivematica and then finalizes the AIP.
*/
function drush_archidora_send_collection_to_archivematica() {
$target = drush_get_option('target');
batch_set(archidora_send_collection_create_batch($target));
drush_backend_batch_process();
}
/**
* Creates a sample batch to go and add all members of a collection.
*
* @param string $target
* The root pid to start our RI query from.
*
* @return array
* An array describing the batch to be ran.
*/
function archidora_send_collection_create_batch($target) {
return array(
'operations' => array(
array('archidora_send_collection_batch_operation', array($target)),
),
'title' => t('Sending the collection to Archivematica...'),
'init_message' => t('Preparing to send the collection.'),
'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaining @estimate.'),
'error_message' => t('An error has occurred.'),
'file' => drupal_get_path('module', 'archidora') . '/archidora.drush.inc',
);
}
/**
* Constructs and performs the send collection operation.
*
* @param string $target
* The root pid to start our RI query from.
*
* @param array $context
* The context of the Drupal batch.
*/
function archidora_send_collection_batch_operation($target, &$context) {
$object_update = 10;
$query = <<<EOQ
SELECT ?pid FROM <#ri>
WHERE {
{
?pid <fedora-rels-ext:isMemberOf> <info:fedora/$target>
}
UNION
{
?pid <fedora-rels-ext:isMemberOfCollection> <info:fedora/$target>
}
}
EOQ;
$connection = islandora_get_tuque_connection();
$sandbox = &$context['sandbox'];
if (!isset($sandbox['offset'])) {
$sparql_count = $connection->repository->ri->countQuery($query, 'sparql');
$sandbox['offset'] = 0;
$sandbox['total'] = $sparql_count;
if ($sandbox['total'] === 0) {
return;
}
}
$context['message'] = t('Processing results @start to @end.', array(
'@start' => $sandbox['offset'],
'@end' => min($sandbox['offset'] + $object_update, $sandbox['total']),
));
$offset_start = $sandbox['offset'];
$query .= "
LIMIT $object_update
OFFSET $offset_start
";
$results = $connection->repository->ri->sparqlQuery($query);
foreach ($results as $result) {
module_load_include('inc', 'archidora', 'includes/archivematica');
$object = islandora_object_load($result['pid']['value']);
archidora_send_to_archivematica($object);
}
$sandbox['offset'] += $object_update;
$context['finished'] = $sandbox['offset'] / $sandbox['total'];
}