tripal_ws.api.inc 24 KB

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