tripal_ws.module 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Implements hook_init()
  4. */
  5. function tripal_ws_init() {
  6. global $base_url;
  7. $version = 'v0.1';
  8. $api_url = $base_url . '/ws/' . $version;
  9. // Following the WC3 Hydra documentation, we want to add LINK to the header
  10. // of the site that indicates where the API documentation can be found.
  11. // This allows a hydra-enabled client to discover the API and use it.
  12. $attributes = array(
  13. 'rel' => 'http://www.w3.org/ns/hydra/core#apiDocumentation',
  14. 'href' => $api_url . '/ws-doc/',
  15. );
  16. drupal_add_html_head_link($attributes, $header = FALSE);
  17. }
  18. /**
  19. * Implements hook_menu().
  20. * Defines all menu items needed by Tripal Core
  21. *
  22. * @ingroup tripal_ws
  23. */
  24. function tripal_ws_menu() {
  25. // Web Services API callbacks.
  26. $items['ws'] = array(
  27. 'title' => 'Tripal Web Services API',
  28. 'page callback' => 'tripal_ws_services',
  29. 'access arguments' => array('access content'),
  30. 'type' => MENU_CALLBACK,
  31. );
  32. // Tripal Web Services setting groups
  33. $items['admin/tripal/storage/ws'] = array(
  34. 'title' => 'Web Services',
  35. 'description' => t("Import data from other Tripal sites using the web services."),
  36. 'weight' => 20,
  37. 'page callback' => 'system_admin_menu_block_page',
  38. 'access arguments' => array('administer tripal'),
  39. 'file' => 'system.admin.inc',
  40. 'file path' => drupal_get_path('module', 'system'),
  41. );
  42. $items['admin/tripal/storage/ws/tripal_sites'] = array(
  43. 'title' => 'Other Tripal Sites',
  44. 'description' => t('Provides information about other Tripal sites.
  45. This allows data exchange and communication betwen Tripal
  46. enabled sites through the web services.'),
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('tripal_ws_tripal_sites_form'),
  49. 'access arguments' => array('administer tripal'),
  50. 'type' => MENU_NORMAL_ITEM,
  51. 'weight' => 0,
  52. 'file' => 'includes/tripal_ws.admin.inc',
  53. 'file path' => drupal_get_path('module', 'tripal_ws'),
  54. );
  55. $items['admin/tripal/storage/ws/tripal_sites/edit'] = array(
  56. 'title' => 'Add Tripal Site',
  57. 'description' => 'Add a Tripal site',
  58. 'page callback' => 'drupal_get_form',
  59. 'page arguments' => array('tripal_ws_tripal_sites_edit_form'),
  60. 'access arguments' => array('administer tripal'),
  61. 'file' => 'includes/tripal_ws.admin.inc',
  62. 'file path' => drupal_get_path('module', 'tripal_ws'),
  63. 'type' => MENU_LOCAL_ACTION,
  64. 'weight' => 2
  65. );
  66. $items['admin/tripal/storage/ws/tripal_sites/remove/%'] = array(
  67. 'title' => 'Remove Tripal Site',
  68. 'description' => 'Remove a Tripal site',
  69. 'page callback' => 'drupal_get_form',
  70. 'page arguments' => array('tripal_ws_tripal_sites_remove_form', 6),
  71. 'access arguments' => array('administer tripal'),
  72. 'file' => 'includes/tripal_ws.admin.inc',
  73. 'file path' => drupal_get_path('module', 'tripal_ws'),
  74. 'type' => MENU_CALLBACK,
  75. 'weight' => 2
  76. );
  77. return $items;
  78. }
  79. /**
  80. * The callback function for all RESTful web services.
  81. *
  82. */
  83. function tripal_ws_services() {
  84. $ws_path = func_get_args();
  85. $params = $_GET;
  86. unset($params['q']);
  87. // The web services should never be cached.
  88. drupal_page_is_cacheable(FALSE);
  89. // Using the provided version number, determine which web services
  90. // verion to call.
  91. $version = array_shift($ws_path);
  92. if ($version and preg_match('/v\d+\.\d+/', $version)) {
  93. $api_url = 'ws/' . $version;
  94. // Add the file with the appropriate web services.
  95. module_load_include('inc', 'tripal_ws', 'includes/tripal_ws.rest_' . $version);
  96. $version = preg_replace('/\./', '_', $version);
  97. $function = 'tripal_ws_services_' . $version;
  98. $response = array();
  99. if (function_exists($function)) {
  100. $response = $function($api_url, $ws_path, $params);
  101. }
  102. }
  103. else {
  104. // TODO: What do we do if no version is provided?
  105. }
  106. drupal_add_http_header('Content-Type', 'application/ld+json');
  107. print drupal_json_encode($response);
  108. }