TripalVocabService_v0_1.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. class TripalVocabService_v0_1 extends TripalWebService {
  3. /**
  4. * The human-readable label for this web service.
  5. */
  6. public static $label = 'Vocabulary';
  7. /**
  8. * A bit of text to describe what this service provides.
  9. */
  10. public static $description = 'Provides access to vocabulary terms that are in use by this site.';
  11. /**
  12. * A machine-readable type for this service. This name must be unique
  13. * among all Tripal web services and is used to form the URL to access
  14. * this service.
  15. */
  16. public static $type = 'vocab';
  17. /**
  18. * The list of web services.
  19. */
  20. private $services;
  21. /**
  22. * Constructor for the TripalVocabService_v0_1 class.
  23. */
  24. public function __construct($base_path) {
  25. parent::__construct($base_path);
  26. // Get the list of services provided by this Tripal site.
  27. $this->services = tripal_get_web_services();
  28. foreach ($this->services as $sindex => $service_class) {
  29. // Load the class code.
  30. tripal_load_include_web_service_class($service_class);
  31. // Remove this class from the list of services.
  32. if ($service_class::$type == 'vocab') {
  33. unset($this->services[$sindex]);
  34. }
  35. }
  36. }
  37. /**
  38. * @see TripalWebService::handleRequest()
  39. */
  40. public function handleRequest() {
  41. $this->resource->addContextItem('vocab', $this->getServicePath() . '#');
  42. $this->resource->addContextItem('apiDocumentation', 'hydra:apiDocumentation');
  43. $this->resource->addContextItem('supportedClass', 'hydra:supportedClass');
  44. $this->resource->setType('apiDocumentation');
  45. $this->resource->setID('vocab/' . $this->getVersion());
  46. // Iterate through all of the web services and build their documentation
  47. foreach ($this->services as $service_class) {
  48. $service = new $service_class($this->base_path);
  49. $supported_classes = $service->getSupportedClasses();
  50. foreach ($supported_classes as $supported) {
  51. $this->resource->addProperty('supportedClass', $supported);
  52. }
  53. }
  54. // Now add the EntryPoint class.
  55. $entry_point = $this->getEntryPointClass();
  56. $this->resource->addProperty('supportedClass', $entry_point);
  57. // Add the Collection class.
  58. $collection = new TripalWebServiceResource($this->getServicePath());
  59. $collection->addContextItem('supportedOperation', 'hydra:supportedOperation');
  60. $collection->addContextItem('supportedProperty', 'hydra:supportedProperty');
  61. $collection->addContextItem('property', array(
  62. "@id" => "hydra:property",
  63. "@type" => "@id"
  64. ));
  65. $collection->setID('hydra:Collection');
  66. $collection->setType('hydra:Class');
  67. $collection->addProperty('hydra:title', 'Collection');
  68. $collection->addProperty('hydra:description', '');
  69. $vocab = tripal_get_vocabulary_details('hydra');
  70. $url = preg_replace('/{accession}/', 'member', $vocab['urlprefix']);
  71. $collection->addProperty('supportedProperty', array(
  72. "property" => $url,
  73. "hydra:title" => "member",
  74. "hydra:description" => "The members of this collection.",
  75. "required" => null,
  76. "readonly" => FALSE,
  77. "writeonly" => FALSE
  78. ));
  79. $collection->addProperty('supportedProperty', array(
  80. "property" => $url,
  81. "hydra:title" => "totalItems",
  82. "hydra:description" => "The total number of items in the collection.",
  83. "required" => null,
  84. "readonly" => FALSE,
  85. "writeonly" => FALSE
  86. ));
  87. $collection_ops = array();
  88. $collection_op_get = new TripalWebServiceResource($this->base_path);
  89. $collection_op_get->setID('_:collection_retrieve');
  90. $collection_op_get->setType('hydra:Operation');
  91. $collection_op_get->addContextItem('method', 'hydra:method');
  92. $collection_op_get->addContextItem('label', 'rdfs:label');
  93. $collection_op_get->addContextItem('description', 'rdfs:comment');
  94. $collection_op_get->addContextItem('expects', array(
  95. "@id" => "hydra:expects",
  96. "@type" => "@id"
  97. ));
  98. $collection_op_get->addContextItem('returns', array(
  99. "@id" => "hydra:returns",
  100. "@type" => "@id"
  101. ));
  102. $collection_op_get->addContextItem('statusCodes', 'hydra:statusCodes');
  103. $collection_op_get->addProperty('statusCodes', array());
  104. $collection_op_get->addProperty('method', 'GET');
  105. $collection_op_get->addProperty('label', 'Retrieves members of the collection.');
  106. $collection_op_get->addProperty('description', null);
  107. $collection_op_get->addProperty('expects', null);
  108. $collection_op_get->addProperty('returns', 'vocab:Collection');
  109. $collection_op_gets[] = $collection_op_get;
  110. $collection->addProperty('supportedOperation', $collection_op_gets);
  111. $this->resource->addProperty('supportedClass', $collection);
  112. }
  113. /**
  114. * Generates the EntryPoint class for the API documents.
  115. *
  116. * @return TripalWebServiceResource
  117. *
  118. */
  119. private function getEntryPointClass(){
  120. // We need to list the content types as properties of the EntryPoint.
  121. $entry_properties = array();
  122. // Add the EntryPoint Class.
  123. $entry_point = new TripalWebServiceResource($this->base_path);
  124. $entry_point->setID('vocab:EntryPoint');
  125. $entry_point->setType('hydra:Class');
  126. $entry_point->addContextItem('supportedOperation', 'hydra:supportedOperation');
  127. $entry_point->addContextItem('supportedProperty', 'hydra:supportedProperty');
  128. $entry_point->addContextItem('label', 'rdfs:label');
  129. $entry_point->addContextItem('description', 'rdfs:comment');
  130. $entry_point->addContextItem('subClassOf', array(
  131. "@id" => "rdfs:subClassOf",
  132. "@type" => "@id"
  133. ));
  134. $entry_point->addProperty('label', 'EntryPoint');
  135. $entry_point->addProperty('description', 'The main entry point or homepage of the API');
  136. $entry_point->addProperty('subClassOf', NULL);
  137. foreach ($this->services as $service_class) {
  138. $service = new $service_class($this->base_path);
  139. $service_prop_prop = new TripalWebServiceResource($this->getServicePath());
  140. $service_prop_prop->setID('vocab:EntryPoint/' . $service::$type);
  141. $service_prop_prop->setType('hydra:Link');
  142. $service_prop_prop->addContextItem('label', 'rdfs:label');
  143. $service_prop_prop->addContextItem('description', 'rdfs:comment');
  144. $service_prop_prop->addContextItem('domain', array(
  145. "@id" => "rdfs:domain",
  146. "@type" => "@id"
  147. ));
  148. $service_prop_prop->addContextItem('range', array(
  149. "@id" => "rdfs:range",
  150. "@type" => "@id"
  151. ));
  152. $service_prop_prop->addContextItem('supportedOperation', 'hydra:supportedOperation');
  153. $service_prop_prop->addContextItem('property', array(
  154. "@id" => "hydra:property",
  155. "@type" => "@id"
  156. ));
  157. $service_prop_prop->addProperty('label', $service_class::$label);
  158. $service_prop_prop->addProperty('description', $service_class::$description);
  159. $service_prop_prop->addProperty('domain', 'vocab:EntryPoint');
  160. $service_prop_prop->addProperty('range', 'hydra:Collection');
  161. // Add the GET operation to the property.
  162. // TODO: this should be part of the class.
  163. $service_prop_prop_ops = array();
  164. $service_prop_prop_op_get = new TripalWebServiceResource($this->getServicePath());
  165. $service_prop_prop_op_get->setID('_:collection_retrieve');
  166. $service_prop_prop_op_get->setType('hydra:Operation');
  167. $service_prop_prop_op_get->addContextItem('method', 'hydra:method');
  168. $service_prop_prop_op_get->addContextItem('label', 'rdfs:label');
  169. $service_prop_prop_op_get->addContextItem('description', 'rdfs:comment');
  170. $service_prop_prop_op_get->addContextItem('expects', array(
  171. "@id" => "hydra:expects",
  172. "@type" => "@id"
  173. ));
  174. $service_prop_prop_op_get->addContextItem('returns', array(
  175. "@id" => "hydra:returns",
  176. "@type" => "@id"
  177. ));
  178. $service_prop_prop_op_get->addContextItem('statusCodes', 'hydra:statusCodes');
  179. $service_prop_prop_op_get->addProperty('label', 'Retrieves all ' . $service_class::$label . ' entities.');
  180. $service_prop_prop_op_get->addProperty('description', NULL);
  181. $service_prop_prop_op_get->addProperty('method', 'GET');
  182. $service_prop_prop_op_get->addProperty('expects', null);
  183. $service_prop_prop_op_get->addProperty('returns', 'hydra:Collection');
  184. $service_prop_prop_op_get->addProperty('statusCodes', array());
  185. $service_prop_prop_ops[] = $service_prop_prop_op_get;
  186. // Now add the supported operations to this property.
  187. $service_prop_prop->addProperty('supportedOperation', $service_prop_prop_ops);
  188. // Finally, create the property for the EntryPoint that corresponds
  189. // to this service type.
  190. $service_prop = array(
  191. 'property' => $service_prop_prop,
  192. 'hydra:title' => $service_class::$type,
  193. 'hydra:description' => $service_class::$description,
  194. 'required' => null,
  195. 'readonly' => TRUE,
  196. 'writeonly' => FALSE,
  197. );
  198. $entry_properties[] = $service_prop;
  199. }
  200. $entry_point->addProperty('supportedProperty', $entry_properties);
  201. $service_ops = array();
  202. $service_op_get = new TripalWebServiceResource($this->base_path);
  203. $service_op_get->setID('_:entry_point');
  204. $service_op_get->setType('hydra:Operation');
  205. $service_op_get->addContextItem('method', 'hydra:method');
  206. $service_op_get->addContextItem('label', 'rdfs:label');
  207. $service_op_get->addContextItem('description', 'rdfs:comment');
  208. $service_op_get->addContextItem('expects', array(
  209. "@id" => "hydra:expects",
  210. "@type" => "@id"
  211. ));
  212. $service_op_get->addContextItem('returns', array(
  213. "@id" => "hydra:returns",
  214. "@type" => "@id"
  215. ));
  216. $service_op_get->addContextItem('statusCodes', 'hydra:statusCodes');
  217. $service_op_get->addProperty('statusCodes', array());
  218. $service_op_get->addProperty('method', 'GET');
  219. $service_op_get->addProperty('label', 'The APIs main entry point.');
  220. $service_op_get->addProperty('description', null);
  221. $service_op_get->addProperty('expects', null);
  222. $service_op_get->addProperty('returns', 'vocab:EntryPoint');
  223. $service_ops[] = $service_op_get;
  224. $entry_point->addProperty('supportedOperation', $service_ops);
  225. return $entry_point;
  226. }
  227. }