1
|
<?php
|
2
|
// $Id$
|
3
|
|
4
|
/**
|
5
|
* @file
|
6
|
* Functions which are required or useful when accessing and processing CDM Data Store Webservices
|
7
|
*
|
8
|
* Naming conventions:
|
9
|
* ----------------------
|
10
|
*
|
11
|
* - all webservice access methods are prefixed with cdm_ws
|
12
|
*
|
13
|
*
|
14
|
* Copyright (C) 2007 EDIT
|
15
|
* European Distributed Institute of Taxonomy
|
16
|
* http://www.e-taxonomy.eu
|
17
|
*/
|
18
|
require_once ('xml2json.php');
|
19
|
require_once ('commons.php');
|
20
|
require_once ('uuids.php');
|
21
|
require_once ('cdm_node.php');
|
22
|
|
23
|
define(DEFAULT_TAXONTREE_RANKLIMIT, '');//TODO Genus UUID
|
24
|
|
25
|
|
26
|
/**
|
27
|
* Implementation of hook_requirements()
|
28
|
*/
|
29
|
function cdm_api_requirements() {
|
30
|
|
31
|
$requirements['cdm_api'] = array(
|
32
|
'title' => t('CDM API')
|
33
|
);
|
34
|
|
35
|
if( function_exists('curl_init') ){
|
36
|
$requirements['cdm_api']['description'] = ''; // description below title is not jet in use
|
37
|
$requirements['cdm_api']['value'] = 'CURL php extension is available and will be used by the cdm api. HTTP requests thus will be up to 20x faster';
|
38
|
} else {
|
39
|
$requirements['cdm_api']['value'] = 'CURL php extension is missing. If CURL lib is installed HTTP requests will be up to 20x faster';
|
40
|
}
|
41
|
|
42
|
//FIXME: once _get_content_fsockopen is implemented change severity to REQUIREMENT_WARNING,
|
43
|
$requirements['cdm_api']['severity'] = (function_exists('curl_init') ? REQUIREMENT_OK : REQUIREMENT_INFO);
|
44
|
|
45
|
return $requirements;
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* Implementation of hook_menu()
|
51
|
*/
|
52
|
function cdm_api_menu($may_cache) {
|
53
|
$items = array();
|
54
|
if ($may_cache) {
|
55
|
$items[] = array(
|
56
|
// usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");
|
57
|
'path' => 'cdm_api/proxy',
|
58
|
'callback' => 'proxy_content',
|
59
|
'access' => true,
|
60
|
'type' => MENU_CALLBACK,
|
61
|
);
|
62
|
|
63
|
$items[] = array(
|
64
|
// usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");
|
65
|
'path' => 'cdm_api/setvalue/session',
|
66
|
'callback' => 'setvalue_session',
|
67
|
'access' => true,
|
68
|
'type' => MENU_CALLBACK,
|
69
|
);
|
70
|
|
71
|
}
|
72
|
|
73
|
return $items;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Configures the settings form for the CDM-API module.
|
78
|
*
|
79
|
* @return Array Drupal settings form
|
80
|
*/
|
81
|
function cdm_api_settings_form(){
|
82
|
|
83
|
$form['cdm_webservice'] = array(
|
84
|
'#type' => 'fieldset',
|
85
|
'#title' => t('CDM Web Service'),
|
86
|
'#collapsible' => FALSE,
|
87
|
'#collapsed' => FALSE,
|
88
|
);
|
89
|
|
90
|
$form['cdm_webservice']['cdm_webservice_url'] = array(
|
91
|
'#type' => 'textfield',
|
92
|
'#title' => t('CDM Web Service URL'),
|
93
|
'#description' => t('The URL of CDM Webservice which delivers the data to be published.'),
|
94
|
'#default_value' => variable_get('cdm_webservice_url', NULL),
|
95
|
);
|
96
|
|
97
|
$form['cdm_webservice']['taxontree_ranklimit'] = array(
|
98
|
'#type' => 'select',
|
99
|
'#title' => t('Rank of highest displayed taxon'),
|
100
|
'#default_value' => variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT_UUID),
|
101
|
'#options' => cdm_rankVocabulary_as_option(),
|
102
|
'#description' => t('The rank of the highest displayed taxon in the taxontree.'),
|
103
|
);
|
104
|
|
105
|
$form['cdm_webservice']['cdm_webservice_cache'] = array(
|
106
|
'#type' => 'checkbox',
|
107
|
'#title' => t('Enable Caching'),
|
108
|
'#default_value' => variable_get('cdm_webservice_cache', 1),
|
109
|
'#description' => t('Enable caching of webservice responses on simple requests, '
|
110
|
.'that is requests which only have one parameter generally a UUID or a concatenation of UUIDs')
|
111
|
);
|
112
|
|
113
|
$form['cdm_webservice']['proxy'] = array(
|
114
|
'#type' => 'fieldset',
|
115
|
'#title' => t('Proxy'),
|
116
|
'#collapsible' => TRUE,
|
117
|
'#collapsed' => TRUE
|
118
|
);
|
119
|
|
120
|
$form['cdm_webservice']['proxy']['cdm_webservice_proxy_url'] = array(
|
121
|
'#type' => 'textfield',
|
122
|
'#title' => t('Proxy URL'),
|
123
|
'#description' => t('If this proxy url is set the cdm api tries
|
124
|
to connect the web service over the given proxy server.
|
125
|
Otherwise proxy usage is deactivated.'),
|
126
|
'#default_value' => variable_get('cdm_webservice_proxy_url', false),
|
127
|
);
|
128
|
|
129
|
$form['cdm_webservice']['proxy']['cdm_webservice_proxy_port'] = array(
|
130
|
'#type' => 'textfield',
|
131
|
'#title' => t('Proxy Port'),
|
132
|
'#default_value' => variable_get('cdm_webservice_proxy_port', '80'),
|
133
|
);
|
134
|
|
135
|
$form['cdm_webservice']['proxy']['cdm_webservice_proxy_usr'] = array(
|
136
|
'#type' => 'textfield',
|
137
|
'#title' => t('Login'),
|
138
|
'#default_value' => variable_get('cdm_webservice_proxy_usr', false),
|
139
|
);
|
140
|
|
141
|
$form['cdm_webservice']['proxy']['cdm_webservice_proxy_pwd'] = array(
|
142
|
'#type' => 'textfield',
|
143
|
'#title' => t('Password'),
|
144
|
'#default_value' => variable_get('cdm_webservice_proxy_pwd', false),
|
145
|
);
|
146
|
|
147
|
$form['cdm_webservice']['cdm_webservice_debug'] = array(
|
148
|
'#type' => 'checkbox',
|
149
|
'#title' => t('Debug CDM Web Service'),
|
150
|
'#default_value' => variable_get('cdm_webservice_debug', 1),
|
151
|
'#description' => t('Enable CDM Web Service debugging messages. Only visible for the super administrator or for users having the permission <em>administer cdm_api</em>!')
|
152
|
);
|
153
|
|
154
|
return $form;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* Implementation of hook_cron().
|
159
|
*
|
160
|
* Expire outdated cache entries
|
161
|
*/
|
162
|
function cdm_api_cron() {
|
163
|
cache_clear_all(NULL, 'cache_cdm_ws');
|
164
|
}
|
165
|
|
166
|
function cdm_api_perm() {
|
167
|
return array(
|
168
|
'administer cdm_api'
|
169
|
);
|
170
|
}
|
171
|
|
172
|
// ----------------------------------------------------------- //
|
173
|
|
174
|
|
175
|
/**
|
176
|
* Converts an array of TagedText items into a sequence of corresponding html tags whereas
|
177
|
* each item will provided with a class attribute which set to the key of the TaggedText item.
|
178
|
*
|
179
|
* @param array $taggedtxt
|
180
|
* @param String $tag
|
181
|
* @param String $glue the string by which the chained text tokens are concatenated together.
|
182
|
* Default is a blank character
|
183
|
* @return String of HTML
|
184
|
*/
|
185
|
function cdm_taggedtext2html(array &$taggedtxt, $tag = 'span', $glue = ' ', $skiptags = array()){
|
186
|
$out = '';
|
187
|
$i = 0;
|
188
|
foreach($taggedtxt as $tt){
|
189
|
if(!in_array($tt->type, $skiptags) && strlen($tt->text) > 0){
|
190
|
$out .= (strlen($out) > 0 && ++$i < count($taggedtxt)? $glue : '').'<'.$tag.' class="'.$tt->type.'">'.t($tt->text).'</'.$tag.'>';
|
191
|
}
|
192
|
}
|
193
|
return $out;
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Finds the text tagged with $$tag_type in an array of taggedText instances
|
198
|
*
|
199
|
* @param array $taggedtxt
|
200
|
* @param string $tag_type
|
201
|
* @return the text mapped by $tag_type or an empty string
|
202
|
*/
|
203
|
function cdm_taggedtext_value(array &$taggedtxt = array(), $tag_type){
|
204
|
foreach($taggedtxt as $tagtxt){
|
205
|
if($tagtxt->type == $tag_type)
|
206
|
return $tagtxt->text;
|
207
|
}
|
208
|
return '';
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* media Array [4]
|
213
|
* representations Array [3]
|
214
|
* mimeType image/jpeg
|
215
|
* representationParts Array [1]
|
216
|
* duration 0
|
217
|
* heigth 0
|
218
|
* size 0
|
219
|
* uri http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/jpeg/Acanthocephalus_p1.jpg
|
220
|
* uuid 15c687f1-f79d-4b79-992f-7ba0f55e610b
|
221
|
* width 0
|
222
|
* suffix jpg
|
223
|
* uuid 930b7d51-e7b6-4350-b21e-8124b14fe29b
|
224
|
* title
|
225
|
* uuid 17e514f1-7a8e-4daa-87ea-8f13f8742cf9
|
226
|
*
|
227
|
* @param unknown_type $media
|
228
|
* @param array $mimeTypes
|
229
|
* @param unknown_type $width
|
230
|
* @param unknown_type $height
|
231
|
* @return unknown
|
232
|
*/
|
233
|
function cdm_preferred_media_representations($media, array $mimeTypes, $width = 400, $height = 300){
|
234
|
/**
|
235
|
|
236
|
*
|
237
|
*/
|
238
|
$prefRepr = array();
|
239
|
if(!isset($media->representations[0])){
|
240
|
return $prefRepr;
|
241
|
}
|
242
|
|
243
|
while(count($mimeTypes) > 0){
|
244
|
// getRepresentationByMimeType
|
245
|
$mimeType = array_shift($mimeTypes);
|
246
|
foreach($media->representations as $representation){
|
247
|
if($representation->mimeType == $mimeType){
|
248
|
// preffered mimetype found -> erase all remaining mimetypes to end loop
|
249
|
$mimeTypes = array();
|
250
|
$dwa = 0;
|
251
|
// look for part with the best matching size
|
252
|
foreach($representation->parts as $part){
|
253
|
$dw = $part->width * $part->height - $height * $width;
|
254
|
if($dw < 0){
|
255
|
$dw *= -1;
|
256
|
}
|
257
|
$dwa+= $dw;
|
258
|
}
|
259
|
$dwa = (count($representation->parts)>0) ? $dwa / count($representation->parts) : 0;
|
260
|
$prefRepr[$dwa.'_'.$mimeTypeKey] = $representation;
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
// sort
|
265
|
krsort($prefRepr);
|
266
|
// return
|
267
|
return $prefRepr;
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* expects an ISO 8601 time representations of a org.joda.time.Partial
|
272
|
* of the form yyyy-MM-dd and returns the year as String.
|
273
|
* In case the year is unknown (= ????) null is returned.
|
274
|
*
|
275
|
* @param ISO 8601 time representations of a org.joda.time.Partial
|
276
|
* @return String
|
277
|
*/
|
278
|
function partialToYear($partial){
|
279
|
if(is_string($partial)){
|
280
|
$year = substr($partial, 0, 4);
|
281
|
if($year != '??'){
|
282
|
return $year;
|
283
|
}
|
284
|
}
|
285
|
return;
|
286
|
}
|
287
|
/**
|
288
|
* expects an ISO 8601 time representations of a org.joda.time.Partial
|
289
|
* of the form yyyy-MM-dd and returns the month as String.
|
290
|
* In case the month is unknown (= ???) null is returned.
|
291
|
*
|
292
|
* @param ISO 8601 time representations of a org.joda.time.Partial
|
293
|
* @return String
|
294
|
*/
|
295
|
function partialToMonth($partial){
|
296
|
if(is_string($partial)){
|
297
|
$month = substr($partial, 5, 2);
|
298
|
if($month != '??'){
|
299
|
return $month;
|
300
|
}
|
301
|
}
|
302
|
return;
|
303
|
}
|
304
|
/**
|
305
|
* expects an ISO 8601 time representations of a org.joda.time.Partial
|
306
|
* of the form yyyy-MM-dd and returns the day as String.
|
307
|
* In case the day is unknown (= ???) null is returned.
|
308
|
*
|
309
|
* @param ISO 8601 time representations of a org.joda.time.Partial
|
310
|
* @return String
|
311
|
*/
|
312
|
function partialToDay($partial){
|
313
|
if(is_string($partial)){
|
314
|
$day = substr($partial, 7, 2);
|
315
|
if($day != '??'){
|
316
|
return $day;
|
317
|
}
|
318
|
}
|
319
|
return;
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
*
|
324
|
* @param $uri_pattern
|
325
|
* @param $pathParameters an array of path elements, or a single element
|
326
|
* @param $query A query string to append to the URL.
|
327
|
* @return unknown_type
|
328
|
*/
|
329
|
function cdm_compose_url($uri_pattern, $pathParameters = array(), $query = NULL ){
|
330
|
|
331
|
$request_params = '';
|
332
|
$path_params = '';
|
333
|
|
334
|
/* (1)
|
335
|
* substitute all place holders ($0, $1, ..) in the
|
336
|
* $uri_pattern by the according element of the $pathParameters array
|
337
|
*/
|
338
|
static $helperArray = array();
|
339
|
if($pathParameters && !is_array($pathParameters)){
|
340
|
$helperArray[0] = $pathParameters;
|
341
|
$pathParameters = $helperArray;
|
342
|
}
|
343
|
|
344
|
$i = 0;
|
345
|
while(strpos($uri_pattern, "$".$i) !== FALSE){
|
346
|
if(count($pathParameters) <= $i){
|
347
|
drupal_set_message('cdm_compose_url(): missing pathParameters', 'debug');
|
348
|
}
|
349
|
$uri_pattern = str_replace("$".$i, rawurlencode($pathParameters[$i]), $uri_pattern);
|
350
|
++$i;
|
351
|
}
|
352
|
|
353
|
/* (2)
|
354
|
* Append all remaining element of the $pathParameters array as path elements
|
355
|
*/
|
356
|
if(count($pathParameters) > $i){
|
357
|
// strip trailing slashes
|
358
|
if(strrchr($uri_pattern, '/') == strlen($uri_pattern)){
|
359
|
$uri_pattern = substr($uri_pattern, 0, strlen($uri_pattern) - 1);
|
360
|
}
|
361
|
while(count($pathParameters) > $i){
|
362
|
$uri_pattern .= '/' . $pathParameters[$i];
|
363
|
++$i;
|
364
|
}
|
365
|
}
|
366
|
|
367
|
/* (3)
|
368
|
* Append the query string supplied by $query
|
369
|
*/
|
370
|
if (isset($query)) {
|
371
|
$uri_pattern .= (strpos($uri_pattern, '?') !== FALSE ? '&' : '?') . $query;
|
372
|
}
|
373
|
|
374
|
$path = $ws_name.$uri_pattern;
|
375
|
|
376
|
$uri = variable_get('cdm_webservice_url', '').$path;
|
377
|
return $uri;
|
378
|
}
|
379
|
|
380
|
|
381
|
function proxy_content($uri, $theme = null){
|
382
|
|
383
|
$args = func_get_args();
|
384
|
|
385
|
$uriEncoded = array_shift($args);
|
386
|
$uri = urldecode($uriEncoded);
|
387
|
$theme = array_shift($args);
|
388
|
|
389
|
//find comma sepatated string in all args
|
390
|
foreach($args as &$arg){
|
391
|
if(strpos($arg, ',')){
|
392
|
$arg = explode(',', $arg);
|
393
|
}
|
394
|
}
|
395
|
|
396
|
$request_method = strtoupper($_SERVER["REQUEST_METHOD"]);
|
397
|
|
398
|
if($request_method == "POST"){
|
399
|
|
400
|
$parameters = $_POST;
|
401
|
|
402
|
$post_data = array();
|
403
|
foreach ($parameters as $k=>$v)
|
404
|
{
|
405
|
$post_data[] = "$k=".utf8_encode($v);
|
406
|
}
|
407
|
$post_data = implode(',', $post_data);
|
408
|
|
409
|
// testing
|
410
|
$data = cdm_http_request($uri, "POST", $post_data);
|
411
|
print $data;
|
412
|
|
413
|
}else if(strpos($theme, '/') > 0){ // must be a mimetype
|
414
|
header('Content-Type: '.$theme);
|
415
|
$data = _http_request_binary($uri);
|
416
|
print $data;
|
417
|
exit;
|
418
|
} else {
|
419
|
// in all other cases perform a simple get request
|
420
|
//TODO reconsider caching logic in this function
|
421
|
if(!$theme){
|
422
|
// print out JSON, the cache cannot be used since it contains objects
|
423
|
$data = cdm_http_request($uri);
|
424
|
print $data;
|
425
|
exit;
|
426
|
} else {
|
427
|
$obj = cdm_ws_get($uri, null, null, null, TRUE);
|
428
|
array_unshift($args, $theme, $obj);
|
429
|
print call_user_func_array('theme', $args);
|
430
|
}
|
431
|
}
|
432
|
}
|
433
|
|
434
|
function setvalue_session(){
|
435
|
|
436
|
if(strlen(arg(3)) > 0){
|
437
|
$keys = explode('|', arg(3));
|
438
|
}
|
439
|
$val = arg(4);
|
440
|
|
441
|
// prevent from malicous tags
|
442
|
$val = strip_tags($val);
|
443
|
|
444
|
$var = &$_SESSION;
|
445
|
$i = 0;
|
446
|
foreach($keys as $key){
|
447
|
$hasMoreKeys = ++$i < count($var);
|
448
|
if($hasMoreKeys && (!isset($var[$key]) || !is_array($var[$key]))){
|
449
|
$var[$key] = array();
|
450
|
}
|
451
|
$var = &$var[$key];
|
452
|
}
|
453
|
$var = $val;
|
454
|
}
|
455
|
|
456
|
function uri_uriByProxy($uri, $theme = false){
|
457
|
// usage: url('cdm_api/proxy/'.urlencode($content_url)."/$theme");)
|
458
|
return url('cdm_api/proxy/'.urlencode($uri).($theme?"/$theme":''));
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Enter description here...
|
463
|
*
|
464
|
* @param String $resourceURI
|
465
|
* @param pageSize
|
466
|
* the maximum number of entities returned per page (can be null
|
467
|
* to return all entities in a single page)
|
468
|
* @param pageNumber
|
469
|
* the number of the page to be returned, the first page has the
|
470
|
* pageNumber = 1
|
471
|
* @return unknown
|
472
|
*/
|
473
|
function cdm_ws_page($resourceURI, $pageSize, $pageNumber){
|
474
|
return cdm_ws_get($resourceURI, null, queryString(array("page" => $pageNumber, 'pageSize'=>$pageSize)));
|
475
|
}
|
476
|
|
477
|
function cdm_ws_taxonomy_find($uuid, $rank = null, $viewUuid = null){
|
478
|
return cdm_ws_get(CDM_WS_TAXONOMY, null, queryString(array("uuid" => $uuid, 'rank'=>$rank)));
|
479
|
}
|
480
|
|
481
|
/**
|
482
|
* Enter description here...
|
483
|
*
|
484
|
* @param unknown_type $secUuid
|
485
|
* @param unknown_type $path
|
486
|
* @return unknown
|
487
|
*/
|
488
|
function cdm_ws_taxonomy($path = null){
|
489
|
return cdm_ws_get(cdm_ws_taxonomy_compose_resourcePath($path));
|
490
|
}
|
491
|
|
492
|
function cdm_ws_taxonomy_compose_resourcePath($path = null){
|
493
|
$viewrank = _cdm_taxonomy_compose_viewrank();
|
494
|
return CDM_WS_TAXONOMY . '/' . ($viewrank ? $viewrank : '' ) . ($path ? '/' . $path : '') ;
|
495
|
}
|
496
|
|
497
|
/**
|
498
|
* Enter description here...
|
499
|
*
|
500
|
* @param UUID $secUuid
|
501
|
* @param String $path
|
502
|
* @return unknown
|
503
|
*/
|
504
|
function cdm_ws_taxonomy_pathFromRoot($path){
|
505
|
$viewrank = _cdm_taxonomy_compose_viewrank();
|
506
|
return cdm_ws_get(CDM_WS_TAXONOMY . ($viewrank ? '/' .$viewrank : '' ) . '/' . $path . '/path' );
|
507
|
}
|
508
|
|
509
|
/**
|
510
|
* Enter description here...
|
511
|
*
|
512
|
* @param UUID $viewUuid
|
513
|
* @param String $rank
|
514
|
* @return unknown
|
515
|
*/
|
516
|
function _cdm_taxonomy_compose_viewrank(){
|
517
|
$viewUuid = variable_get('cdm_taxonomictree_uuid', false);
|
518
|
if(!$viewUuid){
|
519
|
return;
|
520
|
}
|
521
|
$rank = variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT);
|
522
|
return $viewUuid . (empty($rank) ? '' : ','.$rank);
|
523
|
}
|
524
|
|
525
|
|
526
|
function cdm_rankVocabulary_as_option(){
|
527
|
global $rankVocabularyOptions;
|
528
|
if(!$rankVocabularyOptions){
|
529
|
$vocab = cdm_ws_get(CDM_WS_TERMVOCABULARY, UUID_RANK);
|
530
|
$rankVocabularyOptions = array();
|
531
|
foreach($vocab->terms as $term){
|
532
|
$rankVocabularyOptions[$term->uuid] = t($term->representation_L10n);
|
533
|
}
|
534
|
array_reverse($rankVocabularyOptions);
|
535
|
}
|
536
|
return $rankVocabularyOptions;
|
537
|
}
|
538
|
|
539
|
|
540
|
function cdm_ws_descriptions_by_featuretree($featureTree, $descriptions, $isDescriptionsSeparated = false){
|
541
|
|
542
|
if(!$featureTree){
|
543
|
drupal_set_message('cdm_ws_descriptions_by_featuretree() - No feature supplied', 'error');
|
544
|
return false;
|
545
|
}
|
546
|
|
547
|
$mergedTrees = array();
|
548
|
|
549
|
if($isDescriptionsSeparated){
|
550
|
// merge any description into a sparate feature tree
|
551
|
foreach($descriptions as $desc){
|
552
|
$mergedNodes = _mergeFeatureTreeDesciptions($featureTree->root->children, $desc->elements);
|
553
|
|
554
|
$mergedTree = clone $featureTree;
|
555
|
$mergedTree->root->children = $mergedNodes;
|
556
|
$mergedTrees[] = $mergedTree;
|
557
|
}
|
558
|
} else {
|
559
|
// combine all descripions into one feature tree
|
560
|
foreach($descriptions as $desc){
|
561
|
$mergedNodes = _mergeFeatureTreeDesciptions($featureTree->root->children, $desc->elements);
|
562
|
$featureTree->root->children = $mergedNodes;
|
563
|
}
|
564
|
$mergedTrees[] = $featureTree;
|
565
|
}
|
566
|
|
567
|
return $mergedTrees;
|
568
|
}
|
569
|
|
570
|
function _mergeFeatureTreeDesciptions($featureNodes, $descriptionElements){
|
571
|
|
572
|
foreach($featureNodes as &$node){
|
573
|
|
574
|
// append corresponding elements to an additional node field: $node->descriptionElements
|
575
|
foreach($descriptionElements as $element){
|
576
|
if($element->feature->uuid == $node->feature->uuid){
|
577
|
if(!isset($node->descriptionElements)){
|
578
|
$node->descriptionElements = array();
|
579
|
}
|
580
|
$node->descriptionElements[] = $element;
|
581
|
}
|
582
|
}
|
583
|
|
584
|
// recurse into node children
|
585
|
if(is_array($node->children)){
|
586
|
foreach($node->children as $nodes){
|
587
|
$mergedChildNodes = _mergeFeatureTreeDesciptions($nodes, $descriptionElements);
|
588
|
$node->children = $mergedChildNodes;
|
589
|
}
|
590
|
}
|
591
|
|
592
|
}
|
593
|
return $featureNodes;
|
594
|
}
|
595
|
|
596
|
|
597
|
/**
|
598
|
* Send a HTTP GET request to the RESTService and deserializes
|
599
|
* and returns the response as object.
|
600
|
* The response objects coming from the webservice configured in the 'cdm_webservice_url' variable
|
601
|
* are beeing cached in a level 1 (L1) and or in a level 2 (L2) cache.
|
602
|
*
|
603
|
* Since the L1 cache is implemented as static variable of the cdm_ws_get() function,
|
604
|
* this cache persists only per each single page executiuon. Any object coming from the webservice is stored into it by default.
|
605
|
* Incontrast to this default cacheich mechanism the L2 cache only is used if the 'cdm_webservice_cache' varialby is set to TRUE
|
606
|
* which can be set using the modules administrative settings section. Object stored in this L2 cache are serialized and stored
|
607
|
* using the drupal cache in the '{prefix}cache_cdm_ws' cache table. So the objects are sored in a database will persist as
|
608
|
* log as the drupal cache is not beeing cleared and are availabel across multiple sript executions.
|
609
|
*
|
610
|
* @param $uri
|
611
|
* @param $pathParameters
|
612
|
* @param $query
|
613
|
* @param $method the HTTP method to use, valuid values are "GET" or "POST";
|
614
|
* @param $absoluteURI
|
615
|
* @return unknown_type
|
616
|
*/
|
617
|
function cdm_ws_get($uri, $pathParameters = array(), $query = null, $method="GET", $absoluteURI = false){
|
618
|
|
619
|
static $cacheL1;
|
620
|
if(!isset($cacheL1)){
|
621
|
$cacheL1 = array();
|
622
|
}
|
623
|
|
624
|
// transform the given uri path or patthern into a proper webservice uri
|
625
|
if(!$absoluteURI){
|
626
|
$uri = cdm_compose_url($uri, $pathParameters, $query);
|
627
|
}
|
628
|
|
629
|
$is_cdm_ws_uri = str_beginsWith($uri, variable_get('cdm_webservice_url', '#EMPTY#'));
|
630
|
$use_cacheL2 = variable_get('cdm_webservice_cache', 1);
|
631
|
|
632
|
$cacheL1_obj = $cacheL1[$uri];
|
633
|
//print $cacheL1_obj;
|
634
|
$set_cacheL1 = false;
|
635
|
if($is_cdm_ws_uri && !$cacheL1_obj){
|
636
|
$set_cacheL1 = true;
|
637
|
}
|
638
|
|
639
|
// only cache cdm webservice URIs
|
640
|
$set_cacheL2 = $use_cacheL2 && $is_cdm_ws_uri && $set_cacheL1;
|
641
|
$cacheL2_entry = false;
|
642
|
|
643
|
if($use_cacheL2){
|
644
|
// try to get object from cacheL2
|
645
|
$cacheL2_entry = cache_get($uri, 'cache_cdm_ws');
|
646
|
}
|
647
|
|
648
|
if($cacheL1_obj){
|
649
|
//
|
650
|
// The object has been found in the L1 cache
|
651
|
//
|
652
|
$obj = $cacheL1_obj;
|
653
|
_add_debugMessageStr('Using cacheL1 for: '.$uri);
|
654
|
} else if($cacheL2_entry) {
|
655
|
//
|
656
|
// The object has been found in the L2 cache
|
657
|
//
|
658
|
$obj = unserialize($cacheL2_entry->data);
|
659
|
if(variable_get('cdm_webservice_debug', 1) && user_access('administer')){
|
660
|
_add_status_message_toggler();
|
661
|
_add_debugMessageStr('Using cacheL2 for: '.$uri);
|
662
|
}
|
663
|
} else {
|
664
|
//
|
665
|
// Get the object from the webservice and cache it
|
666
|
//
|
667
|
$time_get_start = microtime(true);
|
668
|
// request data from webservice JSON or XML
|
669
|
$datastr = cdm_http_request($uri, $method);
|
670
|
$time_get = microtime(true) - $time_get_start;
|
671
|
|
672
|
$time_parse_start = microtime(true);
|
673
|
// parse data and create object
|
674
|
$obj = cdm_load_obj($datastr);
|
675
|
|
676
|
$time_parse = microtime(true) - $time_parse_start;
|
677
|
if(variable_get('cdm_webservice_debug', 1) && user_access('administer')){
|
678
|
$success_msg = $obj || $datastr == "[]" ? 'valid':'invalid';
|
679
|
_add_debugMessage($uri, $time_get, $time_parse, strlen($datastr), $success_msg);
|
680
|
}
|
681
|
if($set_cacheL2) {
|
682
|
// store the object in cacheL2
|
683
|
cache_set($uri, 'cache_cdm_ws', serialize($obj), CACHE_TEMPORARY);
|
684
|
}
|
685
|
|
686
|
}
|
687
|
if($obj){
|
688
|
// store the object in cacheL1
|
689
|
if($set_cacheL1) {
|
690
|
$cacheL1[$uri] = $obj;
|
691
|
}
|
692
|
}
|
693
|
|
694
|
return $obj;
|
695
|
}
|
696
|
function _add_debugMessageStr($msg){
|
697
|
_add_status_message_toggler();
|
698
|
drupal_set_message($message, 'debug');
|
699
|
|
700
|
}
|
701
|
function _add_debugMessage($uri, $time_get, $time_parse, $datasize, $success_msg){
|
702
|
|
703
|
static $cummulated_time_parse;
|
704
|
static $cummulated_time_get;
|
705
|
_add_status_message_toggler();
|
706
|
|
707
|
$cummulated_time_get += $time_get;
|
708
|
$cummulated_time_parse += $time_parse;
|
709
|
|
710
|
// decompose uri into path and query element
|
711
|
$uri_parts = explode("?", $uri);
|
712
|
if(count($uri_parts) == 2){
|
713
|
$path = $uri_parts[0];
|
714
|
$query = $uri_parts[1];
|
715
|
} else {
|
716
|
$path = $uri;
|
717
|
}
|
718
|
|
719
|
$message = '<span class="uri">'.$uri.'</span><br />';
|
720
|
$message .= '[fetched in: '.sprintf('%3.3f', $time_get).'s('.sprintf('%3.3f', $cummulated_time_get).'s); ';
|
721
|
$message .= 'parsed in '.sprintf('%3.3f', $time_parse).' s('.sprintf('%3.3f', $cummulated_time_parse).'s); ';
|
722
|
$message .= 'size:'.sprintf('%3.1f', ($datasize / 1024)).' kb of '.$success_msg.' data: ';
|
723
|
$message .= '<a href="'.url($path.'.xml', $query).'" target="data" class="'.$success_msg.'">xml</a>,';
|
724
|
$message .= '<a href="'.url($path.'.json', $query).'" target="data" class="'.$success_msg.'">json</a>';
|
725
|
$message .= '] ';
|
726
|
drupal_set_message($message, 'debug');
|
727
|
|
728
|
}
|
729
|
|
730
|
|
731
|
function cdm_load_obj($datastr){
|
732
|
|
733
|
// NOTICE: the cdm dataportal currently does not support xml so the line below are commented out
|
734
|
/*
|
735
|
// if the web service delivers XML convert it into json
|
736
|
if(variable_get('cdm_webservice_type', 'json') == 'xml'){
|
737
|
$datastr = xml2json::transformXmlStringToJson($datastr);
|
738
|
}
|
739
|
*/
|
740
|
|
741
|
$obj = json_decode($datastr);
|
742
|
|
743
|
if(!(is_object($obj) || is_array($obj)) ){
|
744
|
ob_start();
|
745
|
$obj_dump = ob_get_contents();
|
746
|
ob_clean();
|
747
|
return false;
|
748
|
}
|
749
|
|
750
|
return $obj;
|
751
|
}
|
752
|
|
753
|
/**
|
754
|
*
|
755
|
* @param $uri
|
756
|
* @param $method the HTTP method to use, valuid values are "GET" or "POST"; efaults to "GET" even if null,
|
757
|
* false or any invalid value is supplied.
|
758
|
* @param $parameters
|
759
|
* @param $header
|
760
|
* @return unknown_type
|
761
|
*/
|
762
|
function cdm_http_request($uri, $method="GET", $parameters = array(), $header = false){
|
763
|
global $locale; // drupal variable containing the current locale i.e. language
|
764
|
static $header;
|
765
|
|
766
|
if($method != "GET" && $method != "POST"){
|
767
|
$method = "GET";
|
768
|
}
|
769
|
|
770
|
if(!$header){
|
771
|
$header = array();
|
772
|
$header['Accept'] = (variable_get('cdm_webservice_type', 'json') == 'json' ? 'application/json' : 'text/xml');
|
773
|
$header['Accept-Language'] = $locale;
|
774
|
$header['Accept-Charset'] = 'UTF-8';
|
775
|
}
|
776
|
|
777
|
if(function_exists('curl_init')){
|
778
|
// use the CURL lib if installed it is supposed to be 20x faster
|
779
|
return _http_request_using_curl($uri, $header, $method, $parameters);
|
780
|
} else {
|
781
|
return _http_request_using_fsockopen($uri, $header, $method, $parameters);
|
782
|
}
|
783
|
}
|
784
|
|
785
|
function _http_request_using_fsockopen($uri, $header = array(), $method = "GET"){
|
786
|
$response = drupal_http_request($uri, $header, $method);
|
787
|
return $response->data;
|
788
|
}
|
789
|
|
790
|
|
791
|
/**
|
792
|
* Return string content from a remote file
|
793
|
*
|
794
|
* @param string $uri
|
795
|
* @return string
|
796
|
*
|
797
|
* @author Luiz Miguel Axcar (lmaxcar@yahoo.com.br)
|
798
|
*/
|
799
|
function _http_request_using_curl($uri, $headers = array(), $method = "GET", $parameters = array())
|
800
|
{
|
801
|
$ch = curl_init();
|
802
|
|
803
|
curl_setopt ($ch, CURLOPT_URL, $uri);
|
804
|
// set proxy settings
|
805
|
if(variable_get('cdm_webservice_proxy_url', false)){
|
806
|
curl_setopt($ch, CURLOPT_PROXY, variable_get('cdm_webservice_proxy_url', ''));
|
807
|
curl_setopt($ch, CURLOPT_PROXYPORT, variable_get('cdm_webservice_proxy_port', '80'));
|
808
|
if(variable_get('cdm_webservice_proxy_usr', false)){
|
809
|
curl_setopt ($ch, CURLOPT_PROXYUSERPWD, variable_get('cdm_webservice_proxy_usr', '').':'.variable_get('cdm_webservice_proxy_pwd', ''));
|
810
|
}
|
811
|
}
|
812
|
// modify headers array to be used by curl
|
813
|
foreach($headers as $header_key=>$header_val){
|
814
|
$curl_headers[] = $header_key.': '.$header_val;
|
815
|
}
|
816
|
curl_setopt ($ch, CURLOPT_HTTPHEADER, $curl_headers);
|
817
|
// set method if not default
|
818
|
if($method != "GET"){
|
819
|
if($method == "POST"){
|
820
|
|
821
|
curl_setopt ($ch, CURLOPT_POST, 1);
|
822
|
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parameters);
|
823
|
|
824
|
}else{
|
825
|
// other obscure http methods get passed to curl directly
|
826
|
// TODO generic parameter/body handling
|
827
|
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
|
828
|
}
|
829
|
}
|
830
|
|
831
|
ob_start();
|
832
|
curl_exec ($ch);
|
833
|
if(curl_errno($ch)){
|
834
|
watchdog('CDM_API', '_http_request_curl() - '.curl_error($ch).'; REQUEST-METHOD:'.$method.' URL: '.substr($uri.' ', 0, 150), WATCHDOG_ERROR);
|
835
|
if(variable_get('cdm_webservice_debug', 1) && user_access('administer') ){
|
836
|
drupal_set_message('_http_request_curl() - '.curl_error($ch).'; REQUEST-METHOD:'.$method.' URL: '.substr($uri.' ', 0, 150), 'error');
|
837
|
}
|
838
|
}
|
839
|
curl_close ($ch);
|
840
|
$string = ob_get_contents();
|
841
|
ob_end_clean();
|
842
|
|
843
|
return $string;
|
844
|
}
|
845
|
|
846
|
function cdm_api_secref_cache_prefetch(&$secUuids){
|
847
|
global $secref_cache;
|
848
|
if(!is_array($secref_cache)){
|
849
|
$secref_cache = array();
|
850
|
}
|
851
|
$uniqueUuids = array_unique($secUuids);
|
852
|
$i = 0;
|
853
|
$param = '';
|
854
|
while($i++ < count($uniqueUuids)){
|
855
|
$param .= $secUuids[$i].',';
|
856
|
if(strlen($param) + 37 > 2000){
|
857
|
_cdm_api_secref_cache_add($param);
|
858
|
$param = '';
|
859
|
}
|
860
|
}
|
861
|
if($param){
|
862
|
_cdm_api_secref_cache_add($param);
|
863
|
}
|
864
|
}
|
865
|
|
866
|
function cdm_api_secref_cache_get($secUuid){
|
867
|
global $secref_cache;
|
868
|
if(!is_array($secref_cache)){
|
869
|
$secref_cache = array();
|
870
|
}
|
871
|
if(!array_key_exists($secUuid, $secref_cache)){
|
872
|
_cdm_api_secref_cache_add($secUuid);
|
873
|
}
|
874
|
return $secref_cache[$secUuid];
|
875
|
}
|
876
|
|
877
|
function cdm_api_secref_cache_clear(){
|
878
|
global $secref_cache;
|
879
|
$secref_cache = array();
|
880
|
}
|
881
|
|
882
|
function _cdm_api_secref_cache_add($secUuidsStr){
|
883
|
global $secref_cache;
|
884
|
$ref = cdm_ws_get(CDM_WS_REFERENCE, $secUuidsStr);
|
885
|
// batch fetching not jet reimplemented thus:
|
886
|
/*$assocRefSTOs = array();
|
887
|
if($refSTOs) {
|
888
|
foreach($refSTOs as $ref){
|
889
|
$assocRefSTOs[$ref->uuid] = $ref;
|
890
|
}
|
891
|
$secref_cache = array_merge($secref_cache, $assocRefSTOs);
|
892
|
}*/
|
893
|
$secref_cache[$ref->uuid] = $ref;
|
894
|
}
|
895
|
|
896
|
function queryString($elements) {
|
897
|
$query = '';
|
898
|
foreach($elements as $key=>$value){
|
899
|
if(is_array($value)){
|
900
|
foreach($value as $v){
|
901
|
$query .= (strlen($query) > 0 ? '&' : '').$key.'='.urlencode($v);
|
902
|
}
|
903
|
} else{
|
904
|
$query .= (strlen($query) > 0 ? '&' : '').$key.'='.urlencode($value);
|
905
|
}
|
906
|
}
|
907
|
return $query;
|
908
|
}
|
909
|
|
910
|
/**
|
911
|
* implementation of the magic method __clone to allow deep cloning of objects and arrays
|
912
|
*/
|
913
|
function __clone(){
|
914
|
foreach($this as $name => $value){
|
915
|
if(gettype($value)=='object' || gettype($value)=='array'){
|
916
|
$this->$name= clone($this->$name);
|
917
|
}
|
918
|
}
|
919
|
}
|
920
|
|
921
|
/**
|
922
|
* Make a complete deep copy of an array replacing
|
923
|
* references with deep copies until a certain depth is reached
|
924
|
* ($maxdepth) whereupon references are copied as-is...
|
925
|
* [From http://us3.php.net/manual/en/ref.array.php]
|
926
|
* @param $array
|
927
|
* @param $copy
|
928
|
* @param $maxdepth
|
929
|
* @param $depth
|
930
|
* @return unknown_type
|
931
|
*/
|
932
|
function array_deep_copy (&$array, &$copy, $maxdepth=50, $depth=0) {
|
933
|
if($depth > $maxdepth) { $copy = $array; return; }
|
934
|
if(!is_array($copy)) $copy = array();
|
935
|
foreach($array as $k => &$v) {
|
936
|
if(is_array($v)) { array_deep_copy($v,$copy[$k],$maxdepth,++$depth);
|
937
|
} else {
|
938
|
$copy[$k] = $v;
|
939
|
}
|
940
|
}
|
941
|
}
|
942
|
|
943
|
/**
|
944
|
* Implementation of theme_status_messages($display = NULL)
|
945
|
* @see includes/theme.inc
|
946
|
*
|
947
|
* @param $display
|
948
|
* @return unknown_type
|
949
|
*/
|
950
|
function _add_status_message_toggler() {
|
951
|
static $isAdded;
|
952
|
if(!$isAdded){
|
953
|
|
954
|
drupal_add_js(
|
955
|
'$(document).ready(function(){
|
956
|
|
957
|
$(\'.messages.debug\').before( \'<h6 class="messages_toggler debug">Debug Messages (klick to toggle)</h6>\' );
|
958
|
$(\'.messages_toggler\').click(function(){
|
959
|
$(this).next().slideToggle(\'fast\');
|
960
|
return false;
|
961
|
}).next().hide();
|
962
|
|
963
|
});'
|
964
|
, 'inline');
|
965
|
$isAdded = TRUE;
|
966
|
}
|
967
|
}
|
968
|
|
969
|
define('CDM_WS_MEDIA', 'portal/media');
|
970
|
|
971
|
define('CDM_WS_MEDIA_METADATA', 'media/$0/metadata');
|
972
|
|
973
|
define('CDM_WS_NAME', 'name');
|
974
|
|
975
|
define('CDM_WS_NAME_TYPEDESIGNATIONS', 'name/$0/typeDesignations');
|
976
|
|
977
|
define('CDM_WS_TAXON_NAMETYPEDESIGNATIONS', 'portal/taxon/$0/nameTypeDesignations');
|
978
|
|
979
|
define('CDM_WS_TAXON_DESCRIPTIONS', 'portal/taxon/$0/descriptions');
|
980
|
|
981
|
define('CDM_WS_NAME_DESCRIPTIONS', 'portal/name/$0/descriptions');
|
982
|
|
983
|
define('CDM_WS_REFERENCE', 'reference');
|
984
|
|
985
|
define('CDM_WS_NOMENCLATURAL_REFERENCE_CITATION', 'reference/$0/nomenclaturalCitation/$1');
|
986
|
|
987
|
define('CDM_WS_FIND_TAXA', 'portal/taxon/find');
|
988
|
|
989
|
define('CDM_WS_TAXON', 'portal/taxon');
|
990
|
|
991
|
/**
|
992
|
* @parameters $0 : the taxon uuid
|
993
|
*
|
994
|
* @returns
|
995
|
*/
|
996
|
define('CDM_WS_TAXON_SYNONYMY', 'portal/taxon/$0/synonymy');
|
997
|
|
998
|
define('CDM_WS_TAXON_RELATIONS', 'portal/taxon/$0/taxonRelationships');
|
999
|
|
1000
|
define('CDM_WS_TAXON_NAMERELATIONS', 'portal/taxon/$0/nameRelationships');
|
1001
|
|
1002
|
|
1003
|
/**
|
1004
|
* @parameters $0 : the taxon uuid
|
1005
|
*
|
1006
|
* @returns the taxon which is the accepted synonym for the taxon given as
|
1007
|
* parameter taxonUuid. If the taxon specified by taxonUuid is itself the
|
1008
|
* accepted taxon, this one will be returned.
|
1009
|
*/
|
1010
|
define('CDM_WS_TAXON_ACCEPTED', 'portal/taxon/$0/accepted');
|
1011
|
|
1012
|
define('CDM_WS_TAXON_MEDIA', 'portal/taxon/$0/media/$1/$2');
|
1013
|
|
1014
|
/**
|
1015
|
*
|
1016
|
* Gets the root nodes of the taxonomic concept tree for the concept
|
1017
|
* reference specified by the secUuid parameter.
|
1018
|
*
|
1019
|
* stub: treenode_root
|
1020
|
*/
|
1021
|
define('CDM_WS_TAXONOMY', 'portal/taxontree');
|
1022
|
define('CDM_WS_TAXONOMY_MEDIA', 'portal/taxontree/$0/$1');
|
1023
|
|
1024
|
define('CDM_WS_TERMVOCABULARY', 'term/$0');
|
1025
|
|
1026
|
define('CDM_WS_TERM_COMPARE', 'term/$0/compareTo/$1');
|
1027
|
|
1028
|
define('CDM_WS_TDWG_LEVEL', 'term/tdwg/$0');
|
1029
|
|
1030
|
/**
|
1031
|
* returns FeatureTrees that are stored in this community store
|
1032
|
*
|
1033
|
*/
|
1034
|
define('CDM_WS_FEATURETREE', 'featuretree/$0');
|
1035
|
|
1036
|
define('CDM_WS_FEATURETREES', 'featuretree');
|
1037
|
|
1038
|
define('CDM_WS_GEOSERVICE_DISTRIBUTIONMAP', 'geo/map/distribution/$0');
|