forked from Islandora/islandora_batch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_batch.module
274 lines (248 loc) · 9.08 KB
/
islandora_batch.module
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
<?php
/**
* @file
* Module for Batch ingests.
*/
define('ISLANDORA_BATCH_STATE__ERROR', -1);
define('ISLANDORA_BATCH_STATE__NOT_READY', 0);
define('ISLANDORA_BATCH_STATE__READY', 1);
define('ISLANDORA_BATCH_STATE__PENDING_CHILDREN', 2);
define('ISLANDORA_BATCH_STATE__DONE', 3);
define('ISLANDORA_BATCH_MANAGE_QUEUE_PERMISSION', 'islandora_batch_manage_queue');
// Hooks.
define('ISLANDORA_BATCH_PROCESS_FINISHED_HOOK', 'islandora_batch_process_finished');
define('ISLANDORA_BATCH_OBJECT_PROCESSED_HOOK', 'islandora_batch_object_processed');
/**
* Implements hook_menu().
*/
function islandora_batch_menu() {
$items = array();
$items['admin/islandora/tools/batch'] = array(
'title' => 'Islandora Batch Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_batch_admin_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['islandora_batch/item/%/delete'] = array(
'title' => 'Delete "@item" from queue',
'title arguments' => array('@item' => 2),
'page callback' => 'islandora_batch_delete_item_page_callback',
'page arguments' => array(2),
'access callback' => 'islandora_batch_item_access',
'access arguments' => array(2),
'file' => 'includes/menu.inc',
);
$items['islandora_batch/item/%/set_state'] = array(
'title' => 'Set "@item" state',
'title arguments' => array('@item' => 2),
'page callback' => 'islandora_batch_set_item_state_page_callback',
'page arguments' => array(2),
'access callback' => 'islandora_batch_item_access',
'access arguments' => array(2),
'file' => 'includes/menu.inc',
);
$delete_set_base = array(
'page callback' => 'islandora_batch_delete_set_page_callback',
'access callback' => 'islandora_batch_set_access',
'file' => 'includes/menu.inc',
);
$items['islandora_batch/set/%/delete'] = $delete_set_base + array(
'title' => 'Delete items in "@set" from queue',
'title arguments' => array('@set' => 2),
'page arguments' => array(2),
'access arguments' => array(2),
);
$items['islandora_batch/reports/queue/%islandora_batch_set/delete'] = $delete_set_base + array(
'title' => 'Delete set',
'page arguments' => array(3, FALSE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
$items['admin/reports/islandora_batch_queue/%islandora_batch_set/delete'] = $delete_set_base + array(
'title' => 'Delete set',
'page arguments' => array(3, TRUE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
$set_set_state_base = array(
'page callback' => 'islandora_batch_set_set_items_state_page_callback',
'access callback' => 'islandora_batch_set_access',
'file' => 'includes/menu.inc',
);
$items['islandora_batch/set/%/set_state'] = $set_set_state_base + array(
'title' => 'Set state on items in "@set" in queue',
'title arguments' => array('@set' => 2),
'page arguments' => array(2),
'access arguments' => array(2),
);
$items['islandora_batch/reports/queue/%islandora_batch_set/set_state'] = $set_set_state_base + array(
'title' => 'Set state of all items',
'page arguments' => array(3, FALSE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
$items['admin/reports/islandora_batch_queue/%islandora_batch_set/set_state'] = $set_set_state_base + array(
'title' => 'Set state of all items',
'page arguments' => array(3, TRUE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
$process_set_base = array(
'page callback' => 'islandora_batch_set_restart_batch_page_callback',
'access callback' => 'islandora_batch_set_restart_batch_access_callback',
'file' => 'includes/menu.inc',
);
$items['islandora_batch/set/%/restart_batch'] = $process_set_base + array(
'title' => 'Process items in set "@set"',
'title arguments' => array('@set' => 2),
'page arguments' => array(2),
'access arguments' => array(2),
);
$items['islandora_batch/reports/queue/%islandora_batch_set/restart_set'] = $process_set_base + array(
'title' => 'Process Set',
'page arguments' => array(3, FALSE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
$items['admin/reports/islandora_batch_queue/%islandora_batch_set/restart_set'] = $process_set_base + array(
'title' => 'Process Set',
'page arguments' => array(3, TRUE),
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
);
return $items;
}
/**
* Map values.
*
* Needed to avoid the set being an optional parameter in the queue displays.
*
* @param string $value
* A value to map to a set.
*
* @return string|bool
* The input $value if it was set; otherwise, FALSE.
*/
function islandora_batch_set_to_arg($value) {
return (empty($value) || $value == '%') ? FALSE : $value;
}
/**
* Check permissions for an item.
*
* Check if the user has the "manage all" permission, or the item belongs to
* the current user.
*
* @param string $id
* A string representing the ID of an item in the queue.
*
* @return bool
* TRUE if the current user has permission to manage all items, or the item
* is in a set owned by the given user; otherwise, FALSE.
*/
function islandora_batch_item_access($id) {
global $user;
module_load_include('inc', 'islandora_batch', 'includes/utilities');
return islandora_batch_item_exists($id) && (user_access(ISLANDORA_BATCH_MANAGE_QUEUE_PERMISSION, $user) || islandora_batch_item_is_owner($id, $user));
}
/**
* Check permissions for an set.
*
* Check if the user has the "manage all" permission, or the set belongs to
* the current user.
*
* @param string $id
* A string representing the ID of an set in the queue.
*
* @return bool
* TRUE if the current user has permission to manage all sets, or the set
* is in a set owned by the given user; otherwise, FALSE.
*/
function islandora_batch_set_access($id) {
global $user;
module_load_include('inc', 'islandora_batch', 'includes/utilities');
return islandora_batch_set_exists($id) && (user_access(ISLANDORA_BATCH_MANAGE_QUEUE_PERMISSION, $user) || islandora_batch_set_is_owner($id, $user));
}
/**
* Check if a lock exists, and if we have access to mess with the set.
*
* Checks as per islandora_batch_set_access(), with an additional check for the
* existence of a lock.
*
* @param string $id
* A string representing the ID of an set in the queue.
*
* @return bool
* TRUE if the set appears to be locked and the current user has permission
* to manage all sets, or the set is in a set owned by the given user;
* otherwise, FALSE.
*/
function islandora_batch_set_restart_batch_access_callback($id) {
module_load_include('inc', 'islandora_batch', 'includes/ingest.batch');
$lock_name = islandora_batch_get_lock_name($id);
// NULL represents the "global" ingest lock, which we should not be allowed
// to break here... Should fail in the islandora_batch_set_access() bit,
// but... paranoia?
return $id && lock_may_be_available($lock_name) && islandora_batch_set_access($id);
}
/**
* Function to handle preprocessing.
*/
function islandora_batch_handle_preprocessor(IslandoraBatchPreprocessor $preprocessor) {
return $preprocessor->preprocess();
}
/**
* Generate and set the batch operations.
*
* After calling this, it will be necessary to call one of the functions which
* actually execute the batch, such as batch_process(),
* drush_backend_batch_process() or background_batch_process_batch().
*
* Queries the database for preprocessed entries, and attempts to ingest them.
*/
function islandora_batch_set_operations(array $parameters) {
$batch = array(
'title' => t('Islandora Batch Ingest'),
'finished' => 'islandora_batch_finished',
'init_message' => t('Initializing...'),
'file' => drupal_get_path('module', 'islandora_batch') . '/includes/ingest.batch.inc',
'operations' => array(),
'progress_message' => t('Time elapsed: @elapsed.'),
);
if (isset($parameters['preprocessor'])) {
$batch['operations'][] = array('islandora_batch_ingest_preprocess', array($parameters['preprocessor']));
}
if (isset($parameters['preprocessor']) || isset($parameters['ingest_set'])) {
$batch['progress_message'] = t('Time elapsed: @elapsed <br/>Estimated time remaining @estimate.');
}
$batch['operations'][] = array('islandora_batch_ingest_process', array($parameters));
batch_set($batch);
}
/**
* Dumbly sets the batch operations and calls batch_process().
*/
function islandora_batch_ingest(array $parameters) {
islandora_batch_set_operations($parameters);
batch_process();
}
/**
* Implements hook_views_api().
*/
function islandora_batch_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'islandora_batch') . '/views',
);
}
/**
* Implements hook_permission().
*/
function islandora_batch_permission() {
$perms = array();
$perms[ISLANDORA_BATCH_MANAGE_QUEUE_PERMISSION] = array(
'title' => t('Islandora Batch Queue Management'),
'description' => t('Allow management of all items and sets in the batch queue.'),
);
return $perms;
}