-
Notifications
You must be signed in to change notification settings - Fork 0
/
cals_importer.s3paths.inc
executable file
·282 lines (240 loc) · 8.06 KB
/
cals_importer.s3paths.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
<?php
/**
* @file
* Updates s3 paths via batch update views
*/
/**
* Sets up the batch process
*
* @param $node
*
*/
function _cals_batch_update_s3_paths_by_node($node) {
global $user;
$arr = _cals_get_array_csv($node->nid);
if (empty($arr)) {
drupal_set_message(t("Problem parsing the file: can't access the file or generate an array. This is probably due to an issue reading / opening the file."), "error");
drupal_goto("node/" . $node->nid);
}
else {
$tmp = strpos($arr[0][1], "s3://"); //check first row to ensure well structured.
if ($tmp === FALSE) {
drupal_set_message(t("Problem parsing the file: please check its structure, esp. the S3 paths."), "error");
drupal_goto("node/" . $node->nid);
}
}
$batch = array(
'title' => t('Update S3 Paths'),
//'operations' => array('_cals_run_batch_update_s3_path', array($nid, $arr) ),
'operations' => array(array('_cals_run_batch_update_s3_path', array($node->nid, $arr)),
),
//'progress_message' => t('Update. Operation @current out of @total.'),
'error_message' => t('Error!'),
'finished' => '_cals_batch_update_s3_path_finished',
);
($batch);
batch_set($batch);
batch_process('node/' . $node->nid);
}
/**
* Invokes Drupal's batch api to update S3 paths
*
* @param $nid
* the node id
*/
function _cals_batch_update_s3_paths($nid) {
global $user;
$null_path = "admin/content/cals/s3-record-sets";
$msg_file_str = t("Can't parse the S3 file. Please check its structure.");
if (empty($nid)) drupal_goto($null_path); //printPre("HEY, empty!");
//get array from csv file. Pass this into the initial batch
$arr = _cals_get_array_csv($nid);
if (empty($arr)) {
drupal_set_message(t("Problem parsing the file: can't access the file or generate an array. This is probably due to an issue reading / opening the file."), "error");
drupal_goto($null_path);
}
else {
//check first row to ensure well structured.
$tmp = strpos($arr[0][1], "s3://");
if ($tmp === FALSE) {
drupal_set_message(t("Problem parsing the file: please check its structure, esp. the S3 paths."), "error");
drupal_goto($null_path);
}
}
$batch = array(
'title' => t('Update S3 Paths'),
//'operations' => array('_cals_run_batch_update_s3_path', array($nid, $arr) ),
'operations' => array(array('_cals_run_batch_update_s3_path', array($nid, $arr)),
),
//'progress_message' => t('Update. Operation @current out of @total.'),
'error_message' => t('Error!'),
'finished' => '_cals_batch_update_s3_path_finished',
);
($batch);
batch_set($batch);
batch_process('admin/content/cals/s3-record-sets');
}
/**
* The main batch process to update S3 paths
*
* @param $nid
* the node id
* @param $arr
* the array of data from the csv attached to the S3 recordset
*
* @return
* $val, a string
*/
function _cals_run_batch_update_s3_path($nid, $arr, &$context) {
ini_set('max_execution_time', 400); //~6.67 minutes, 1000 paths @ 2.5 a second
$limit = 10; //10;
// Get Count of products
if (empty($context['sandbox']['progress']) ) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($arr); //count($pids);
$context['sandbox']['arr'] = $arr;
$context['sandbox']['current'] = 0;
$context['sandbox']['node'] = node_load($nid);
//_cals_delete_s3_mapping($context['sandbox']['node']);
//watchdog('import', 'update s3 paths');
//dpm($arr);
}
$i = $context['sandbox']['progress'];
$total = $i + 10;
if ($total > $context['sandbox']['max']) $total = $context['sandbox']['max'];
for ($i = $context['sandbox']['progress']; $i < $context['sandbox']['max']; $i++) {
$current_row = $context['sandbox']['progress'];
$data = $context['sandbox']['arr'][$current_row];
//$bibid = $data[0];
$sysnum = $data[0];
$path = $data[1];
$nid2 = _cals_get_node_by_sysnum($sysnum);
if ($nid2 > 0) {
$paths = array();
$node = node_load($nid2);
if (isset($node->field_s3_path[LANGUAGE_NONE]) && count($node->field_s3_path[LANGUAGE_NONE]) > 1) {
foreach ($node->field_s3_path[LANGUAGE_NONE] as $k => $v) {
$paths[] = $v['value'];
}
}
$paths[] = $path;
$paths = array_unique($paths);
unset($node->field_s3_path[LANGUAGE_NONE]); //reset this variable so we can dedupe with unique values
foreach ($paths as $path) {
$node->field_s3_path[LANGUAGE_NONE][]['value'] = $path;
}
//$node->field_bibid[LANGUAGE_NONE][0]['value'] = $bibid;
node_save($node);
//drupal_set_message(t("%cr{$current_row}. updated node " . l($nid2, "node/" . $nid2) . ": " . $node->title);
$context['results'][] = check_plain($node->title); // Track nodes updated
$context['message'] = "updated node " . $node->nid;
}
else {
drupal_set_message(t("%c. could not find %s"), array('%c' => $current_row, '%s' => $sysnum));
}
_cals_populate_s3_mapping($context['sandbox']['node'], $nid2, $path, $sysnum);
$context['sandbox']['progress']++; // Used to keep track of rows processed
//array_shift($context['sandbox']['arr']);
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Creates and populates new field collection entity, saves node.
*
* @param $node
* the S3 recordset node
* @param $nid
* the nid of the repository item being referenced
* @param $path
* the s3 path
* @param $sysnum
* the system number, i.e., the field is used to match the s3 path to the repos item
*/
function _cals_populate_s3_mapping($node, $nid, $path, $sysnum) {
// Create new field collection item.
$fc_item = entity_create('field_collection_item', array('field_name' => 'field_s3_mapping'));
// Attach it to the node.
$fc_item->setHostEntity('node', $node);
$fc_item->field_system_control_number[LANGUAGE_NONE][0]['value'] = $sysnum;
$fc_item->field_s3_path[LANGUAGE_NONE][0]['value'] = $path;
if($nid > 0) $fc_item->field_nid_s3[LANGUAGE_NONE][0]['nid'] = $nid;
$fc_item->save(); // Save field-collection item.
node_save($node);
drupal_set_message( t("updated field collection: sysnum = @sysnum | nid = @nid",
array('@sysnum' => $sysnum, '@nid' => $nid) ) );
}
/**
* Finish batch
*/
function _cals_batch_update_s3_path_finished($success, $results, $operations) {
if ($success) {
$message = format_plural(count($results), 'One node updated', '@count nodes updated.');
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
//watchdog('update', 's3 paths updated');
}
/**
* Reads CSV file; returns array
*
* @param $nid
* The node id with the CSV attachment
*
* @return
* an array.
*/
function _cals_get_array_csv($nid) {
$node = node_load($nid);
$records = $node->field_s3_paths[LANGUAGE_NONE][0]['value'];
$arr = array();
//$records = explode("\n", $records);
$records = str_getcsv($records, "\n");
foreach($records as $k => $v) {
if($k > 0) $arr[] = str_getcsv($v, "\t");
}
return $arr;
//NOTE TO SELF, why does this return here???
/* */
$file = drupal_realpath($node->field_file_csv[LANGUAGE_NONE][0]['uri']);
$row = 1;
$arr = array();
if (($handle = fopen($file, "r")) !== FALSE) {
//while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($row > 1 ) {
$arr[] = $data;
}
$row++;
}
fclose($handle);
}
return $arr;
}
/**
* Reads CSV file; returns array
*
* @param $file
* The full filepath
*
* @return
* an array.
*/
function _cals_get_array_csv_by_file($file) {
$row = 1;
$arr = array();
if (($handle = fopen($file, "r")) !== FALSE) {
//while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($row > 1 ) {
$arr[$data[0]] = $data[1];
}
$row++;
}
fclose($handle);
}
return $arr;
}