This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkan_harvest.drush.inc
83 lines (78 loc) · 3.06 KB
/
dkan_harvest.drush.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
<?php
/**
* @file
* dkan_harvest.drush.inc.
*/
/**
* Implements hook_drush_command().
*/
function dkan_harvest_drush_command() {
// Cache the remote ckan endpoint.
$items['dkan-cache-harvested-data'] = array(
'aliases' => array('dkan-chd'),
'description' => 'Caches remote data.json endpoints locally',
'callback' => 'dkan_harvest_drush_chd',
'options' => array(
'sources' => 'A list of comma delimited source keys as provided in hook_harvest_sources.',
),
);
// Runs the migration.
$items['dkan-migrate-cached-data'] = array(
'aliases' => array('dkan-mcd'),
'description' => 'Migrates cached data.json endpoints',
'callback' => 'dkan_harvest_migrate_data',
);
// Cache + Migration run.
$items['dkan-harvest-run'] = array(
'aliases' => array('dkan-hr'),
'description' => 'Caches data and runs the migration for data.json endpoints',
'callback' => 'dkan_harvest_run',
);
$items['orphaned-resources-count'] = array(
'description' => "Count resources that are not linked to datasets.",
'drupal dependencies' => array('dkan_dataset'),
'aliases' => array('dkan-or-count', 'orphaned-resources'),
);
$items['orphaned-resources-delete'] = array(
'description' => "Count resources that are not linked to datasets.",
'drupal dependencies' => array('dkan_dataset'),
'aliases' => array('dkan-or-delete', 'orphaned-resources-delete'),
);
return $items;
}
/**
* Get a count of orphaned resources.
*/
function drush_dkan_harvest_orphaned_resources_count() {
$result = db_query("SELECT count(node.nid) AS nid FROM
{node} node
LEFT JOIN {field_data_field_dataset_ref} field_data_field_dataset_ref ON node.nid = field_data_field_dataset_ref.entity_id AND (field_data_field_dataset_ref.entity_type = 'node' AND field_data_field_dataset_ref.deleted = '0')
WHERE (( (node.status = '1') AND (node.type IN ('resource')) AND (field_data_field_dataset_ref.field_dataset_ref_target_id IS NULL ) ))
")->fetchAll();
drush_print(dt('There are !count orphaned resources', array('!count' => $result[0]->nid)));
return TRUE;
}
/**
* Delete orphaned resources.
*/
function drush_dkan_harvest_orphaned_resources_delete() {
$results = db_query("SELECT node.nid AS nid FROM
{node} node
LEFT JOIN {field_data_field_dataset_ref} field_data_field_dataset_ref ON node.nid = field_data_field_dataset_ref.entity_id AND (field_data_field_dataset_ref.entity_type = 'node' AND field_data_field_dataset_ref.deleted = '0')
WHERE (( (node.status = '1') AND (node.type IN ('resource')) AND (field_data_field_dataset_ref.field_dataset_ref_target_id IS NULL ) ))
")->fetchAll();
foreach ($results as $result) {
drush_print(dt('Deleting nid !nid', array('!nid' => $result->nid)));
node_delete($result->nid);
}
return TRUE;
}
/**
* Callback function for dkan-chd command.
*/
function dkan_harvest_drush_chd() {
$options = array();
$options['sources'] = drush_get_option('sources', FALSE);
$options['sources'] = $options['sources'] ? explode(',', $options['sources']) : array();
return dkan_harvest_cache_data($options);
}