tripal_ws.rest.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. function tripal_ws_rest() {
  3. global $base_url;
  4. // Set some initial variables.
  5. $response = array();
  6. $status = 'success';
  7. $version = 'v0.1';
  8. $message = '';
  9. $api_url = $base_url . '/ws/' . $version;
  10. $page_limit = 25;
  11. $pager_id = 0;
  12. $response['@context'] = array(
  13. 'schema' => 'http://schema.org/',
  14. 'foaf' => 'http://xmlns.com/foaf/0.1/',
  15. 'dc' => 'http://purl.org/dc/elements/1.1/',
  16. );
  17. $response['@id'] = $api_url;
  18. $namespace = arg(3) ? arg(3) : '';
  19. $bundle_name = arg(4) ? arg(4) : '';
  20. // Lump everything ito a try block so that if there is a problem we can
  21. // throw an error and have that returned in the response.
  22. try {
  23. if (!$namespace) {
  24. tripal_ws_get_content_types($api_url, $response);
  25. }
  26. if ($namespace and !$bundle_name) {
  27. tripal_ws_get_content_type($api_url, $response, $namespace);
  28. }
  29. }
  30. catch (Exception $e) {
  31. watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
  32. $message = $e->getMessage();
  33. $status = 'error';
  34. }
  35. // The responses follow a similar format as the AGAVE API with a
  36. // status, message, version and all data in the 'result' object.
  37. /* $response['status'] = $status;
  38. $response['message'] = $message;
  39. $response['api_version'] = $version;
  40. $response['source'] = array(
  41. 'site_name' => variable_get('site_name', 'Unspecified'),
  42. 'site_url' => $base_url,
  43. 'site_slogan' => variable_get('site_slogan', 'Unspecified'),
  44. 'site_email' => variable_get('site_mail', 'Unspecified'),
  45. ); */
  46. print drupal_json_output($response);
  47. }
  48. /**
  49. *
  50. * @param unknown $api_url
  51. * @param unknown $response
  52. */
  53. function tripal_ws_get_content_types($api_url, &$response) {
  54. // Add in the term's will use for keys to the context.
  55. $response['@context']['itemListElement'] = 'schema:itemListElement';
  56. $response['@context']['position'] = 'schema:position';
  57. $response['@context']['item'] = 'schema:item';
  58. $response['@context']['name'] = 'foaf:name';
  59. $response['@context']['description'] = 'dc:description';
  60. $response['@context']['numberOfItems'] = 'schema:numberOfItems';
  61. $response['@context']['itemListOrder'] = 'schema:itemListOrder';
  62. $response['@context']['numberOfItems'] = 'schema:numberOfItems';
  63. $response['@context']['ListItem'] = 'schema:ListItem';
  64. // Start the list.
  65. $response['@type'] = 'ItemList';
  66. $response['itemListOrder'] = 'ItemListOrderAscending';
  67. $response['numberOfItems'] = 0;
  68. $response['name'] = 'Content Types';
  69. // Get the list of published terms (these are the bundle IDs)
  70. $bundles = db_select('tripal_bundle', 'tb')
  71. ->fields('tb')
  72. ->orderBy('tb.label', 'ASC')
  73. ->execute();
  74. $terms = array();
  75. while ($bundle = $bundles->fetchObject()) {
  76. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  77. $terms[$bundle->label] = reset($entity);
  78. }
  79. $i = 0;
  80. foreach ($terms as $name => $term) {
  81. $vocab = $term->vocab;
  82. if (!array_key_exists($vocab->namespace, $response['@context'])) {
  83. // If there is no URL prefix then use this API's vocabulary API
  84. if ($term->urlprefix) {
  85. $response['@context'][$vocab->namespace] = $term->urlprefix;
  86. }
  87. else {
  88. $response['@context'][$vocab->namespace] = $api_url . '/vocab/' . $vocab->namespace . '/';
  89. }
  90. }
  91. $response['itemListElement'][] = array(
  92. '@type' => 'ListItem',
  93. 'position' => $i + 1,
  94. 'item' => array(
  95. '@id' => $api_url . '/bio-data/' . $term->name,
  96. '@type' => $vocab->namespace . ':' . $term->accession,
  97. 'name' => $term->name,
  98. 'description' => $term->definition,
  99. ),
  100. );
  101. $i++;
  102. }
  103. $response['numberOfItems'] = $i;
  104. }
  105. /**
  106. *
  107. * @param unknown $api_url
  108. * @param unknown $response
  109. * @param unknown $vocab
  110. * @param unknown $name
  111. */
  112. function tripal_ws_get_content_type($api_url, &$response, $vocab, $name) {
  113. }