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.module
236 lines (221 loc) · 6.19 KB
/
dkan_harvest.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
<?php
/**
* @file
* Harvesting module for dkan.
*/
define(
'DKAN_HARVEST_CACHE_DIR',
'public://dkan-harvest-cache'
);
/**
* Returns configuration for harvest sources.
*/
function dkan_harvest_sources_definition() {
$modules = module_implements('harvest_sources');
$subscribed_values = array();
// Get a list of licenses to display.
foreach ($modules as $module) {
$function = $module . '_harvest_sources';
$values = $function();
foreach ($values as $key => $value) {
// Make sure other module didn't provide the same source.
if (!isset($subscribed_values[$key])) {
$subscribed_values[$key] = $value;
}
}
}
return $subscribed_values;
}
/**
* Callback for dkan-harvest-run.
*/
function dkan_harvest_run() {
dkan_harvest_cache_data();
dkan_harvest_migrate_data();
}
/**
* Callback for dkan-migrate-cached-data.
*/
function dkan_harvest_migrate_data() {
migrate_static_registration();
$migration = Migration::getInstance('dkan_harvest_data_json');
$migration->processImport();
}
/**
* Callback for dkan-cache-harvested-data.
*/
function dkan_harvest_cache_data($options = array()) {
$harvest_last_updated = microtime();
$sources = dkan_harvest_sources_definition();
if (!empty($options['sources'])) {
$sources = array_intersect_key($sources, array_flip($options['sources']));
}
return dkan_harvest_cache_data_process($sources, $harvest_last_updated);
}
/**
* Callback for dkan-cache-harvested-data.
*/
function dkan_harvest_cache_data_process($sources, $harvest_last_updated) {
$context = stream_context_create(
array(
'http' => array(
'timeout' => 36000,
),
'https' => array(
'timeout' => 36000,
),
)
);
$counters = array();
foreach ($sources as $key => $source) {
$remote = file_get_contents($source['remote'], 0, $context);
$counters[$key] = 0;
if ($remote) {
switch ($source['type']) {
case 'data.xml':
$xml = simplexml_load_string($remote);
$json = json_encode($xml);
$array = json_decode($json, TRUE);
if ($array) {
dkan_harvest_cache_pod_1_1_json($array, $key, $source, $counters);
}
break;
case 'data.json':
$remote = utf8_encode($remote);
$json = drupal_json_decode($remote);
if (!$json) {
# Anticipate encoding errors.
$remote = utf8_encode($remote);
$json = drupal_json_decode($remote);
}
if ($json) {
dkan_harvest_cache_pod_1_1_json($json, $key, $source, $counters);
}
break;
}
}
}
$n = 0;
foreach ($counters as $key => $counter) {
$message = $counter . ' datasets harvested for ' . $key;
dkan_harvest_log($message, 'success');
$n += $counter;
}
$message = $n . ' datasets harvested in total';
return dkan_harvest_log($message, 'success');
}
/**
* Cache the pod_1_1_json datasets.
*/
function dkan_harvest_cache_pod_1_1_json($json, $key, $source, &$counters) {
$dir = dkan_harvest_sources_prepare_cache_dir($key);
$datasets = $json['dataset'];
$datasets = dkan_harvest_filter_datasets($datasets, $source['filters'], $source['excludes']);
foreach ($datasets as $dataset) {
$identifier = dkan_harvest_prepare_item_id($dataset['identifier']);
$dataset['harvest_object_id'] = $identifier;
$dataset['harvest_source_id'] = $key;
$dataset['harvest_last_updated'] = $harvest_last_updated;
$dataset_file = implode('/', array($dir, $identifier));
$data = drupal_json_encode($dataset);
$test = file_put_contents($dataset_file, $data);
if ($test) {
dkan_harvest_log($dataset_file . ' created for dataset with id: ' . $identifier, 'success');
}
else {
dkan_harvest_log('failed to create ' . $dataset_file . 'for dataset with id: ' . $identifier, 'failed');
}
$counters[$key] = $counters[$key] + 1;
}
}
/**
* Messages to drush_log if available.
*/
function dkan_harvest_log($message, $type) {
if (function_exists('drush_log')) {
drush_log($message, $type);
}
else {
watchdog('dkan_harvest', $message);
}
return $message;
}
/**
* Filters an array of data.json datasets based on a set of filters.
*/
function dkan_harvest_filter_datasets($datasets, $filters, $excludes = array()) {
$r = array();
foreach ($datasets as $dataset) {
$passes = TRUE;
foreach ($filters as $filter => $value) {
if (!isset($dataset[$filter])) {
$dataset[$filter] = array();
}
if (count(array_intersect($value, $dataset[$filter])) < 1) {
$passes = FALSE;
break;
}
}
foreach ($excludes as $exclude => $value) {
if (!isset($dataset[$exclude])) {
$dataset[$exclude] = array();
}
if (is_array($dataset[$exclude])) {
if (count(array_intersect($value, $dataset[$exclude])) > 0) {
$passes = FALSE;
break;
}
}
else if ($value == $dataset[$exclude]) {
$passes = FALSE;
break;
};
}
if ($passes) {
$r[] = $dataset;
}
}
return $r;
}
/**
* Creates a directory prepended with DKAN_HARVEST_CACHE_DIR.
*
* @param string $dir
*
* @return string
* PHP filesteream location.
*/
function dkan_harvest_sources_prepare_cache_dir($dir) {
// Create base dir.
if (!is_dir(DKAN_HARVEST_CACHE_DIR)) {
drupal_mkdir(DKAN_HARVEST_CACHE_DIR);
}
// Create dir for source.
$dir = implode('/', array(DKAN_HARVEST_CACHE_DIR, $dir));
if (!is_dir($dir)) {
drupal_mkdir($dir);
}
// Delete all files in dir.
$files = glob($dir . '/*');
foreach($files as $file){
if (is_file($file)) {
unlink($file);
}
}
return $dir;
}
/**
* Removes URL and returns directory from URL string.
*
* Example: "http://example.com/whathat" returns "whatwhat"
*/
function dkan_harvest_prepare_item_id($identifier) {
if (filter_var($identifier, FILTER_VALIDATE_URL)) {
$identifier = parse_url($identifier, PHP_URL_PATH);
$frag = explode('/', $identifier);
// Does not produce "Strict warning: Only variables should be passed by
// reference" like end(explode('/', $identifier));
$identifier = $frag[count($frag) - 1];
}
return $identifier;
}