-
Notifications
You must be signed in to change notification settings - Fork 0
/
cals_importer.batch.inc
executable file
·146 lines (125 loc) · 5.01 KB
/
cals_importer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @file
* CALS Importer Batch Module.
*/
/**
* Implements hook_submit()
*/
function cals_importer_form_submit($form, & $form_state) {
global $user;
$batch_proc = FALSE;
$extensions = 'xml bib enw mrc ris txt';
$validators['file_validate_extensions'] = array();
$validators['file_validate_extensions'][0] = $extensions;
$userid = $user->uid;
$filetype = $form_state['values']['filetype'];
$filesize = sprintf("%01.1f", $import_file->filesize / 1000);
$filesize = " ($filesize KB)";
$session_id = microtime();
$batch = array(
'title' => t('Importing') . " ",
'operations' => array(
array('cals_import_batch_operations', array($session_id, $user, $userid, $terms))
),
'progressive' => TRUE,
'finished' => 'cals_import_batch_finished',
'init_message' => t('Parsing file...'),
'progress_message' => t('Saving nodes...'),
);
batch_set($batch);
return $batch;
$base = variable_get('biblio_base', 'biblio');
batch_process("$base/import");
}
function cals_import_batch_operations($session_id, $filename, $userid, &$context) {
$limit = 10;
$ns = "http://www.loc.gov/MARC21/slim";
if (empty($context['sandbox'])) {
// Initiate multistep processing.
$context['results']['session_id'] = $session_id;
$context['results']['userid'] = $userid;
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['results']['nids'] = array();
$context['filename'] = $filename; // FILE_FOLDER . ;
$context['xml'] = _cals_load_records($filename);
$context['sandbox']['max'] = count($xml->children($ns));
$context['sandbox']['itters'] = $context['sandbox']['max'] / $limit;
$context['sandbox']['eta'] = 0;
}
// Bail out if the cache is empty.
if ($context['sandbox']['max'] == 0) {
return;
}
// Process the next 20 nodes.
for ($i = $context['sandbox']['current_id']; $i < $context['sandbox']['current_id'] + $limit; ++$i) {
drupal_set_message(t("processing record id: %i", array('%i' => $i));
/*
if ($node = unserialize(base64_decode($row->data))) {
cals_importer_save_node($node, $terms);
$context['results']['nids'][] = $node->nid;
}
*/
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $i;
}
// Multistep processing : report progress.
if ($context['sandbox']['progress'] <= $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function cals_import_batch_finished($success, $results, $operations) {
cals_importer_import_finalize($success, $results);
//clean up import cache...
db_delete('cals_importer_import_cache')
->condition('session_id', $results['session_id'])
->execute();
}
function cals_importer_import_finalize($success, $results) {
global $user;
$format = $results['format'];
$nids = $results['nids'];
$dups = $results['dups'];
$total = count($nids) + count($dups);
// drupal_set_message(t("<i><b>%count</b></i> of <i><b>%total</b></i> nodes were successfully imported.", array('%count' => count($nids), '%total' => $total)), (count($nids) != $total)?'warning':'status');
if ($success && (count($nids) || count($dups))) {
$message = t("The file <i><b>@file</b></i> was successfully uploaded.", array('@file' => $results['file']->filename));
drupal_set_message($message, 'status');
watchdog($format, $message);
$count = count($nids);
$message = format_plural($count, 'One of @total node imported.', '@count of @total nodes imported.', array('@total' => $total));
drupal_set_message($message, 'status');
watchdog($format, $message, array('@count' => $count, '@total' => $total), WATCHDOG_INFO);
if (count($dups)) {
$count = count($dups);
$message = format_plural($count, 'One duplicate node skipped.', '@count duplicate nodes skipped.');
drupal_set_message($message, 'status');
watchdog($format, $message, array('@count' => $count), WATCHDOG_INFO);
foreach ($dups as $nid) {
$message = '';
$message = t('The item you are trying to import already exists in the database, see');
$message .= " " . l(t('node/%n', array('%n' => $nid), 'node/' . $nid);
drupal_set_message($message, 'status');
watchdog($format, $message, array(), WATCHDOG_ERROR);
}
}
}
else {
$count = count($nids);
$message = t('Import finished with an error!') . ' ' . format_plural($count, 'One node imported.', '@count nodes imported.');
drupal_set_message($message, 'error');
watchdog($format, $message, array('@count' => $count), WATCHDOG_ERROR);
}
$userid = isset($results['userid']) ? $results['userid'] : $user->uid;
if (user_access('administer biblio') && count($nids) && $user->uid != $userid) {
db_update('node')
->fields(array('uid' => $results['userid']))
->condition('nid', $nids, 'IN')
->execute();
db_update('node_revision')
->fields(array('uid' => $results['userid']))
->condition('nid', $nids, 'IN')
->execute();
}
}