tripal_ws.api.inc

This file provides the Tripal Web Services API: a set of functions for interacting with the Tripal Web Services.

File

tripal_ws/api/tripal_ws.api.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file provides the Tripal Web Services API: a set of functions for
  6. * interacting with the Tripal Web Services.
  7. */
  8. /**
  9. * @defgroup tripal_ws_api Web Services
  10. *
  11. * @ingroup tripal_api
  12. * {@
  13. * The Tripal Web Services API provides a set of functions for interacting
  14. * with the Tripal Web Services.
  15. * @}
  16. */
  17. /**
  18. * Adjust the values of a field for display in web services.
  19. *
  20. * This hook should be used sparingly. It is meant primarily to adjust 3rd
  21. * Party (non Tripal) fields so that they work with web
  22. * services. The caller should adjust the $items array as needed.
  23. * This change only affects the value displayed in web services. Web services
  24. * expect that every field have a 'value' element for each of the items. If a
  25. * field for some reason does not have a 'value' element then this hook will
  26. * allow setting of that element.
  27. *
  28. * @param $items
  29. * The list of items for the field.
  30. * @param $field
  31. * The field array.
  32. * @param $instance
  33. * The field instance array.
  34. *
  35. * @ingroup tripal_ws_api
  36. */
  37. function hook_tripal_ws_value(&$items, $field, $instance) {
  38. // The image module doesn't properly set the 'value' field, so we'll do it
  39. // here.
  40. if($field['type'] == 'image' and $field['module'] == 'image') {
  41. foreach ($items as $delta => $details) {
  42. if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
  43. $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
  44. }
  45. }
  46. }
  47. }
  48. /**
  49. * Retrieves a list of TripalWebService implementations.
  50. *
  51. * The TripalWebService classes can be added by a site developer that wishes
  52. * to create a new Tripal compatible web serivce. The class file should
  53. * be placed in the [module]/includes/TripalWebService directory. Tripal will
  54. * support any service as long as it is in this directory and extends the
  55. * TripalWebService class.
  56. *
  57. * @return
  58. * A list of TripalWebService names.
  59. *
  60. * @ingroup tripal_ws_api
  61. */
  62. function tripal_get_web_services() {
  63. $services = array();
  64. $modules = module_list(TRUE);
  65. foreach ($modules as $module) {
  66. // Find all of the files in the tripal_chado/includes/fields directory.
  67. $service_path = drupal_get_path('module', $module) . '/includes/TripalWebService';
  68. $service_files = file_scan_directory($service_path, '/.inc$/');
  69. // Iterate through the fields, include the file and run the info function.
  70. foreach ($service_files as $file) {
  71. $class = $file->name;
  72. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  73. if (class_exists($class) and is_subclass_of($class, 'TripalWebService')) {
  74. $services[] = $class;
  75. }
  76. }
  77. }
  78. return $services;
  79. }
  80. /**
  81. * Loads the TripalWebService class file into scope.
  82. *
  83. * @param $class
  84. * The TripalWebService class to include.
  85. *
  86. * @return
  87. * TRUE if the field type class file was found, FALSE otherwise.
  88. *
  89. * @ingroup tripal_ws_api
  90. */
  91. function tripal_load_include_web_service_class($class) {
  92. $modules = module_list(TRUE);
  93. foreach ($modules as $module) {
  94. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalWebService/' . $class . '.inc';
  95. if (file_exists($file_path)) {
  96. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  97. if (class_exists($class)) {
  98. return TRUE;
  99. }
  100. }
  101. }
  102. return FALSE;
  103. }
  104. /**
  105. * Adds a new site to the web services table.
  106. *
  107. * @param $name
  108. * Name of site to be included.
  109. * @param $url
  110. * URL of site to be added.
  111. * @param $version
  112. * Version of the API being used. default to 1
  113. * @param $description
  114. * A description of the site and any additional info that
  115. * would be helpful for admins.
  116. *
  117. * @return
  118. * TRUE if the site is successfully added, FALSE otherwise.
  119. *
  120. * @ingroup tripal_ws_api
  121. */
  122. function tripal_add_site($name, $url, $version, $description) {
  123. $check_url = NULL;
  124. $check_name = NULL;
  125. $write_to_db = TRUE;
  126. // When inserting a record.
  127. $check_url =
  128. db_select('tripal_sites', 'ts')
  129. ->fields('ts', array('id'))
  130. ->condition('url', $url)
  131. ->condition('version', $version)
  132. ->execute()
  133. ->fetchField();
  134. $check_name =
  135. db_select('tripal_sites', 'ts')
  136. ->fields('ts', array('id'))
  137. ->condition('name', $name)
  138. ->execute()
  139. ->fetchField();
  140. if ($check_url) {
  141. drupal_set_message(t('The URL and version is used by another site.'), 'error');
  142. $write_to_db = FALSE;
  143. }
  144. if ($check_name) {
  145. drupal_set_message(t('The name is used by another site.'), 'error');
  146. $write_to_db = FALSE;
  147. }
  148. if ($write_to_db === TRUE) {
  149. db_insert('tripal_sites')
  150. ->fields(array(
  151. 'name' => $name,
  152. 'url' => $url,
  153. 'version' => $version,
  154. 'description' => $description
  155. ))
  156. ->execute();
  157. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  158. return $write_to_db;
  159. }
  160. return $write_to_db;
  161. }
  162. /**
  163. * Remove a site from the web services table.
  164. *
  165. * @param $record_id
  166. * ID of the record to be deleted.
  167. *
  168. * @return
  169. * TRUE if the record was successfully deleted, FALSE otherwise.
  170. *
  171. * @ingroup tripal_ws_api
  172. */
  173. function tripal_remove_site($record_id) {
  174. if ($record_id) {
  175. db_delete('tripal_sites')
  176. ->condition('id', $record_id)
  177. ->execute();
  178. drupal_set_message('The Tripal site \'' . $record_id . '\' has been removed.');
  179. return TRUE;
  180. }
  181. return FALSE;
  182. }
  183. /**
  184. * Makes a request to the "content" service of a remote Tripal web site.
  185. *
  186. * @param $site_id
  187. * The numeric site ID for the remote Tripal site.
  188. * @param $path
  189. * The web service path for the content (excluding
  190. * 'web-servcies/vX.x/content'). To retrieve the full content listing
  191. * leave this paramter empty.
  192. * @param $query
  193. * An query string to appear after the ? in a URL.
  194. *
  195. * @return
  196. * The JSON response formatted in a PHP array or FALSE if a problem occured.
  197. *
  198. * @ingroup tripal_ws_api
  199. */
  200. function tripal_get_remote_content($site_id, $path = '', $query = '') {
  201. if (!$site_id) {
  202. throw new Exception('Please provide a numeric site ID for the tripal_get_remote_content function.');
  203. }
  204. // Fetch the record for this site id.
  205. $remote_site = db_select('tripal_sites', 'ts')
  206. ->fields('ts')
  207. ->condition('ts.id', $site_id)
  208. ->execute()
  209. ->fetchObject();
  210. if (!$remote_site) {
  211. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  212. t('Could not find a remote tripal site using the id provided: !id.',
  213. array('!id' => $site_id)));
  214. return FALSE;
  215. }
  216. // Build the URL to the remote web services.
  217. $ws_version = $remote_site->version;
  218. $ws_url = $remote_site->url;
  219. $ws_url = trim($ws_url, '/');
  220. $ws_url .= '/web-services/content/' . $ws_version . '/' . $path;
  221. // Build the Query and make and substitions needed.
  222. if ($query) {
  223. $ws_url = $ws_url . '?' . $query;
  224. }
  225. // TODO: something is wrong here, the query is not being recognized on
  226. // the remote Tripal site. It's just returning the default.
  227. $data = drupal_http_request($ws_url);
  228. if (!$data) {
  229. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  230. t('Could not connect to the remote web service.'));
  231. return FALSE;
  232. }
  233. // If the data object has an error then this is some sort of
  234. // connection error (not a Tripal web servcies error).
  235. if (property_exists($data, 'error')) {
  236. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  237. 'Remote web Services reports the following error: !error. Using URL: !url',
  238. array('!error' => $error, '!url' => $ws_url));
  239. return FALSE;
  240. }
  241. // We got a response, so convert it to a PHP array.
  242. $data = drupal_json_decode($data->data);
  243. // Check if there was a Tripal Web Services error.
  244. if (array_key_exists('error', $data)) {
  245. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  246. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  247. 'Tripal remote web services reports the following error: !error. Using URL: !url',
  248. array('!error' => $error, '!url' => $ws_url));
  249. return FALSE;
  250. }
  251. return $data;
  252. }
  253. /**
  254. * Retrieves the JSON-LD context for any remote Tripal web service.
  255. *
  256. * @param $context_url
  257. * The Full URL for the context file on the remote Tripal site. This URL
  258. * can be found in the '@context' key of any response from a remote Tripal
  259. * web services call.
  260. * @param $cache_id
  261. * A unique ID for caching of this context result to speed furture
  262. * queries.
  263. * @return
  264. * The JSON-LD context mapping array, or FALSE if the context could not
  265. * be retrieved.
  266. *
  267. * @ingroup tripal_ws_api
  268. */
  269. function tripal_get_remote_context($context_url, $cache_id) {
  270. if (!$context_url) {
  271. throw new Exception('PLease provide a context_url for the tripal_get_remote_context function.');
  272. }
  273. if (!$cache_id) {
  274. throw new Exception('PLease provide unique $cache_id for the tripal_get_remote_context function.');
  275. }
  276. if ($cache = cache_get($cache_id)) {
  277. return $cache->data;
  278. }
  279. $context = drupal_http_request($context_url);
  280. if (!$context) {
  281. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  282. 'There was a poblem retrieving the context from the remote site: !context_url.',
  283. array('!context_url' => $context_url));
  284. return FALSE;
  285. }
  286. $context = drupal_json_decode($context->data);
  287. $context = $context['@context'];
  288. cache_set($cache_id, $context);
  289. return $context;
  290. }
  291. /**
  292. * Retrieves the JSON-LD context for a bundle or field on a remote Tripal site.
  293. *
  294. * The $site_id, $bundle_accession and $field_accession variables are not
  295. * needed to retrieve the context, but are used for caching the context to
  296. * make subsequent calls execute faster. This function is meant to be used
  297. * only for the 'content' service provided by Tripal.
  298. *
  299. * @param $site_id
  300. * The numeric site ID for the remote Tripal site.
  301. * @param $context_url
  302. * The Full URL for the context file on the remote Tripal site. This URL
  303. * can be found in the '@context' key of any response from a remote Tripal
  304. * web services call.
  305. * @param $bundle_accession
  306. * The controlled vocabulary term accession for the content type
  307. * on the remote Tripal site.
  308. * @param $field_accession
  309. * The controlled vocabulary term accession for the property (i.e. field) of
  310. * the Class (i.e. content type).
  311. * @return
  312. * The JSON-LD context mapping array.
  313. *
  314. * @ingroup tripal_ws_api
  315. */
  316. function tripal_get_remote_content_context($site_id, $context_url, $bundle_accession, $field_accession = '') {
  317. $cache_id = substr('trp_ws_context_' . $site_id . '-' . $bundle_accession . '-' . $field_accession, 0, 254);
  318. $context = tripal_get_remote_context($context_url, $cache_id);
  319. return $context;
  320. }
  321. /**
  322. * Clears the cached remote site documentation and context.
  323. *
  324. * When querying a remote website, the site's API documenation and page context
  325. * is cached to make re-use of that information easier in the future. This
  326. * function can be used to clear those caches.
  327. *
  328. * @param $site_id
  329. * The numeric site ID for the remote Tripal site.
  330. *
  331. * @ingroup tripal_ws_api
  332. */
  333. function tripal_clear_remote_cache($site_id) {
  334. if (!$site_id) {
  335. throw new Exception('Please provide a numeric site ID for the tripal_clear_remote_cache function.');
  336. }
  337. cache_clear_all('trp_ws_context_' . $site_id , 'cache', TRUE);
  338. cache_clear_all('trp_ws_doc_' . $site_id , 'cache', TRUE);
  339. }
  340. /**
  341. * Retrieves the API documentation for a remote Tripal web service.
  342. *
  343. * @param $site_id
  344. * The numeric site ID for the remote Tripal site.
  345. * @return
  346. * The vocabulary of a remote Tripal web service, or FALSE if an error
  347. * occured.
  348. *
  349. * @ingroup tripal_ws_api
  350. */
  351. function tripal_get_remote_API_doc($site_id) {
  352. $site_doc = '';
  353. if (!$site_id) {
  354. throw new Exception('Please provide a numeric site ID for the tripal_get_remote_API_doc function.');
  355. }
  356. $cache_name = 'trp_ws_doc_' . $site_id;
  357. if ($cache = cache_get($cache_name)) {
  358. return $cache->data;
  359. }
  360. // Get the site url from the tripal_sites table.
  361. $remote_site = db_select('tripal_sites', 'ts')
  362. ->fields('ts')
  363. ->condition('ts.id', $site_id)
  364. ->execute()
  365. ->fetchObject();
  366. if (!$remote_site) {
  367. throw new Exception(t('Cannot find a remote site with id: "!id"', array('!id' => $site_id)));
  368. }
  369. // Build the URL to the remote web services.
  370. $ws_version = $remote_site->version;
  371. $ws_url = $remote_site->url;
  372. $ws_url = trim($ws_url, '/');
  373. $ws_url .= '/web-services/doc/' . $ws_version;
  374. // Build and make the request.
  375. $options = [];
  376. $data = drupal_http_request($ws_url, $options);
  377. if (!$data) {
  378. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  379. t('Could not connect to the remote web service.'));
  380. return FALSE;
  381. }
  382. // If the data object has an error then this is some sort of
  383. // connection error (not a Tripal web servcies error).
  384. if (property_exists($data, 'error')) {
  385. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  386. 'Remote web services document reports the following error: !error. Using URL: !url',
  387. array('!error' => $error, '!url' => $ws_url));
  388. return FALSE;
  389. }
  390. // We got a response, so convert it to a PHP array.
  391. $site_doc = drupal_json_decode($data->data);
  392. // Check if there was a Tripal Web Services error.
  393. if (array_key_exists('error', $data)) {
  394. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  395. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  396. 'Tripal Remote web services document reports the following error: !error. Using URL: !url',
  397. array('!error' => $error, '!url' => $ws_url));
  398. return FALSE;
  399. }
  400. cache_set($cache_name, $site_doc);
  401. return $site_doc;
  402. }
  403. /**
  404. * Queries a remote site for an array of bulk entity ids.
  405. *
  406. * This function returns an array of "fake" entities containing values
  407. * for fields specified.
  408. *
  409. * @param $remote_entity_ids
  410. * Array of the remote ids.
  411. * @param $site_id
  412. * The numeric site ID for the remote Tripal site.
  413. * @param $bundle_accession
  414. * The controlled vocabulary term accession for the content type
  415. * on the remote Tripal site.
  416. * @param $field_ids
  417. * The controlled vocabulary term accessions for the fields available
  418. * on the remote content type. Any remote fields that matches these IDs will
  419. * be added to the entity returned.
  420. * @return
  421. * An array of fake entity objects where the key is the entity_id and
  422. * the value is the object.
  423. *
  424. * @ingroup tripal_ws_api
  425. */
  426. function tripal_load_remote_entities($remote_entity_ids, $site_id, $bundle_accession, $field_ids) {
  427. if (!$remote_entity_ids) {
  428. throw new Exception('Please provide the list of remote entity ids for the tripal_load_remote_entities function.');
  429. }
  430. if (!is_array($remote_entity_ids)) {
  431. throw new Exception('Please provide an array for the remote entity ids for the tripal_load_remote_entities function.');
  432. }
  433. if (!$site_id) {
  434. throw new Exception('Please provide a numeric site ID for the tripal_load_remote_entities function.');
  435. }
  436. if (!$bundle_accession) {
  437. throw new Exception('Please provide the bundle accession for the tripal_load_remote_entities function.');
  438. }
  439. if (!$field_ids) {
  440. throw new Exception('Please provide the list of field IDs for the tripal_load_remote_entities function.');
  441. }
  442. if (!is_array($field_ids)) {
  443. throw new Exception('Please provide an array for the field IDs for the tripal_load_remote_entities function.');
  444. }
  445. // Get the site documentation (loads from cache if already retrieved).
  446. $site_doc = tripal_get_remote_API_doc($site_id);
  447. // Generate an array for the query and then execute it.
  448. $query = 'page=1&limit=' . count($remote_entity_ids) .
  449. '&ids=' . urlencode(implode(",", $remote_entity_ids)) .
  450. '&fields=' . urlencode(implode(",", $field_ids));
  451. $results = tripal_get_remote_content($site_id, $bundle_accession, $query);
  452. if (!$results) {
  453. return FALSE;
  454. }
  455. // Get the context JSON for this remote entity, we'll use it to map
  456. $context_url = $results['@context'];
  457. $context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
  458. if (!$context) {
  459. return $entity;
  460. }
  461. $total_items = $results['totalItems'];
  462. $members = $results['member'];
  463. $entities = array();
  464. foreach($members as $member) {
  465. // Start building the fake entity.
  466. $entity_id = preg_replace('/^.*?(\d+)$/', '$1', $member['@id']);
  467. $entity = new stdClass();
  468. $entity->entityType = 'TripalEntity';
  469. $entity->entityInfo = [];
  470. $entity->id = $entity_id;
  471. $entity->type = 'TripalEntity';
  472. $entity->bundle = $bundle_accession;
  473. $entity->site_id = $site_id;
  474. $member = _tripal_update_remote_entity_field($member, $context, 1);
  475. foreach ($member as $field_id => $value) {
  476. $field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
  477. $instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
  478. $field_name = $field['field_name'];
  479. $entity->{$field_name}['und'][0]['value'] = $value;
  480. }
  481. $entities[$entity_id] = $entity;
  482. }
  483. return $entities;
  484. }
  485. /**
  486. * Queries a remote site for an entity.
  487. *
  488. * This function returns a "fake" entity containing values for all fields
  489. * specified.
  490. *
  491. * @param $remote_entity_id
  492. * A remote entity ID.
  493. * @param $site_id
  494. * The numeric site ID for the remote Tripal site.
  495. * @param $bundle_accession
  496. * The controlled vocabulary term accession for the content type
  497. * on the remote Tripal site.
  498. * @param $field_ids
  499. * The controlled vocabulary term accessions for the fields available
  500. * on the remote content type. Any remote fields that matches these IDs will
  501. * be added to the entity returned.
  502. * @return
  503. * A fake entity object.
  504. *
  505. * @ingroup tripal_ws_api
  506. */
  507. function tripal_load_remote_entity($remote_entity_id, $site_id, $bundle_accession, $field_ids) {
  508. // Get the site documentation (loads from cache if already retrieved).
  509. $site_doc = tripal_get_remote_API_doc($site_id);
  510. // Get the remote entity and create the fake entity.
  511. $remote_entity = tripal_get_remote_content($site_id, $bundle_accession . '/' . $remote_entity_id);
  512. if (!$remote_entity) {
  513. return FALSE;
  514. }
  515. // Start building the fake entity.
  516. $entity = new stdClass();
  517. $entity->entityType = 'TripalEntity';
  518. $entity->entityInfo = [];
  519. $entity->id = $remote_entity_id;
  520. $entity->type = 'TripalEntity';
  521. $entity->bundle = $bundle_accession;
  522. $entity->site_id = $site_id;
  523. // Get the context JSON for this remote entity, we'll use it to map
  524. $context_url = $remote_entity['@context'];
  525. $context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
  526. if (!$context) {
  527. return $entity;
  528. }
  529. // Iterate through the fields and the those values to the entity.
  530. foreach ($field_ids as $field_id) {
  531. $field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
  532. $instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
  533. $field_name = $field['field_name'];
  534. $field_key = '';
  535. foreach ($context as $k => $v) {
  536. if (!is_array($v) and $v == $field_id) {
  537. $field_key = $k;
  538. }
  539. }
  540. // If the field is not in this remote bundle then add an empty value.
  541. if (!$field_key) {
  542. $entity->{$field_name}['und'][0]['value'] = '';
  543. continue;
  544. }
  545. if (!array_key_exists($field_key, $remote_entity)) {
  546. $entity->{$field_name}['und'][0]['value'] = '';
  547. continue;
  548. }
  549. // If the key is for a field that is not "auto attached' then we need
  550. // to get that field through a separate call.
  551. $attached = TRUE;
  552. if (array_key_exists($field_id, $context) and is_array($context[$field_id]) and
  553. array_key_exists('@type', $context[$field_id]) and $context[$field_id]['@type'] == '@id'){
  554. $attached = FALSE;
  555. }
  556. // Set the value for this field.
  557. $value = '';
  558. if (is_array($remote_entity[$field_key])) {
  559. $value = _tripal_update_remote_entity_field($remote_entity[$field_key], $context, 1);
  560. }
  561. else {
  562. $value = $remote_entity[$field_key];
  563. }
  564. // If the field is not attached then we have to query another level.
  565. if (!$attached) {
  566. $field_data = drupal_http_request($value);
  567. if (!$field_data) {
  568. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  569. 'There was a poblem retrieving the unattached field, "!field:", for the remote entity: !entity_id.',
  570. array('!field' => $field_id, '!entity_id' => $remote_entity_id));
  571. $value = '';
  572. }
  573. $field_data = drupal_json_decode($field_data->data);
  574. // Get the context for this field so we can map the keys to the
  575. // controlled vocabulary accessions. If it fails then skip this field.
  576. $field_context_url = $field_data['@context'];
  577. $field_context = tripal_get_remote_content_context($site_id, $field_context_url, $bundle_accession, $field_id);
  578. if (!$field_context) {
  579. continue;
  580. }
  581. $value = _tripal_update_remote_entity_field($field_data, $field_context);
  582. }
  583. $entity->{$field_name}['und'][0]['value'] = $value;
  584. }
  585. return $entity;
  586. }
  587. /**
  588. * A helper function for the tripal_get_remote_entity() function.
  589. *
  590. * This function converts the field's key elements to their
  591. * vocabulary term accessions.
  592. *
  593. * @param $field_data
  594. * The field array as returned by web services.
  595. * @param $context
  596. * The web service JSON-LD context for the bundle to which the field belongs.
  597. *
  598. * @ingroup tripal_ws_api
  599. */
  600. function _tripal_update_remote_entity_field($field_data, $context, $depth = 0) {
  601. // Check if this is an array.
  602. if ($field_data['@type'] == 'Collection') {
  603. $members = array();
  604. foreach ($field_data['member'] as $member) {
  605. $next_depth = $depth + 1;
  606. $members[] = _tripal_update_remote_entity_field($member, $context, $next_depth);
  607. }
  608. // If we only have one item then just return it as a single item.
  609. // TODO: we may need to check cardinality of the field and be more
  610. // strict about how we return the value.
  611. if ($field_data['totalItems'] == 1){
  612. return $members[0];
  613. }
  614. else {
  615. return $members;
  616. }
  617. }
  618. $value = array();
  619. foreach ($field_data as $k => $v) {
  620. // Skip the JSON-LD keys.
  621. if ($k == '@id' or $k == '@type' or $k == '@context') {
  622. continue;
  623. }
  624. // Find the term accession for this element, and if the key's value is an
  625. // array then recurse.
  626. $accession = $context[$k];
  627. if (is_array($v)) {
  628. $next_depth = $depth + 1;
  629. $subvalue = _tripal_update_remote_entity_field($v, $context, $next_depth);
  630. $value[$accession] = $subvalue;
  631. }
  632. else {
  633. $value[$accession] = $v;
  634. }
  635. }
  636. return $value;
  637. }
  638. /**
  639. * Behaves similar to the field_info_field() function but for remote fields.
  640. *
  641. * Returns a "fake" field info array for fields attached to content types
  642. * on remote Tripal sites.
  643. *
  644. * @param $site_id
  645. * The numeric site ID for the remote Tripal site.
  646. * @param $bundle_accession
  647. * The controlled vocabulary term accession for the content type
  648. * on the remote Tripal site.
  649. * @param $field_accession
  650. * The controlled vocabulary term accession for the property (i.e. field) of
  651. * the Class (i.e. content type).
  652. * @return
  653. * An array similar to that returned by the field_info_field function
  654. * of Drupal for local fields.
  655. *
  656. * @ingroup tripal_ws_api
  657. */
  658. function tripal_get_remote_field_info($site_id, $bundle_accession, $field_accession){
  659. // Get the site documentation (loads from cache if already retrieved).
  660. $site_doc = tripal_get_remote_API_doc($site_id);
  661. // Get the property from the document for this field.
  662. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  663. // Now create the fake field and instance.
  664. list($vocab, $accession) = explode(':', $field_accession);
  665. $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
  666. $field = array(
  667. 'field_name' => $field_name,
  668. 'type' => $field_name,
  669. 'storage' => array(
  670. 'type' => 'tripal_remote_site'
  671. ),
  672. );
  673. return $field;
  674. }
  675. /**
  676. * Behaves similar to the field_info_instance() function but for remote fields.
  677. *
  678. * Returns a "fake" instance info array for fields attached to content types
  679. * on remote Tripal sites.
  680. *
  681. * @param $site_id
  682. * The numeric site ID for the remote Tripal site.
  683. * @param $bundle_accession
  684. * The controlled vocabulary term accession for the content type
  685. * on the remote Tripal site.
  686. * @param $field_accession
  687. * The controlled vocabulary term accession for the property (i.e. field) of
  688. * the Class (i.e. content type).
  689. * @return
  690. * An array similar to that returned by the field_info_instance function
  691. * of Drupal for local fields.
  692. *
  693. * @ingroup tripal_ws_api
  694. */
  695. function tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_accession){
  696. // Get the site documentation (loads from cache if already retrieved).
  697. $site_doc = tripal_get_remote_API_doc($site_id);
  698. // Get the property from the document for this field.
  699. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  700. list($vocab, $accession) = explode(':', $field_accession);
  701. $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
  702. list($vocab, $accession) = explode(':', $field_accession);
  703. $instance = array(
  704. 'label' => $property['hydra:title'],
  705. 'description' => $property['hydra:description'],
  706. 'formatters' => $property['tripal_formatters'],
  707. 'settings' => array(
  708. 'term_vocabulary' => $vocab,
  709. 'term_accession' => $accession
  710. ),
  711. 'field_name' => $field_name,
  712. 'entity_type' => 'TripalEntity',
  713. 'bundle_name' => $bundle_accession,
  714. );
  715. return $instance;
  716. }
  717. /**
  718. * Retreive the content type information from a remote Tripal site.
  719. *
  720. * The array returned is equivalent to the Hydra Vocabulary "supportedClass"
  721. * stanza.
  722. *
  723. * @param $site_id
  724. * The numeric site ID for the remote Tripal site.
  725. * @param $bundle_accession
  726. * The controlled vocabulary term accession for the content type
  727. * on the remote Tripal site.
  728. * @return
  729. * A PHP array corresponding to the Hydra Class stanza (i.e. a content type).
  730. * Returns NULL if the class ID cannot be found.
  731. *
  732. * @ingroup tripal_ws_api
  733. */
  734. function tripal_get_remote_content_doc($site_id, $bundle_accession){
  735. // Get the site documentation (loads from cache if already retrieved).
  736. $site_doc = tripal_get_remote_API_doc($site_id);
  737. // Get the class that matches this bundle.
  738. $classes = $site_doc['supportedClass'];
  739. $class = NULL;
  740. foreach ($classes as $item) {
  741. if ($item['@id'] == $bundle_accession) {
  742. return $item;
  743. }
  744. }
  745. return NULL;
  746. }
  747. /**
  748. * Retrieves the field information for a content type from a remote Tripal site.
  749. *
  750. * The array returned is equivalent to the Hydra Vocabulary "supportedProperty"
  751. * stanza that belongs to a Hydra Class (content type).
  752. *
  753. * @param $site_id
  754. * The numeric site ID for the remote Tripal site.
  755. * @param $bundle_accession
  756. * The controlled vocabulary term accession for the content type
  757. * on the remote Tripal site.
  758. * @param $field_accession
  759. * The controlled vocabulary term accession for the property (i.e. field) of
  760. * the Class (i.e. content type).
  761. * @return
  762. * A PHP array corresponding to the Hydra property stanza (field) that
  763. * belongs to the given Class (i.e. a content type). Retruns NULL if the
  764. * property cannot be found.
  765. *
  766. * @ingroup tripal_ws_api
  767. */
  768. function tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession){
  769. // Get the site documentation (loads from cache if already retrieved).
  770. $site_doc = tripal_get_remote_API_doc($site_id);
  771. $class = tripal_get_remote_content_doc($site_id, $bundle_accession);
  772. $properties = $class['supportedProperty'];
  773. foreach ($properties as $item) {
  774. if ($item['property'] == $field_accession) {
  775. return $item;
  776. }
  777. }
  778. return NULL;
  779. }
  780. /**
  781. * Retrieves the list of download formatters for a remote field.
  782. *
  783. * All Tripal fields support the abilty for inclusion in files that can
  784. * downloaded. This function is used to identify what formatters these
  785. * fields are appropriate for. If those download formatter classes exist
  786. * on this site then the field can be used with that formatter.
  787. *
  788. * @param $site_id
  789. * The numeric site ID for the remote Tripal site.
  790. * @param $bundle_accession
  791. * The controlled vocabulary term accession for the content type
  792. * on the remote Tripal site.
  793. * @param $field_accession
  794. * The controlled vocabulary term accession for the property (i.e. field) of
  795. * the Class (i.e. content type).
  796. * @return
  797. * An array of field downloader class names that are compoatible with the
  798. * field and which exist on this site.
  799. *
  800. * @ingroup tripal_ws_api
  801. */
  802. function tripal_get_remote_field_formatters($site_id, $bundle_accession, $field_accession) {
  803. $flist = array();
  804. // Get the site documentation (loads from cache if already retrieved).
  805. $site_doc = tripal_get_remote_API_doc($site_id);
  806. $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
  807. if (!$property) {
  808. return $flist;
  809. }
  810. $formatters = $property['tripal_formatters'];
  811. foreach($formatters as $formatter) {
  812. if (tripal_load_include_downloader_class($formatter)) {
  813. $flist[$formatter] = $formatter::$full_label;
  814. }
  815. }
  816. return $flist;
  817. }