-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdb.inc
416 lines (393 loc) · 11 KB
/
db.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* @file
* Database Helper Functions.
*/
/**
* Delete batch set from the drupal database and mark files for removal.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return int
* An integer of objects removed from queue.
*/
function islandora_batch_delete_set($set_id) {
$deleted = 0;
if ($set_id !== NULL) {
module_load_include('inc', 'islandora_batch', 'includes/utilities');
$transaction = db_transaction();
$s = db_select('islandora_batch_queue', 'q')
->fields('q', array('id'))
->condition('sid', $set_id);
$items = $s->execute()->fetchCol();
foreach ($items as $item) {
islandora_batch_remove_file_usages($item);
}
db_delete('islandora_batch_state')
->condition('id', $s, 'IN')
->execute();
db_delete('islandora_batch_queue_messages')
->condition('id', $s, 'IN')
->execute();
db_delete('islandora_batch_resources')
->condition('id', $s, 'IN')
->execute();
$deleted = db_delete('islandora_batch_queue')
->condition('sid', $set_id)
->execute();
db_delete('islandora_batch_set')
->condition('id', $set_id)
->execute();
// Mark any files as being unused--free to be delete.
$q = db_select('file_usage', 'u')
->fields('u', array('fid'))
->condition('module', 'islandora_batch')
->condition('type', 'islandora_batch_set')
->execute();
$files = file_load_multiple($q->fetchCol());
foreach ($files as $file) {
file_usage_delete($file, 'islandora_batch', 'islandora_batch_set', $set_id);
}
}
return $deleted;
}
/**
* Delete single object from the Drupal database and mark file for removal.
*
* @param string $pid
* Object PID to remove from queue.
*/
function islandora_batch_delete_object($pid) {
if ($pid !== NULL) {
module_load_include('inc', 'islandora_batch', 'includes/utilities');
$transaction = db_transaction();
islandora_batch_remove_file_usages($pid);
db_delete('islandora_batch_state')
->condition('id', $pid)
->execute();
db_delete('islandora_batch_resources')
->condition('id', $pid)
->execute();
db_delete('islandora_batch_queue_messages')
->condition('id', $pid)
->execute();
db_delete('islandora_batch_queue')
->condition('id', $pid)
->execute();
}
}
/**
* Get all Batch sets stored in the database.
*
* @param int|bool|NULL $completed
* An optional timestamp:
* - An integer timestamp will return sets dependent on $op.
* - Boolean TRUE will provide all completed sets.
* - Boolean FALSE will provide all incomplete sets.
* - NULL (the default) will provide ALL sets.
* @param string $op
* Only used with non-boolean/NULL values of $completed; represents the
* operator to use in the comparison. Defaults to '<=', to select
* objects completed before (or on) a specific timestamp.
*
* @return array
* An array of batch set ids for batches that exist in the database.
*/
function islandora_batch_get_sets($completed = NULL, $op = '<=') {
$query = db_select('islandora_batch_set', 's')
->fields('s', array('id'));
if ($completed !== NULL) {
if ($completed === TRUE) {
$query->isNotNull('completed');
}
elseif ($completed === FALSE) {
$query->isNull('completed');
}
else {
$query->condition('completed', $completed, $op);
}
}
$sets = $query->execute();
return $sets->fetchCol();
}
/**
* Get all objects queued for a specified set.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return array
* An array of objects queued for a specific set.
*/
function islandora_batch_get_queued_items_for_set($set_id) {
$results = array();
$q = db_select('islandora_batch_queue', 'q');
$q->join('islandora_batch_state', 's', 's.id = q.id');
$items = $q->fields('q', array('id'))
->condition('q.sid', $set_id)
->execute();
while ($item = $items->fetchAssoc()) {
$results[] = $item['id'];
}
return $results;
}
/**
* Insert or update the data of a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
* @param string $data
* Updated data value.
*/
function islandora_batch_merge_queued_object_data($pid, $data) {
db_merge('islandora_batch_queue')
->key(array('id' => $pid))
->fields(array(
'data' => $data,
))
->execute();
}
/**
* Insert record or update the state of a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
* @param int $state
* Updated state value.
*/
function islandora_batch_merge_object_state($pid, $state) {
db_merge('islandora_batch_state')
->key(array('id' => $pid))
->fields(array(
'state' => $state,
))
->execute();
}
/**
* Insert record or update the message of a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
* @param string $message
* Message for the object.
*/
function islandora_batch_merge_object_message($pid, $message) {
db_merge('islandora_batch_queue_messages')
->key(array('id' => $pid))
->fields(array('message' => $message))
->execute();
}
/**
* Get the batch set id related to a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
*
* @return int
* The id of the batch set the object is apart of.
*/
function islandora_batch_get_set_for_object($pid) {
$set_id = db_select('islandora_batch_queue', 'q')
->fields('q', array('sid'))
->condition('id', $pid)
->execute()
->fetchField();
return $set_id;
}
/**
* Get the batch id related to a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
*
* @return int
* The batch id for the object.
*/
function islandora_batch_get_batch_id_for_object($pid) {
$id = db_select('islandora_batch_queue', 'q')
->fields('q', array('bid'))
->condition('id', $pid)
->execute()
->fetchField();
return $id;
}
/**
* Get the count of queued objects in the specified batch set.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return int
* The count of queued objects in the specified batch set.
*/
function islandora_batch_get_count_of_queued_set_objects($set_id) {
$count = db_select('islandora_batch_queue', 'q')
->fields('q', array())
->condition('sid', $set_id)
->countQuery()
->execute()
->fetchField();
return $count;
}
/**
* Update the state of all objects in the specified set.
*
* @param int $id
* The set id identifying the queued batch set.
* @param int $new_state
* The new state value to update to.
* @param string $source_state
* The selected source state.
* @param int $filter_on_state
* The old state id to use for filtering.
*
* @return int
* The number of updated objects.
*/
function islandora_batch_update_set_object_states($id, $new_state, $source_state, $filter_on_state) {
$s = db_select('islandora_batch_queue', 'q')
->fields('q', array('id'))
->condition('sid', $id);
$q = db_update('islandora_batch_state')
->fields(array('state' => $new_state))
->condition('id', $s, 'IN');
if ($source_state !== 'any') {
$q->condition('state', $filter_on_state);
}
$updated = $q->execute();
return $updated;
}
/**
* Update the state of a queued object.
*
* @param string $pid
* The object id identifying the queued batch object.
* @param int $state
* Updated state value.
*/
function islandora_batch_update_object_state($pid, $state) {
db_update('islandora_batch_state')
->fields(array('state' => $state))
->condition('id', $pid)
->execute();
}
/**
* Get the current state of an object.
*
* @param string $pid
* The object id identifying the queued batch object.
*/
function islandora_batch_get_object_state($pid) {
$state = db_select('islandora_batch_state', 's')
->fields('s', array('state'))
->condition('id', $pid)
->execute()
->fetchField();
return $state;
}
/**
* Check to see if a batch set was fully ingested without errors.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return bool
* TRUE if the batch sets was completely ingested without errors or not
* importing all objects in the set; otherwise, FALSE.
*/
function islandora_batch_check_if_set_is_fully_ingested($set_id) {
$query = db_select('islandora_batch_queue', 'q');
$query->join('islandora_batch_state', 's', 's.id = q.id');
$result = $query->fields('q', array('id'))
->condition('q.sid', $set_id)
->condition('s.state', ISLANDORA_BATCH_STATE__DONE, '<')
->countQuery()
->execute()
->fetchField();
if ($result == 0) {
return TRUE;
}
return FALSE;
}
/**
* Check to see if a batch set has failed ingesting any objects.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return bool
* TRUE if the batch sets has any failed to ingest objects.
*/
function islandora_batch_check_if_set_failed_ingest($set_id) {
$query = db_select('islandora_batch_queue', 'q');
$query->join('islandora_batch_state', 's', 's.id = q.id');
$result = $query->fields('q', array('id'))
->condition('q.sid', $set_id)
->condition('s.state', ISLANDORA_BATCH_STATE__ERROR)
->countQuery()
->execute()
->fetchField();
if ($result) {
return TRUE;
}
return FALSE;
}
/**
* Get the count of queued sets that are ready for processing.
*
* @return int
* The count of queued sets that are ready for processing.
*/
function islandora_batch_get_count_of_queued_sets_ready_to_process() {
$count = 0;
// Get all the sets in the queue and check each one to see if they are ready.
$sets = islandora_batch_get_sets();
if (!empty($sets)) {
foreach ($sets as $set) {
if (islandora_batch_check_set_for_objects_ready_for_processing($set)) {
$count++;
}
}
}
return $count;
}
/**
* Get the count of queued objects ready for processing.
*
* @return int
* The count of all queued objects ready for processing.
*/
function islandora_batch_get_count_of_queued_objects_ready_to_process() {
$count = db_select('islandora_batch_state', 'q')
->fields('q', array())
->condition('state', array(ISLANDORA_BATCH_STATE__READY, ISLANDORA_BATCH_STATE__PENDING_CHILDREN), 'IN')
->countQuery()
->execute()
->fetchField();
return $count;
}
/**
* Check to see if a batch set has objects that need processing.
*
* @param int $set_id
* An integer identifying a batch set.
*
* @return bool
* TRUE if the batch sets has objects that are ready for ingesting; otherwise,
* FALSE.
*/
function islandora_batch_check_set_for_objects_ready_for_processing($set_id) {
$query = db_select('islandora_batch_queue', 'q');
$query->join('islandora_batch_state', 's', 's.id = q.id');
$result = $query->fields('q', array('id'))
->condition('q.sid', $set_id)
->condition('s.state', array(ISLANDORA_BATCH_STATE__READY, ISLANDORA_BATCH_STATE__PENDING_CHILDREN), 'IN')
->countQuery()
->execute()
->fetchField();
if ($result > 0) {
return TRUE;
}
return FALSE;
}