tripal_ws.module 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/ws'] = array(
  34. 'title' => 'Web Services',
  35. 'description' => t("Exchange data between 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/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. return $items;
  56. }
  57. /**
  58. * The callback function for all RESTful web services.
  59. *
  60. */
  61. function tripal_ws_services() {
  62. $ws_path = func_get_args();
  63. $params = $_GET;
  64. unset($params['q']);
  65. // The web services should never be cached.
  66. drupal_page_is_cacheable(FALSE);
  67. // Using the provided version number, determine which web services
  68. // verion to call.
  69. $version = array_shift($ws_path);
  70. if ($version and preg_match('/v\d+\.\d+/', $version)) {
  71. $api_url = 'ws/' . $version;
  72. // Add the file with the appropriate web services.
  73. module_load_include('inc', 'tripal_ws', 'includes/tripal_ws.rest_' . $version);
  74. $version = preg_replace('/\./', '_', $version);
  75. $function = 'tripal_ws_services_' . $version;
  76. $response = array();
  77. if (function_exists($function)) {
  78. $response = $function($api_url, $ws_path, $params);
  79. }
  80. }
  81. else {
  82. // TODO: What do we do if no version is provided?
  83. }
  84. drupal_add_http_header('Content-Type', 'application/ld+json');
  85. print drupal_json_encode($response);
  86. }