tripal_ws.module 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. $items['remote/%/%/%/%'] = array(
  33. 'page callback' => 'tripal_ws_load_remote_entity',
  34. 'page arguments' => array(1, 2, 3, 4),
  35. 'access arguments' => array('access content'),
  36. 'type' => MENU_CALLBACK,
  37. );
  38. // Tripal Web Services setting groups
  39. $items['admin/tripal/storage/ws'] = array(
  40. 'title' => 'Remote Tripal Sites',
  41. 'description' => t("Create mashups of content using data from this site and remote Tripal sites."),
  42. 'weight' => 20,
  43. 'page callback' => 'system_admin_menu_block_page',
  44. 'access arguments' => array('administer tripal'),
  45. 'file' => 'system.admin.inc',
  46. 'file path' => drupal_get_path('module', 'system'),
  47. );
  48. $items['admin/tripal/storage/ws/tripal_sites'] = array(
  49. 'title' => 'Configuration',
  50. 'description' => t('Provides information about other Tripal sites.
  51. This allows data exchange and communication betwen Tripal
  52. enabled sites through the web services.'),
  53. 'page callback' => 'drupal_get_form',
  54. 'page arguments' => array('tripal_ws_tripal_sites_form'),
  55. 'access arguments' => array('administer tripal'),
  56. 'type' => MENU_NORMAL_ITEM,
  57. 'weight' => 0,
  58. 'file' => 'includes/tripal_ws.admin.inc',
  59. 'file path' => drupal_get_path('module', 'tripal_ws'),
  60. );
  61. $items['admin/tripal/storage/ws/tripal_sites/edit'] = array(
  62. 'title' => 'Add Tripal Site',
  63. 'description' => 'Add a Tripal site',
  64. 'page callback' => 'drupal_get_form',
  65. 'page arguments' => array('tripal_ws_tripal_sites_edit_form'),
  66. 'access arguments' => array('administer tripal'),
  67. 'file' => 'includes/tripal_ws.admin.inc',
  68. 'file path' => drupal_get_path('module', 'tripal_ws'),
  69. 'type' => MENU_LOCAL_ACTION,
  70. 'weight' => 2
  71. );
  72. $items['admin/tripal/storage/ws/tripal_sites/remove/%'] = array(
  73. 'title' => 'Remove Tripal Site',
  74. 'description' => 'Remove a Tripal site',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('tripal_ws_tripal_sites_remove_form', 6),
  77. 'access arguments' => array('administer tripal'),
  78. 'file' => 'includes/tripal_ws.admin.inc',
  79. 'file path' => drupal_get_path('module', 'tripal_ws'),
  80. 'type' => MENU_CALLBACK,
  81. 'weight' => 2
  82. );
  83. return $items;
  84. }
  85. /**
  86. * The callback function for all RESTful web services.
  87. *
  88. */
  89. function tripal_ws_services() {
  90. $ws_path = func_get_args();
  91. $params = $_GET;
  92. unset($params['q']);
  93. // The web services should never be cached.
  94. drupal_page_is_cacheable(FALSE);
  95. // Using the provided version number, determine which web services
  96. // verion to call.
  97. $version = array_shift($ws_path);
  98. if ($version and preg_match('/v\d+\.\d+/', $version)) {
  99. $api_url = 'ws/' . $version;
  100. // Add the file with the appropriate web services.
  101. module_load_include('inc', 'tripal_ws', 'includes/tripal_ws.rest_' . $version);
  102. $version = preg_replace('/\./', '_', $version);
  103. $function = 'tripal_ws_services_' . $version;
  104. $response = array();
  105. if (function_exists($function)) {
  106. $response = $function($api_url, $ws_path, $params);
  107. }
  108. }
  109. else {
  110. // TODO: What do we do if no version is provided?
  111. }
  112. drupal_add_http_header('Content-Type', 'application/ld+json');
  113. print drupal_json_encode($response);
  114. }
  115. function tripal_ws_entity_load($entities, $type) {
  116. foreach ($entities as $entity) {
  117. //if ($entiy->type = 'TripalEntity') {
  118. // dpm($entity);
  119. //}
  120. }
  121. }
  122. function tripal_ws_load_remote_entity($site_id, $api_version, $ctype, $id) {
  123. // Get the content type on this site
  124. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  125. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  126. $term = reset($term);
  127. $vocab = $term->vocab;
  128. $query = db_select('tripal_sites', 'ts');
  129. $query->fields('ts');
  130. $query->condition('id', $site_id);
  131. $site = $query->execute()->fetchObject();
  132. if (!$site) {
  133. return 'Could not find specified site.';
  134. }
  135. // Get the content from the web services of the remote site.
  136. $url = $site->url . "/ws/v0.1/content/" . $ctype . "/" . $id;
  137. $json = file_get_contents($url);
  138. $response = json_decode($json, TRUE);
  139. // Set the title for this page to match the title provided.
  140. drupal_set_title($response['label']);
  141. // Attribute this data to the proper source.
  142. $source_url = l($response['label'], $response['itemPage'], array('attributes' => array('target' => '_blank')));
  143. $content = '<div><strong>Source:</strong> ' . $site->name . ': ' . $source_url . '</div>';
  144. // Fake an entity so we can display this content using the same
  145. // entity type on this site.
  146. $entity = new TripalEntity(array(), 'TripalEntity');
  147. $entity->id = 807;
  148. $entity->type = 'TripalEntity';
  149. $entity->bundle = $bundle->name;
  150. $entity->term_id = $term->id;
  151. $entity->title = $response['label'];
  152. $entity->uid = 1;
  153. $entity->status = 1;
  154. // Get the fields and create a list of those that are attached to the bundle.
  155. $fields = field_info_fields();
  156. $my_fields = array();
  157. foreach ($fields as $field) {
  158. if (isset($field['bundles']['TripalEntity'])) {
  159. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  160. if ($bundle_name == $bundle->name) {
  161. $my_fields[] = $field;
  162. }
  163. }
  164. }
  165. }
  166. // Add in the value for the 'content_type' field.
  167. $entity->content_type = array();
  168. $entity->content_type['und'][0]['value'] = $bundle->label;
  169. // For each field we know about that should be attached to our bundle,
  170. // see if we can find a corresponding entry in the results returned from
  171. // the web service call. If so, then add the field to our fake entity.
  172. foreach ($my_fields as $field) {
  173. // Get the semantic web term for this field.
  174. $field_name = $field['field_name'];
  175. $settings = $field['settings'];
  176. // If the field does not have a semantic web mapping, then skip it.
  177. if (!isset($settings['semantic_web'])) {
  178. continue;
  179. }
  180. // Convert the term into it's db and accession elements and look it up
  181. // for more details.
  182. list($vocabulary, $accession) = explode(':', $settings['semantic_web']);
  183. $term = tripal_get_term_details($vocabulary, $accession);
  184. // Convert the term to lowercase and remove spaces so we can compare
  185. // correctly.
  186. $term_name = strtolower(preg_replace('/ /', '_', $term['name']));
  187. // TODO: check for the term in the response makes the assumption
  188. // that the term is the same on both sides. This may not be true. The
  189. // acutal vocab and accession for both terms should be compared.
  190. if (isset($response[$term_name])) {
  191. // If this field is of type '@id' then this links out to another
  192. // URL where that information can be retrieved. We'll have to
  193. // handle that separately.
  194. if (isset($response['@context'][$term_name]['@type']) and
  195. $response['@context'][$term_name]['@type'] == '@id') {
  196. $subquery = json_decode(file_get_contents($response[$term_name]), TRUE);
  197. // If the result is a collection then we want to add each value with
  198. // it's own delta value.
  199. if (array_key_exists('@type', $subquery) and $subquery['@type'] == 'Collection') {
  200. $i = 0;
  201. $f = array();
  202. foreach ($subquery['member'] as $member) {
  203. $f['und'][$i]['value'] = $member;
  204. $i++;
  205. }
  206. $entity->$field_name = $f;
  207. }
  208. // If the result is not a collection then just add it.
  209. else {
  210. unset($subquery['@context']);
  211. unset($subquery['@id']);
  212. $f = array();
  213. $f['und'][0]['value'] = $subquery;
  214. $entity->$field_name = $f;
  215. }
  216. }
  217. // For all fields that are currently attached, add the field and
  218. // value to the entity.
  219. else {
  220. $f = array();
  221. $f['und'][0]['value'] = $response[$term_name];
  222. $entity->$field_name = $f;
  223. }
  224. }
  225. }
  226. // Generate the View for this entity
  227. $entities = array();
  228. $entities[] = $entity;
  229. $view = entity_view('TripalEntity', $entities);
  230. $content .= drupal_render($view['TripalEntity'][807]);
  231. return $content;
  232. }