tripal_core.ws_hal.inc 12 KB

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