tripal_core.ws_hal.inc 12 KB

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