forked from Islandora/islandora_solution_pack_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbs.batch.inc
131 lines (124 loc) · 4.45 KB
/
thumbs.batch.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
<?php
/**
* @file
* Batch for collection thumbnail generation based on content.
*/
/**
* Construct a bath for pulling thumbnails from children to collections.
*/
function islandora_basic_collection_thumbs_batch($collection) {
return array(
'title' => t('Pull Thumbnails from Children'),
'init_message' => t('Preparing to pull thumbnails from children.'),
'progress_message' => t('Time elapsed: @elapsed <br/>Estimated time remaining @estimate.'),
'error_message' => t('An error has occurred.'),
'file' => drupal_get_path('module', 'islandora_basic_collection') . '/includes/thumbs.batch.inc',
'operations' => array(
array('islandora_basic_collection_thumbs_batch_operation', array($collection)),
),
);
}
/**
* Batch operation for pulling child thumbs.
*
* @param string $collection
* PID of the collection to start the recursal at.
* @param array $context
* Context array for this batch. Used to tell batch when we're finished.
*/
function islandora_basic_collection_thumbs_batch_operation($collection, &$context) {
module_load_include('inc', 'islandora', 'includes/utilities');
$tuque = islandora_get_tuque_connection();
$sandbox = &$context['sandbox'];
// Initial setup.
if (!isset($sandbox['stack'])) {
$sandbox['stack'] = array($collection);
$sandbox['retry_stack'] = array($collection);
$sandbox['completed'] = 0;
}
if ($sandbox['stack']) {
$current_pid = array_pop($sandbox['stack']);
$retry = FALSE;
}
elseif ($sandbox['retry_stack']) {
$current_pid = array_pop($sandbox['retry_stack']);
$retry = TRUE;
}
$replaced = FALSE;
// If not a retry check TN and push children collections to stack.
if (!$retry) {
$map_results = function($o) {
return $o['child']['value'];
};
$child_collections_query = <<<EOQ
SELECT ?child
FROM <#ri>
WHERE {
?child <fedora-rels-ext:isMemberOfCollection> <info:fedora/$current_pid> .
?child <fedora-model:hasModel> <info:fedora/islandora:collectionCModel>
}
EOQ;
$collections_results = $tuque->repository->ri->sparqlQuery($child_collections_query);
// Add children to stack.
$sandbox['stack'] = array_merge($sandbox['stack'], array_map($map_results, $collections_results));
}
// Test first child for TN.
$child_tn_query = <<<EOQ
SELECT ?child
FROM <#ri>
WHERE {
?child <fedora-rels-ext:isMemberOfCollection> <info:fedora/$current_pid> .
?child <fedora-view:disseminates> ?ds .
?ds <fedora-view:disseminationType> <info:fedora/*/TN>
}
EOQ;
$tn_results = $tuque->repository->ri->sparqlQuery($child_tn_query);
$replace = FALSE;
if ($tn_results) {
$tn_result = reset($tn_results);
$first_child_pid = $tn_result['child']['value'];
$first_child = islandora_object_load($first_child_pid);
if (isset($first_child['TN'])) {
$copied_file = file_create_filename('TN.png', file_default_scheme() . '://');
$first_child['TN']->getContent($copied_file);
$copied_file_object = islandora_temp_file_entry($copied_file);
// TN size check to avoid checksum expense, not trusting Fedora's size
// (observed off by one errors).
if ($copied_file_object->filesize != 5137) {
$replace = TRUE;
}
// If size the same do checksum, also don't trust Fedora, some
// checksums are lies (only observed with Fedora re-formating XML).
elseif (md5_file($copied_file_object->uri) == 'cb971815171163beb105f8074eae1bde') {
$replace = TRUE;
}
file_delete($copied_file_object);
}
}
if ($replace) {
$current_object = islandora_object_load($current_pid);
if (!isset($current_object['TN'])) {
$current_object->ingestDatastream($first_child['TN']);
}
else {
$current_object['TN']->mimetype = $first_child['TN']->mimetype;
$current_object['TN']->label = $first_child['TN']->label;
$current_object['TN']->content = $first_child['TN']->content;
}
$replaced = TRUE;
}
// Throw warning to watchdog if final try had no TN.
if ($retry && !$replaced) {
watchdog(
'warning',
"Couldn't replace TN for !current_pid, this can still be done manually.",
array('!current_pid', $current_pid)
);
}
// Abort and return to stack if not retry and child has default TN.
elseif (!$retry && !$replaced) {
array_push($sandbox['retry_stack'], $current_pid);
}
$sandbox['completed'] += 1;
$context['finished'] = $sandbox['completed'] / (count($sandbox['retry_stack']) + count($sandbox['stack']) + $sandbox['completed']);
}