forked from discoverygarden/manidora
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanidora.drush.inc
200 lines (177 loc) · 5.72 KB
/
manidora.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
<?php
/**
* @file
* Book batch ingest script for University of Manitoba.
*/
/**
* Implements hook_drush_command().
*/
function manidora_drush_command() {
$items = array();
$items['manidora-book-batch'] = array(
'options' => array(
'timeout' => array(
'description' => 'The duration to run for (in seconds), before stopping. Default is to run until completion.',
'value' => 'optional',
),
'target' => array(
'description' => 'The target directory for preprocessing.',
'value' => 'optional',
),
),
'drupal dependencies' => array(
'islandora_book_batch',
'manidora',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['manidora_generate_hq_jpegs'] = array(
'callback' => 'manidora_derive_hq_jpeg_datastreams',
'description' => 'Derives the HQ_JPG datastream for downloading pdfs of newspaper pages for those pages who missed having these derivatives created upon ingest.',
'examples' => array(
'drush -u 1 -l http://mysite manidora_derive_hq_jpegs',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
);
$items['manidora-wipe-hocr-doctypes'] = array(
'options' => array(
'before' => array(
'description' => 'HOCR datastreams from before the given date with be transformed. If not provided, all HOCR streams will be processed.',
'value' => 'optional',
),
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
'drupal dependencies' => array(
'islandora_ocr',
'islandora',
'manidora',
),
);
return $items;
}
/**
* Drush command callback.
*
* Triggers the ingest process. Attempts to ingest, preprocess, and ingest, in
* that order.
*/
function drush_manidora_book_batch() {
$t = 'manidora_book_batch_timeout';
$timeout = drush_get_option('timeout', NULL);
timer_start($t);
$ops = array(
'timeout' => $timeout,
);
drush_invoke_process('@self', 'islandora_batch_ingest', array(), $ops);
$remaining = ($timeout * 1000) - timer_read($t);
if ($timeout === NULL || $remaining > 0) {
drush_invoke_process('@self', 'islandora_book_batch_preprocess', array(), array(
'type' => 'directory',
'target' => drush_get_option('target', '/usr/local/fedora/staging'),
'parent' => 'uofm:rarebooks',
'directory_dedup' => TRUE,
'wait_for_metadata' => TRUE,
'email_admin' => TRUE,
'create_pdfs' => TRUE,
));
$remaining = ($timeout * 1000) - timer_read($t);
if ($timeout === NULL || $remaining > 0) {
if ($timeout !== NULL) {
// Timeout is in seconds, remaining is in ms.
$ops['timeout'] = $remaining / 1000;
}
drush_invoke_process('@self', 'islandora_batch_ingest', array(), $ops);
}
}
timer_stop($t);
}
/**
* Drush command callback.
*
* Generate HQ_JPG derivatives.
*/
function manidora_derive_hq_jpeg_datastreams() {
$obj = '<info:fedora/*/OBJ>';
$q = <<<EOQ
SELECT ?obj
FROM <#ri>
WHERE {
?obj <fedora-model:hasModel> <info:fedora/islandora:newspaperPageCModel> ;
<fedora-view:disseminates> [<fedora-view:disseminationType> $obj]
}
EOQ;
$tuque = islandora_get_tuque_connection();
foreach ($tuque->repository->ri->sparqlQuery($q) as $result) {
$pid = $result['obj']['value'];
$object = islandora_object_load($pid);
if (!isset($object['HQ_JPG'])) {
manidora_islandora_newspaperpagecmodel_islandora_object_ingested($object);
}
}
}
/**
* Batch operation.
*/
function manidora_batch_wipe_hocr_doctypes($before_date, &$context) {
if ($before_date) {
$before_date_filter = "FILTER(?datastream_date < '$before_date'^^<http://www.w3.org/2001/XMLSchema#dateTime>)";
}
elseif (isset($context['results']['first_altered_date'])) {
$before_date_filter = "FILTER(?datastream_date < '{$context['results']['first_altered_date']}'^^<http://www.w3.org/2001/XMLSchema#dateTime>)";
}
else {
$before_date_filter = '';
}
$query = <<<EOQ
SELECT ?object ?datastream_date
WHERE {
?object <fedora-view:disseminates> ?datastream ;
<fedora-model:state> <fedora-model:Active> .
?datastream <fedora-model:state> <fedora-model:Active> ;
<fedora-view:disseminationType> <info:fedora/*/HOCR> ;
<fedora-view:lastModifiedDate> ?datastream_date .
$before_date_filter
}
ORDER BY ?datastream_date
LIMIT 50
EOQ;
$connection = islandora_get_tuque_connection();
$results = $connection->repository->ri->sparqlQuery($query);
// If there were no results, we're finished.
$context['finished'] = empty($results);
$temp_file = drupal_tempnam('temporary://', 'hocr-dtd');
foreach ($results as $result) {
$object = islandora_object_load($result['object']['value']);
$datastream = $object['HOCR'];
$datastream->getContent($temp_file);
HOCR::stripDoctypeFromFile($temp_file);
$datastream->setContentFromFile($temp_file);
if (!$before_date && !isset($context['results']['first_altered_date'])) {
// The resource index calls it "lastModifiedDate"... Looks like objects
// call it "createdDate"... Hurray inconsistencies!
$context['results']['first_altered_date'] = (string) $datastream->createdDate;
}
$context['message'] = dt('Wiped DOCTYPE from HOCR in @pid. New lastModified/created date "@date".', array(
'@pid' => $object->id,
'@date' => (string) $datastream->createdDate,
));
}
file_unmanaged_delete($temp_file);
}
/**
* Drush callback.
*/
function drush_manidora_wipe_hocr_doctypes() {
$batch = array(
'operations' => array(
array(
'manidora_batch_wipe_hocr_doctypes', array(
drush_get_option('before', FALSE),
),
),
),
'title' => dt('Wiping HOCR DOCTYPE declarations...'),
);
batch_set($batch);
drush_backend_batch_process();
}