tripal_ws.module

The Tripal Web Service Module

File

tripal_ws/tripal_ws.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * The Tripal Web Service Module
  5. */
  6. /**
  7. * @defgroup tripal_ws Tripal Web Services Module
  8. * @ingroup tripal
  9. * @{
  10. * The Tripal Web Services module provides functionality for managing RESTful
  11. * web services based on the W3C Hydra standard.
  12. * @}
  13. */
  14. /**
  15. * @defgroup tripal_api Tripal API
  16. * @ingroup tripal
  17. * @{
  18. * Tripal provides an application programming interface (API) to support
  19. * customizations and creation of new extensions.
  20. * @
  21. */
  22. require_once "api/tripal_ws.api.inc";
  23. require_once "includes/tripal_ws.field_storage.inc";
  24. require_once "includes/tripal_ws.fields.inc";
  25. require_once "includes/TripalWebService.inc";
  26. require_once "includes/TripalWebServiceResource.inc";
  27. require_once "includes/TripalWebServiceCollection.inc";
  28. // Web Services Fields
  29. require_once "includes/TripalFields/WebServicesField.inc";
  30. require_once "includes/TripalFields/WebServicesFieldWidget.inc";
  31. require_once "includes/TripalFields/WebServicesFieldFormatter.inc";
  32. /**
  33. * Implements hook_init()
  34. */
  35. function tripal_ws_init() {
  36. global $base_url;
  37. $api_url = $base_url . '/web-sevices/';
  38. $vocab = tripal_get_vocabulary_details('hydra');
  39. // Following the WC3 Hydra documentation, we want to add LINK to the header
  40. // of the site that indicates where the API documentation can be found.
  41. // This allows a hydra-enabled client to discover the API and use it.
  42. $attributes = array(
  43. 'rel' => $vocab['sw_url'] . 'apiDocumentation',
  44. 'href' => $api_url . '/doc/v0.1',
  45. );
  46. drupal_add_html_head_link($attributes, $header = FALSE);
  47. }
  48. /**
  49. * Implements hook_menu().
  50. * Defines all menu items needed by Tripal Core
  51. *
  52. * @ingroup tripal_ws
  53. */
  54. function tripal_ws_menu() {
  55. // Web Services API callbacks.
  56. $items['web-services'] = array(
  57. 'title' => 'Tripal Web Services API',
  58. 'page callback' => 'tripal_ws_get_services',
  59. 'access arguments' => array('access content'),
  60. 'type' => MENU_CALLBACK,
  61. );
  62. $items['remote/%/%/%/%'] = array(
  63. 'page callback' => 'tripal_ws_load_remote_entity',
  64. 'page arguments' => array(1, 2, 3, 4),
  65. 'access arguments' => array('access content'),
  66. 'type' => MENU_CALLBACK,
  67. );
  68. // Tripal Web Services setting groups
  69. $items['admin/tripal/storage/ws'] = array(
  70. 'title' => 'Remote Tripal Sites',
  71. 'description' => t("Create mashups of content using data from this site and remote Tripal sites."),
  72. 'weight' => 20,
  73. 'page callback' => 'system_admin_menu_block_page',
  74. 'access arguments' => array('administer tripal'),
  75. 'file' => 'system.admin.inc',
  76. 'file path' => drupal_get_path('module', 'system'),
  77. );
  78. $items['admin/tripal/storage/ws/tripal_sites'] = array(
  79. 'title' => 'Configuration',
  80. 'description' => t('Provides information about other Tripal sites.
  81. This allows data exchange and communication betwen Tripal
  82. enabled sites through the web services.'),
  83. 'page callback' => 'drupal_get_form',
  84. 'page arguments' => array('tripal_ws_tripal_sites_form'),
  85. 'access arguments' => array('administer tripal'),
  86. 'type' => MENU_NORMAL_ITEM,
  87. 'weight' => 0,
  88. 'file' => 'includes/tripal_ws.admin.inc',
  89. 'file path' => drupal_get_path('module', 'tripal_ws'),
  90. );
  91. $items['admin/tripal/storage/ws/tripal_sites/edit'] = array(
  92. 'title' => 'Add Tripal Site',
  93. 'description' => 'Add a Tripal site',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_ws_tripal_sites_edit_form'),
  96. 'access arguments' => array('administer tripal'),
  97. 'file' => 'includes/tripal_ws.admin.inc',
  98. 'file path' => drupal_get_path('module', 'tripal_ws'),
  99. 'type' => MENU_LOCAL_ACTION,
  100. 'weight' => 2
  101. );
  102. $items['admin/tripal/storage/ws/tripal_sites/remove/%'] = array(
  103. 'title' => 'Remove Tripal Site',
  104. 'description' => 'Remove a Tripal site',
  105. 'page callback' => 'drupal_get_form',
  106. 'page arguments' => array('tripal_ws_tripal_sites_remove_form', 6),
  107. 'access arguments' => array('administer tripal'),
  108. 'file' => 'includes/tripal_ws.admin.inc',
  109. 'file path' => drupal_get_path('module', 'tripal_ws'),
  110. 'type' => MENU_CALLBACK,
  111. 'weight' => 2
  112. );
  113. return $items;
  114. }
  115. /**
  116. * The callback function for all RESTful web services.
  117. *
  118. */
  119. function tripal_ws_get_services() {
  120. global $base_url;
  121. $service_path = $base_url . '/web-services';
  122. // This should go out as ld+json
  123. drupal_add_http_header('Content-Type', 'application/ld+json');
  124. // Add a link header for the vocabulary service so that clients
  125. // know where to find the docs.
  126. tripal_load_include_web_service_class('TripalDocService_v0_1');
  127. $service = new TripalDocService_v0_1($service_path);
  128. $vocab = tripal_get_vocabulary_details('hydra');
  129. drupal_add_http_header('Link', '<' . $service->getServicePath() . '>; rel="' . $vocab['sw_url'] . 'apiDocumentation"');
  130. drupal_add_http_header('Cache-Control', "no-cache");
  131. try {
  132. $ws_path = func_get_args();
  133. $args = $_GET;
  134. unset($args['q']);
  135. // The web services should never be cached.
  136. drupal_page_is_cacheable(FALSE);
  137. // The Tripal web services bath will be:
  138. // [base_path]/web-services/[service name]/v[major_version].[minor_version]
  139. $matches = array();
  140. $service = '';
  141. $major_version = '';
  142. $minor_version = '';
  143. $list_services = FALSE;
  144. // If there is no path then we should list all of the services available.
  145. if (empty($ws_path)) {
  146. tripal_ws_list_services();
  147. return;
  148. }
  149. // A service path will have the service name in $ws_path[0] and the
  150. // version in $ws_path[1]. If we check that the version is correctly
  151. // formatted then we can look for the service class and invoke it.
  152. else if (preg_match("/^v(\d+)\.(\d+)$/", $ws_path[1], $matches)) {
  153. $service_type = $ws_path[0];
  154. $major_version = $matches[1];
  155. $minor_version = $matches[2];
  156. $service_version = 'v' . $major_version . '.' . $minor_version;
  157. }
  158. // If the URL doesn't match then return not found.
  159. else {
  160. throw new Exception("Unsupported service URL: '" . $ws_path[1] . "'");
  161. }
  162. // Get the service that matches the service_name
  163. $service = NULL;
  164. $services = tripal_get_web_services();
  165. foreach ($services as $service_class) {
  166. tripal_load_include_web_service_class($service_class);
  167. if ($service_class::$type == $service_type) {
  168. $service = new $service_class($service_path);
  169. if ($service->getVersion() == $service_version) {
  170. break;
  171. }
  172. $service = NULL;
  173. }
  174. }
  175. // If a service was not provided then return an error.
  176. if (!$service) {
  177. throw new Exception('The service type, "' . $service_type . '", is not available');
  178. }
  179. // Adjust the path to remove the service type and the version.
  180. $adj_path = $ws_path;
  181. array_shift($adj_path);
  182. array_shift($adj_path);
  183. // Now call the service to handle the request.
  184. $service->setPath($adj_path);
  185. $service->setParams($args);
  186. $service->handleRequest();
  187. $response = $service->getResponse();
  188. print drupal_json_encode($response);
  189. }
  190. catch (Exception $e) {
  191. $service = new TripalWebService($service_path);
  192. $service->setError($e->getMessage());
  193. $response = $service->getResponse();
  194. print drupal_json_encode($response);
  195. }
  196. }
  197. /**
  198. * Generates the list of services as the "home page" for Tripal web services.
  199. */
  200. function tripal_ws_list_services() {
  201. global $base_url;
  202. $base_path = $base_url . '/web-services';
  203. // Create an instance of the TriaplWebService class and use it to build
  204. // the entry point for the web serivces.
  205. $service = new TripalWebService($base_path);
  206. // Get the list of web service classes.
  207. $services = tripal_get_web_services();
  208. // Create the parent resource which is a collection.
  209. $resource = new TripalWebServiceResource($base_path);
  210. // Add the vocabulary to the context.
  211. tripal_load_include_web_service_class('TripalDocService_v0_1');
  212. $service = new TripalDocService_v0_1($base_path);
  213. $resource->addContextItem('vocab', $service->getServicePath() . '#');
  214. $resource->addContextItem('EntryPoint', 'vocab:EntryPoint');
  215. $resource->setType('EntryPoint');
  216. // Now add the services as properties.
  217. foreach ($services as $service_class) {
  218. tripal_load_include_web_service_class($service_class);
  219. if ($service_class == 'TripalDocService_v0_1') {
  220. continue;
  221. }
  222. $service = new $service_class($base_path);
  223. $resource->addContextItem($service_class::$type, array(
  224. '@id' => 'vocab:EntryPoint/' . $service_class::$type,
  225. '@type' => '@id',
  226. ));
  227. $resource->addProperty($service_class::$type, $service->getServicePath());
  228. }
  229. $service->setResource($resource);
  230. $response = $service->getResponse();
  231. print drupal_json_encode($response);
  232. }
  233. /**
  234. * The callback function for all RESTful web services.
  235. *
  236. */
  237. function tripal_ws_services() {
  238. $ws_path = func_get_args();
  239. $params = $_GET;
  240. unset($params['q']);
  241. // The web services should never be cached.
  242. drupal_page_is_cacheable(FALSE);
  243. // Using the provided version number, determine which web services
  244. // verion to call.
  245. $version = array_shift($ws_path);
  246. if ($version and preg_match('/v\d+\.\d+/', $version)) {
  247. $api_url = 'ws/' . $version;
  248. // Add the file with the appropriate web services.
  249. module_load_include('inc', 'tripal_ws', 'includes/tripal_ws.rest_' . $version);
  250. $version = preg_replace('/\./', '_', $version);
  251. $function = 'tripal_ws_services_' . $version;
  252. $response = array();
  253. if (function_exists($function)) {
  254. $response = $function($api_url, $ws_path, $params);
  255. }
  256. }
  257. else {
  258. // TODO: What do we do if no version is provided?
  259. }
  260. drupal_add_http_header('Content-Type', 'application/ld+json');
  261. print drupal_json_encode($response);
  262. }
  263. /**
  264. *
  265. * @param $site_id
  266. * @param $api_version
  267. * @param $ctype
  268. * @param $id
  269. *
  270. * @return
  271. */
  272. function tripal_ws_load_remote_entity($site_id, $api_version, $ctype, $id) {
  273. // Get the content type on this site
  274. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  275. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  276. $term = reset($term);
  277. $vocab = $term->vocab;
  278. $query = db_select('tripal_sites', 'ts');
  279. $query->fields('ts');
  280. $query->condition('id', $site_id);
  281. $site = $query->execute()->fetchObject();
  282. if (!$site) {
  283. return 'Could not find specified site.';
  284. }
  285. // Get the content from the web services of the remote site.
  286. $url = $site->url . "/ws/v0.1/content/" . $ctype . "/" . $id;
  287. $json = file_get_contents($url);
  288. $response = json_decode($json, TRUE);
  289. // Set the title for this page to match the title provided.
  290. drupal_set_title($response['label']);
  291. // Attribute this data to the proper source.
  292. $source_url = l($response['label'], $response['ItemPage'], array('attributes' => array('target' => '_blank')));
  293. $content = '<div><strong>Source:</strong> ' . $site->name . ': ' . $source_url . '</div>';
  294. // Fake an entity so we can display this content using the same
  295. // entity type on this site.
  296. $entity = new TripalEntity(array(), 'TripalEntity');
  297. $entity->id = 807;
  298. $entity->type = 'TripalEntity';
  299. $entity->bundle = $bundle->name;
  300. $entity->term_id = $term->id;
  301. $entity->title = $response['label'];
  302. $entity->uid = 1;
  303. $entity->status = 1;
  304. // Get the fields and create a list of those that are attached to the bundle.
  305. $fields = field_info_fields();
  306. $my_fields = array();
  307. foreach ($fields as $field) {
  308. if (isset($field['bundles']['TripalEntity'])) {
  309. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  310. if ($bundle_name == $bundle->name) {
  311. $my_fields[] = $field;
  312. }
  313. }
  314. }
  315. }
  316. // Add in the value for the 'content_type' field.
  317. $entity->content_type = array();
  318. $entity->content_type['und'][0]['value'] = $bundle->label;
  319. // For each field we know about that should be attached to our bundle,
  320. // see if we can find a corresponding entry in the results returned from
  321. // the web service call. If so, then add the field to our fake entity.
  322. foreach ($my_fields as $field) {
  323. // Get the semantic web term for this field.
  324. $field_name = $field['field_name'];
  325. $settings = $field['settings'];
  326. // If the field does not have a semantic web mapping, then skip it.
  327. if (!isset($settings['semantic_web'])) {
  328. continue;
  329. }
  330. // Convert the term into it's db and accession elements and look it up
  331. // for more details.
  332. list($vocabulary, $accession) = explode(':', $settings['semantic_web']);
  333. $term = tripal_get_term_details($vocabulary, $accession);
  334. // Convert the term to lowercase and remove spaces so we can compare
  335. // correctly.
  336. $term_name = strtolower(preg_replace('/ /', '_', $term['name']));
  337. // TODO: check for the term in the response makes the assumption
  338. // that the term is the same on both sides. This may not be true. The
  339. // acutal vocab and accession for both terms should be compared.
  340. if (isset($response[$term_name])) {
  341. // If this field is of type '@id' then this links out to another
  342. // URL where that information can be retrieved. We'll have to
  343. // handle that separately.
  344. if (isset($response['@context'][$term_name]['@type']) and
  345. $response['@context'][$term_name]['@type'] == '@id') {
  346. $subquery = json_decode(file_get_contents($response[$term_name]), TRUE);
  347. // If the result is a collection then we want to add each value with
  348. // it's own delta value.
  349. if (array_key_exists('@type', $subquery) and $subquery['@type'] == 'Collection') {
  350. $i = 0;
  351. $f = array();
  352. foreach ($subquery['member'] as $member) {
  353. $f['und'][$i]['value'] = $member;
  354. $i++;
  355. }
  356. $entity->$field_name = $f;
  357. }
  358. // If the result is not a collection then just add it.
  359. else {
  360. unset($subquery['@context']);
  361. unset($subquery['@id']);
  362. $f = array();
  363. $f['und'][0]['value'] = $subquery;
  364. $entity->$field_name = $f;
  365. }
  366. }
  367. // For all fields that are currently attached, add the field and
  368. // value to the entity.
  369. else {
  370. $f = array();
  371. $f['und'][0]['value'] = $response[$term_name];
  372. $entity->$field_name = $f;
  373. }
  374. }
  375. }
  376. // Generate the View for this entity
  377. $entities = array();
  378. $entities[] = $entity;
  379. $view = entity_view('TripalEntity', $entities);
  380. $content .= drupal_render($view['TripalEntity'][807]);
  381. return $content;
  382. }
  383. function tripal_ws_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  384. if ($form['#instance']['entity_type'] == 'TripalEntity') {
  385. $form['field']['cardinality']['#access'] = FALSE;
  386. }
  387. }
  388. /*
  389. * Returns the decoded json data for a specific field.
  390. */
  391. function tripal_ws_remote_data_single_field_pull($field, $entity_url){
  392. $options = array();
  393. $full_url = $entity_url . '/' . $field;
  394. $data = drupal_http_request($full_url, $options);
  395. if(!empty($data)){
  396. $data = drupal_json_decode($data->data);
  397. }
  398. return $data;
  399. }
  400. /**
  401. * Implements hook_entity_info_alter()
  402. *
  403. * Add the web services display as a view mode.
  404. */
  405. function tripal_ws_entity_info_alter(&$entity_info) {
  406. // Set the controller class for nodes to an alternate implementation of the
  407. // DrupalEntityController interface.
  408. $entity_info['TripalEntity']['view modes'] += array(
  409. 'tripal_ws' => array(
  410. 'label' => t('Tripal Web Services'),
  411. 'custom settings' => FALSE,
  412. ),
  413. );
  414. }