tripal_core.ws_hal.inc

File

tripal_core/includes/tripal_core.ws_hal.inc
View source
  1. <?php
  2. function tripal_core_chado_hal_api() {
  3. global $base_url;
  4. // Set some initial variables.
  5. $response = array();
  6. $result = array();
  7. $status = 'success';
  8. $version = 'v0.1';
  9. $message = '';
  10. $api_url = "$base_url/ws/chado/$version";
  11. $page_limit = 25;
  12. $pager_id = 0;
  13. // Lump everything ito a try block so that if there is a problem we can
  14. // throw an error and have that returned in the response.
  15. try {
  16. $id = 0;
  17. $action = '';
  18. // If we don't have a table name then return the list of all tables.
  19. if (!arg(3)) {
  20. $tables = chado_get_table_names(FALSE);
  21. foreach ($tables as $table) {
  22. $result['_links']['chado_tables'][] = array('href' => "$api_url/$table");
  23. }
  24. // $tables = chado_get_custom_table_names(FALSE);
  25. // foreach ($tables as $table) {
  26. // $result['_links']['custom_tables'][] = array('href' => "$api_url/$table");
  27. // }
  28. // $tables = chado_get_mview_table_names();
  29. // foreach ($tables as $table) {
  30. // $result['_links']['mviews'][] = array('href' => "$api_url/$table");
  31. // }
  32. }
  33. else {
  34. // GET THE BASE TABLE TO QUERY
  35. // The table name is always specifid as the 3rd argument in the
  36. // current Drupal path.
  37. $table_name = arg(3);
  38. if (!chado_table_exists($table_name)) {
  39. throw new Exception("Table, '$table_name', is not a valid table.");
  40. }
  41. $schema = chado_get_schema($table_name);
  42. $pkey = $schema['primary key'][0];
  43. // GET THE RECORD AND THE ACTION TO PERFORM
  44. // If the fourth argument is numeric then the user is requesting a
  45. // record from the table. Otherwise the users is specifying an
  46. // action to perform on the table.
  47. if (is_numeric(arg(4))) {
  48. $id = arg(4);
  49. if (arg(5)) {
  50. $action = arg(5);
  51. }
  52. }
  53. else {
  54. $action = arg(4);
  55. }
  56. // Get any URL query arguments
  57. $query = drupal_get_query_parameters();
  58. switch ($action) {
  59. case 'schema':
  60. $result = $schema;
  61. break;
  62. case 'add':
  63. case 'edit':
  64. case 'delete':
  65. throw new Exception("Action, '$action', is currently not supported.");
  66. break;
  67. default:
  68. // Specify the values for selecing records.
  69. $values = array();
  70. if ($id) {
  71. $values[$pkey] = $id;
  72. }
  73. // Specify the options for retrieving data.
  74. $options = array(
  75. 'return_array' => 1,
  76. 'pager' => array(
  77. 'limit' => $page_limit,
  78. 'element' => $pager_id
  79. ),
  80. );
  81. // Generate the chado variable.
  82. $var = chado_generate_var($table_name, $values, $options);
  83. // If we have more than one record returned then this is a collection and
  84. // we should create the appropriate JSON for a collection.
  85. if (count($var) > 1) {
  86. // Get the total number of records
  87. $total = chado_pager_get_count($pager_id);
  88. $curr_page = array_key_exists('page', $query) ? $query['page'] : 0;
  89. $first_page = '0';
  90. $last_page = ceil($total / $page_limit) - 1;
  91. $result['_links']['first'] = array('href' => "$api_url/$table_name");
  92. if ($curr_page > 0) {
  93. $prev_page = $curr_page - 1;
  94. if ($prev_page != $first_page) {
  95. $result['_links']['previous'] = array('href' => "$api_url/$table_name?page=$prev_page");
  96. }
  97. else {
  98. $result['_links']['previous'] = $result['_links']['first'];
  99. }
  100. }
  101. if ($curr_page < $last_page) {
  102. $next_page = $curr_page + 1;
  103. $result['_links']['next'] = array('href' => "$api_url/$table_name?page=$next_page");
  104. }
  105. if ($last_page > $first_page) {
  106. $result['_links']['last'] = array('href' => "$api_url/$table_name?page=$last_page");
  107. }
  108. // Add the number of elements for this collection
  109. $result['count'] = count($var);
  110. $result['total'] = (integer) $total;
  111. $result['current_page'] = (integer) $curr_page;
  112. $result['items_per_page'] = $page_limit;
  113. // Do any expansion requested.
  114. if ($action == 'expand') {
  115. $var = tripal_core_chado_ws_api_expand_object($var, $query);
  116. }
  117. // recursively reformat the expanded objects to match HAL requirements.
  118. foreach ($var as $item) {
  119. $item = tripal_core_chado_ws_api_object_format($table_name, $item, $schema, $api_url, $query);
  120. $result['_embedded'][$table_name][] = $item;
  121. }
  122. }
  123. // If we only have one record then add it as a single record to the JSON.
  124. else {
  125. $item = $var[0];
  126. // Do any expansion requested.
  127. if ($action == 'expand') {
  128. $item = tripal_core_chado_ws_api_expand_object($item, $query);
  129. }
  130. // recursively reformat the expanded objects to match HAL requirements.
  131. $item = tripal_core_chado_ws_api_object_format($table_name, $item, $schema, $api_url, $query);
  132. $result = $item;
  133. }
  134. }
  135. }
  136. }
  137. catch (Exception $e) {
  138. watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
  139. $message = $e->getMessage();
  140. $status = 'error';
  141. $result = array();
  142. }
  143. // The responses follow the same format as the AGAVE API with a
  144. // status, message, version and all data in the "result" object.
  145. $response['status'] = $status;
  146. $response['message'] = $message;
  147. $response['version'] = $version;
  148. $response['source'] = array(
  149. 'site_name' => variable_get('site_name', "Unspecified"),
  150. 'site_url' => $base_url,
  151. 'site_slogan' => variable_get('site_slogan', "Unspecified"),
  152. 'site_email' => variable_get('site_mail', "Unspecified"),
  153. );
  154. $response['result'] = $result;
  155. print drupal_json_output($response);
  156. }
  157. /**
  158. *
  159. * @param unknown $object
  160. * @param unknown $query
  161. */
  162. function tripal_core_chado_ws_api_expand_object($var, $query) {
  163. $page_limit = 25;
  164. $pager_id = 0;
  165. $options = array(
  166. 'return_array' => 1,
  167. 'pager' => array(
  168. 'limit' => $page_limit,
  169. 'element' => $pager_id
  170. ),
  171. );
  172. // If the user has requested to expand any fields then do that
  173. if (array_key_exists('table', $query)) {
  174. $expand_tables = explode(',', $query['table']);
  175. foreach($expand_tables as $table) {
  176. // Does the table exist?
  177. if(!chado_table_exists($table)) {
  178. throw new Exception("Table, '$table', is not a valid table and thus cannot be expanded.");
  179. }
  180. // Expand the variable.
  181. $var = chado_expand_var($var, 'table', $table, $options);
  182. // if the table could not be expanded then the chado_expand_var
  183. // function just returns an empty record but sets the table name
  184. // in the object. For the JSON, we still want to create an _embedded
  185. // record so we have to create a stdClass object and set the
  186. // table name.
  187. if (property_exists($var, $table) and !isset($var->$table)) {
  188. $var->$table = new stdClass();
  189. $var->$table->tablename = $table;
  190. }
  191. }
  192. }
  193. if (array_key_exists('field', $query)) {
  194. $expand_fields = explode(',', $query['field']);
  195. foreach($expand_fields as $field) {
  196. // TODO: check to make sure the field exists
  197. $var = chado_expand_var($var, 'field', $field);
  198. }
  199. }
  200. if (array_key_exists('fkey', $query)) {
  201. $expand_fkeys = explode(',', $query['fkey']);
  202. foreach($expand_fkeys as $fkey) {
  203. // TODO: check to make sure the fkey exists
  204. $var = chado_expand_var($var, 'foreign_key', $fkey);
  205. }
  206. }
  207. return $var;
  208. }
  209. /**
  210. *
  211. * @param $object
  212. * @param $schema
  213. * @param $api_url
  214. */
  215. function tripal_core_chado_ws_api_object_format($table_name, $object, $schema, $api_url, $query) {
  216. global $base_url;
  217. $pkey = $schema['primary key'][0];
  218. $id = $object->$pkey;
  219. // Add the self link first
  220. if ($id) {
  221. $object->_links['self'] = array('href' => "$api_url/$table_name/$id");
  222. $object->_links['show_expansion'] = array('href' => "$api_url/$table_name/$id?show_expansion=1");
  223. }
  224. else {
  225. $object->_links['self'] = array('href' => "$api_url/$table_name");
  226. $object->_links['show_expansion'] = array('href' => "$api_url/$table_name?show_expansion=1");
  227. }
  228. // Add the links for the table.
  229. $object->_links["tables"] = array('href' => "$api_url");
  230. $object->_links["schema"] = array('href' => "$api_url/$table_name/schema");
  231. // Add links for editing, insert, delete but only if user has permission.
  232. // TODO: how do we set permissions?
  233. $object->_links["add"] = array('href' => "$api_url/$table_name/add");
  234. // Add the links if an id is available.
  235. if ($id) {
  236. $object->_links["edit"] = array('href' => "$api_url/$table_name/$id/edit");
  237. $object->_links["delete"] = array('href' => "$api_url/$table_name/$id/delete");
  238. }
  239. // Add the link to the Drupal page if a node exists.
  240. if (property_exists($object, 'nid')) {
  241. $object->_links["view"] = array('href' => $base_url . url("node/$object->nid"));
  242. // Unset the node ID because it's really only needed within the context
  243. // of the local Drupal site.
  244. unset($object->nid);
  245. }
  246. // It doesn't make sense to allow expansion of node information outside
  247. // of the context of the local Drupal site so remove this object.
  248. unset($object->expandable_nodes);
  249. // Only include links for expanding if the option to exclude them has not
  250. // been passed.
  251. if (!array_key_exists('show_expansion', $query)) {
  252. unset($object->expandable_fields);
  253. unset($object->expandable_foreign_keys);
  254. unset($object->expandable_tables);
  255. }
  256. // Deal with the expandable tables/fields/fkeys/nodes. Sometimes there are
  257. // specified in PHP with explicit numerical indexes and sometimes not. But,
  258. // the JSON converter will maintain the indexes if present which creates
  259. // an inconsistent look. So, we use the array_values function to just
  260. // get the list.
  261. if (array_key_exists('expandable_tables', $object)) {
  262. $object->expandable_tables = array_values($object->expandable_tables);
  263. if (count($object->expandable_tables) > 0) {
  264. $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/expand?table={table}[,{table}...]");
  265. $object->_links["expand_table"][] = array('href' => "$api_url/$table_name/$id/expand?table={table}[,{table}...]");
  266. }
  267. else {
  268. unset($object->expandable_tables);
  269. }
  270. }
  271. if (array_key_exists('expandable_fields', $object)) {
  272. $object->expandable_fields = array_values($object->expandable_fields);
  273. if (count($object->expandable_fields) > 0) {
  274. $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/expand?field={field}[,{field}...]");
  275. $object->_links["expand_field"][] = array('href' => "$api_url/$table_name/$id/expand?field={field}[,{field}...]");
  276. }
  277. else {
  278. unset($object->expandable_fields);
  279. }
  280. }
  281. if (array_key_exists('expandable_foreign_keys', $object)) {
  282. $object->expandable_foreign_keys = array_values($object->expandable_foreign_keys);
  283. if (count($object->expandable_foreign_keys) > 0) {
  284. $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/expand?fkey={fkey}[,{fkey}...]");
  285. $object->_links["expand_fkey"][] = array('href' => "$api_url/$table_name/$id/expand?fkey={fkey}[,{fkey}...]");
  286. }
  287. else {
  288. unset($object->expandable_foreign_keys);
  289. }
  290. }
  291. // iterate through the items in the object and see if they in turn are
  292. // objects. If so, then recurse.
  293. foreach ($object as $key => $value) {
  294. // If any values are objects then recurse and format them correctly.
  295. if (is_object($value)) {
  296. $table_name = $value->tablename;
  297. $schema = chado_get_schema($table_name);
  298. if ($schema) {
  299. // Replace the object with the actual value if it exists. If there is
  300. // no key value then this is probably an expanded table so just unset
  301. if (property_exists($value, $key)) {
  302. $object->$key = $value->$key;
  303. }
  304. else {
  305. unset($object->$key);
  306. }
  307. // Recursively format the object.
  308. $value = tripal_core_chado_ws_api_object_format($table_name, $value, $schema, $api_url, $query);
  309. // Add the object as an "_embedded" object of the JSON.
  310. if (property_exists($object,'_embedded') and
  311. array_key_exists($table_name, $object->_embedded)) {
  312. // If the element is already an array then add to it, otherwise
  313. // convert it into an array.
  314. if (is_array($object->_embedded[$table_name])) {
  315. $object->_embedded[$table_name][] = $value;
  316. }
  317. else {
  318. $first = $object->_embedded[$table_name];
  319. $object->_embedded[$table_name] = array();
  320. $object->_embedded[$table_name] = $first;
  321. $object->_embedded[$table_name][] = $value;
  322. }
  323. }
  324. // This is the first time this embedded table has been seen
  325. // there fore, add the value as a single element.
  326. else {
  327. $object->_embedded[$table_name] = $value;
  328. }
  329. }
  330. else {
  331. throw new Exception("Table, '$table_name', is not a valid table.");
  332. }
  333. }
  334. }
  335. if (array_key_exists('no_links', $query)) {
  336. unset($object->_links);
  337. }
  338. return $object;
  339. }