-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdcate.inc
executable file
·249 lines (230 loc) · 6.83 KB
/
sdcate.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
<?php
/**
* @file
* Helper functions for writing DCAT.
*/
module_load_include('inc', 'sdcate', 'sdcate.rdf');
module_load_include('inc', 'sdcate', 'sdcate.scrape');
/**
* Get the URI of the DCAT Catalog.
*
* @return string
* String respresentation of DCAT Catalog URI.
*/
function sdcate_uri_catalog() {
$cat = &drupal_static(__FUNCTION__);
if (!isset($cat)) {
$cat = variable_get_value('sdcate_uri') . '/catalog';
}
return $cat;
}
/**
* Get the URI of a DCAT Dataset.
*
* @param int $did
* Dataset ID, typically the node id.
*
* @return string
* String respresentation of DCAT dataset URI.
*/
function sdcate_uri_dataset($did) {
return sdcate_uri_catalog() . '/dataset/' . $did;
}
/**
* Get the URI of a DCAT Distribution.
*
* @param int $fid
* Distribution ID, typically the file id.
*
* @return string
* String respresentation of DCAT distribution URI.
*/
function sdcate_uri_distribution($fid) {
return sdcate_uri_catalog() . '/distribution/' . $fid;
}
/**
* Generate DCAT Catalog.
*
* @return string
* DCAT Catalog as RDF triples.
*/
function sdcate_catalog() {
global $base_url;
global $language;
$cat = sdcate_uri_catalog();
$triples = sdcate_triple($cat, SDCATE_RDF_TYPE, SDCATE_DCAT_ACAT);
$triples .= sdcate_triple($cat, SDCATE_FOAF_HOME, $base_url);
$triples .= sdcate_triple($cat, SDCATE_DT_PUBLISHER, $base_url );
/**
* Here, you need the fetch the publisher name from the Drupal website
* I didn't find a solution yet for this one.
**/
$triples .= sdcate_literal($base_url,SDCATE_FOAF_NAME, "Publisher" );
$triples .= sdcate_literal($base_url,SDCATE_DT_LANG, $language->language);
if ($geo = variable_get_value('sdcate_geo')) {
$triples .= sdcate_triple($cat, SDCATE_DT_GEO, $geo);
}
if ($theme = variable_get_value('sdcate_theme')) {
$triples .= sdcate_triple($cat, SDCATE_DCAT_THEME, $theme);
}
$title = variable_get('site_name');
$triples .= sdcate_literal($cat, SDCATE_DT_TITLE, $title);
$descr = variable_get('site_slogan');
if(isset($descr)) {
$triples .= sdcate_literal($cat, SDCATE_DT_DESC, $descr);
}
return $triples;
}
/**
* Generate a DCAT Distribution.
*
* @param string $dset
* URI of the DCAT Dataset.
* @param array $file
* File to be exported.
*
* @return string
* DCAT Distribution as RDF triples.
*/
function sdcate_distribution($dset, $file) {
$dist = sdcate_uri_distribution($file['fid']);
$url = file_create_url($file['uri']);
$data = sdcate_triple($dset, SDCATE_DCAT_DIST, $dist);
$data .= sdcate_triple($dist, SDCATE_RDF_TYPE, SDCATE_DCAT_ADIST);
$data .= sdcate_literal($dist, SDCATE_DT_TITLE, $file['filename']);
if (isset($file['description']) && !empty($file['description'])) {
$data .= sdcate_literal($dist, SDCATE_DT_DESC, $file['description']);
}
$data .= sdcate_date($dist, SDCATE_DT_CREAT, $file['timestamp']);
$data .= sdcate_decimal($dist, SDCATE_DCAT_SIZE, $file['filesize']);
$data .= sdcate_literal($dist, SDCATE_DCAT_TYPE, $file['filemime']);
$data .= sdcate_triple($dist, SDCATE_DCAT_URL, $url);
$data .= sdcate_literal($url, SDCATE_DCAT_URL, $url);
return $data;
}
/**
* Check if file is public and is a machine-friendly file format.
*
* @param array $file
* File to be checked.
*
* @return bool
* TRUE if a file is to public and n a open format.
*/
function sdcate_file_check($file) {
$check = FALSE;
// Check if file is public
if ((substr($file['uri'], 0, 9) == 'public://') && ($file['display'] == 1)) {
// Check file extension
if ($pos = strrpos($file['uri'], '.')) {
$type = substr($file['uri'], $pos + 1);
if ($type) {
$types = explode(' ', variable_get_value('sdcate_files'));
$check = in_array(strtolower($type), $types);
}
}
}
return $check;
}
/**
* Generate DCAT Distributions.
*
* @param string $dset
* URI of the DCAT Dataset.
* @param object $node
* Node to be exported.
*
* @return string
* DCAT Distributions as RDF triples.
*/
function sdcate_distributions($dset, $node) {
$data = '';
// Get the list of file fields.
$fields = sdcate_filefields_list();
foreach ($fields[$node->type] as $field) {
if (isset($node->{$field}) && !empty($node->{$field})) {
foreach (array_keys($node->{$field}) as $lang) {
foreach ($node->{$field}[$lang] as $file) {
if (sdcate_file_check($file)) {
$data .= sdcate_distribution($dset, $file);
}
}
}
}
}
return $data;
}
/**
* Generate a DCAT Dataset.
*
* @param string $cat
* URI of the DCAT Catalog.
* @param object $node
* Node to be exported.
*
* @return as string
* DCAT Dataset as RDF triples.
*/
function sdcate_dataset($cat, $node) {
global $language;
$dset = sdcate_uri_dataset($node->nid);
$keywords = '';
$data = sdcate_triple($cat, SDCATE_DCAT_DSET, $dset);
$data .= sdcate_triple($dset, SDCATE_RDF_TYPE, SDCATE_DCAT_ADSET);
if($node->language == "und") {
$node->language = $language->language;
}
$data .= sdcate_literal($dset, SDCATE_DT_TITLE, $node->title, $node->language);
/**
* This following code is the keywords integration
* 'field_file_tags' is the name of the field for the tags, he has to be change
* if the name change in the website.
*/
$tags = field_get_items('node',$node,'field_file_tags');
if(!empty($tags)) {
for( $i = 0; $i < sizeof( $tags ); $i++ ) {
$tag_name = field_view_value('node', $node, 'field_file_tags', $tags[$i]);
$keywords .= $tag_name['#title'];
$data .= sdcate_literal($dset, SDCATE_DCAT_TAG, $tag_name['#title'], $node->language);
}
}
/**
* This following code is the description integration
* 'field_file_attachments' is the name of the field for the attachments, he has to be change
* if the name change in the website.
*/
$attachments = field_get_items('node',$node,'field_file_attachments');
if(!empty($attachments)) {
for( $i = 0; $i < sizeof( $attachments ); $i++ ) {
if (isset($attachments[$i]['description']) && !empty($attachments[$i]['description'])) {
$data .= sdcate_literal($dset, SDCATE_DT_DESC, $attachments[$i]['description']);
}
}
}
$data .= sdcate_date($dset, SDCATE_DT_CREAT, $node->created);
$data .= sdcate_date($dset, SDCATE_DT_MODIF, $node->changed);
$data .= sdcate_distributions($dset, $node);
return $data;
}
/**
* Generate DCAT Datasets.
*
* @return string
* DCAT Datasets as RDF triples.
*/
function sdcate_datasets() {
$data = '';
$cat = sdcate_uri_catalog();
$nids = sdcate_nodes_list();
foreach ($nids as $id) {
$nodes = entity_load('node', array($id));
if (isset($nodes) && !empty($nodes)) {
$node = array_pop($nodes);
// Check if anonymous user has access to this node.
if (node_access('view', $node, 0)) {
$data .= sdcate_dataset($cat, $node);
}
}
}
return $data;
}