tripal_ws.api.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file provides the Tripal Web Services API: a set of functions for
  6. * interacting with the Tripal Web Services.
  7. */
  8. /**
  9. * @defgroup tripal_ws_api Tripal Web Services
  10. *
  11. * @ingroup tripal_api
  12. * The Tripal Web Services API provides a set of functions for interacting
  13. * with the Tripal Web Services.
  14. *
  15. */
  16. /**
  17. * Adjust the values of a field for display in web services.
  18. *
  19. * This hook should be used sparingly. It is meant primarily to adjust 3rd
  20. * Party (non Tripal) fields so that they work with web
  21. * services. The caller should adjust the $items array as needed.
  22. * This change only affects the value displayed in web services. Web services
  23. * expect that every field have a 'value' element for each of the items. If a
  24. * field for some reason does not have a 'value' element then this hook will
  25. * allow setting of that element.
  26. *
  27. * @param $items
  28. * The list of items for the field.
  29. * @param $field
  30. * The field array.
  31. * @param $instance
  32. * The field instance array.
  33. *
  34. * @ingroup tripal_ws_api
  35. */
  36. function hook_tripal_ws_value(&$items, $field, $instance) {
  37. // The image module doesn't properly set the 'value' field, so we'll do it
  38. // here.
  39. if($field['type'] == 'image' and $field['module'] == 'image') {
  40. foreach ($items as $delta => $details) {
  41. if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
  42. $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * Retrieves a list of TripalWebService implementations.
  49. *
  50. * The TripalWebService classes can be added by a site developer that wishes
  51. * to create a new Tripal compatible web serivce. The class file should
  52. * be placed in the [module]/includes/TripalWebService directory. Tripal will
  53. * support any service as long as it is in this directory and extends the
  54. * TripalWebService class.
  55. *
  56. * @return
  57. * A list of TripalWebService names.
  58. */
  59. function tripal_get_web_services() {
  60. $services = array();
  61. $modules = module_list(TRUE);
  62. foreach ($modules as $module) {
  63. // Find all of the files in the tripal_chado/includes/fields directory.
  64. $service_path = drupal_get_path('module', $module) . '/includes/TripalWebService';
  65. $service_files = file_scan_directory($service_path, '/.inc$/');
  66. // Iterate through the fields, include the file and run the info function.
  67. foreach ($service_files as $file) {
  68. $class = $file->name;
  69. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  70. if (class_exists($class) and is_subclass_of($class, 'TripalWebService')) {
  71. $services[] = $class;
  72. }
  73. }
  74. }
  75. return $services;
  76. }
  77. /**
  78. * Loads the TripalWebService class file into scope.
  79. *
  80. * @param $class
  81. * The TripalWebService class to include.
  82. *
  83. * @return
  84. * TRUE if the field type class file was found, FALSE otherwise.
  85. */
  86. function tripal_load_include_web_service_class($class) {
  87. $modules = module_list(TRUE);
  88. foreach ($modules as $module) {
  89. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalWebService/' . $class . '.inc';
  90. if (file_exists($file_path)) {
  91. module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
  92. if (class_exists($class)) {
  93. return TRUE;
  94. }
  95. }
  96. }
  97. return FALSE;
  98. }
  99. /**
  100. * Adds a new site to the web services table.
  101. *
  102. * @param $name
  103. * Name of site to be included.
  104. * @param $url
  105. * URL of site to be added.
  106. * @param $version
  107. * Version of the API being used. default to 1
  108. * @param $description
  109. * A description of the site and any additional info that
  110. * would be helpful for admins.
  111. *
  112. * @return
  113. * TRUE if the site is successfully added, FALSE otherwise.
  114. */
  115. function tripal_add_site($name, $url, $version, $description) {
  116. $check_url = NULL;
  117. $check_name = NULL;
  118. $write_to_db = TRUE;
  119. // When inserting a record.
  120. $check_url =
  121. db_select('tripal_sites', 'ts')
  122. ->fields('ts', array('id'))
  123. ->condition('url', $url)
  124. ->condition('version', $version)
  125. ->execute()
  126. ->fetchField();
  127. $check_name =
  128. db_select('tripal_sites', 'ts')
  129. ->fields('ts', array('id'))
  130. ->condition('name', $name)
  131. ->execute()
  132. ->fetchField();
  133. if ($check_url) {
  134. drupal_set_message(t('The URL and version is used by another site.'), 'error');
  135. $write_to_db = FALSE;
  136. }
  137. if ($check_name) {
  138. drupal_set_message(t('The name is used by another site.'), 'error');
  139. $write_to_db = FALSE;
  140. }
  141. if ($write_to_db === TRUE) {
  142. db_insert('tripal_sites')
  143. ->fields(array(
  144. 'name' => $name,
  145. 'url' => $url,
  146. 'version' => $version,
  147. 'description' => $description
  148. ))
  149. ->execute();
  150. drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
  151. return $write_to_db;
  152. }
  153. return $write_to_db;
  154. }
  155. /**
  156. * Remove a site from the web services table.
  157. *
  158. * @param $record_id
  159. * ID of the record to be deleted.
  160. *
  161. * @return
  162. * TRUE if the record was successfully deleted, FALSE otherwise.
  163. */
  164. function tripal_remove_site($record_id) {
  165. if ($record_id) {
  166. db_delete('tripal_sites')
  167. ->condition('id', $record_id)
  168. ->execute();
  169. drupal_set_message('The Tripal site \'' . $record_id . '\' has been removed.');
  170. return TRUE;
  171. }
  172. return FALSE;
  173. }
  174. /**
  175. * When passed a site_id from the tripal_sites table the full
  176. * site's vocab service is returned in json format
  177. *
  178. *
  179. */
  180. function tripal_web_services_vocab_request($site_id) {
  181. // Get the site url from the tripal_sites table.
  182. if ($site_id) {
  183. $remote_site = db_select('tripal_sites', 'ts')
  184. ->fields('ts')
  185. ->condition('ts.id', $site_id)
  186. ->execute()
  187. ->fetchObject();
  188. }
  189. // Build the URL to the remote web services.
  190. $ws_version = $remote_site->version;
  191. $ws_url = $remote_site->url;
  192. $ws_url = trim($ws_url, '/');
  193. $ws_url .= '/web-services/vocab/' . $ws_version;
  194. // Build and make the request.
  195. $options = [];
  196. $data = drupal_http_request($ws_url, $options);
  197. if (!$data) {
  198. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  199. t('Could not connect to the remote web service.'));
  200. return FALSE;
  201. }
  202. // If the data object has an error then this is some sort of
  203. // connection error (not a Tripal web servcies error).
  204. if (property_exists($data, 'error')) {
  205. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  206. 'Web Services error on remote site: %error.',
  207. array('%error' => $data->error));
  208. return FALSE;
  209. }
  210. // We got a response, so convert it to a PHP array.
  211. $data = drupal_json_decode($data->data);
  212. // Check if there was a Tripal Web Services error.
  213. if (array_key_exists('error', $data)) {
  214. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  215. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  216. 'Web Services error on remote site: %error.',
  217. array('%error' => $error));
  218. return FALSE;
  219. }
  220. return $data;
  221. }
  222. /**
  223. * Makes a request to a remote Tripal web services site.
  224. *
  225. * @param $query
  226. * The query string. This string is added to the URL for the remote
  227. * website.
  228. */
  229. function tripal_web_services_remote_request($remote_site, $query) {
  230. $ctype = $query;
  231. $qdata = '';
  232. if (preg_match('/\?/', $query)) {
  233. list($ctype, $qdata) = explode('?', $query);
  234. }
  235. // Build the URL to the remote web services.
  236. $ws_version = $remote_site->version;
  237. $ws_url = $remote_site->url;
  238. $ws_url = trim($ws_url, '/');
  239. $ws_url .= '/web-services/content/' . $ws_version . '/' . $ctype;
  240. // Build the Query and make and substitions needed.
  241. //dpm($ws_url . '?' . $query);
  242. $options = array(
  243. 'data' => $qdata,
  244. );
  245. $data = drupal_http_request($ws_url, $options);
  246. if (!$data) {
  247. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  248. t('Could not connect to the remote web service.'));
  249. return FALSE;
  250. }
  251. // If the data object has an error then this is some sort of
  252. // connection error (not a Tripal web servcies error).
  253. if (property_exists($data, 'error')) {
  254. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  255. 'Web Services error on remote site: %error.',
  256. array('%error' => $data->error));
  257. return FALSE;
  258. }
  259. // We got a response, so convert it to a PHP array.
  260. $data = drupal_json_decode($data->data);
  261. // Check if there was a Tripal Web Services error.
  262. if (array_key_exists('error', $data)) {
  263. $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
  264. tripal_report_error('tripal_ws', TRIPAL_ERROR,
  265. 'Web Services error on remote site: %error.',
  266. array('%error' => $error));
  267. return FALSE;
  268. }
  269. return $data;
  270. }