TripalWebService.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. class TripalWebService {
  3. // --------------------------------------------------------------------------
  4. // EDITABLE STATIC CONSTANTS
  5. //
  6. // The following constants SHOULD be set for each descendent class. They are
  7. // used by the static functions to provide information to Drupal about
  8. // the field and it's default widget and formatter.
  9. // --------------------------------------------------------------------------
  10. /**
  11. * The human-readable label for this web service.
  12. */
  13. public static $label = 'Base WebService';
  14. /**
  15. * A bit of text to describe what this service provides.
  16. */
  17. public static $description = 'This is the base class for Tripal web services as is not meant to be used on it\'s own';
  18. /**
  19. * A machine-readable type for this service. This name must be unique
  20. * among all Tripal web services and is used to form the URL to access
  21. * this service.
  22. */
  23. public static $type = 'services';
  24. // --------------------------------------------------------------------------
  25. // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
  26. // --------------------------------------------------------------------------
  27. /**
  28. * The resource that will be returned by the webservice given the
  29. * arguments provided. This is a private
  30. */
  31. protected $resource;
  32. /**
  33. * An array containing the elements of the URL path. Each level of the
  34. * URL appears in a separate element of the array. The service type and
  35. * version are automatically removed from the array.
  36. */
  37. protected $path;
  38. /**
  39. * The set of paramters provided to the sesrvice. These are the values
  40. * that would occur in a URL after the question mark in an HTTP GET or
  41. * the data items of an HTTP POST.
  42. */
  43. protected $params;
  44. // --------------------------------------------------------------------------
  45. // CONSTRUCTORS
  46. // --------------------------------------------------------------------------
  47. /**
  48. * Implements the constructor.
  49. */
  50. public function __construct() {
  51. $this->resource = new TripalWebServiceResource();
  52. $this->path = array();
  53. $this->params = array();
  54. }
  55. // --------------------------------------------------------------------------
  56. // OVERRIDEABLE FUNCTIONS
  57. // --------------------------------------------------------------------------
  58. /**
  59. * Responds to the request argument provided to the service.
  60. *
  61. * This function should be implemented by a TripalWebService child class.
  62. *
  63. */
  64. public function handleRequest() {
  65. // TODO: make sure the $this->path and $this->params are set before
  66. // continuing.
  67. }
  68. // --------------------------------------------------------------------------
  69. // CLASS FUNCTIONS -- DO NOT OVERRIDE
  70. // --------------------------------------------------------------------------
  71. /**
  72. * Sets the URL path for the resource being called.
  73. *
  74. * @param $path
  75. * An array containing the elements of the URL path. Each level of the
  76. * URL appears in a separate element of the array. The service type and
  77. * version are automatically removed from the array. For example, a
  78. * URL of the type http://localhost/web-services/content/v0.1/Gene/sequence
  79. * will result in a $path array containing the following:
  80. * @code
  81. * array(
  82. * 'Gene',
  83. * 'sequence',
  84. * );
  85. * @endcode
  86. *
  87. * @param unknown $path
  88. */
  89. public function setPath($path) {
  90. $this->path = $path;
  91. }
  92. /**
  93. * Sets the parameters for the resource.
  94. *
  95. * @param $params
  96. * The set of paramters provided to the sesrvice. These are the values
  97. * that would occur in a URL after the question mark in an HTTP GET or
  98. * the data items of an HTTP POST.
  99. */
  100. public function setParams($params) {
  101. $this->params = $params;
  102. }
  103. /**
  104. * Retrieves the version number for this web service.
  105. *
  106. * Each web service must have version number built into the name of the
  107. * class. The version number appears at the end of the class name, begins
  108. * with a lower-case 'v' and is followed by two numbers (major and minor) that
  109. * are separated by an underscore. This function identifies the version
  110. * from the class name and returns it here in a human-readable format.
  111. *
  112. * @param $sanatize
  113. * Set to TRUE to convert the period to underscore.
  114. *
  115. * @return
  116. * The version number for this web service.
  117. */
  118. public function getVersion($sanatize = FALSE) {
  119. $class = get_class($this);
  120. $major_version = '';
  121. $minor_version = '';
  122. if (preg_match('/v(\d+)_(\d+)$/', $class, $matches)) {
  123. $major_version = $matches[1];
  124. $minor_version = $matches[2];
  125. return 'v' . $major_version . '.' . $minor_version;
  126. }
  127. return '';
  128. }
  129. /**
  130. * Retrieves the context section of the response.
  131. *
  132. * The JSON-LD response constists of two sections the '@context' section
  133. * and the data section. This function only returns the context section
  134. * of the response.
  135. *
  136. * @return
  137. * An associative array containing the context section of the response.
  138. */
  139. public function getContext() {
  140. return $this->resource->getContext();
  141. }
  142. /**
  143. * Returns the full web service response.
  144. *
  145. * The response includes both the @context and data sections of the
  146. * JSON-LD response.
  147. *
  148. * @return
  149. * An associative array containing that can be converted to JSON.
  150. */
  151. public function getResponse() {
  152. $context = $this->resource ? $this->resource->getContext() : array();
  153. $type = $this->resource ? $this->resource->getType() : 'unknown';
  154. $json_ld = array(
  155. '@context' => $context,
  156. '@id' => '',
  157. '@type' => $type,
  158. );
  159. // Get the data array and set the IRIs fore each ID.
  160. $data = $this->getData();
  161. $this->setIDs($data);
  162. return array_merge($json_ld, $data);
  163. }
  164. /**
  165. * A recursive function for setting the IRI (i.e. JSON-LD @id property).
  166. *
  167. * @param $data
  168. * An array of data as returned by the $this->getData() function
  169. */
  170. protected function setIDs(&$data) {
  171. global $base_url;
  172. if(array_key_exists('@id', $data)) {
  173. $class = get_class($this);
  174. $version = $this->getVersion();
  175. $type = $class::$type;
  176. $data['@id'] = $base_url . '/web-services/' . $type . '/' . $version . '/' . $data['@id'];
  177. }
  178. foreach ($data as $key => $val) {
  179. if (is_array($val)) {
  180. $this->setIDs($data[$key]);
  181. }
  182. }
  183. }
  184. /**
  185. * Retrieves the data section of the response.
  186. *
  187. * The JSON-LD response constists of two sections the '@context' section
  188. * and the data section. This function only returns the data section
  189. * of the response.
  190. *
  191. * @return
  192. * An associative array containing the data section of the response.
  193. */
  194. public function getData() {
  195. if ($this->resource) {
  196. return $this->resource->getData();
  197. }
  198. return array();
  199. }
  200. /**
  201. * Sets the resource to be returned by this web service.
  202. *
  203. * @param $resource.
  204. * An implementation of a TripalWebServiceResource.
  205. */
  206. public function setResource($resource) {
  207. // Make sure the $servcie provides is a TripalWebServcie class.
  208. if (!is_a($resource, 'TripalWebServiceResource')) {
  209. throw new Exception("Cannot add a new resource to this web service as it is not a TripalWebServiceResource.");
  210. }
  211. $this->resource = $resource;
  212. }
  213. /**
  214. * Set an error for the service.
  215. *
  216. * @param $message
  217. * The error message to report.
  218. */
  219. public function setError($message) {
  220. $this->resource = new TripalWebServiceResource();
  221. $this->resource->addContextItem('error', 'rdfs:error');
  222. $this->resource->addProperty('error', $message);
  223. }
  224. }