-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdkan_datastore.module
628 lines (580 loc) · 17.5 KB
/
dkan_datastore.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* @file
* Creates DKAN Datastore.
*/
// Datastore is created for a given resource.
define('DKAN_DATASTORE_EMPTY', 0);
// Datastore is created for a given resource.
define('DKAN_DATASTORE_EXISTS', 1);
// File is attached to a resource, but not added to the datastore.
define('DKAN_DATASTORE_FILE_EXISTS', 2);
// File is attached to a resource, but cannot be added to the datastore.
define('DKAN_DATASTORE_WRONG_TYPE', 3);
include_once "dkan_datastore.features.inc";
/**
* Implements hook_menu().
*/
function dkan_datastore_menu() {
$items['node/%dkan_datastore_resource/download'] = array(
'title callback' => 'dkan_datastore_download_title',
'title arguments' => array(1),
'page callback' => 'dkan_datastore_download',
'page arguments' => array(1),
'access callback' => 'dkan_datastore_download_access',
'access arguments' => array('view', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '20',
'type' => MENU_LOCAL_TASK,
);
$items['node/%dkan_datastore_resource/data'] = array(
'page callback' => 'dkan_datastore_proxy',
'page arguments' => array(1),
'access callback' => 'dkan_datastore_download_access',
'access arguments' => array('view', 1),
'file' => 'dkan_datastore.pages.inc',
'type' => MENU_CALLBACK,
);
$items['node/%node/api'] = array(
'title' => 'Data API',
'page callback' => 'dkan_datastore_datastore_api',
'page arguments' => array(1),
'access callback' => 'dkan_datastore_datastore_api_access',
'access arguments' => array(1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '25',
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore'] = array(
'title' => 'Manage Datastore',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_import_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('import', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => '15',
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/import'] = array(
'title' => 'Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_import_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('import', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['node/%node/datastore/delete-items'] = array(
'title' => 'Delete items',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_delete_tab_form', NULL, 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('clear', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 11,
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/unlock'] = array(
'title' => 'Unlock',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_unlock_tab_form', NULL, 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('unlock', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 11,
'type' => MENU_LOCAL_TASK,
);
$items['node/%node/datastore/drop'] = array(
'title' => 'Drop Datastore',
'page callback' => 'drupal_get_form',
'page arguments' => array('dkan_datastore_drop_tab_form', 1),
'access callback' => 'dkan_datastore_feeds_access',
'access arguments' => array('drop', 1),
'file' => 'dkan_datastore.pages.inc',
'weight' => 12,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Retrieves the url to the physical resource and
* the type of it.
*/
function dkan_datastore_resource_link_type($node) {
$node_wrapper = entity_metadata_wrapper('node', $node);
$file_field = dkan_datastore_file_upload_field();
$remote_file_field = dkan_datastore_file_link_field();
$api_field = dkan_datastore_api_link_field();
$type = dkan_datastore_node_type();
if($node->type == $type && $file = $node_wrapper->{$file_field}->value()) {
$uri = isset($file->uri) ? $file->uri : $file['uri'];
$url = file_create_url($uri);
return array('url' => $url, 'type' => 'upload');
} else if($remote_file = $node_wrapper->{$remote_file_field}->value()) {
return array('url' => $remote_file['uri'], 'type' => 'remote');
} else if($link = $node_wrapper->{$api_field}->value()){
return array('url' => $link['url'], 'type' => 'link');
} else {
drupal_set_message(t('No download available for this resource'));
return array('type' => 'upload');
}
}
/**
* Title callback to change the download link text
* based on the type of the resource link.
*/
function dkan_datastore_download_title($node) {
$resource_link_type = dkan_datastore_resource_link_type($node);
return ($resource_link_type['type'] == 'link') ? t('Go to resource') : t('Download');
}
/**
* Implements hook_menu_alter().
*/
function dkan_datastore_menu_alter(&$items) {
// Removing these here, readding in hook_menu().
unset($items['node/%node/delete-items']);
unset($items['node/%node/import']);
unset($items['node/%node/unlock']);
}
/**
* Copy of feeds_access.
*/
function dkan_datastore_feeds_access($action, $node) {
if (!in_array($action, array('import', 'clear', 'unlock', 'drop'))) {
// If $action is not one of the supported actions, we return access denied.
return FALSE;
}
if ($action == 'drop' && user_access('manage datastore')) {
return TRUE;
}
if ($action != 'import') {
if ($importer_ids = feeds_get_importer_ids($node->type, $node->nid)) {
foreach ($importer_ids as $importer_id) {
$source_config = feeds_source($importer_id, $node->nid);
$fetcher = get_class($source_config->importer->fetcher);
$source = isset($source_config->config[$fetcher]['source']) ? $source_config->config[$fetcher]['source'] : '';
if ($source) {
return TRUE;
}
}
return FALSE;
}
else {
return FALSE;
}
}
if (is_string($node)) {
$importer_id = $node;
}
elseif ($node->type) {
$importer_id = feeds_get_importer_id($node->type);
}
// Check for permissions if feed id is present, otherwise return FALSE.
if ($importer_id) {
if (user_access('administer feeds') || user_access("{$action} {$importer_id} feeds")) {
return TRUE;
}
}
return FALSE;
}
/**
* Access callback for Download.
*/
function dkan_datastore_download_access($action, $resource) {
$type = dkan_datastore_node_type();
$node = is_object($resource) ? $resource : node_load($resource);
if ($node->type != $type) {
return FALSE;
}
return node_access('view', $node);
}
/**
* Access callback for Data API instructions.
*/
function dkan_datastore_datastore_api_access($resource) {
$status = dkan_datastore_status($resource);
if ($status == DKAN_DATASTORE_FILE_EXISTS && !user_access('manage datastore')) {
return FALSE;
}
if ($status != DKAN_DATASTORE_WRONG_TYPE) {
$file_field = dkan_datastore_file_upload_field();
$link_field = dkan_datastore_file_link_field();
$type = dkan_datastore_node_type();
$node = is_object($resource) ? $resource : node_load($resource);
$upload = '';
$link = '';
if (isset($node->$file_field) && $node->$file_field) {
$upload = isset($node->{$file_field}[$node->language]) ? $node->{$file_field}[$node->language] : $node->{$file_field}['und'];
}
if (isset($node->{$link_field}) && $node->{$link_field}) {
$link = isset($node->{$link_field}[$node->language]) ? $node->{$link_field}[$node->language] : $node->{$link_field}['und'];
}
if ($node->type == $type && ($upload || $link)) {
return node_access('view', $node);
}
else {
return FALSE;
}
}
return FALSE;
}
/**
* Load node by id or uuid.
*/
function dkan_datastore_resource_load($id) {
try {
// Id is a nid.
if (is_numeric($id)) {
return node_load($id);
}
// Id is a uuid and uuid is installed.
else if (function_exists('entity_uuid_load')) {
$node = entity_uuid_load('node', array($id));
return ($node) ? reset($node) : FALSE;
}
}
catch (Exception $e) {
return FALSE;
}
return FALSE;
}
/**
* Access callback for back link.
*/
function dkan_datastore_back_access($node) {
if ($node->type != 'resource') {
return FALSE;
}
else {
return node_access('view', $node);
}
}
/**
* Access callback for 'Add Resource' tab.
*/
function dkan_add_resource($node) {
if ($node->type != 'dataset') {
return FALSE;
}
else {
return _node_add_access();
}
}
/**
* Implements hook_node_update().
*/
function dkan_datastore_node_insert($node) {
$type = dkan_datastore_node_type();
if ($node->type == $type) {
if (!isset($node->feeds)) {
// Feeds only saves if there is a form present. We want this to work
// programmatically as well.
feeds_node_prepare($node);
feeds_node_update($node);
}
}
}
/**
* Implements hook_node_update().
*/
function dkan_datastore_node_update($node) {
$type = dkan_datastore_node_type();
if ($node->type == $type) {
$file_field = dkan_datastore_file_upload_field();
$link_field = dkan_datastore_file_link_field();
// See if we have a file link or upload.
$wrapper = entity_metadata_wrapper('node', $node);
$file = $wrapper->{$file_field}->value();
if (!$file) {
$link = $wrapper->{$link_field}->value();
if (isset($link) && isset($link['fid'])) {
$file = file_load($link['fid']);
}
elseif (isset($link) && isset($link->fid)) {
$file = $link;
}
else {
$file = '';
}
}
if ($file) {
if (!isset($node->feeds)) {
// Feeds only saves if there is a form present. We want this to work
// programmatically as well.
feeds_node_prepare($node);
}
// Remove node from feeds list if not csv.
elseif ($file->filemime != 'text/csv') {
feeds_node_delete($node);
}
}
}
}
/**
* Implements hook_permission().
*/
function dkan_datastore_permission() {
return array(
'manage datastore' => array(
'title' => t('Manage Datastore'),
),
'manage datastore settings' => array(
'title' => t('Manage Datastore Settings')
)
);
}
/**
* Implements hook_node_view().
*/
function dkan_datastore_node_view($node, $view_mode, $langcode) {
$type = dkan_datastore_node_type();
if ($node->type == $type) {
$status = dkan_datastore_status($node);
if (user_access('manage datastore') && $status && $view_mode == 'full') {
if ($status == DKAN_DATASTORE_FILE_EXISTS) {
drupal_set_message(t('Your file for this resource is not added to the datastore. Click "Manage Datastore" to import file into the datastore.'));
}
elseif ($status == DKAN_DATASTORE_EXISTS) {
drupal_set_message(t('Your file for this resource has been added to the datastore.'));
}
}
}
}
/**
* Determines status of datastore attached to resource node.
*/
function dkan_datastore_status($node) {
if($file = dkan_datastore_file_field($node)) {
if(dkan_datastore_is_importable($file)){
$node = entity_metadata_wrapper('node', $node);
if($status = $node->field_datastore_status->value()) {
return $status;
} else {
return DKAN_DATASTORE_FILE_EXISTS;
}
} else {
return DKAN_DATASTORE_WRONG_TYPE;
}
} else {
return DKAN_DATASTORE_EMPTY;
}
}
/**
* Determines if a resource can be imported
*/
function dkan_datastore_is_importable($file) {
return (is_object($file) && $file->filemime == 'text/csv') || (is_array($file) && array_key_exists('filemime', $file) && $file['filemime'] == 'text/csv');
}
/**
* Determines if records exist in a datastore.
*
* @param string $nid
* Node id for resource node.
*/
function dkan_datastore_records($nid) {
if (function_exists('dkan_datastore_api_get_feeds_source')) {
$source_id = dkan_datastore_api_get_feeds_source($nid);
if ($table = feeds_flatstore_processor_table_name($source_id, $nid)) {
if (db_table_exists($table)) {
$query = db_select($table, 't')
->fields('t', array('timestamp'))
->range(0, 1);
if ($result = $query->execute()) {
return TRUE;
}
}
}
}
return FALSE;
}
/**
* Retrieves loaded file from resource node.
*/
function dkan_datastore_file_field($node) {
$type = dkan_datastore_node_type();
if (isset($node->type) && $node->type == $type) {
$file_field = dkan_datastore_file_upload_field();
$link_field = dkan_datastore_file_link_field();
$wrapper = entity_metadata_wrapper('node', $node);
$file = isset($wrapper->{$file_field}) ? $wrapper->{$file_field}->value() : '';
$link = isset($wrapper->{$link_field}) ? $wrapper->{$link_field}->value() : '';
if (isset($file)) {
return $file;
}
elseif ($link) {
return $link;
}
}
return FALSE;
}
/**
* Implements hook_field_formatter_info().
*/
function dkan_datastore_field_formatter_info() {
$formatters = array(
'dkan_datastore_status_formatter' => array(
'label' => t('Datastore Status'),
'field types' => array('recline_field'),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_view().
*/
function dkan_datastore_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$item['entity'] = $entity;
$element[$delta] = array(
'#theme' => 'dkan_datastore_status_formatter',
'#item' => $item,
);
}
return $element;
}
/**
* Implements hook_theme().
*/
function dkan_datastore_theme() {
return array(
'dkan_datastore_status_formatter' => array(
'variables' => array('item' => NULL),
),
);
}
/**
* Returns HTML for an recline field formatter.
*
* @param array $variables
* An associative array containing:
* - item: Associative array of recline field.
*
* @ingroup themeable
*/
function theme_dkan_datastore_status_formatter(array $variables) {
$status = dkan_datastore_status($variables['item']['entity']);
if ($status === DKAN_DATASTORE_FILE_EXISTS) {
return '<span class="circle false" title="' . t('A file has been uploaded but not added to the datastore') . '">' . t('Data ready to be added') . '</span>';
}
elseif ($status === DKAN_DATASTORE_EXISTS) {
return '<span class="circle true">' . t('Datastore enabled') . '</span>';
}
return '<span class="circle na">' . t('Correct file type not attached to resource') . '</span>';
}
/**
* Gets a datastore instance.
*
* @param string $id
* The unique id of the importer object.
*
* @return Datastore
* A Datastore object or an object of a class defined by the Drupal
* variable 'dkan_datastore_class'. There is only one importer object
* per $id system-wide.
*/
function dkan_datastore_go($id = NULL, $class = NULL) {
if (!$class) {
$class = variable_get('dkan_datastore_class', 'DkanDatastore');
}
return Datastore::instance($class, $id);
}
/**
* Returns name of upload field.
*/
function dkan_datastore_file_upload_field() {
static $field;
if (!$field) {
$field = 'field_upload';
drupal_alter('dkan_datastore_file_upload_field', $field);
}
return $field;
}
/**
* Returns name of link api field.
*/
function dkan_datastore_api_link_field() {
static $field;
if (!$field) {
$field = 'field_link_api';
drupal_alter('dkan_datastore_field_link_api', $field);
}
return $field;
}
/**
* Returns name of remote file field.
*/
function dkan_datastore_file_link_field() {
static $field;
if (!$field) {
$field = 'field_link_remote_file';
drupal_alter('dkan_datastore_field_link_remote_file', $field);
}
return $field;
}
/**
* Returns name of node type that the Datastore is attached to.
*/
function dkan_datastore_node_type() {
static $node_type;
if (!$node_type) {
$node_type = 'resource';
drupal_alter('dkan_datastore_node_type', $node_type);
}
return $node_type;
}
/**
* Implements hook_feeds_after_import().
*/
function dkan_datastore_feeds_after_import(FeedsSource $source) {
$importer_id = $source->importer->id;
$importers = array('dkan_file', 'dkan_link');
if(empty($source->exception) && in_array($importer_id, $importers)) {
$node = node_load($source->feed_nid);
$node = entity_metadata_wrapper('node', $node);
$node->field_datastore_status->set(DKAN_DATASTORE_EXISTS);
$node->save();
}
}
/**
* Implements hook_node_presave().
*/
function dkan_datastore_node_presave($node) {
if(is_object($node) && $node->type == 'resource') {
$wrap = entity_metadata_wrapper('node', $node);
$wrap->field_datastore_status->set(dkan_datastore_status($node));
}
}
/**
* Implements hook_cron_queue_info().
*/
function dkan_datastore_cron_queue_info() {
$queues['dkan_datastore_queue'] = array(
'worker callback' => 'dkan_datastore_queue_import_worker',
'time' => 120,
'skip on cron' => FALSE,
);
return $queues;
}
/**
* Utility function for add a new resource to datastore queue.
*/
function dkan_datastore_queue_import($uuid, $fid) {
$item = array(
'uuid' => $uuid,
'fid' => $fid
);
DrupalQueue::get('dkan_datastore_queue')->createItem($item);
watchdog('dkan_datastore', t('Added %file to queue for import into datastore', array('%file' => $fid)));
}
/**
* Callback used with queue for index content into datastore.
*/
function dkan_datastore_queue_import_worker($item) {
$file = file_load($item['fid']);
$uri = $file->uri;
$file_path = file_create_url($uri);
$datastore = dkan_datastore_go($item['uuid']);
$datastore->updateByFile($file_path);
}