1 |
6657531f
|
Andreas Kohlbecker
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* Required or useful functions for using CDM Data Store Webservices.
|
5 |
|
|
*
|
6 |
|
|
* Naming conventions:
|
7 |
|
|
* ----------------------
|
8 |
|
|
* - All webservice access methods are prefixed with cdm_ws.
|
9 |
|
|
*
|
10 |
|
|
* @copyright
|
11 |
|
|
* (C) 2007-2012 EDIT
|
12 |
|
|
* European Distributed Institute of Taxonomy
|
13 |
|
|
* http://www.e-taxonomy.eu
|
14 |
|
|
*
|
15 |
|
|
* The contents of this module are subject to the Mozilla
|
16 |
|
|
* Public License Version 1.1.
|
17 |
|
|
* @see http://www.mozilla.org/MPL/MPL-1.1.html
|
18 |
|
|
*
|
19 |
|
|
* @author
|
20 |
|
|
* - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
|
21 |
|
|
* - Wouter Addink <w.addink@eti.uva.nl> (migration from Drupal 5 to Drupal7)
|
22 |
|
|
*/
|
23 |
|
|
|
24 |
|
|
module_load_include('php', 'cdm_api', 'xml2json');
|
25 |
|
|
module_load_include('php', 'cdm_api', 'commons');
|
26 |
|
|
module_load_include('php', 'cdm_api', 'uuids');
|
27 |
|
|
module_load_include('php', 'cdm_api', 'webservice_uris');
|
28 |
|
|
module_load_include('php', 'cdm_api', 'cdm_node');
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
/**
|
32 |
|
|
* Implements hook_menu().
|
33 |
|
|
*/
|
34 |
|
|
function cdm_api_menu() {
|
35 |
|
|
$items = array();
|
36 |
|
|
|
37 |
|
|
// usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");
|
38 |
|
|
$items['cdm_api/proxy'] = array(
|
39 |
|
|
'page callback' => 'proxy_content',
|
40 |
|
|
'access arguments' => array(
|
41 |
|
|
'access content',
|
42 |
|
|
),
|
43 |
|
|
'type' => MENU_CALLBACK,
|
44 |
|
|
);
|
45 |
|
|
|
46 |
|
|
$items['cdm_api/setvalue/session'] = array(
|
47 |
|
|
'page callback' => 'setvalue_session',
|
48 |
|
|
'access arguments' => array(
|
49 |
|
|
'access content',
|
50 |
|
|
),
|
51 |
|
|
'type' => MENU_CALLBACK,
|
52 |
|
|
);
|
53 |
|
|
|
54 |
|
|
return $items;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Implements hook_cron().
|
59 |
|
|
*
|
60 |
|
|
* Expire outdated cache entries.
|
61 |
|
|
*/
|
62 |
|
|
function cdm_api_cron() {
|
63 |
|
|
cache_clear_all(NULL, 'cache_cdm_ws');
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* @todo Please document this function.
|
68 |
|
|
* @see http://drupal.org/node/1354
|
69 |
|
|
*/
|
70 |
|
|
function cdm_api_permission() {
|
71 |
|
|
return array(
|
72 |
|
|
'administer cdm_api' => array(
|
73 |
|
|
'title' => t('administer cdm_api'),
|
74 |
|
|
'description' => t("TODO Add a description for 'administer cdm_api'"),
|
75 |
|
|
),
|
76 |
|
|
);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Converts an array of TaggedText items into corresponding html tags.
|
81 |
|
|
*
|
82 |
|
|
* Each item is provided with a class attribute which is set to the key of the
|
83 |
|
|
* TaggedText item.
|
84 |
|
|
*
|
85 |
|
|
* @param array $taggedtxt
|
86 |
|
|
* Array with text items to convert.
|
87 |
|
|
* @param string $tag
|
88 |
|
|
* Html tag name to convert the items into, default is 'span'.
|
89 |
|
|
* @param string $glue
|
90 |
|
|
* The string by which the chained text tokens are concatenated together.
|
91 |
|
|
* Default is a blank character.
|
92 |
|
|
*
|
93 |
|
|
* @return string
|
94 |
|
|
* A string with HTML.
|
95 |
|
|
*/
|
96 |
|
|
function cdm_taggedtext2html(array $taggedtxt, $tag = 'span', $glue = ' ', $skiptags = array()) {
|
97 |
|
|
$out = '';
|
98 |
|
|
$i = 0;
|
99 |
|
|
foreach ($taggedtxt as $tt) {
|
100 |
|
|
if (!in_array($tt->type, $skiptags) && strlen($tt->text) > 0) {
|
101 |
|
|
$out .= (strlen($out) > 0 && ++$i < count($taggedtxt) ? $glue : '') . '<' . $tag . ' class="' . $tt->type . '">' . t($tt->text) . '</' . $tag . '>';
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
return $out;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Finds the text tagged with $tag_type in an array of taggedText instances.
|
109 |
|
|
*
|
110 |
|
|
* Comment @WA: this function seems unused.
|
111 |
|
|
*
|
112 |
|
|
* @param array $taggedtxt
|
113 |
|
|
* Array with text items.
|
114 |
|
|
* @param string $tag_type
|
115 |
|
|
* The type of tag for which to find text items in the $taggedtxt array.
|
116 |
|
|
*
|
117 |
|
|
* @return array
|
118 |
|
|
* An array with the texts mapped by $tag_type.
|
119 |
|
|
*/
|
120 |
|
|
function cdm_taggedtext_values(array $taggedtxt, $tag_type) {
|
121 |
|
|
$tokens = array();
|
122 |
|
|
if (!empty($taggedtxt)) {
|
123 |
|
|
foreach ($taggedtxt as $tagtxt) {
|
124 |
|
|
if ($tagtxt->type == $tag_type) {
|
125 |
|
|
$tokens[] = $tagtxt->text;
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
return $tokens;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/**
|
133 |
|
|
* Returns the currently classification tree in use.
|
134 |
|
|
*/
|
135 |
|
|
function get_taxonomictree_uuid_selected() {
|
136 |
|
|
if (isset($_SESSION['cdm']['taxonomictree_uuid']) && is_uuid($_SESSION['cdm']['taxonomictree_uuid'])) {
|
137 |
|
|
return $_SESSION['cdm']['taxonomictree_uuid'];
|
138 |
|
|
}
|
139 |
|
|
else {
|
140 |
|
|
return variable_get(CDM_TAXONOMICTREE_UUID, FALSE);
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/**
|
145 |
|
|
* Returns the FeatureTree profile as selected.
|
146 |
|
|
*
|
147 |
|
|
* The FeatureTree profile returned is the one that has been set in the
|
148 |
|
|
* dataportal settings (layout->taxon:profile).
|
149 |
|
|
* When the chosen FeatureTree is not found in the database,
|
150 |
|
|
* the standard feature tree (UUID_DEFAULT_FEATURETREE) will be returned.
|
151 |
|
|
*
|
152 |
|
|
* @return mixed
|
153 |
|
|
* A feature profile tree object.
|
154 |
|
|
*/
|
155 |
|
|
function get_profile_featureTree() {
|
156 |
|
|
$profile_featureTree = cdm_ws_get(
|
157 |
|
|
CDM_WS_FEATURETREE,
|
158 |
|
|
variable_get(CDM_PROFILE_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE)
|
159 |
|
|
);
|
160 |
|
|
if (!$profile_featureTree) {
|
161 |
|
|
$profile_featureTree = cdm_ws_get(
|
162 |
|
|
CDM_WS_FEATURETREE,
|
163 |
|
|
UUID_DEFAULT_FEATURETREE
|
164 |
|
|
);
|
165 |
|
|
}
|
166 |
|
|
return $profile_featureTree;
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/**
|
170 |
|
|
* @todo Please document this function.
|
171 |
|
|
* @see http://drupal.org/node/1354
|
172 |
|
|
*/
|
173 |
|
|
function switch_to_taxonomictree_uuid($taxonomictree_uuid) {
|
174 |
|
|
$_SESSION['cdm']['taxonomictree_uuid'] = $taxonomictree_uuid;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/**
|
178 |
|
|
* @todo Please document this function.
|
179 |
|
|
* @see http://drupal.org/node/1354
|
180 |
|
|
*/
|
181 |
|
|
function reset_taxonomictree_uuid($taxonomictree_uuid) {
|
182 |
|
|
unset($_SESSION['cdm']['taxonomictree_uuid']);
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* @todo Please document this function.
|
187 |
|
|
* @see http://drupal.org/node/1354
|
188 |
|
|
*/
|
189 |
|
|
function set_last_taxon_page_tab($taxonPageTab) {
|
190 |
|
|
$_SESSION['cdm']['taxon_page_tab'] = $taxonPageTab;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
/**
|
194 |
|
|
* @todo Please document this function.
|
195 |
|
|
* @see http://drupal.org/node/1354
|
196 |
|
|
*/
|
197 |
|
|
function get_last_taxon_page_tab() {
|
198 |
|
|
if (isset($_SESSION['cdm']['taxon_page_tab'])) {
|
199 |
|
|
return $_SESSION['cdm']['taxon_page_tab'];
|
200 |
|
|
}
|
201 |
|
|
else {
|
202 |
|
|
return FALSE;
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
/**
|
207 |
|
|
* @todo Improve the documentation of this function.
|
208 |
|
|
*
|
209 |
|
|
* media Array [4]
|
210 |
|
|
* representations Array [3]
|
211 |
|
|
* mimeType image/jpeg
|
212 |
|
|
* representationParts Array [1]
|
213 |
|
|
* duration 0
|
214 |
|
|
* heigth 0
|
215 |
|
|
* size 0
|
216 |
|
|
* uri
|
217 |
|
|
* http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/jpeg/Acanthocephalus_p1.jpg
|
218 |
|
|
* uuid 15c687f1-f79d-4b79-992f-7ba0f55e610b
|
219 |
|
|
* width 0
|
220 |
|
|
* suffix jpg
|
221 |
|
|
* uuid 930b7d51-e7b6-4350-b21e-8124b14fe29b
|
222 |
|
|
* title
|
223 |
|
|
* uuid 17e514f1-7a8e-4daa-87ea-8f13f8742cf9
|
224 |
|
|
*
|
225 |
|
|
* @param object $media
|
226 |
|
|
* @param array $mimeTypes
|
227 |
|
|
* @param int $width
|
228 |
|
|
* @param int $height
|
229 |
|
|
*
|
230 |
|
|
* @return array
|
231 |
|
|
* An array with preferred media representations or else an empty array.
|
232 |
|
|
*/
|
233 |
|
|
function cdm_preferred_media_representations($media, array $mimeTypes, $width = 400, $height = 300) {
|
234 |
|
|
$prefRepr = array();
|
235 |
|
|
if (!isset($media->representations[0])) {
|
236 |
|
|
return $prefRepr;
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
while (count($mimeTypes) > 0) {
|
240 |
|
|
// getRepresentationByMimeType
|
241 |
|
|
$mimeType = array_shift($mimeTypes);
|
242 |
|
|
|
243 |
|
|
foreach ($media->representations as &$representation) {
|
244 |
|
|
// If the mimetype is not known, try inferring it.
|
245 |
|
|
if (!$representation->mimeType) {
|
246 |
|
|
if (isset($representation->parts[0])) {
|
247 |
|
|
$representation->mimeType = infer_mime_type($representation->parts[0]->uri);
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
if ($representation->mimeType == $mimeType) {
|
252 |
|
|
// Preferred mimetype found -> erase all remaining mimetypes
|
253 |
|
|
// to end loop.
|
254 |
|
|
$mimeTypes = array();
|
255 |
|
|
$dwa = 0;
|
256 |
|
|
$dw = 0;
|
257 |
|
|
// Look for part with the best matching size.
|
258 |
|
|
foreach ($representation->parts as $part) {
|
259 |
|
|
if (isset($part->width) && isset($part->height)) {
|
260 |
|
|
$dw = $part->width * $part->height - $height * $width;
|
261 |
|
|
}
|
262 |
|
|
if ($dw < 0) {
|
263 |
|
|
$dw *= -1;
|
264 |
|
|
}
|
265 |
|
|
$dwa += $dw;
|
266 |
|
|
}
|
267 |
|
|
$dwa = (count($representation->parts) > 0) ? $dwa / count($representation->parts) : 0;
|
268 |
|
|
// @WA: $mimeTypeKey is not defined.
|
269 |
|
|
// $prefRepr[$dwa.'_'.$mimeTypeKey] = $representation;
|
270 |
|
|
$prefRepr[$dwa . '_'] = $representation;
|
271 |
|
|
}
|
272 |
|
|
}
|
273 |
|
|
}
|
274 |
|
|
// Sort the array.
|
275 |
|
|
krsort($prefRepr);
|
276 |
|
|
return $prefRepr;
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/**
|
280 |
|
|
* Infers the mime type of a file using the filename extension.
|
281 |
|
|
*
|
282 |
|
|
* The filename extension is used to infer the mime type.
|
283 |
|
|
*
|
284 |
|
|
* @param string $filepath
|
285 |
|
|
* The path to the respective file.
|
286 |
|
|
*
|
287 |
|
|
* @return string
|
288 |
|
|
* The mimetype for the file or FALSE if the according mime type could
|
289 |
|
|
* not be found.
|
290 |
|
|
*/
|
291 |
|
|
function infer_mime_type($filepath) {
|
292 |
|
|
static $mimemap = NULL;
|
293 |
|
|
if (!$mimemap) {
|
294 |
|
|
$mimemap = array(
|
295 |
|
|
'jpg' => 'image/jpeg',
|
296 |
|
|
'jpeg' => 'image/jpeg',
|
297 |
|
|
'png' => 'image/png',
|
298 |
|
|
'gif' => 'image/gif',
|
299 |
|
|
'giff' => 'image/gif',
|
300 |
|
|
'tif' => 'image/tif',
|
301 |
|
|
'tiff' => 'image/tif',
|
302 |
|
|
'pdf' => 'application/pdf',
|
303 |
|
|
'html' => 'text/html',
|
304 |
|
|
'htm' => 'text/html',
|
305 |
|
|
);
|
306 |
|
|
}
|
307 |
|
|
$extension = substr($filepath, strrpos($filepath, '.') + 1);
|
308 |
|
|
if (isset($mimemap[$extension])) {
|
309 |
|
|
return $mimemap[$extension];
|
310 |
|
|
}
|
311 |
|
|
else {
|
312 |
|
|
// FIXME remove this hack just return FALSE;
|
313 |
|
|
return 'text/html';
|
314 |
|
|
}
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
/**
|
318 |
|
|
* Converts an ISO 8601 org.joda.time.Partial to a year.
|
319 |
|
|
*
|
320 |
|
|
* The function expects an ISO 8601 time representation of a
|
321 |
|
|
* org.joda.time.Partial of the form yyyy-MM-dd.
|
322 |
|
|
*
|
323 |
|
|
* @param string $partial
|
324 |
|
|
* ISO 8601 time representation of a org.joda.time.Partial.
|
325 |
|
|
*
|
326 |
|
|
* @return string
|
327 |
|
|
* Returns the year. In case the year is unknown (= ????), it returns NULL.
|
328 |
|
|
*/
|
329 |
|
|
function partialToYear($partial) {
|
330 |
|
|
if (is_string($partial)) {
|
331 |
|
|
$year = substr($partial, 0, 4);
|
332 |
|
|
if (preg_match("/[0-9][0-9][0-9][0-9]/", $year)) {
|
333 |
|
|
return $year;
|
334 |
|
|
}
|
335 |
|
|
}
|
336 |
|
|
return;
|
337 |
|
|
}
|
338 |
|
|
|
339 |
|
|
/**
|
340 |
|
|
* Converts an ISO 8601 org.joda.time.Partial to a month.
|
341 |
|
|
*
|
342 |
|
|
* This function expects an ISO 8601 time representation of a
|
343 |
|
|
* org.joda.time.Partial of the form yyyy-MM-dd.
|
344 |
|
|
* In case the month is unknown (= ???) NULL is returned.
|
345 |
|
|
*
|
346 |
|
|
* @param string $partial
|
347 |
|
|
* ISO 8601 time representation of a org.joda.time.Partial.
|
348 |
|
|
*
|
349 |
|
|
* @return string
|
350 |
|
|
* A month.
|
351 |
|
|
*/
|
352 |
|
|
function partialToMonth($partial) {
|
353 |
|
|
if (is_string($partial)) {
|
354 |
|
|
$month = substr($partial, 5, 2);
|
355 |
|
|
if (preg_match("/[0-9][0-9]/", $month)) {
|
356 |
|
|
return $month;
|
357 |
|
|
}
|
358 |
|
|
}
|
359 |
|
|
return;
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
/**
|
363 |
|
|
* Converts an ISO 8601 org.joda.time.Partial to a day.
|
364 |
|
|
*
|
365 |
|
|
* This function expects an ISO 8601 time representation of a
|
366 |
|
|
* org.joda.time.Partial of the form yyyy-MM-dd and returns the day as string.
|
367 |
|
|
* In case the day is unknown (= ???) NULL is returned.
|
368 |
|
|
*
|
369 |
|
|
* @param string $partial
|
370 |
|
|
* ISO 8601 time representation of a org.joda.time.Partial.
|
371 |
|
|
*
|
372 |
|
|
* @return string
|
373 |
|
|
* A day.
|
374 |
|
|
*/
|
375 |
|
|
function partialToDay($partial) {
|
376 |
|
|
if (is_string($partial)) {
|
377 |
|
|
$day = substr($partial, 7, 2);
|
378 |
|
|
if (preg_match("/[0-9][0-9]/", $day)) {
|
379 |
|
|
return $day;
|
380 |
|
|
}
|
381 |
|
|
}
|
382 |
|
|
return;
|
383 |
|
|
}
|
384 |
|
|
|
385 |
|
|
/**
|
386 |
|
|
* Converts an ISO 8601 org.joda.time.Partial to YYYY-MM-DD.
|
387 |
|
|
*
|
388 |
|
|
* This function expects an ISO 8601 time representations of a
|
389 |
|
|
* org.joda.time.Partial of the form yyyy-MM-dd and returns
|
390 |
|
|
* four digit year, month and day with dashes:
|
391 |
|
|
* YYYY-MM-DD eg: "2012-06-30", "1956-00-00"
|
392 |
|
|
*
|
393 |
|
|
* The partial may contain question marks eg: "1973-??-??",
|
394 |
|
|
* these are turned in to '00' or are stripped depending of the $stripZeros
|
395 |
|
|
* parameter.
|
396 |
|
|
*
|
397 |
|
|
* @param string $partial
|
398 |
|
|
* org.joda.time.Partial.
|
399 |
|
|
* @param bool $stripZeros
|
400 |
|
|
* If set to TRUE the zero (00) month and days will be hidden:
|
401 |
|
|
* eg 1956-00-00 becomes 1956. The default is TRUE.
|
402 |
|
|
*
|
403 |
|
|
* @return string
|
404 |
|
|
* YYYY-MM-DD formatted year, month, day.
|
405 |
|
|
*/
|
406 |
|
|
function partialToDate($partial, $stripZeros = TRUE) {
|
407 |
|
|
$y = partialToYear($partial);
|
408 |
|
|
$m = partialToMonth($partial);
|
409 |
|
|
$d = partialToDay($partial);
|
410 |
|
|
|
411 |
|
|
$y = $y ? $y : '00';
|
412 |
|
|
$m = $m ? $m : '00';
|
413 |
|
|
$d = $d ? $d : '00';
|
414 |
|
|
|
415 |
|
|
$date = '';
|
416 |
|
|
|
417 |
|
|
if ($y == '00' && $stripZeros) {
|
418 |
|
|
return;
|
419 |
|
|
}
|
420 |
|
|
else {
|
421 |
|
|
$date = $y;
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
if ($m == '00' && $stripZeros) {
|
425 |
|
|
return $date;
|
426 |
|
|
}
|
427 |
|
|
else {
|
428 |
|
|
$date .= "-" . $m;
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
if ($d == '00' && $stripZeros) {
|
432 |
|
|
return $date;
|
433 |
|
|
}
|
434 |
|
|
else {
|
435 |
|
|
$date .= "-" . $d;
|
436 |
|
|
}
|
437 |
|
|
return $date;
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
/**
|
441 |
|
|
* Converts a time period to a string.
|
442 |
|
|
*
|
443 |
|
|
* See also partialToDate($partial, $stripZeros).
|
444 |
|
|
*
|
445 |
|
|
* @param object $period
|
446 |
|
|
* An JodaTime org.joda.time.Period object.
|
447 |
|
|
* @param bool $stripZeros
|
448 |
|
|
* If set to True, the zero (00) month and days will be hidden:
|
449 |
|
|
* eg 1956-00-00 becomes 1956. The default is TRUE.
|
450 |
|
|
*
|
451 |
|
|
* @return string
|
452 |
|
|
* Returns a date in the form of a string.
|
453 |
|
|
*/
|
454 |
|
|
function timePeriodToString($period, $stripZeros = TRUE) {
|
455 |
|
|
$dateString = '';
|
456 |
|
|
if ($period->start) {
|
457 |
|
|
$dateString = partialToDate($period->start, $stripZeros);
|
458 |
|
|
}
|
459 |
|
|
if ($period->end) {
|
460 |
|
|
$dateString .= (strlen($dateString) > 0 ? ' ' . t('to') . ' ' : '') . partialToDate($period->end, $stripZeros);
|
461 |
|
|
}
|
462 |
|
|
return $dateString;
|
463 |
|
|
}
|
464 |
|
|
|
465 |
|
|
/**
|
466 |
|
|
* Composes a CDM webservice URL with parameters and querystring.
|
467 |
|
|
*
|
468 |
|
|
* @param string $uri_pattern
|
469 |
|
|
* String with place holders ($0, $1, ..) that should be replaced by the
|
470 |
|
|
* according element of the $pathParameters array.
|
471 |
|
|
* @param array $pathParameters
|
472 |
|
|
* An array of path elements, or a single element.
|
473 |
|
|
* @param string $query
|
474 |
|
|
* A query string to append to the URL.
|
475 |
|
|
*
|
476 |
|
|
* @return string
|
477 |
|
|
* A complete URL with parameters to a CDM webservice.
|
478 |
|
|
*/
|
479 |
|
|
function cdm_compose_url($uri_pattern, $pathParameters = array(), $query = NULL) {
|
480 |
|
|
if (empty($pathParameters)) {
|
481 |
|
|
$pathParameters = array();
|
482 |
|
|
}
|
483 |
|
|
|
484 |
|
|
$request_params = '';
|
485 |
|
|
$path_params = '';
|
486 |
|
|
|
487 |
|
|
// (1)
|
488 |
|
|
// Substitute all place holders ($0, $1, ..) in the $uri_pattern by the
|
489 |
|
|
// according element of the $pathParameters array.
|
490 |
|
|
static $helperArray = array();
|
491 |
|
|
if (isset($pathParameters) && !is_array($pathParameters)) {
|
492 |
|
|
$helperArray[0] = $pathParameters;
|
493 |
|
|
$pathParameters = $helperArray;
|
494 |
|
|
}
|
495 |
|
|
|
496 |
|
|
$i = 0;
|
497 |
|
|
while (strpos($uri_pattern, "$" . $i) !== FALSE) {
|
498 |
|
|
if (count($pathParameters) <= $i) {
|
499 |
|
|
if (module_exists("user") && user_access('administer')) {
|
500 |
|
|
drupal_set_message(t('cdm_compose_url(): missing pathParameters'), 'debug');
|
501 |
|
|
}
|
502 |
|
|
break;
|
503 |
|
|
}
|
504 |
|
|
$uri_pattern = str_replace("$" . $i, rawurlencode($pathParameters[$i]), $uri_pattern);
|
505 |
|
|
++$i;
|
506 |
|
|
}
|
507 |
|
|
|
508 |
|
|
// (2)
|
509 |
|
|
// Append all remaining element of the $pathParameters array as path
|
510 |
|
|
// elements.
|
511 |
|
|
if (count($pathParameters) > $i) {
|
512 |
|
|
// Strip trailing slashes.
|
513 |
|
|
if (strrchr($uri_pattern, '/') == strlen($uri_pattern)) {
|
514 |
|
|
$uri_pattern = substr($uri_pattern, 0, strlen($uri_pattern) - 1);
|
515 |
|
|
}
|
516 |
|
|
while (count($pathParameters) > $i) {
|
517 |
|
|
$uri_pattern .= '/' . rawurlencode($pathParameters[$i]);
|
518 |
|
|
++$i;
|
519 |
|
|
}
|
520 |
|
|
}
|
521 |
|
|
|
522 |
|
|
// (3)
|
523 |
|
|
// Append the query string supplied by $query.
|
524 |
|
|
if (isset($query)) {
|
525 |
|
|
$uri_pattern .= (strpos($uri_pattern, '?') !== FALSE ? '&' : '?') . $query;
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
// @WA: $ws_name seems unused
|
529 |
|
|
// $path = $ws_name.$uri_pattern;
|
530 |
|
|
$path = $uri_pattern;
|
531 |
|
|
|
532 |
|
|
$uri = variable_get('cdm_webservice_url', '') . $path;
|
533 |
|
|
return $uri;
|
534 |
|
|
}
|
535 |
|
|
|
536 |
|
|
/**
|
537 |
|
|
* @todo Please document this function.
|
538 |
|
|
* @see http://drupal.org/node/1354
|
539 |
|
|
*/
|
540 |
|
|
function proxy_content($uri, $theme = NULL) {
|
541 |
|
|
$args = func_get_args();
|
542 |
|
|
|
543 |
|
|
$uriEncoded = array_shift($args);
|
544 |
|
|
$uri = urldecode($uriEncoded);
|
545 |
|
|
$theme = array_shift($args);
|
546 |
|
|
|
547 |
|
|
// Find and deserialize arrays.
|
548 |
|
|
foreach ($args as &$arg) {
|
549 |
|
|
// FIXME use regex to find serialized arrays.
|
550 |
|
|
if (strpos($arg, "a:") === 0) {
|
551 |
|
|
$arg = unserialize($arg);
|
552 |
|
|
}
|
553 |
|
|
// Find comma separated string in all args.
|
554 |
|
|
// if(strpos($arg, ',')){
|
555 |
|
|
// $arg = explode(',', $arg);
|
556 |
|
|
// }
|
557 |
|
|
}
|
558 |
|
|
|
559 |
|
|
$request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
|
560 |
|
|
if ($request_method == "POST") {
|
561 |
|
|
$parameters = $_POST;
|
562 |
|
|
$post_data = array();
|
563 |
|
|
|
564 |
|
|
foreach ($parameters as $k => $v) {
|
565 |
|
|
$post_data[] = "$k=" . utf8_encode($v);
|
566 |
|
|
}
|
567 |
|
|
$post_data = implode(',', $post_data);
|
568 |
|
|
|
569 |
|
|
// For testing.
|
570 |
|
|
$data = drupal_http_request($uri, array('headers' => "POST", 'method' => $post_data));
|
571 |
|
|
print $data;
|
572 |
|
|
}
|
573 |
|
|
|
574 |
|
|
// Must be a mimetype.
|
575 |
|
|
elseif (strpos($theme, '/') > 0) {
|
576 |
|
|
header('Content-Type: ' . $theme);
|
577 |
|
|
// @WA: function _http_request_binary does not exist..
|
578 |
|
|
// @todo create this function, if you want to show something here
|
579 |
|
|
// $data = _http_request_binary($uri);
|
580 |
|
|
// print $data;
|
581 |
|
|
exit();
|
582 |
|
|
}
|
583 |
|
|
else {
|
584 |
|
|
// In all other cases perform a simple get request.
|
585 |
|
|
// TODO reconsider caching logic in this function.
|
586 |
|
|
if (empty($theme)) {
|
587 |
|
|
// Print out JSON, the cache cannot be used since it contains objects.
|
588 |
|
|
$http_response = drupal_http_request($uri);
|
589 |
|
|
if (isset($http_response->headers)) {
|
590 |
|
|
foreach ($http_response->headers as $hname => $hvalue) {
|
591 |
|
|
drupal_add_http_header($hname, $hvalue);
|
592 |
|
|
}
|
593 |
|
|
}
|
594 |
|
|
if (isset($http_response->data)) {
|
595 |
|
|
print $http_response->data;
|
596 |
|
|
}
|
597 |
|
|
exit();
|
598 |
|
|
}
|
599 |
|
|
else {
|
600 |
|
|
$obj = cdm_ws_get($uri, NULL, NULL, NULL, TRUE);
|
601 |
|
|
|
602 |
|
|
// array_unshift($args, $theme, $obj);
|
603 |
|
|
// print call_user_func_array('theme', $args);
|
604 |
|
|
switch($theme) {
|
605 |
|
|
case 'cdm_taxontree':
|
606 |
|
|
$variables = array(
|
607 |
|
|
'tree' => $obj,
|
608 |
|
|
'filterIncludes' => isset($args[0]) ? $args[0] : NULL,
|
609 |
|
|
'show_filter_switch' => isset($args[1]) ? $args[1] : FALSE,
|
610 |
|
|
'tree_node_callback' => isset($args[2]) ? $args[2] : FALSE,
|
611 |
|
|
'element_name'=> isset($args[3]) ? $args[3] : FALSE,
|
612 |
|
|
);
|
613 |
|
|
print theme($theme, $variables);
|
614 |
|
|
break;
|
615 |
|
|
|
616 |
|
|
case 'cdm_media_caption':
|
617 |
|
|
$variables = array(
|
618 |
|
|
'media' => $obj,
|
619 |
|
|
// $args[0] is set in taxon_image_gallery_default in
|
620 |
|
|
// cdm_dataportal.page.theme.
|
621 |
|
|
'elements' => isset($args[0]) ? $args[0] : array(
|
622 |
|
|
'title',
|
623 |
|
|
'description',
|
624 |
|
|
'artist',
|
625 |
|
|
'location',
|
626 |
|
|
'rights',
|
627 |
|
|
),
|
628 |
|
|
'fileUri' => isset($args[1]) ? $args[1] : NULL,
|
629 |
|
|
);
|
630 |
|
|
print theme($theme, $variables);
|
631 |
|
|
break;
|
632 |
|
|
|
633 |
|
|
case 'cdm_derivedUnitFacade':
|
634 |
|
|
$variables = array(
|
635 |
|
|
'derivedUnitFacade' => $obj,
|
636 |
|
|
);
|
637 |
|
|
print theme($theme, $variables);
|
638 |
|
|
break;
|
639 |
|
|
|
640 |
|
|
default:
|
641 |
|
|
drupal_set_message(t(
|
642 |
|
|
'Theme !theme is not supported yet by function !function.', array(
|
643 |
|
|
'!theme' => $theme,
|
644 |
|
|
'!function' => __FUNCTION__,
|
645 |
|
|
)), 'error');
|
646 |
|
|
break;
|
647 |
|
|
}
|
648 |
|
|
}
|
649 |
|
|
}
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
/**
|
653 |
|
|
* @todo Please document this function.
|
654 |
|
|
* @see http://drupal.org/node/1354
|
655 |
|
|
*/
|
656 |
|
|
function setvalue_session() {
|
657 |
|
|
if ($_REQUEST['var'] && strlen($_REQUEST['var']) > 4) {
|
658 |
|
|
$keys = substr($_REQUEST['var'], 1, strlen($_REQUEST['var']) - 2);
|
659 |
|
|
$keys = explode('][', $keys);
|
660 |
|
|
}
|
661 |
|
|
else {
|
662 |
|
|
return;
|
663 |
|
|
}
|
664 |
|
|
$val = $_REQUEST['val'] ? $_REQUEST['val'] : NULL;
|
665 |
|
|
|
666 |
|
|
// Prevent from malicous tags.
|
667 |
|
|
$val = strip_tags($val);
|
668 |
|
|
|
669 |
|
|
$var = &$_SESSION;
|
670 |
|
|
$i = 0;
|
671 |
|
|
foreach ($keys as $key) {
|
672 |
|
|
$hasMoreKeys = ++$i < count($var);
|
673 |
|
|
if ($hasMoreKeys && (!isset($var[$key]) || !is_array($var[$key]))) {
|
674 |
|
|
$var[$key] = array();
|
675 |
|
|
}
|
676 |
|
|
$var = &$var[$key];
|
677 |
|
|
}
|
678 |
|
|
$var = $val;
|
679 |
|
|
if (isset($_REQUEST['destination'])) {
|
680 |
|
|
drupal_goto($_REQUEST['destination']);
|
681 |
|
|
}
|
682 |
|
|
}
|
683 |
|
|
|
684 |
|
|
/**
|
685 |
|
|
* @todo Please document this function.
|
686 |
|
|
* @see http://drupal.org/node/1354
|
687 |
|
|
*/
|
688 |
|
|
function uri_uriByProxy($uri, $theme = FALSE) {
|
689 |
|
|
// usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");)
|
690 |
|
|
return url('cdm_api/proxy/' . urlencode($uri) . (isset($theme) ? "/$theme" : ''));
|
691 |
|
|
}
|
692 |
|
|
|
693 |
|
|
/**
|
694 |
|
|
* @todo Please document this function.
|
695 |
|
|
* @see http://drupal.org/node/1354
|
696 |
|
|
*/
|
697 |
|
|
function cdm_compose_annotations_url($cdmBase) {
|
698 |
|
|
if (!$cdmBase->uuid) {
|
699 |
|
|
return;
|
700 |
|
|
}
|
701 |
|
|
|
702 |
|
|
$ws_base_uri = NULL;
|
703 |
|
|
switch ($cdmBase->class) {
|
704 |
|
|
case 'TaxonBase':
|
705 |
|
|
case 'Taxon':
|
706 |
|
|
case 'Synonym':
|
707 |
|
|
$ws_base_uri = CDM_WS_TAXON;
|
708 |
|
|
break;
|
709 |
|
|
|
710 |
|
|
case 'TaxonNameBase':
|
711 |
|
|
case 'NonViralName':
|
712 |
|
|
case 'BacterialName':
|
713 |
|
|
case 'BotanicalName':
|
714 |
|
|
case 'CultivarPlantName':
|
715 |
|
|
case 'ZoologicalName':
|
716 |
|
|
case 'ViralName':
|
717 |
|
|
$ws_base_uri = CDM_WS_NAME;
|
718 |
|
|
break;
|
719 |
|
|
|
720 |
|
|
case 'Media':
|
721 |
|
|
$ws_base_uri = CDM_WS_MEDIA;
|
722 |
|
|
break;
|
723 |
|
|
|
724 |
|
|
case 'Reference':
|
725 |
|
|
$ws_base_uri = CDM_WS_REFERENCE;
|
726 |
|
|
break;
|
727 |
|
|
|
728 |
|
|
case 'Distribution':
|
729 |
|
|
case 'TextData':
|
730 |
|
|
case 'TaxonInteraction':
|
731 |
|
|
case 'QuantitativeData':
|
732 |
|
|
case 'IndividualsAssociation':
|
733 |
|
|
case 'Distribution':
|
734 |
|
|
case 'CommonTaxonName':
|
735 |
|
|
case 'CategoricalData':
|
736 |
|
|
$ws_base_uri = CDM_WS_DESCRIPTIONELEMENT;
|
737 |
|
|
break;
|
738 |
|
|
|
739 |
|
|
case 'PolytomousKey':
|
740 |
|
|
case 'MediaKey':
|
741 |
|
|
case 'MultiAccessKey':
|
742 |
|
|
$ws_base_uri = $cdmBase->class;
|
743 |
|
|
$ws_base_uri{0} = strtolower($ws_base_uri{0});
|
744 |
|
|
break;
|
745 |
|
|
|
746 |
|
|
default:
|
747 |
|
|
trigger_error(check_plain('Unsupported CDM Class - no annotations available for ' . $cdmBase->class), E_USER_ERROR);
|
748 |
|
|
return;
|
749 |
|
|
}
|
750 |
|
|
return cdm_compose_url($ws_base_uri, array(
|
751 |
|
|
$cdmBase->uuid,
|
752 |
|
|
'annotations',
|
753 |
|
|
));
|
754 |
|
|
}
|
755 |
|
|
|
756 |
|
|
/**
|
757 |
|
|
* Enter description here...
|
758 |
|
|
*
|
759 |
|
|
* @param string $resourceURI
|
760 |
|
|
* @param mixed $pageSize
|
761 |
|
|
* The maximum number of entities returned per page (can be NULL
|
762 |
|
|
* to return all entities in a single page).
|
763 |
|
|
* @param int $pageNumber
|
764 |
|
|
* The number of the page to be returned, the first page has the
|
765 |
|
|
* pageNumber = 1.
|
766 |
|
|
*
|
767 |
|
|
* @return unknown
|
768 |
|
|
*/
|
769 |
|
|
function cdm_ws_page($resourceURI, $pageSize, $pageNumber) {
|
770 |
|
|
return cdm_ws_get($resourceURI, NULL, queryString(array(
|
771 |
|
|
"page" => $pageNumber,
|
772 |
|
|
'pageSize' => $pageSize,
|
773 |
|
|
)));
|
774 |
|
|
}
|
775 |
|
|
|
776 |
|
|
/*
|
777 |
|
|
function cdm_ws_taxonomy_compose_resourcePath($path = NULL){
|
778 |
|
|
$viewrank = _cdm_taxonomy_compose_viewrank();
|
779 |
|
|
return CDM_WS_PORTAL_TAXONOMY . '/' . ($viewrank ? $viewrank : '' ) . ($path
|
780 |
|
|
? '/' . $path : '') ;
|
781 |
|
|
}
|
782 |
|
|
*/
|
783 |
|
|
|
784 |
|
|
/**
|
785 |
|
|
* @todo Enter description here...
|
786 |
|
|
*
|
787 |
|
|
* @param string $taxonUuid
|
788 |
|
|
*
|
789 |
|
|
* @return unknown
|
790 |
|
|
*/
|
791 |
|
|
function cdm_compose_taxonomy_path($taxonUuid = FALSE) {
|
792 |
|
|
$viewUuid = get_taxonomictree_uuid_selected();
|
793 |
|
|
$rankUuid = variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT);
|
794 |
|
|
|
795 |
|
|
if (!empty($taxonUuid)) {
|
796 |
|
|
return cdm_compose_url(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_OF_TAXON, array(
|
797 |
|
|
$viewUuid,
|
798 |
|
|
$taxonUuid,
|
799 |
|
|
));
|
800 |
|
|
}
|
801 |
|
|
else {
|
802 |
|
|
if (!empty($rankUuid)) {
|
803 |
|
|
return cdm_compose_url(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_AT_RANK, array(
|
804 |
|
|
$viewUuid,
|
805 |
|
|
$rankUuid,
|
806 |
|
|
));
|
807 |
|
|
}
|
808 |
|
|
else {
|
809 |
|
|
return cdm_compose_url(CDM_WS_PORTAL_TAXONOMY, array(
|
810 |
|
|
$viewUuid,
|
811 |
|
|
));
|
812 |
|
|
}
|
813 |
|
|
}
|
814 |
|
|
}
|
815 |
|
|
|
816 |
|
|
/**
|
817 |
|
|
* @todo Please document this function.
|
818 |
|
|
* @see http://drupal.org/node/1354
|
819 |
|
|
*/
|
820 |
|
|
function cdm_ws_taxonomy($taxonUuid = NULL) {
|
821 |
|
|
$response = NULL;
|
822 |
|
|
|
823 |
|
|
$response = cdm_ws_get(cdm_compose_taxonomy_path($taxonUuid), NULL, NULL, NULL, TRUE);
|
824 |
|
|
|
825 |
|
|
if ($response == NULL) {
|
826 |
|
|
// Error handing.
|
827 |
|
|
if (isset($_SESSION['cdm']['taxonomictree_uuid']) && is_uuid($_SESSION['cdm']['taxonomictree_uuid'])) {
|
828 |
|
|
// Delete the session value and try again with the default.
|
829 |
|
|
unset($_SESSION['cdm']['taxonomictree_uuid']);
|
830 |
|
|
return cdm_ws_taxonomy($taxonUuid);
|
831 |
|
|
}
|
832 |
|
|
else {
|
833 |
|
|
// Check if taxonomictree_uuid is valid.
|
834 |
|
|
$test = cdm_ws_get(cdm_compose_taxonomy_path(), NULL, NULL, NULL, TRUE);
|
835 |
|
|
if ($test == NULL) {
|
836 |
|
|
// The default set by the admin seems to be invalid or is not even set.
|
837 |
|
|
drupal_set_message(_no_classfication_uuid_message(), 'warning');
|
838 |
|
|
}
|
839 |
|
|
}
|
840 |
|
|
}
|
841 |
|
|
|
842 |
|
|
return $response;
|
843 |
|
|
}
|
844 |
|
|
|
845 |
|
|
/**
|
846 |
|
|
* @todo Enter description here...
|
847 |
|
|
*
|
848 |
|
|
* @param string $taxonUuid
|
849 |
|
|
*
|
850 |
|
|
* @return unknown
|
851 |
|
|
*/
|
852 |
|
|
function cdm_ws_taxonomy_pathFromRoot($taxonUuid) {
|
853 |
|
|
$viewUuid = get_taxonomictree_uuid_selected();
|
854 |
|
|
$rankUuid = variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT);
|
855 |
|
|
|
856 |
|
|
$response = NULL;
|
857 |
|
|
if ($rankUuid) {
|
858 |
|
|
$response = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_PATH_FROM_TO_RANK, array(
|
859 |
|
|
$viewUuid,
|
860 |
|
|
$taxonUuid,
|
861 |
|
|
$rankUuid,
|
862 |
|
|
));
|
863 |
|
|
}
|
864 |
|
|
else {
|
865 |
|
|
$response = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_PATH_FROM, array(
|
866 |
|
|
$viewUuid,
|
867 |
|
|
$taxonUuid,
|
868 |
|
|
));
|
869 |
|
|
}
|
870 |
|
|
|
871 |
|
|
if ($response == NULL) {
|
872 |
|
|
// Error handing.
|
873 |
|
|
if (is_uuid($_SESSION['cdm']['taxonomictree_uuid'])) {
|
874 |
|
|
// Delete the session value and try again with the default.
|
875 |
|
|
unset($_SESSION['cdm']['taxonomictree_uuid']);
|
876 |
|
|
return cdm_ws_taxonomy_pathFromRoot($taxonUuid);
|
877 |
|
|
}
|
878 |
|
|
else {
|
879 |
|
|
// Check if taxonomictree_uuid is valid.
|
880 |
|
|
$test = cdm_ws_get(cdm_compose_taxonomy_path(), NULL, NULL, NULL, TRUE);
|
881 |
|
|
if ($test == NULL) {
|
882 |
|
|
// The default set by the admin seems to be invalid or is not even set.
|
883 |
|
|
drupal_set_message(_no_classfication_uuid_message(), 'warning');
|
884 |
|
|
}
|
885 |
|
|
}
|
886 |
|
|
}
|
887 |
|
|
|
888 |
|
|
return $response;
|
889 |
|
|
}
|
890 |
|
|
|
891 |
|
|
/**
|
892 |
|
|
* @todo Please document this function.
|
893 |
|
|
* @see http://drupal.org/node/1354
|
894 |
|
|
*/
|
895 |
|
|
function cdm_rankVocabulary_as_option() {
|
896 |
|
|
return cdm_Vocabulary_as_option(UUID_RANK);
|
897 |
|
|
}
|
898 |
|
|
|
899 |
|
|
/**
|
900 |
|
|
*
|
901 |
|
|
* @param Object $definedTermBase
|
902 |
|
|
* of cdm type DefinedTermBase
|
903 |
|
|
* @return string
|
904 |
|
|
* the localized representation_L10n of the term,
|
905 |
|
|
* otherwise the titleCache as fall back,
|
906 |
|
|
* otherwise an empty string
|
907 |
|
|
*/
|
908 |
|
|
function cdm_term_representation($definedTermBase) {
|
909 |
|
|
if ( isset($definedTermBase->representation_L10n) ) {
|
910 |
|
|
return $definedTermBase->representation_L10n;
|
911 |
|
|
} elseif ( isset($definedTermBase->titleCache)) {
|
912 |
|
|
return $definedTermBase->titleCache;
|
913 |
|
|
}
|
914 |
|
|
return '';
|
915 |
|
|
}
|
916 |
|
|
|
917 |
|
|
/**
|
918 |
|
|
* @todo Improve documentation of this function.
|
919 |
|
|
*
|
920 |
|
|
* eu.etaxonomy.cdm.model.description.
|
921 |
|
|
* CategoricalData
|
922 |
|
|
* CommonTaxonName
|
923 |
|
|
* Distribution
|
924 |
|
|
* IndividualsAssociation
|
925 |
|
|
* QuantitativeData
|
926 |
|
|
* TaxonInteraction
|
927 |
|
|
* TextData
|
928 |
|
|
*/
|
929 |
|
|
function cdm_descriptionElementTypes_as_option($prependEmptyElement = FALSE) {
|
930 |
|
|
static $types = array(
|
931 |
|
|
"CategoricalData",
|
932 |
|
|
"CommonTaxonName",
|
933 |
|
|
"Distribution",
|
934 |
|
|
"IndividualsAssociation",
|
935 |
|
|
"QuantitativeData",
|
936 |
|
|
"TaxonInteraction",
|
937 |
|
|
"TextData",
|
938 |
|
|
);
|
939 |
|
|
|
940 |
|
|
static $options = NULL;
|
941 |
|
|
if ($options == NULL) {
|
942 |
|
|
$options = array();
|
943 |
|
|
if ($prependEmptyElement) {
|
944 |
|
|
$options[' '] = '';
|
945 |
|
|
}
|
946 |
|
|
foreach ($types as $type) {
|
947 |
|
|
// No internatianalization here since these are purely technical terms.
|
948 |
|
|
$options["eu.etaxonomy.cdm.model.description." . $type] = $type;
|
949 |
|
|
}
|
950 |
|
|
}
|
951 |
|
|
return $options;
|
952 |
|
|
}
|
953 |
|
|
|
954 |
|
|
/**
|
955 |
|
|
* @todo Please document this function.
|
956 |
|
|
* @see http://drupal.org/node/1354
|
957 |
|
|
*/
|
958 |
|
|
function cdm_Vocabulary_as_option($vocabularyUuid, $term_label_callback = NULL) {
|
959 |
|
|
static $vocabularyOptions = array();
|
960 |
|
|
|
961 |
|
|
// @WA: $rankVocabularyOptions is not defined
|
962 |
|
|
// if(!$rankVocabularyOptions){
|
963 |
|
|
if (!isset($vocabularyOptions[$vocabularyUuid])) {
|
964 |
|
|
$vocab = cdm_ws_get(CDM_WS_TERMVOCABULARY, $vocabularyUuid);
|
965 |
|
|
$vocabularyOptions[$vocabularyUuid] = array();
|
966 |
|
|
|
967 |
|
|
if ($vocab) {
|
968 |
|
|
foreach ($vocab->terms as $term) {
|
969 |
|
|
if ($term_label_callback && function_exists($term_label_callback)) {
|
970 |
|
|
$vocabularyOptions[$vocabularyUuid][$term->uuid] = call_user_func($term_label_callback, $term);
|
971 |
|
|
}
|
972 |
|
|
else {
|
973 |
|
|
$vocabularyOptions[$vocabularyUuid][$term->uuid] = t($term->representation_L10n);
|
974 |
|
|
}
|
975 |
|
|
}
|
976 |
|
|
array_reverse($vocabularyOptions[$vocabularyUuid]);
|
977 |
|
|
}
|
978 |
|
|
}
|
979 |
|
|
// }
|
980 |
|
|
return $vocabularyOptions[$vocabularyUuid];
|
981 |
|
|
}
|
982 |
|
|
|
983 |
|
|
/**
|
984 |
|
|
* @todo Please document this function.
|
985 |
|
|
* @see http://drupal.org/node/1354
|
986 |
|
|
*/
|
987 |
|
|
function _cdm_relationship_type_term_label_callback($term) {
|
988 |
|
|
if (isset($term->representation_L10n_abbreviatedLabel)) {
|
989 |
|
|
return $term->representation_L10n_abbreviatedLabel . ' : ' . t($term->representation_L10n);
|
990 |
|
|
}
|
991 |
|
|
else {
|
992 |
|
|
return t($term->representation_L10n);
|
993 |
|
|
}
|
994 |
|
|
}
|
995 |
|
|
|
996 |
|
|
/**
|
997 |
|
|
* @todo Please document this function.
|
998 |
|
|
* @see http://drupal.org/node/1354
|
999 |
|
|
*/
|
1000 |
|
|
function cdm_ws_descriptions_by_featuretree($featureTree, $descriptions, $isDescriptionsSeparated = FALSE) {
|
1001 |
|
|
if (!$featureTree) {
|
1002 |
|
|
drupal_set_message(check_plain(t("No 'FeatureTree' has been set so far.
|
1003 |
|
|
In order to see the species profiles of your taxa, please select a
|
1004 |
|
|
'FeatureTree' in the !settings"), array('!settings' => l(t('CDM Dataportal Settings'), 'admin/config/cdm_dataportal/layout'))), 'warning');
|
1005 |
|
|
return FALSE;
|
1006 |
|
|
}
|
1007 |
|
|
|
1008 |
|
|
$mergedTrees = array();
|
1009 |
|
|
|
1010 |
|
|
if ($isDescriptionsSeparated) {
|
1011 |
|
|
// Merge any description into a separate feature tree.
|
1012 |
|
|
foreach ($descriptions as $desc) {
|
1013 |
|
|
$mergedNodes = _mergeFeatureTreeDescriptions($featureTree->root->children, $desc->elements);
|
1014 |
|
|
|
1015 |
|
|
$mergedTree = clone $featureTree;
|
1016 |
|
|
$mergedTree->root->children = $mergedNodes;
|
1017 |
|
|
$mergedTrees[] = $mergedTree;
|
1018 |
|
|
}
|
1019 |
|
|
}
|
1020 |
|
|
else {
|
1021 |
|
|
// Combine all descripions into one feature tree.
|
1022 |
|
|
foreach ($descriptions as $desc) {
|
1023 |
|
|
$mergedNodes = _mergeFeatureTreeDescriptions($featureTree->root->children, $desc->elements);
|
1024 |
|
|
$featureTree->root->children = $mergedNodes;
|
1025 |
|
|
}
|
1026 |
|
|
$mergedTrees[] = $featureTree;
|
1027 |
|
|
}
|
1028 |
|
|
|
1029 |
|
|
return $mergedTrees;
|
1030 |
|
|
}
|
1031 |
|
|
|
1032 |
|
|
/**
|
1033 |
|
|
* Get a list of annotations for a cdm entity.
|
1034 |
|
|
*
|
1035 |
|
|
* @param string $cdmBase
|
1036 |
|
|
* An annotatable cdm entity.
|
1037 |
|
|
* @param array $includeTypes
|
1038 |
|
|
* If an array of annotation type uuids is supplied by this parameter the
|
1039 |
|
|
* list of annotations is resticted to those which belong to this type.
|
1040 |
|
|
*
|
1041 |
|
|
* @return array
|
1042 |
|
|
* An array of Annotation objects or an empty array.
|
1043 |
|
|
*/
|
1044 |
|
|
function cdm_ws_getAnnotationsFor($cdmBase, $includeTypes = FALSE) {
|
1045 |
|
|
$annotationUrl = cdm_compose_annotations_url($cdmBase);
|
1046 |
|
|
if ($annotationUrl) {
|
1047 |
|
|
$annotationPager = cdm_ws_get($annotationUrl, NULL, NULL, NULL, TRUE);
|
1048 |
|
|
if (isset($annotationPager->records) && is_array($annotationPager->records)) {
|
1049 |
|
|
$annotations = array();
|
1050 |
|
|
foreach ($annotationPager->records as $annotation) {
|
1051 |
|
|
if ($includeTypes) {
|
1052 |
|
|
if ((isset($annotation->annotationType->uuid) && in_array($annotation->annotationType->uuid, $includeTypes, TRUE)) || ($annotation->annotationType === NULL && in_array('NULL_VALUE', $includeTypes, TRUE))) {
|
1053 |
|
|
$annotations[] = $annotation;
|
1054 |
|
|
}
|
1055 |
|
|
}
|
1056 |
|
|
else {
|
1057 |
|
|
$annotations[] = $annotation;
|
1058 |
|
|
}
|
1059 |
|
|
}
|
1060 |
|
|
return $annotations;
|
1061 |
|
|
}
|
1062 |
|
|
}
|
1063 |
|
|
}
|
1064 |
|
|
|
1065 |
|
|
/**
|
1066 |
|
|
* Get a NomenclaturalReference string.
|
1067 |
|
|
*
|
1068 |
|
|
* Returns the NomenclaturalReference string with correctly placed
|
1069 |
|
|
* microreference (= reference detail) e.g.
|
1070 |
|
|
* in Phytotaxa 43: 1-48. 2012.
|
1071 |
|
|
*
|
1072 |
|
|
* @param string $referenceUuid
|
1073 |
|
|
* UUID of the reference.
|
1074 |
|
|
* @param string $microreference
|
1075 |
|
|
* Reference detail.
|
1076 |
|
|
*
|
1077 |
|
|
* @return string
|
1078 |
|
|
* a NomenclaturalReference.
|
1079 |
|
|
*/
|
1080 |
|
|
function cdm_ws_getNomenclaturalReference($referenceUuid, $microreference) {
|
1081 |
|
|
$obj = cdm_ws_get(CDM_WS_NOMENCLATURAL_REFERENCE_CITATION, array(
|
1082 |
|
|
$referenceUuid,
|
1083 |
|
|
), "microReference=" . urlencode($microreference));
|
1084 |
|
|
|
1085 |
|
|
if ($obj) {
|
1086 |
|
|
return $obj->String;
|
1087 |
|
|
}
|
1088 |
|
|
else {
|
1089 |
|
|
return NULL;
|
1090 |
|
|
}
|
1091 |
|
|
}
|
1092 |
|
|
|
1093 |
|
|
/**
|
1094 |
|
|
* @todo Please document this function.
|
1095 |
|
|
* @see http://drupal.org/node/1354
|
1096 |
|
|
*/
|
1097 |
|
|
function _mergeFeatureTreeDescriptions($featureNodes, $descriptionElements) {
|
1098 |
|
|
foreach ($featureNodes as &$node) {
|
1099 |
|
|
// Append corresponding elements to an additional node field:
|
1100 |
|
|
// $node->descriptionElements.
|
1101 |
|
|
foreach ($descriptionElements as $element) {
|
1102 |
|
|
if ($element->feature->uuid == $node->feature->uuid) {
|
1103 |
|
|
if (!isset($node->descriptionElements)) {
|
1104 |
|
|
$node->descriptionElements = array();
|
1105 |
|
|
}
|
1106 |
|
|
$node->descriptionElements[] = $element;
|
1107 |
|
|
}
|
1108 |
|
|
}
|
1109 |
|
|
|
1110 |
|
|
// Recurse into node children.
|
1111 |
|
|
if (isset($node->children) && is_array($node->children)) {
|
1112 |
|
|
$mergedChildNodes = _mergeFeatureTreeDescriptions($node->children, $descriptionElements);
|
1113 |
|
|
$node->children = $mergedChildNodes;
|
1114 |
|
|
}
|
1115 |
|
|
}
|
1116 |
|
|
return $featureNodes;
|
1117 |
|
|
}
|
1118 |
|
|
|
1119 |
|
|
/**
|
1120 |
|
|
* Sends a GET request to a CDM RESTService and returns a deserialized object.
|
1121 |
|
|
*
|
1122 |
|
|
* The response from the HTTP GET request is returned as object.
|
1123 |
|
|
* The response objects coming from the webservice configured in the
|
1124 |
|
|
* 'cdm_webservice_url' variable are beeing cached in a level 1 (L1) and / or
|
1125 |
|
|
* in a level 2 (L2) cache.
|
1126 |
|
|
*
|
1127 |
|
|
* Since the L1 cache is implemented as static variable of the cdm_ws_get()
|
1128 |
|
|
* function, this cache persists only per each single page execution.
|
1129 |
|
|
* Any object coming from the webservice is stored into it by default.
|
1130 |
|
|
* In contrast to this default caching mechanism the L2 cache only is used if
|
1131 |
|
|
* the 'cdm_webservice_cache' variable is set to TRUE,
|
1132 |
|
|
* which can be set using the modules administrative settings section.
|
1133 |
|
|
* Objects stored in this L2 cache are serialized and stored
|
1134 |
|
|
* using the drupal cache in the '{prefix}cache_cdm_ws' cache table. So the
|
1135 |
|
|
* objects that are stored in the database will persist as
|
1136 |
|
|
* long as the drupal cache is not beeing cleared and are available across
|
1137 |
|
|
* multiple script executions.
|
1138 |
|
|
*
|
1139 |
|
|
* @param string $uri
|
1140 |
|
|
* URL to the webservice.
|
1141 |
|
|
* @param array $pathParameters
|
1142 |
|
|
* An array of path parameters.
|
1143 |
|
|
* @param string $query
|
1144 |
|
|
* A query string to be appended to the URL.
|
1145 |
|
|
* @param string $method
|
1146 |
|
|
* The HTTP method to use, valid values are "GET" or "POST";
|
1147 |
|
|
* @param bool $absoluteURI
|
1148 |
|
|
* TRUE when the URL should be treated as absolute URL.
|
1149 |
|
|
*
|
1150 |
|
|
* @return object
|
1151 |
|
|
* The deserialized webservice response object.
|
1152 |
|
|
*/
|
1153 |
|
|
function cdm_ws_get($uri, $pathParameters = array(), $query = NULL, $method = "GET", $absoluteURI = FALSE) {
|
1154 |
|
|
// static $cacheL1; if(!isset($cacheL1)){ $cacheL1 = array(); }
|
1155 |
|
|
static $cacheL1 = array();
|
1156 |
|
|
|
1157 |
|
|
// Transform the given uri path or patthern into a proper webservice uri.
|
1158 |
|
|
if (!$absoluteURI) {
|
1159 |
|
|
$uri = cdm_compose_url($uri, $pathParameters, $query);
|
1160 |
|
|
}
|
1161 |
|
|
|
1162 |
|
|
$is_cdm_ws_uri = _is_cdm_ws_uri($uri);
|
1163 |
|
|
$use_cacheL2 = variable_get('cdm_webservice_cache', 1);
|
1164 |
|
|
|
1165 |
|
|
if (array_key_exists($uri, $cacheL1)) {
|
1166 |
|
|
$cacheL1_obj = $cacheL1[$uri];
|
1167 |
|
|
}
|
1168 |
|
|
|
1169 |
|
|
$set_cacheL1 = FALSE;
|
1170 |
|
|
if ($is_cdm_ws_uri && !isset($cacheL1_obj)) {
|
1171 |
|
|
$set_cacheL1 = TRUE;
|
1172 |
|
|
}
|
1173 |
|
|
|
1174 |
|
|
// Only cache cdm webservice URIs.
|
1175 |
|
|
$set_cacheL2 = $use_cacheL2 && $is_cdm_ws_uri && $set_cacheL1;
|
1176 |
|
|
$cacheL2_entry = FALSE;
|
1177 |
|
|
|
1178 |
|
|
if ($use_cacheL2) {
|
1179 |
|
|
// Try to get object from cacheL2.
|
1180 |
|
|
$cacheL2_entry = cache_get($uri, 'cache_cdm_ws');
|
1181 |
|
|
}
|
1182 |
|
|
|
1183 |
|
|
if (isset($cacheL1_obj)) {
|
1184 |
|
|
//
|
1185 |
|
|
// The object has been found in the L1 cache.
|
1186 |
|
|
//
|
1187 |
|
|
$obj = $cacheL1_obj;
|
1188 |
|
|
if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
|
1189 |
|
|
_add_status_message_toggler();
|
1190 |
|
|
_add_debugMessageStr('Using cacheL1 for: ' . $uri);
|
1191 |
|
|
}
|
1192 |
|
|
}
|
1193 |
|
|
elseif ($cacheL2_entry) {
|
1194 |
|
|
//
|
1195 |
|
|
// The object has been found in the L2 cache.
|
1196 |
|
|
//
|
1197 |
|
|
$obj = unserialize($cacheL2_entry->data);
|
1198 |
|
|
|
1199 |
|
|
if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
|
1200 |
|
|
_add_status_message_toggler();
|
1201 |
|
|
_add_debugMessageStr('Using cacheL2 for: ' . $uri);
|
1202 |
|
|
}
|
1203 |
|
|
}
|
1204 |
|
|
else {
|
1205 |
|
|
//
|
1206 |
|
|
// Get the object from the webservice and cache it.
|
1207 |
|
|
//
|
1208 |
|
|
$time_get_start = microtime(TRUE);
|
1209 |
|
|
// Request data from webservice JSON or XML.
|
1210 |
|
|
$datastr = cdm_http_request($uri, $method);
|
1211 |
|
|
$time_get = microtime(TRUE) - $time_get_start;
|
1212 |
|
|
|
1213 |
|
|
$time_parse_start = microtime(TRUE);
|
1214 |
|
|
|
1215 |
|
|
// Parse data and create object.
|
1216 |
|
|
$obj = cdm_load_obj($datastr);
|
1217 |
|
|
|
1218 |
|
|
$time_parse = microtime(TRUE) - $time_parse_start;
|
1219 |
|
|
if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
|
1220 |
|
|
if ($obj || $datastr == "[]") {
|
1221 |
|
|
$success_msg = 'valid';
|
1222 |
|
|
}
|
1223 |
|
|
else {
|
1224 |
|
|
$success_msg = 'invalid';
|
1225 |
|
|
}
|
1226 |
|
|
_add_debugMessage($uri, $time_get, $time_parse, strlen($datastr), $success_msg);
|
1227 |
|
|
}
|
1228 |
|
|
if ($set_cacheL2) {
|
1229 |
|
|
// Store the object in cache L2.
|
1230 |
|
|
// Comment @WA perhaps better if Drupal serializes here? Then the
|
1231 |
|
|
// flag serialized is set properly in the cache table.
|
1232 |
|
|
cache_set($uri, serialize($obj), 'cache_cdm_ws', CACHE_TEMPORARY);
|
1233 |
|
|
}
|
1234 |
|
|
}
|
1235 |
|
|
if ($obj) {
|
1236 |
|
|
// Store the object in cache L1.
|
1237 |
|
|
if ($set_cacheL1) {
|
1238 |
|
|
$cacheL1[$uri] = $obj;
|
1239 |
|
|
}
|
1240 |
|
|
}
|
1241 |
|
|
return $obj;
|
1242 |
|
|
}
|
1243 |
|
|
|
1244 |
|
|
/**
|
1245 |
|
|
* @todo Please document this function.
|
1246 |
|
|
* @see http://drupal.org/node/1354
|
1247 |
|
|
*/
|
1248 |
|
|
function _add_debugMessageStr($message) {
|
1249 |
|
|
_add_status_message_toggler();
|
1250 |
|
|
drupal_set_message(check_plain($message), 'debug');
|
1251 |
|
|
}
|
1252 |
|
|
|
1253 |
|
|
/**
|
1254 |
|
|
* @todo Please document this function.
|
1255 |
|
|
* @see http://drupal.org/node/1354
|
1256 |
|
|
*/
|
1257 |
|
|
function _add_debugMessage($uri, $time_get, $time_parse, $datasize, $success_msg) {
|
1258 |
|
|
static $cumulated_time_parse = 0;
|
1259 |
|
|
static $cumulated_time_get = 0;
|
1260 |
|
|
_add_status_message_toggler();
|
1261 |
|
|
|
1262 |
|
|
$cumulated_time_get += $time_get;
|
1263 |
|
|
$cumulated_time_parse += $time_parse;
|
1264 |
|
|
|
1265 |
|
|
// Decompose uri into path and query element.
|
1266 |
|
|
$uri_parts = explode("?", $uri);
|
1267 |
|
|
$query = '';
|
1268 |
|
|
if (count($uri_parts) == 2) {
|
1269 |
|
|
$path = $uri_parts[0];
|
1270 |
|
|
$queryparts = explode("=", $uri_parts[1]);
|
1271 |
|
|
$query = array($queryparts[0] => $queryparts[1]);
|
1272 |
|
|
}
|
1273 |
|
|
else {
|
1274 |
|
|
$path = $uri;
|
1275 |
|
|
}
|
1276 |
|
|
|
1277 |
|
|
$message = '<span class="uri">' . $uri . '</span><br />';
|
1278 |
|
|
$message .= '[fetched in: ' . sprintf('%3.3f', $time_get) . 's(' . sprintf('%3.3f', $cumulated_time_get) . 's); ';
|
1279 |
|
|
$message .= 'parsed in ' . sprintf('%3.3f', $time_parse) . ' s(' . sprintf('%3.3f', $cumulated_time_parse) . 's); ';
|
1280 |
|
|
$message .= 'size:' . sprintf('%3.1f', ($datasize / 1024)) . ' kb of ' . $success_msg . ' data: ';
|
1281 |
|
|
if (_is_cdm_ws_uri($path)) {
|
1282 |
|
|
$message .= '<a href="' . url($path . '.xml', array(
|
1283 |
|
|
'query' => $query,
|
1284 |
|
|
)) . '" target="data" class="' . $success_msg . '">xml</a>-';
|
1285 |
|
|
$message .= '<a href="' . url('cdm_api/proxy/' . urlencode(url($path . '.xml', array(
|
1286 |
|
|
'query' => $query,
|
1287 |
|
|
)))) . '" target="data" class="' . $success_msg . '">proxied</a>,';
|
1288 |
|
|
$message .= '<a href="' . url($path . '.json', array(
|
1289 |
|
|
'query' => $query,
|
1290 |
|
|
)) . '" target="data" class="' . $success_msg . '">json</a>-';
|
1291 |
|
|
$message .= '<a href="' . url('cdm_api/proxy/' . urlencode(url($path . '.json', array(
|
1292 |
|
|
'query' => $query,
|
1293 |
|
|
)))) . '" target="data" class="' . $success_msg . '">proxied</a>';
|
1294 |
|
|
}
|
1295 |
|
|
else {
|
1296 |
|
|
$message .= '<a href="' . url($path, array(
|
1297 |
|
|
'query' => $query,
|
1298 |
|
|
)) . '" target="data" class="' . $success_msg . '">open</a>';
|
1299 |
|
|
}
|
1300 |
|
|
$message .= '] ';
|
1301 |
|
|
drupal_set_message(($message), 'debug');
|
1302 |
|
|
}
|
1303 |
|
|
|
1304 |
|
|
/**
|
1305 |
|
|
* @todo Please document this function.
|
1306 |
|
|
* @see http://drupal.org/node/1354
|
1307 |
|
|
*/
|
1308 |
|
|
function cdm_load_obj($datastr) {
|
1309 |
|
|
$obj = json_decode($datastr);
|
1310 |
|
|
|
1311 |
|
|
if (!(is_object($obj) || is_array($obj))) {
|
1312 |
|
|
ob_start();
|
1313 |
|
|
$obj_dump = ob_get_contents();
|
1314 |
|
|
ob_clean();
|
1315 |
|
|
return FALSE;
|
1316 |
|
|
}
|
1317 |
|
|
|
1318 |
|
|
return $obj;
|
1319 |
|
|
}
|
1320 |
|
|
|
1321 |
|
|
/**
|
1322 |
|
|
* Do a http request to a CDM webservice.
|
1323 |
|
|
*
|
1324 |
|
|
* @param string $uri
|
1325 |
|
|
* The webservice url.
|
1326 |
|
|
* @param string $method
|
1327 |
|
|
* The HTTP method to use, valid values are "GET" or "POST"; defaults to
|
1328 |
|
|
* "GET" even if NULL, FALSE or any invalid value is supplied.
|
1329 |
|
|
* @param array $parameters
|
1330 |
|
|
* Parameters to use in the request.
|
1331 |
|
|
* @param array $header
|
1332 |
|
|
* The header to include in the request.
|
1333 |
|
|
*
|
1334 |
|
|
* @return
|
1335 |
|
|
* The response data from the request.
|
1336 |
|
|
*/
|
1337 |
|
|
function cdm_http_request($uri, $method = "GET", $parameters = array(), $header = array()) {
|
1338 |
|
|
static $acceptLanguage = NULL;
|
1339 |
|
|
|
1340 |
|
|
if (!$acceptLanguage) {
|
1341 |
|
|
if (function_exists('apache_request_headers')) {
|
1342 |
|
|
$headers = apache_request_headers();
|
1343 |
|
|
if (isset($headers['Accept-Language'])) {
|
1344 |
|
|
$acceptLanguage = $headers['Accept-Language'];
|
1345 |
|
|
}
|
1346 |
|
|
}
|
1347 |
|
|
if (!$acceptLanguage) {
|
1348 |
|
|
// DEFAULT TODO make configurable.
|
1349 |
|
|
$acceptLanguage = "en";
|
1350 |
|
|
}
|
1351 |
|
|
}
|
1352 |
|
|
|
1353 |
|
|
if ($method != "GET" && $method != "POST") {
|
1354 |
|
|
$method = "GET";
|
1355 |
|
|
}
|
1356 |
|
|
|
1357 |
|
|
if (empty($header) && _is_cdm_ws_uri($uri)) {
|
1358 |
|
|
$header['Accept'] = (variable_get('cdm_webservice_type', 'json') == 'json' ? 'application/json' : 'text/xml');
|
1359 |
|
|
$header['Accept-Language'] = $acceptLanguage;
|
1360 |
|
|
$header['Accept-Charset'] = 'UTF-8';
|
1361 |
|
|
}
|
1362 |
|
|
|
1363 |
|
|
if (FALSE && function_exists('curl_init')) {
|
1364 |
|
|
// !!!!! CURL Disabled due to problems with following redirects
|
1365 |
|
|
// (CURLOPT_FOLLOWLOCATION=1) and safe_mode = on
|
1366 |
|
|
// Use the CURL lib if installed, it is supposed to be 20x faster.
|
1367 |
|
|
return _http_request_using_curl($uri, $header, $method, $parameters);
|
1368 |
|
|
}
|
1369 |
|
|
else {
|
1370 |
|
|
return _http_request_using_fsockopen($uri, $header, $method, $parameters);
|
1371 |
|
|
}
|
1372 |
|
|
}
|
1373 |
|
|
|
1374 |
|
|
/**
|
1375 |
|
|
* @todo Please document this function.
|
1376 |
|
|
* @see http://drupal.org/node/1354
|
1377 |
|
|
*/
|
1378 |
|
|
function _http_request_using_fsockopen($uri, $header = array(), $method = 'GET') {
|
1379 |
|
|
$response = drupal_http_request($uri, array('headers' => $header, 'method' => $method));
|
1380 |
|
|
if (isset($response->data)) {
|
1381 |
|
|
return $response->data;
|
1382 |
|
|
}
|
1383 |
|
|
}
|
1384 |
|
|
|
1385 |
|
|
/**
|
1386 |
|
|
* Return string content from a remote file.
|
1387 |
|
|
*
|
1388 |
|
|
* @author Luiz Miguel Axcar <lmaxcar@yahoo.com.br>
|
1389 |
|
|
*
|
1390 |
|
|
* @param string $uri
|
1391 |
|
|
* The url for which to return content.
|
1392 |
|
|
*
|
1393 |
|
|
* @return string
|
1394 |
|
|
* The returned content.
|
1395 |
|
|
*/
|
1396 |
|
|
function _http_request_using_curl($uri, $headers = array(), $method = "GET", $parameters = array()) {
|
1397 |
|
|
$ch = curl_init();
|
1398 |
|
|
|
1399 |
|
|
curl_setopt($ch, CURLOPT_URL, $uri);
|
1400 |
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
1401 |
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
|
1402 |
|
|
|
1403 |
|
|
// Set proxy settings.
|
1404 |
|
|
if (variable_get('cdm_webservice_proxy_url', FALSE)) {
|
1405 |
|
|
curl_setopt($ch, CURLOPT_PROXY, variable_get('cdm_webservice_proxy_url', ''));
|
1406 |
|
|
curl_setopt($ch, CURLOPT_PROXYPORT, variable_get('cdm_webservice_proxy_port', '80'));
|
1407 |
|
|
if (variable_get('cdm_webservice_proxy_usr', FALSE)) {
|
1408 |
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, variable_get('cdm_webservice_proxy_usr', '') . ':' . variable_get('cdm_webservice_proxy_pwd', ''));
|
1409 |
|
|
}
|
1410 |
|
|
}
|
1411 |
|
|
|
1412 |
|
|
// Modify headers array to be used by curl.
|
1413 |
|
|
foreach ($headers as $header_key => $header_val) {
|
1414 |
|
|
$curl_headers[] = $header_key . ': ' . $header_val;
|
1415 |
|
|
}
|
1416 |
|
|
if (isset($curl_headers)) {
|
1417 |
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
|
1418 |
|
|
}
|
1419 |
|
|
|
1420 |
|
|
// Set method if not default.
|
1421 |
|
|
if ($method != "GET") {
|
1422 |
|
|
if ($method == "POST") {
|
1423 |
|
|
|
1424 |
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
1425 |
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
|
1426 |
|
|
}
|
1427 |
|
|
else {
|
1428 |
|
|
// Other obscure http methods get passed to curl directly.
|
1429 |
|
|
// TODO generic parameter/body handling
|
1430 |
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
1431 |
|
|
}
|
1432 |
|
|
}
|
1433 |
|
|
|
1434 |
|
|
ob_start();
|
1435 |
|
|
curl_exec($ch);
|
1436 |
|
|
$info = curl_getinfo($ch);
|
1437 |
|
|
if (curl_errno($ch)) {
|
1438 |
|
|
watchdog('CDM_API', '_http_request_curl() - ' . curl_error($ch) . '; REQUEST-METHOD:' . $method . ' URL: ' . substr($uri . ' ', 0, 150), WATCHDOG_ERROR);
|
1439 |
|
|
if (variable_get('cdm_webservice_debug', 1) && user_access('administer')) {
|
1440 |
|
|
drupal_set_message(check_plain(t('_http_request_curl() - !curl_error; REQUEST-METHOD:!method URL: !url', array(
|
1441 |
|
|
'!curl_error' => curl_error($ch),
|
1442 |
|
|
'!method' => $method,
|
1443 |
|
|
'!url' => substr($uri . ' ', 0, 150),
|
1444 |
|
|
))), 'error');
|
1445 |
|
|
}
|
1446 |
|
|
}
|
1447 |
|
|
curl_close($ch);
|
1448 |
|
|
$string = ob_get_contents();
|
1449 |
|
|
ob_end_clean();
|
1450 |
|
|
|
1451 |
|
|
return $string;
|
1452 |
|
|
}
|
1453 |
|
|
|
1454 |
|
|
/**
|
1455 |
|
|
* @todo Please document this function.
|
1456 |
|
|
* @see http://drupal.org/node/1354
|
1457 |
|
|
*/
|
1458 |
|
|
function _featureTree_elements_toString($rootNode, $separator = ', ') {
|
1459 |
|
|
$out = '';
|
1460 |
|
|
|
1461 |
|
|
foreach ($rootNode->children as $featureNode) {
|
1462 |
|
|
$out .= ($out ? $separator : '');
|
1463 |
|
|
$out .= $featureNode->feature->representation_L10n;
|
1464 |
|
|
if (is_array($featureNode->children)) {
|
1465 |
|
|
$childlabels = '';
|
1466 |
|
|
foreach ($featureNode->children as $childNode) {
|
1467 |
|
|
$childlabels .= ($childlabels ? $separator : '');
|
1468 |
|
|
}
|
1469 |
|
|
$childlabels .= _featureTree_elements_toString($childNode);
|
1470 |
|
|
}
|
1471 |
|
|
if ($childlabels) {
|
1472 |
|
|
$out .= '[' . $childlabels . ' ]';
|
1473 |
|
|
}
|
1474 |
|
|
}
|
1475 |
|
|
return $out;
|
1476 |
|
|
}
|
1477 |
|
|
|
1478 |
|
|
/**
|
1479 |
|
|
* Create a one-dimensional form options array.
|
1480 |
|
|
*
|
1481 |
|
|
* Creates an array of all features in the feature tree of feature nodes,
|
1482 |
|
|
* the node labels are indented by $node_char and $childIndent depending on the
|
1483 |
|
|
* hierachy level.
|
1484 |
|
|
*
|
1485 |
|
|
* @param - $rootNode
|
1486 |
|
|
* @param - $node_char
|
1487 |
|
|
* @param - $childIndentStr
|
1488 |
|
|
* @param - $childIndent
|
1489 |
|
|
* ONLY USED INTERNALLY!
|
1490 |
|
|
*
|
1491 |
|
|
* @return array
|
1492 |
|
|
* A one dimensional Drupal form options array.
|
1493 |
|
|
*/
|
1494 |
|
|
function _featureTree_nodes_as_feature_options($rootNode, $node_char = "├─ ", $childIndentStr = ' ', $childIndent = '') {
|
1495 |
|
|
$options = array();
|
1496 |
|
|
foreach ($rootNode->children as $featureNode) {
|
1497 |
|
|
$indent_prefix = '';
|
1498 |
|
|
if ($childIndent) {
|
1499 |
|
|
$indent_prefix = $childIndent . $node_char . " ";
|
1500 |
|
|
}
|
1501 |
|
|
$options[$featureNode->feature->uuid] = $indent_prefix . $featureNode->feature->representation_L10n;
|
1502 |
|
|
if (isset($featureNode->children) && is_array($featureNode->children)) {
|
1503 |
|
|
// Foreach ($featureNode->children as $childNode){
|
1504 |
|
|
$childList = _featureTree_nodes_as_feature_options($featureNode, $node_char, $childIndentStr, $childIndent . $childIndentStr);
|
1505 |
|
|
$options = array_merge_recursive($options, $childList);
|
1506 |
|
|
// }
|
1507 |
|
|
}
|
1508 |
|
|
}
|
1509 |
|
|
return $options;
|
1510 |
|
|
}
|
1511 |
|
|
|
1512 |
|
|
/**
|
1513 |
|
|
* @todo Please document this function.
|
1514 |
|
|
* @see http://drupal.org/node/1354
|
1515 |
|
|
*/
|
1516 |
|
|
function cdm_get_featureTrees_as_options($addDefaultFeatureTree = FALSE) {
|
1517 |
|
|
$feature_trees = array();
|
1518 |
|
|
|
1519 |
|
|
// Set tree that contains all features.
|
1520 |
|
|
if ($addDefaultFeatureTree) {
|
1521 |
|
|
$feature_trees[UUID_DEFAULT_FEATURETREE] = t('Default Featuretree (contains all features)');
|
1522 |
|
|
}
|
1523 |
|
|
|
1524 |
|
|
// Get features from database.
|
1525 |
|
|
$persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
|
1526 |
|
|
if (is_array($persisted_trees)) {
|
1527 |
|
|
|
1528 |
|
|
foreach ($persisted_trees as $featureTree) {
|
1529 |
|
|
// Do not add the DEFAULT_FEATURETREE again,
|
1530 |
|
|
if ($featureTree->uuid == UUID_DEFAULT_FEATURETREE) {
|
1531 |
|
|
continue;
|
1532 |
|
|
}
|
1533 |
|
|
|
1534 |
|
|
// $treeRepresentation = $featureTree->titleCache;
|
1535 |
|
|
$treeRepresentation = '';
|
1536 |
|
|
if (is_array($featureTree->root->children) && count($featureTree->root->children) > 0) {
|
1537 |
|
|
|
1538 |
|
|
// Render the hierarchic tree structure.
|
1539 |
|
|
$treeDetails = '<div class="featuretree_structure">'
|
1540 |
|
|
// ._featureTree_elements_toString($featureTree->root)
|
1541 |
|
|
. theme('FeatureTree_hierarchy', array('FeatureTreeUuid' => $featureTree->uuid))
|
1542 |
|
|
. '</div>';
|
1543 |
|
|
|
1544 |
|
|
$form = array();
|
1545 |
|
|
$form['featureTree-' . $featureTree->uuid] = array(
|
1546 |
|
|
'#type' => 'fieldset',
|
1547 |
|
|
'#title' => 'Show details',
|
1548 |
|
|
'#attributes' => array('class' => array('collapsible collapsed')),
|
1549 |
|
|
// '#collapsible' => TRUE,
|
1550 |
|
|
// '#collapsed' => TRUE,
|
1551 |
|
|
);
|
1552 |
|
|
$form['featureTree-' . $featureTree->uuid]['details'] = array(
|
1553 |
|
|
'#markup' => $treeDetails,
|
1554 |
|
|
);
|
1555 |
|
|
// echo drupal_render($form);
|
1556 |
|
|
$treeRepresentation .= drupal_render($form);
|
1557 |
|
|
}
|
1558 |
|
|
$feature_trees[$featureTree->uuid] = $featureTree->titleCache;
|
1559 |
|
|
// $feature_trees[$featureTree->uuid] = $treeRepresentation;
|
1560 |
|
|
}
|
1561 |
|
|
}
|
1562 |
|
|
|
1563 |
|
|
// return $feature_trees;
|
1564 |
|
|
return array('options' => $feature_trees, 'treeRepresentations' => $treeRepresentation);
|
1565 |
|
|
}
|
1566 |
|
|
|
1567 |
|
|
/**
|
1568 |
|
|
* @todo Please document this function.
|
1569 |
|
|
* @see http://drupal.org/node/1354
|
1570 |
|
|
*/
|
1571 |
|
|
function cdm_get_taxontrees_as_options() {
|
1572 |
|
|
$taxonTrees = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY);
|
1573 |
|
|
$taxonomicTreeOptions = array();
|
1574 |
|
|
if ($taxonTrees) {
|
1575 |
|
|
foreach ($taxonTrees as $tree) {
|
1576 |
|
|
$taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
|
1577 |
|
|
}
|
1578 |
|
|
}
|
1579 |
|
|
return $taxonomicTreeOptions;
|
1580 |
|
|
}
|
1581 |
|
|
|
1582 |
|
|
/**
|
1583 |
|
|
* @todo Please document this function.
|
1584 |
|
|
* @see http://drupal.org/node/1354
|
1585 |
|
|
*/
|
1586 |
|
|
function cdm_api_secref_cache_prefetch(&$secUuids) {
|
1587 |
|
|
// Comment @WA: global variables should start with a single underscore
|
1588 |
|
|
// followed by the module and another underscore.
|
1589 |
|
|
global $_cdm_api_secref_cache;
|
1590 |
|
|
if (!is_array($_cdm_api_secref_cache)) {
|
1591 |
|
|
$_cdm_api_secref_cache = array();
|
1592 |
|
|
}
|
1593 |
|
|
$uniqueUuids = array_unique($secUuids);
|
1594 |
|
|
$i = 0;
|
1595 |
|
|
$param = '';
|
1596 |
|
|
while ($i++ < count($uniqueUuids)) {
|
1597 |
|
|
$param .= $secUuids[$i] . ',';
|
1598 |
|
|
if (strlen($param) + 37 > 2000) {
|
1599 |
|
|
_cdm_api_secref_cache_add($param);
|
1600 |
|
|
$param = '';
|
1601 |
|
|
}
|
1602 |
|
|
}
|
1603 |
|
|
if ($param) {
|
1604 |
|
|
_cdm_api_secref_cache_add($param);
|
1605 |
|
|
}
|
1606 |
|
|
}
|
1607 |
|
|
|
1608 |
|
|
/**
|
1609 |
|
|
* @todo Please document this function.
|
1610 |
|
|
* @see http://drupal.org/node/1354
|
1611 |
|
|
*/
|
1612 |
|
|
function cdm_api_secref_cache_get($secUuid) {
|
1613 |
|
|
global $_cdm_api_secref_cache;
|
1614 |
|
|
if (!is_array($_cdm_api_secref_cache)) {
|
1615 |
|
|
$_cdm_api_secref_cache = array();
|
1616 |
|
|
}
|
1617 |
|
|
if (!array_key_exists($secUuid, $_cdm_api_secref_cache)) {
|
1618 |
|
|
_cdm_api_secref_cache_add($secUuid);
|
1619 |
|
|
}
|
1620 |
|
|
return $_cdm_api_secref_cache[$secUuid];
|
1621 |
|
|
}
|
1622 |
|
|
|
1623 |
|
|
/**
|
1624 |
|
|
* @todo Please document this function.
|
1625 |
|
|
* @see http://drupal.org/node/1354
|
1626 |
|
|
*/
|
1627 |
|
|
function cdm_api_secref_cache_clear() {
|
1628 |
|
|
global $_cdm_api_secref_cache;
|
1629 |
|
|
$_cdm_api_secref_cache = array();
|
1630 |
|
|
}
|
1631 |
|
|
|
1632 |
|
|
/**
|
1633 |
|
|
* Validates if the given string is a uuid.
|
1634 |
|
|
*
|
1635 |
|
|
* @param string $str
|
1636 |
|
|
* The string to validate.
|
1637 |
|
|
*
|
1638 |
|
|
* return bool
|
1639 |
|
|
* TRUE if the string is a UUID.
|
1640 |
|
|
*/
|
1641 |
|
|
function is_uuid($str) {
|
1642 |
|
|
return is_string($str) && strlen($str) == 36 && strpos($str, '-');
|
1643 |
|
|
}
|
1644 |
|
|
|
1645 |
|
|
/**
|
1646 |
|
|
* Checks if the given $object is a valid cdm entity.
|
1647 |
|
|
*
|
1648 |
|
|
* An object is considered a cdm entity if it has a string field $object->class
|
1649 |
|
|
* with at least 3 characters and if it has a valid uuid in $object->uuid.
|
1650 |
|
|
*
|
1651 |
|
|
* @author a.kohlbecker <a.kohlbecker@bgbm.org>
|
1652 |
|
|
*
|
1653 |
|
|
* @param mixed $object
|
1654 |
|
|
* The object to validate
|
1655 |
|
|
*
|
1656 |
|
|
* @return bool
|
1657 |
|
|
* True if the object is a cdm entity.
|
1658 |
|
|
*/
|
1659 |
|
|
function is_cdm_entity($object) {
|
1660 |
|
|
return is_string($object->class) && strlen($object->class) > 2 && is_string($object->uuid) && is_uuid($object->uuid);
|
1661 |
|
|
}
|
1662 |
|
|
|
1663 |
|
|
/**
|
1664 |
|
|
* @todo Please document this function.
|
1665 |
|
|
* @see http://drupal.org/node/1354
|
1666 |
|
|
*/
|
1667 |
|
|
function _cdm_api_secref_cache_add($secUuidsStr) {
|
1668 |
|
|
global $_cdm_api_secref_cache;
|
1669 |
|
|
$ref = cdm_ws_get(CDM_WS_REFERENCE, $secUuidsStr);
|
1670 |
|
|
// Batch fetching not jet reimplemented thus:
|
1671 |
|
|
/*
|
1672 |
|
|
$assocRefSTOs = array(); if($refSTOs) { foreach($refSTOs as $ref){
|
1673 |
|
|
$assocRefSTOs[$ref->uuid] = $ref; } $_cdm_api_secref_cache =
|
1674 |
|
|
array_merge($_cdm_api_secref_cache, $assocRefSTOs); }
|
1675 |
|
|
*/
|
1676 |
|
|
$_cdm_api_secref_cache[$ref->uuid] = $ref;
|
1677 |
|
|
}
|
1678 |
|
|
|
1679 |
|
|
/**
|
1680 |
|
|
* Checks if the given uri starts with a cdm webservice url.
|
1681 |
|
|
*
|
1682 |
|
|
* Checks if the uri starts with the cdm webservice url stored in the
|
1683 |
|
|
* Drupal variable 'cdm_webservice_url'.
|
1684 |
|
|
* The 'cdm_webservice_url' can be set in the admins section of the portal.
|
1685 |
|
|
*
|
1686 |
|
|
* @param string $uri
|
1687 |
|
|
* The URI to test.
|
1688 |
|
|
*
|
1689 |
|
|
* @return bool
|
1690 |
|
|
* True if the uri starts with a cdm webservice url.
|
1691 |
|
|
*/
|
1692 |
|
|
function _is_cdm_ws_uri($uri) {
|
1693 |
|
|
return str_beginsWith($uri, variable_get('cdm_webservice_url', '#EMPTY#'));
|
1694 |
|
|
}
|
1695 |
|
|
|
1696 |
|
|
/**
|
1697 |
|
|
* @todo Please document this function.
|
1698 |
|
|
* @see http://drupal.org/node/1354
|
1699 |
|
|
*/
|
1700 |
|
|
function queryString($elements) {
|
1701 |
|
|
$query = '';
|
1702 |
|
|
foreach ($elements as $key => $value) {
|
1703 |
|
|
if (is_array($value)) {
|
1704 |
|
|
foreach ($value as $v) {
|
1705 |
|
|
$query .= (strlen($query) > 0 ? '&' : '') . $key . '=' . urlencode($v);
|
1706 |
|
|
}
|
1707 |
|
|
}
|
1708 |
|
|
else {
|
1709 |
|
|
$query .= (strlen($query) > 0 ? '&' : '') . $key . '=' . urlencode($value);
|
1710 |
|
|
}
|
1711 |
|
|
}
|
1712 |
|
|
return $query;
|
1713 |
|
|
}
|
1714 |
|
|
|
1715 |
|
|
/**
|
1716 |
|
|
* Implementation of the magic method __clone to allow deep cloning of objects
|
1717 |
|
|
* and arrays.
|
1718 |
|
|
*/
|
1719 |
|
|
function __clone() {
|
1720 |
|
|
foreach ($this as $name => $value) {
|
1721 |
|
|
if (gettype($value) == 'object' || gettype($value) == 'array') {
|
1722 |
|
|
$this->$name = clone($this->$name);
|
1723 |
|
|
}
|
1724 |
|
|
}
|
1725 |
|
|
}
|
1726 |
|
|
|
1727 |
|
|
/**
|
1728 |
|
|
* Make a 'deep copy' of an array.
|
1729 |
|
|
*
|
1730 |
|
|
* Make a complete deep copy of an array replacing
|
1731 |
|
|
* references with deep copies until a certain depth is reached
|
1732 |
|
|
* ($maxdepth) whereupon references are copied as-is...
|
1733 |
|
|
*
|
1734 |
|
|
* @see http://us3.php.net/manual/en/ref.array.php
|
1735 |
|
|
*
|
1736 |
|
|
* @param array $array
|
1737 |
|
|
* @param array $copy
|
1738 |
|
|
* @param int $maxdepth
|
1739 |
|
|
* @param int $depth
|
1740 |
|
|
*
|
1741 |
|
|
* @return void
|
1742 |
|
|
*/
|
1743 |
|
|
function array_deep_copy(&$array, &$copy, $maxdepth = 50, $depth = 0) {
|
1744 |
|
|
if ($depth > $maxdepth) {
|
1745 |
|
|
$copy = $array;
|
1746 |
|
|
return;
|
1747 |
|
|
}
|
1748 |
|
|
if (!is_array($copy)) {
|
1749 |
|
|
$copy = array();
|
1750 |
|
|
}
|
1751 |
|
|
foreach ($array as $k => &$v) {
|
1752 |
|
|
if (is_array($v)) {
|
1753 |
|
|
array_deep_copy($v, $copy[$k], $maxdepth, ++$depth);
|
1754 |
|
|
}
|
1755 |
|
|
else {
|
1756 |
|
|
$copy[$k] = $v;
|
1757 |
|
|
}
|
1758 |
|
|
}
|
1759 |
|
|
}
|
1760 |
|
|
|
1761 |
|
|
/**
|
1762 |
|
|
* Implementation of theme_status_messages($display = NULL).
|
1763 |
|
|
*
|
1764 |
|
|
* @see includes/theme.inc
|
1765 |
|
|
*
|
1766 |
|
|
* @return void
|
1767 |
|
|
*/
|
1768 |
|
|
function _add_status_message_toggler() {
|
1769 |
|
|
static $isAdded = FALSE;
|
1770 |
|
|
if (!$isAdded) {
|
1771 |
|
|
drupal_add_js(
|
1772 |
|
|
'jQuery(document).ready(function($){
|
1773 |
|
|
$(\'.messages.debug\').before( \'<h6 class="messages_toggler debug">Debug Messages (click to toggle)</h6>\' );
|
1774 |
|
|
$(\'.messages_toggler\').click(function(){
|
1775 |
|
|
$(this).next().slideToggle(\'fast\');
|
1776 |
7a2a14b3
|
Andreas Kohlbecker
|
return false;
|
1777 |
6657531f
|
Andreas Kohlbecker
|
}).next().hide();
|
1778 |
|
|
});', array('type' => 'inline'));
|
1779 |
|
|
|
1780 |
|
|
$isAdded = TRUE;
|
1781 |
|
|
}
|
1782 |
|
|
}
|
1783 |
|
|
|
1784 |
|
|
/**
|
1785 |
|
|
* @todo Please document this function.
|
1786 |
|
|
* @see http://drupal.org/node/1354
|
1787 |
|
|
*/
|
1788 |
|
|
function _no_classfication_uuid_message() {
|
1789 |
|
|
if (!cdm_ws_get(CDM_WS_PORTAL_TAXONOMY)) {
|
1790 |
|
|
return t('This DataPortal is not configured properly or the CDM-Server may be absent.') . ' Please check the ' . l(t('CDM web service URL'), 'admin/config/cdm_dataportal/settings/general') . t(', or contact the maintainer of this DataPortal.');
|
1791 |
|
|
}
|
1792 |
|
|
return t('This DataPortal is not configured properly.') . l(t('Please choose a valid classification'), 'admin/config/cdm_dataportal/settings/general') . t(', or contact the maintainer of this DataPortal.');
|
1793 |
|
|
}
|
1794 |
|
|
|
1795 |
|
|
/**
|
1796 |
|
|
* Implementation of hook flush_caches
|
1797 |
|
|
*
|
1798 |
|
|
* Add custom cache tables to the list of cache tables that
|
1799 |
|
|
* will be cleared by the Clear button on the Performance page or whenever
|
1800 |
|
|
* drupal_flush_all_caches is invoked.
|
1801 |
|
|
*
|
1802 |
|
|
* @author W.Addink <waddink@eti.uva.nl>
|
1803 |
|
|
*
|
1804 |
|
|
* @return array
|
1805 |
|
|
* An array with custom cache tables to include.
|
1806 |
|
|
*/
|
1807 |
|
|
function cdm_api_flush_caches() {
|
1808 |
|
|
return array('cache_cdm_ws');
|
1809 |
|
|
}
|