TripalWebService.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * Retrieves the path for the current resource.
  70. *
  71. * This web service class always provides a resource that fits within the
  72. * type of class. The URL for each resource is needed for building the
  73. * full IRI. This path can be concatenated to the Tripal webservices path to
  74. * form the complete IRI for the current resource.
  75. */
  76. public function getResourcePath() {
  77. $class = get_class($this);
  78. return $class::$type;
  79. }
  80. // --------------------------------------------------------------------------
  81. // CLASS FUNCTIONS -- DO NOT OVERRIDE
  82. // --------------------------------------------------------------------------
  83. /**
  84. * Sets the URL path for the resource being called.
  85. *
  86. * @param $path
  87. * An array containing the elements of the URL path. Each level of the
  88. * URL appears in a separate element of the array. The service type and
  89. * version are automatically removed from the array. For example, a
  90. * URL of the type http://localhost/web-services/content/v0.1/Gene/sequence
  91. * will result in a $path array containing the following:
  92. * @code
  93. * array(
  94. * 'Gene',
  95. * 'sequence',
  96. * );
  97. * @endcode
  98. *
  99. * @param unknown $path
  100. */
  101. public function setPath($path) {
  102. $this->path = $path;
  103. }
  104. /**
  105. * Sets the parameters for the resource.
  106. *
  107. * @param $params
  108. * The set of paramters provided to the sesrvice. These are the values
  109. * that would occur in a URL after the question mark in an HTTP GET or
  110. * the data items of an HTTP POST.
  111. */
  112. public function setParams($params) {
  113. $this->params = $params;
  114. }
  115. /**
  116. * Retrieves the version number for this web service.
  117. *
  118. * Each web service must have version number built into the name of the
  119. * class. The version number appears at the end of the class name, begins
  120. * with a lower-case 'v' and is followed by two numbers (major and minor) that
  121. * are separated by an underscore. This function identifies the version
  122. * from the class name and returns it here in a human-readable format.
  123. *
  124. * @param $sanatize
  125. * Set to TRUE to convert the period to underscore.
  126. *
  127. * @return
  128. * The version number for this web service.
  129. */
  130. public function getVersion($sanatize = FALSE) {
  131. $class = get_class($this);
  132. $major_version = '';
  133. $minor_version = '';
  134. if (preg_match('/v(\d+)_(\d+)$/', $class, $matches)) {
  135. $major_version = $matches[1];
  136. $minor_version = $matches[2];
  137. return 'v' . $major_version . '.' . $minor_version;
  138. }
  139. return '';
  140. }
  141. /**
  142. * Retrieves the context section of the response.
  143. *
  144. * The JSON-LD response constists of two sections the '@context' section
  145. * and the data section. This function only returns the context section
  146. * of the response.
  147. *
  148. * @return
  149. * An associative array containing the context section of the response.
  150. */
  151. public function getContext() {
  152. return $this->resource->getContext();
  153. }
  154. /**
  155. * Returns the full web service response.
  156. *
  157. * The response includes both the @context and data sections of the
  158. * JSON-LD response.
  159. *
  160. * @return
  161. * An associative array containing that can be converted to JSON.
  162. */
  163. public function getResponse() {
  164. $context = $this->resource ? $this->resource->getContext() : array();
  165. $type = $this->resource ? $this->resource->getType() : 'unknown';
  166. $json_ld = array(
  167. '@context' => $context,
  168. '@id' => '',
  169. '@type' => $type,
  170. );
  171. // Get the data array and set the IRIs fore each ID.
  172. $data = $this->getData();
  173. $this->setIRI($data);
  174. return array_merge($json_ld, $data);
  175. }
  176. /**
  177. * A recursive function for setting the IRI (i.e. JSON-LD @id property).
  178. *
  179. * @param $data
  180. * An array of data as returned by the $this->getData() function
  181. */
  182. protected function setIRI(&$data) {
  183. global $base_url;
  184. if(array_key_exists('@id', $data)) {
  185. $class = get_class($this);
  186. $version = $this->getVersion();
  187. $type = $class::$type;
  188. $data['@id'] = $base_url . '/web-services/' . $type . '/' . $version . '/' . $data['@id'];
  189. }
  190. foreach ($data as $key => $val) {
  191. if (is_array($val)) {
  192. $this->setIRI($data[$key]);
  193. }
  194. }
  195. }
  196. /**
  197. * Retrieves the data section of the response.
  198. *
  199. * The JSON-LD response constists of two sections the '@context' section
  200. * and the data section. This function only returns the data section
  201. * of the response.
  202. *
  203. * @return
  204. * An associative array containing the data section of the response.
  205. */
  206. public function getData() {
  207. if ($this->resource) {
  208. return $this->resource->getData();
  209. }
  210. return array();
  211. }
  212. /**
  213. * Sets the resource to be returned by this web service.
  214. *
  215. * @param $resource.
  216. * An implementation of a TripalWebServiceResource.
  217. */
  218. public function setResource($resource) {
  219. // Make sure the $servcie provides is a TripalWebServcie class.
  220. if (!is_a($resource, 'TripalWebServiceResource')) {
  221. throw new Exception("Cannot add a new resource to this web service as it is not a TripalWebServiceResource.");
  222. }
  223. $this->resource = $resource;
  224. }
  225. /**
  226. * Set an error for the service.
  227. *
  228. * @param $message
  229. * The error message to report.
  230. */
  231. public function setError($message) {
  232. $this->resource = new TripalWebServiceResource();
  233. $this->resource->addContextItem('error', 'rdfs:error');
  234. $this->resource->addProperty('error', $message);
  235. }
  236. }