tripal_ws.rest_v0.1.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. <?php
  2. /**
  3. *
  4. */
  5. function tripal_ws_services_v0_1($api_url, $ws_path, $params) {
  6. // Set some initial variables.
  7. $response = array();
  8. $version = 'v0.1';
  9. // Set some defaults for the response.
  10. $response['@context'] = array();
  11. // Lump everything ito a try block so that if there is a problem we can
  12. // throw an error and have that returned in the response.
  13. try {
  14. // The services is the first argument
  15. $service = (count($ws_path) > 0) ? $ws_path[0] : '';
  16. switch ($service) {
  17. case 'doc':
  18. tripal_ws_services_v0_1_handle_doc_service($api_url, $response);
  19. break;
  20. case 'content':
  21. tripal_ws_services_v0_1_handle_content_service($api_url, $response, $ws_path, $params);
  22. break;
  23. case 'vocab':
  24. tripal_ws_services_v0_1_handle_vocab_service($api_url, $response, $ws_path);
  25. break;
  26. default:
  27. tripal_ws_services_v0_1_handle_no_service($api_url, $response);
  28. }
  29. }
  30. catch (Exception $e) {
  31. watchdog('tripal_ws', $e->getMessage(), array(), WATCHDOG_ERROR);
  32. $message = $e->getMessage();
  33. drupal_add_http_header('Status', '400 Bad Request');
  34. }
  35. return $response;
  36. }
  37. /**
  38. *
  39. * @param $api_url
  40. * @param $response
  41. * @param $ws_path
  42. */
  43. function tripal_ws_services_v0_1_handle_content_service($api_url, &$response, $ws_path, $params) {
  44. // Get the content type.
  45. $ctype = (count($ws_path) > 1) ? $ws_path[1] : '';
  46. $entity_id = (count($ws_path) > 2) ? $ws_path[2] : '';
  47. // If we have no content type then list all of the available content types.
  48. if (!$ctype) {
  49. tripal_ws_services_v0_1_get_content_types($api_url, $response);
  50. }
  51. // If we don't have an entity ID then show a paged list of entities with
  52. // the given type.
  53. else if ($ctype and !$entity_id) {
  54. tripal_ws_services_v0_1_get_content_type($api_url, $response, $ws_path, $ctype, $params);
  55. }
  56. // If we have a content type and an entity ID then show the entity
  57. else {
  58. tripal_ws_services_v0_1_get_content($api_url, $response, $ws_path, $ctype, $entity_id, $params);
  59. }
  60. }
  61. /**
  62. *
  63. * @param $api_url
  64. * @param $response
  65. * @param $ws_path
  66. */
  67. function tripal_ws_services_v0_1_handle_vocab_service($api_url, &$response, $ws_path) {
  68. // Get the vocab name.
  69. $vocabulary = (count($ws_path) > 1) ? $ws_path[1] : '';
  70. $accession = (count($ws_path) > 2) ? $ws_path[2] : '';
  71. // If we have no $vocabulary type then list all of the available vocabs.
  72. if (!$vocabulary) {
  73. tripal_ws_services_v0_1_get_vocabs($api_url, $response);
  74. }
  75. // If we don't have a $vocabulary then show a paged list of terms.
  76. else if ($vocabulary and !$accession) {
  77. tripal_ws_services_v0_1_get_vocab($api_url, $response, $ws_path, $vocabulary);
  78. }
  79. // If we have a content type and an entity ID then show the entity
  80. else if ($vocabulary and $accession) {
  81. tripal_ws_services_v0_1_get_term($api_url, $response, $ws_path, $vocabulary, $accession);
  82. }
  83. }
  84. /**
  85. *
  86. * @param $api_url
  87. * @param $response
  88. */
  89. function tripal_ws_services_v0_1_get_vocabs($api_url, &$response) {
  90. // First, add the vocabularies used into the @context section.
  91. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  92. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  93. // Next add in the ID for tihs resource.
  94. $response['@id'] = url($api_url . '/vocab', array('absolute' => TRUE));
  95. // Start the list.
  96. $response['@type'] = 'Collection';
  97. $response['totalItems'] = 0;
  98. $response['label'] = 'Content Types';
  99. $response['member'] = array();
  100. $vocabs = db_select('tripal_vocab', 'tv')
  101. ->fields('tv')
  102. ->execute();
  103. // Iterate through the vocabularies and add an entry in the collection.
  104. $i = 0;
  105. while ($vocab = $vocabs->fetchObject()) {
  106. // Add the bundle as a content type.
  107. $response['member'][] = array(
  108. '@id' => url($api_url . '/vocab/' . urlencode($vocab->vocabulary), array('absolute' => TRUE)),
  109. '@type' => 'vocabulary',
  110. 'vocabulary' => $vocab->vocabulary,
  111. );
  112. $i++;
  113. }
  114. $response['totalItems'] = $i;
  115. // Lastly, add in the terms used into the @context section.
  116. $response['@context']['Collection'] = 'hydra:Collection';
  117. $response['@context']['totalItems'] = 'hydra:totalItems';
  118. $response['@context']['member'] = 'hydra:member';
  119. $response['@context']['label'] = 'rdfs:label';
  120. $response['@context']['description'] = 'hydra:description';
  121. }
  122. /**
  123. *
  124. * @param $api_url
  125. * @param $response
  126. * @param $ws_path
  127. */
  128. function tripal_ws_services_v0_1_get_vocab($api_url, &$response, $ws_path, $vocabulary) {
  129. // First, add the vocabularies used into the @context section.
  130. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  131. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  132. $response['@context']['schema'] = 'https://schema.org/';
  133. // Next add in the ID for tihs resource.
  134. $response['@id'] = url($api_url . '/vocab/' . $vocabulary, array('absolute' => TRUE));
  135. // Get the vocabulary
  136. $vocab = tripal_load_vocab_entity(array('vocabulary' => $vocabulary));
  137. // Start the list.
  138. $response['@type'] = 'Collection';
  139. $response['totalItems'] = 0;
  140. $response['label'] = vocabulary . " vocabulary collection";
  141. $response['comment'] = 'The following list of terms may not be the full ' .
  142. 'list for the vocabulary. The terms listed here are only those ' .
  143. 'that have associated content on this site.';
  144. // Get the list of terms for this vocab.
  145. $query = db_select('tripal_term', 'tt')
  146. ->fields('tt', array('id'))
  147. ->condition('vocab_id', $vocab->id)
  148. ->orderBy('accession', 'DESC');
  149. // Iterate through the entities and add them to the list.
  150. $terms = $query->execute();
  151. $i = 0;
  152. while($term = $terms->fetchObject()) {
  153. $term = tripal_load_term_entity(array('term_id' => $term->id));
  154. $response['member'][] = array(
  155. '@id' => url($api_url . '/vocab/' . urlencode($vocabulary) . '/' . urlencode($term->accession), array('absolute' => TRUE)),
  156. '@type' => 'vocabulary_term',
  157. 'vocabulary' => $vocab->vocabulary,
  158. 'accession' => $term->accession,
  159. 'name' => $term->name,
  160. 'definition' => $term->definition,
  161. );
  162. $i++;
  163. }
  164. $response['totalItems'] = $i;
  165. // Lastly, add in the terms used into the @context section.
  166. $response['@context']['Collection'] = 'hydra:Collection';
  167. $response['@context']['totalItems'] = 'hydra:totalItems';
  168. $response['@context']['member'] = 'hydra:member';
  169. $response['@context']['label'] = 'rdfs:label';
  170. $response['@context']['comment'] = 'rdfs:comment';
  171. $response['@context']['itemPage'] = 'schema:itemPage';
  172. }
  173. /**
  174. *
  175. * @param $api_url
  176. * @param $response
  177. * @param $ws_path
  178. */
  179. function tripal_ws_services_v0_1_get_term($api_url, &$response, $ws_path, $vocabulary, $accession) {
  180. // First, add the vocabularies used into the @context section.
  181. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  182. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  183. $response['@context']['schema'] = 'https://schema.org/';
  184. // Get the term.
  185. $term = tripal_load_term_entity(array('vocabulary' => $vocabulary, 'accession' => $accession));
  186. // Next add in the ID and Type for this resources.
  187. $response['@id'] = url($api_url . '/vocab/' . urlencode($vocabulary) . '/' . urlencode($accession), array('absolute' => TRUE));
  188. $response['@type'] = 'vocabulary_term';
  189. $response['label'] = $term->name;
  190. $response['vocabulary'] = $vocabulary;
  191. $response['accession'] = $accession;
  192. $response['name'] = $term->name;
  193. $response['definition'] = $term->definition;
  194. if ($term->url) {
  195. $response['URL'] = $term->url;
  196. }
  197. // Lastly, add in the terms used into the @context section.
  198. $response['@context']['label'] = 'rdfs:label';
  199. $response['@context']['itemPage'] = 'schema:itemPage';
  200. }
  201. /**
  202. * Provides a collection (list) of all of the content types.
  203. *
  204. * @param $api_url
  205. * @param $response
  206. */
  207. function tripal_ws_services_v0_1_get_content_types($api_url, &$response) {
  208. // First, add the vocabularies used into the @context section.
  209. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  210. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  211. // Next add in the ID for tihs resource.
  212. $response['@id'] = url($api_url . '/content', array('absolute' => TRUE));
  213. // Start the list.
  214. $response['@type'] = 'Collection';
  215. $response['totalItems'] = 0;
  216. $response['label'] = 'Content Types';
  217. $response['member'] = array();
  218. // Get the list of published terms (these are the bundle IDs)
  219. $bundles = db_select('tripal_bundle', 'tb')
  220. ->fields('tb')
  221. ->orderBy('tb.label', 'ASC')
  222. ->execute();
  223. // Iterate through the terms and add an entry in the collection.
  224. $i = 0;
  225. while ($bundle = $bundles->fetchObject()) {
  226. $entity = entity_load('TripalTerm', array('id' => $bundle->term_id));
  227. $term = reset($entity);
  228. $vocab = $term->vocab;
  229. $response['@context'][$term->name] = $term->url;
  230. // Get the bundle description. If no description is provided then
  231. // use the term definition
  232. $description = tripal_get_bundle_variable('description', $bundle->id);
  233. if (!$description) {
  234. $description = $term->definition;
  235. }
  236. // Add the bundle as a content type.
  237. $response['member'][] = array(
  238. '@id' => url($api_url . '/content/' . urlencode($bundle->label), array('absolute' => TRUE)),
  239. '@type' => $term->name,
  240. 'label' => $bundle->label,
  241. 'description' => $description,
  242. );
  243. $i++;
  244. }
  245. $response['totalItems'] = $i;
  246. // Lastly, add in the terms used into the @context section.
  247. $response['@context']['Collection'] = 'hydra:Collection';
  248. $response['@context']['totalItems'] = 'hydra:totalItems';
  249. $response['@context']['member'] = 'hydra:member';
  250. $response['@context']['label'] = 'rdfs:label';
  251. $response['@context']['description'] = 'hydra:description';
  252. }
  253. /**
  254. *
  255. * @param $api_url
  256. * @param $response
  257. * @param $ws_path
  258. */
  259. function tripal_ws_services_v0_1_get_content_type($api_url, &$response, $ws_path, $ctype, $params) {
  260. // First, add the vocabularies used into the @context section.
  261. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  262. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  263. // Next add in the ID for tihs resource.
  264. $response['@id'] = url($api_url . '/content/' . $ctype, array('absolute' => TRUE));
  265. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  266. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  267. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  268. $term = reset($term);
  269. $response['@context'][$term->name] = $term->url;
  270. // Start the list.
  271. $response['@type'] = 'Collection';
  272. $response['totalItems'] = 0;
  273. $response['label'] = $bundle->label . " collection";
  274. // Iterate through the fields and create a $field_mapping array that makes
  275. // it easier to determine which filter criteria belongs to which field.
  276. $field_mapping = array();
  277. $fields = field_info_fields();
  278. foreach ($fields as $field) {
  279. if (array_key_exists('TripalEntity', $field['bundles'])) {
  280. foreach ($field['bundles']['TripalEntity'] as $bundle_name) {
  281. if ($bundle_name == $bundle->name) {
  282. if (array_key_exists('semantic_web', $field['settings'])) {
  283. list($vocabulary, $accession) = explode(':', $field['settings']['semantic_web']);
  284. $term = tripal_get_term_details($vocabulary, $accession);
  285. $key = $term['name'];
  286. $key = strtolower(preg_replace('/ /', '_', $key));
  287. $field_mapping[$key] = $field['field_name'];
  288. }
  289. }
  290. }
  291. }
  292. }
  293. // Convert the filters to their field names
  294. $new_params = array();
  295. foreach ($params as $param => $value) {
  296. // Break apart any operators
  297. $key = $param;
  298. $op = '=';
  299. $matches = array();
  300. if (preg_match('/^(.+);(.+)$/', $key, $matches)) {
  301. $key = $matches[1];
  302. $op = $matches[2];
  303. }
  304. // Break apart any subkeys and pull the first one out for the term name key.
  305. $subkeys = explode(',', $key);
  306. if (count($subkeys) > 0) {
  307. $key = array_shift($subkeys);
  308. }
  309. // Map the values in the filters to their appropriate field names.
  310. if (array_key_exists($key, $field_mapping)) {
  311. $field_name = $field_mapping[$key];
  312. if (count($subkeys) > 0) {
  313. $field_name .= '.' . implode('.', $subkeys);
  314. }
  315. $new_params[$field_name]['value'] = $value;
  316. $new_params[$field_name]['op'] = $op;
  317. }
  318. else {
  319. throw new Exception("The filter term, '$key', is not available for use.");
  320. }
  321. }
  322. // Get the list of entities for this bundle.
  323. $query = new TripalFieldQuery();
  324. $query->fieldCondition('content_type', $bundle->id);
  325. foreach($new_params as $field_name => $details) {
  326. $value = $details['value'];
  327. switch ($details['op']) {
  328. case 'eq':
  329. $op = '=';
  330. break;
  331. case 'gt':
  332. $op = '>';
  333. break;
  334. case 'gte':
  335. $op = '>=';
  336. break;
  337. case 'lt':
  338. $op = '<';
  339. break;
  340. case 'lte':
  341. $op = '<=';
  342. break;
  343. case 'ne':
  344. $op = '<>';
  345. break;
  346. case 'contains':
  347. $op = 'CONTAINS';
  348. break;
  349. case 'starts':
  350. $op = 'STARTS WITH';
  351. break;
  352. default:
  353. $op = '=';
  354. }
  355. $query->fieldCondition($field_name, $value, $op);
  356. }
  357. $results = $query->execute();
  358. // Iterate through the entities and add them to the list.
  359. $i = 0;
  360. foreach ($results['TripalEntity'] as $entity_id => $stub) {
  361. $vocabulary = '';
  362. $term_name = '';
  363. // We don't need all of the attached fields for an entity so, we'll
  364. // not use the entity_load() function. Instead just pull it from the
  365. // database table.
  366. $entity = db_select('tripal_entity', 'TE')
  367. ->fields('TE')
  368. ->condition('TE.id', $entity_id)
  369. ->execute()
  370. ->fetchObject();
  371. //$entity = tripal_load_entity('TripalEntity', array($entity->id));
  372. $response['member'][] = array(
  373. '@id' => url($api_url . '/content/' . urlencode($ctype) . '/' . $entity->id, array('absolute' => TRUE)),
  374. '@type' => $ctype,
  375. 'label' => $entity->title,
  376. 'itemPage' => url('/bio_data/' . $entity->id, array('absolute' => TRUE)),
  377. );
  378. $i++;
  379. }
  380. $response['totalItems'] = $i;
  381. // Lastly, add in the terms used into the @context section.
  382. $response['@context']['Collection'] = 'hydra:Collection';
  383. $response['@context']['totalItems'] = 'hydra:totalItems';
  384. $response['@context']['member'] = 'hydra:member';
  385. $response['@context']['label'] = 'rdfs:label';
  386. $response['@context']['itemPage'] = 'schema:itemPage';
  387. // $response['operation'][] = array(
  388. // '@type' => 'hydra:CreateResourceOperation',
  389. // 'hydra:method' => 'PUT'
  390. // );
  391. // $response['query'] = array(
  392. // '@id' => $response['@id'],
  393. // '@type' => 'IriTemplate',
  394. // "template" => $response['@id'] . "{?name,}",
  395. // "mapping" => array(
  396. // array(
  397. // "hydra:variable" => 'name',
  398. // "hydra:property" => 'name',
  399. // )
  400. // )
  401. // );
  402. }
  403. /**
  404. *
  405. * @param unknown $response
  406. * @param unknown $ws_path
  407. * @param unknown $ctype
  408. * @param unknown $entity_id
  409. * @param unknown $params
  410. */
  411. function tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_url, &$response, $ws_path, $ctype, $entity_id, $params) {
  412. // Get information about the fields attached to this bundle and sort them
  413. // in the order they were set for the display.
  414. // TODO: should we allow for custom ordering of fields for web services
  415. // or use the default display ordering?
  416. $instances = field_info_instances('TripalEntity', $bundle->name);
  417. uasort($instances, function($a, $b) {
  418. $a_weight = (is_array($a) && isset($a['display']['default']['weight'])) ? $a['display']['default']['weight'] : 0;
  419. $b_weight = (is_array($b) && isset($b['display']['default']['weight'])) ? $b['display']['default']['weight'] : 0;
  420. if ($a_weight == $b_weight) {
  421. return 0;
  422. }
  423. return ($a_weight < $b_weight) ? -1 : 1;
  424. });
  425. // Iterate through the fields and add each value to the response.
  426. //$response['fields'] = $fields;
  427. foreach ($instances as $field_name => $instance) {
  428. // Ignore the content_type field provided by Tripal.
  429. if ($field_name == 'content_type') {
  430. continue;
  431. }
  432. // Skip hidden fields.
  433. if ($instance['display']['default']['type'] == 'hidden') {
  434. continue;
  435. }
  436. // Get the information about this field. It will have settings different
  437. // from the instance.
  438. $field = field_info_field($field_name);
  439. // By default, the label for the key in the output should be the
  440. // term from the vocabulary that the field is assigned. But in the
  441. // case that the field is not assigned a term, we must use the field name.
  442. $field_name = $instance['field_name'];
  443. $field_settings = $field['settings'];
  444. $key = $field_name;
  445. $key_adj = strtolower(preg_replace('/ /', '_', $key));
  446. if (array_key_exists('semantic_web', $field_settings) and $field_settings['semantic_web']) {
  447. list($vocabulary, $accession) = explode(':', $field_settings['semantic_web']);
  448. $term = tripal_get_term_details($vocabulary, $accession);
  449. if ($term) {
  450. $key = $term['name'];
  451. $key_adj = strtolower(preg_replace('/ /', '_', $key));
  452. $response['@context'][$key_adj] = array(
  453. '@id' => $term['url'],
  454. );
  455. }
  456. }
  457. // If this field should not be attached by default then just add a link
  458. // so that the caller can get the information separately.
  459. $instance_settings = $instance['settings'];
  460. if (array_key_exists('auto_attach', $instance_settings) and
  461. $instance_settings['auto_attach'] != TRUE) {
  462. $response['@context'][$key_adj]['@type'] = '@id';
  463. $response[$key_adj] = url($api_url . '/content/' . $ctype . '/' . $entity->id . '/' . urlencode($key), array('absolute' => TRUE));
  464. continue;
  465. }
  466. // Get the details for this field for the JSON-LD response.
  467. tripal_ws_services_v0_1_get_content_add_field($key_adj, $entity, $field, $instance, $api_url, $response);
  468. }
  469. // Lastly, add in the terms used into the @context section.
  470. $response['@context']['label'] = 'rdfs:label';
  471. $response['@context']['itemPage'] = 'schema:itemPage';
  472. // $response['operation'][] = array(
  473. // '@type' => 'hydra:DeleteResourceOperation',
  474. // 'hydra:method' => 'DELETE'
  475. // );
  476. // $response['operation'][] = array(
  477. // '@type' => 'hydra:ReplaceResourceOperation',
  478. // 'hydra:method' => 'POST'
  479. // );
  480. }
  481. /**
  482. *
  483. * @param unknown $field_arg
  484. * @param unknown $api_url
  485. * @param unknown $response
  486. * @param unknown $ws_path
  487. * @param unknown $ctype
  488. * @param unknown $entity_id
  489. * @param unknown $params
  490. */
  491. function tripal_ws_services_v0_1_get_content_find_field($field_arg, $ctype, $entity_id) {
  492. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  493. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  494. $entity = reset($entity);
  495. $term = NULL;
  496. // Find the field whose term matches the one provied.
  497. $value = array();
  498. $instances = field_info_instances('TripalEntity', $bundle->name);
  499. foreach ($instances as $instance) {
  500. $field_name = $instance['field_name'];
  501. $field = field_info_field($field_name);
  502. $field_settings = $field['settings'];
  503. if (array_key_exists('semantic_web', $field_settings) and
  504. $field_settings['semantic_web']) {
  505. list($vocabulary, $accession) = explode(':', $field_settings['semantic_web']);
  506. $temp_term = tripal_get_term_details($vocabulary, $accession);
  507. if ($temp_term['name'] == $field_arg) {
  508. return array($entity, $bundle, $field, $instance, $temp_term);
  509. }
  510. }
  511. }
  512. }
  513. /**
  514. *
  515. * @param unknown $api_url
  516. * @param unknown $response
  517. * @param unknown $ws_path
  518. * @param unknown $ctype
  519. * @param unknown $entity_id
  520. * @param unknown $params
  521. * @return number
  522. */
  523. function tripal_ws_services_v0_1_get_content($api_url, &$response, $ws_path, $ctype, $entity_id, $params) {
  524. // First, add the vocabularies used into the @context section.
  525. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  526. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  527. // If we have an argument in the 4th element (3rd index) then the user
  528. // is requesting to epxand the details of a field that was not
  529. // initially attached to the enity.
  530. $field_arg = '';
  531. if (array_key_exists(3, $ws_path)) {
  532. $field_arg = $ws_path[3];
  533. list($entity, $bundle, $field, $instance, $term) = tripal_ws_services_v0_1_get_content_find_field($field_arg, $ctype, $entity_id);
  534. // Next add in the ID and Type for this resources.
  535. $key = $term['name'];
  536. $response['@context'][$key] = $term['url'];
  537. $response['@id'] = $key;
  538. // Attach the field and then add it's values to the response.
  539. field_attach_load($entity->type, array($entity->id => $entity), FIELD_LOAD_CURRENT,
  540. array('field_id' => $field['id']));
  541. tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $instance, $api_url, $response, TRUE);
  542. return;
  543. }
  544. // If we don't have a 4th argument then we're loading the base record.
  545. // Get the TripalBundle, TripalTerm and TripalVocab type for this type.
  546. $bundle = tripal_load_bundle_entity(array('label' => $ctype));
  547. $term = entity_load('TripalTerm', array('id' => $bundle->term_id));
  548. $term = reset($term);
  549. $vocab = $term->vocab;
  550. // Add the vocabulary for this content type to the @context section.
  551. if (!array_key_exists($vocab->vocabulary, $response['@context'])) {
  552. // If there is no URL prefix then use this API's vocabulary API
  553. if (property_exists($term, 'urlprefix')) {
  554. $response['@context'][$vocab->vocabulary] = $term->urlprefix;
  555. }
  556. else {
  557. $response['@context'][$vocab->vocabulary] = url($api_url . '/vocab/' . $vocab->vocabulary . '/', array('absolute' => TRUE));
  558. }
  559. }
  560. // Get the TripalEntity
  561. $entity = tripal_load_entity('TripalEntity', array('id' => $entity_id));
  562. $entity = reset($entity);
  563. // Next add in the ID and Type for this resources.
  564. $response['@id'] = url($api_url . '/content/' . $ctype . '/' . $entity_id, array('absolute' => TRUE));
  565. $response['@type'] = $term->name;
  566. $response['@context'][$term->name] = $term->url;
  567. $response['label'] = $entity->title;
  568. $response['itemPage'] = url('/bio_data/' . $entity->id, array('absolute' => TRUE));
  569. tripal_ws_services_v0_1_get_content_add_fields($entity, $bundle, $api_url, $response, $ws_path, $ctype, $entity_id, $params);
  570. //tripal_ws_services_v0_1_write_context($response, $ctype);
  571. }
  572. /**
  573. *
  574. * @param $response
  575. * @param $ctype
  576. */
  577. function tripal_ws_services_v0_1_write_context(&$response, $ctype) {
  578. // We want to write the context to a file. Try to open the lock file. if we
  579. // can't then just leave the context in the response.
  580. $context_uri = file_build_uri("ws.v0_1.$ctype.context.jsonld");
  581. $context_file = drupal_realpath($context_uri);
  582. $context_URL = file_create_url($context_uri);
  583. $lock_fp = fopen("$context_file.lock", "w");
  584. if (flock($lock_fp, LOCK_EX|LOCK_NB)) {
  585. $fp = fopen("$context_file", "w");
  586. if ($fp) {
  587. $context = array('@context' => $response['@context']);
  588. fwrite($fp, json_encode($context));
  589. fflush($fp);
  590. // Release the lock.
  591. flock($lock_fp, LOCK_UN);
  592. // Close the files.
  593. fclose($lock_fp);
  594. fclose($fp);
  595. // Change the context to point to the file
  596. $response['@context'] = $context_URL;
  597. }
  598. }
  599. }
  600. /**
  601. *
  602. */
  603. function tripal_ws_services_v0_1_get_content_add_field($key, $entity, $field, $instance, $api_url, &$response, $is_field_page = NULL) {
  604. // Get the field settings.
  605. $field_name = $field['field_name'];
  606. $field_settings = $field['settings'];
  607. $items = field_get_items('TripalEntity', $entity, $field_name);
  608. $values = array();
  609. for ($i = 0; $i < count($items); $i++) {
  610. // See if the keys in the values array have been mapped to semantic
  611. // web terms.
  612. if (array_key_exists('semantic_web', $items[$i])) {
  613. foreach ($items[$i]['semantic_web'] as $k => $v) {
  614. list($vocabulary, $accession) = explode(':', $v);
  615. $term = tripal_get_term_details($vocabulary, $accession);
  616. if ($k == 'type') {
  617. $items[$i]['value']['@type'] = $items[$i]['value']['type'];
  618. unset($items[$i]['value']['type']);
  619. $response['@context'][$term['name']] = $term['url'];
  620. }
  621. else {
  622. $response['@context'][$k] = $term['url'];
  623. }
  624. }
  625. }
  626. // Recurse through the values array and set the entity elemtns
  627. // and add the fields to the context.
  628. tripal_ws_services_v0_1_get_content_add_field_context($items[$i], $response, $api_url);
  629. // Add the remaining values to the $values array.
  630. $values[] = $items[$i]['value'];
  631. }
  632. // If we only have one value then set the response with just the value.
  633. if (count($values) == 1) {
  634. // If the value is an array and this is the field page then all of those
  635. // key/value pairs should be added directly to the response.
  636. if (is_array($values[0])) {
  637. if ($is_field_page) {
  638. foreach ($values[0] as $k => $v) {
  639. $response[$k] = $v;
  640. }
  641. }
  642. else {
  643. $response[$key] = $values[0];
  644. }
  645. }
  646. // If the value is not an array it's a scalar so add it as is to the
  647. // response.
  648. else {
  649. $response[$key] = $values[0];
  650. }
  651. }
  652. // If we have more than one value then set the response to be a collection.
  653. if (count($values) > 1) {
  654. // If this is the field page then the Collection is added directly to the
  655. // response, otherwise, it's added under the field $key.
  656. if ($is_field_page) {
  657. $response['@type'] = 'Collection';
  658. $response['totalItems'] = count($values);
  659. $response['label'] = $instance['label'];
  660. $response['member'] = $values;
  661. }
  662. else {
  663. $response[$key] = array(
  664. '@type' => 'Collection',
  665. 'totalItems' => count($values),
  666. 'label' => $instance['label'],
  667. 'member' => $values,
  668. );
  669. }
  670. }
  671. }
  672. /**
  673. *
  674. */
  675. function tripal_ws_services_v0_1_get_content_add_field_context(&$items, &$response, $api_url) {
  676. foreach ($items as $key => $value) {
  677. if (is_array($value)) {
  678. tripal_ws_services_v0_1_get_content_add_field_context($items[$key], $response, $api_url);
  679. continue;
  680. }
  681. if ($key == 'entity') {
  682. list($item_etype, $item_eid) = explode(':', $items['entity']);
  683. if ($item_eid) {
  684. $item_entity = tripal_load_entity($item_etype, array($item_eid));
  685. $item_entity = reset($item_entity);
  686. $item_term = tripal_load_term_entity(array('term_id' => $item_entity->term_id));
  687. $items['@id'] = url($api_url . '/content/' . $item_term->name . '/' . $item_eid, array('absolute' => TRUE));
  688. }
  689. unset($items['entity']);
  690. }
  691. }
  692. }
  693. /**
  694. * Provides the Hydra compatible apiDocumentation page that describes this API.
  695. *
  696. * @param $api_url
  697. * @param $response
  698. */
  699. function tripal_ws_services_v0_1_handle_doc_service($api_url, &$response) {
  700. // First, add the vocabularies used into the @context section.
  701. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  702. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  703. // Next add in the ID for tihs resource.
  704. $site_name = variable_get('site_name', '');
  705. $response['@id'] = url($api_url . '/doc/', array('absolute' => TRUE));
  706. $response['title'] = $site_name . ": RESTful Web Services API";
  707. $response['entrypoint'] = url($api_url, array('absolute' => TRUE));
  708. $response['description'] = "A fully queryable REST API using JSON-LD and " .
  709. "discoverable using the WC3 Hydra specification.";
  710. // Lastly, add in the terms used into the @context section.
  711. $response['@context']['title'] = 'hydra:title';
  712. $response['@context']['entrypoint'] = array(
  713. "@id" => "hydra:entrypoint",
  714. "@type" => "@id",
  715. );
  716. $response['@context']['description'] = 'hydra:description';
  717. }
  718. /**
  719. * This function specifies the types of resources avaiable via the API.
  720. *
  721. * @param $api_url
  722. * @param $response
  723. * @param $ws_path
  724. */
  725. function tripal_ws_services_v0_1_handle_no_service($api_url, &$response) {
  726. // First, add the vocabularies used into the @context section.
  727. $response['@context']['rdfs'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  728. $response['@context']['hydra'] = 'http://www.w3.org/ns/hydra/core#';
  729. $response['@context']['dc'] = 'http://purl.org/dc/dcmitype/';
  730. $response['@context']['schema'] = 'https://schema.org/';
  731. // Next add in the ID for tihs resource.
  732. $response['@id'] = url($api_url, array('absolute' => TRUE));
  733. // Start the list.
  734. $response['@type'] = 'Collection';
  735. $response['totalItems'] = 0;
  736. $response['label'] = 'Services';
  737. $response['member'] = array();
  738. // Start the list.
  739. $response['member'][] = array(
  740. '@id' => url($api_url . '/content/', array('absolute' => TRUE)),
  741. '@type' => 'Service',
  742. 'label' => 'Content Types',
  743. 'description' => 'Provides acesss to the biological and ' .
  744. 'ancilliary data available on this site. Each content type ' .
  745. 'represents biological data that is defined in a controlled vocabulary '.
  746. '(e.g. Sequence Ontology term: gene (SO:0000704)).',
  747. );
  748. $response['member'][] = array(
  749. '@id' => url($api_url . '/doc/', array('absolute' => TRUE)),
  750. '@type' => 'Service',
  751. 'label' => 'API Documentation',
  752. 'description' => 'The WC3 Hydra compatible documentation for this API.',
  753. );
  754. $response['member'][] = array(
  755. '@id' => url($api_url . '/vocab/', array('absolute' => TRUE)),
  756. '@type' => 'Service',
  757. 'label' => 'Vocabulary',
  758. 'description' => 'Defines in-house locally defined vocabulary terms that ' .
  759. 'have been added specifically for this site. These terms are typically ' .
  760. 'added because no other appropriate term exists in another community-vetted '.
  761. 'controlled vocabulary.',
  762. );
  763. $response['totalItems'] = count($response['member']);
  764. $response['@context']['Collection'] = 'hydra:Collection';
  765. $response['@context']['totalItems'] = 'hydra:totalItems';
  766. $response['@context']['member'] = 'hydra:member';
  767. $response['@context']['Service'] = 'dc:Service';
  768. $response['@context']['label'] = 'rdfs:label';
  769. $response['@context']['description'] = 'hydra:description';
  770. }