WpsHelper.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. Lib::loadClass('Type_WpsProcess');
  3. Lib::loadClass('Api_OwsException');
  4. class Api_WpsHelper {
  5. static function getUserWpsProcessList($userLogin = null) {
  6. // TODO: $userLogin
  7. $defaultWpsProcessList = array_map(function ($wpsProcess) {
  8. return Type_WpsProcess::build($wpsProcess);
  9. }, self::getDefaultWpsProcessList());
  10. $wpsProcessList = []; // [ Type_WpsProcess ]
  11. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  12. $wpsProcessList = array_merge($defaultWpsProcessList, $wpsProcessList);
  13. // return array_map(function ($wpsProcess, $identifier) {
  14. // return (object)[ 'identifier' => $identifier, 'title' => $wpsProcess['title'], 'description' => $wpsProcess['description'] ];
  15. // }, $wpsProcessList, array_keys($wpsProcessList));
  16. return $wpsProcessList;
  17. }
  18. static function getUserWpsProcess($identifier, $userLogin = null) {
  19. // TODO: $userLogin
  20. $defaultWpsProcessList = self::getDefaultWpsProcessList();
  21. if (array_key_exists($identifier, $defaultWpsProcessList)) return Type_WpsProcess::build( $defaultWpsProcessList[$identifier] );
  22. // TODO: fetch from DB - `CRM_#CACHE_ACL_FUNCTIONS` or object like UrlActions
  23. throw new Exception("Forbidden - wps function not exists or access denied", 403);
  24. }
  25. static function getDefaultWpsProcessList() {
  26. $defaultWpsProcessList = [];
  27. $wpsArgTypeName = [
  28. 'minOccurs' => "1", 'maxOccurs' => "1", // 1 arg is required
  29. 'identifier' => 'typeName',
  30. 'title' => "Feature typeName",
  31. 'description' => "Feature typeName eg.: 'default_db/PROBLEMS'",
  32. 'dataType' => 'literal',
  33. 'xsdType' => 'p5:typeName',
  34. ];
  35. $wpsArgPrimaryKey = [
  36. 'minOccurs' => "1", 'maxOccurs' => "unbounded", // min 1 arg is required
  37. 'identifier' => 'primaryKey',
  38. 'title' => "Feature primaryKey",
  39. 'description' => "Feature primaryKey",
  40. 'dataType' => 'literal',
  41. 'xsdType' => 'p5:primaryKey',
  42. ];
  43. $wpsArgRemoteTypeName = [
  44. 'minOccurs' => "1", 'maxOccurs' => "1", // 1 arg is required
  45. 'identifier' => 'remoteTypeName',
  46. 'title' => "Remote Feature typeName",
  47. 'description' => "Remote Feature typeName eg.: 'default_db/PROBLEMS'",
  48. 'dataType' => 'literal',
  49. 'xsdType' => 'p5:typeName',
  50. ];
  51. $wpsArgRemotePrimaryKey = [
  52. 'minOccurs' => "1", 'maxOccurs' => "unbounded", // min 1 arg is required
  53. 'identifier' => 'remotePrimaryKey',
  54. 'title' => "Remote Feature primaryKey",
  55. 'description' => "Remote Feature primaryKey",
  56. 'dataType' => 'literal',
  57. 'xsdType' => 'p5:primaryKey',
  58. ];
  59. { // Selected Feature
  60. $selectFeatureArgFilterQuery = [
  61. 'minOccurs' => "0", 'maxOccurs' => "1", // optional max 1 arg
  62. 'identifier' => 'filterQuery',
  63. 'title' => "Query string",
  64. 'description' => "Query string eg.: 'f_ID=123&sf_Msgs=NEW_MSGS'",
  65. 'dataType' => 'literal',
  66. 'xsdType' => 'p5:queryString',
  67. ];
  68. $selectFeatureBase = [
  69. 'dataInputs' => [
  70. $wpsArgTypeName,
  71. $wpsArgPrimaryKey,
  72. ],
  73. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  74. 'defaultOutput' => [ 'MimeType' => "application/json" ], // TODO: result type - like Insert Wfs Transaction result - list of: Success (primaryKey = 123), ...
  75. 'supportedOutput' => [
  76. [ 'MimeType' => "application/json" ],
  77. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.0" ],
  78. [ 'MimeType' => "text/xml; subtype=wfs-collection/1.1" ],
  79. // [ 'MimeType' => "application/wfs-collection-1.0" ],
  80. // [ 'MimeType' => "application/wfs-collection-1.1" ],
  81. // [ 'MimeType' => "application/zip" ],
  82. ]
  83. ];
  84. $defaultWpsProcessList['p5:selectFeature'] = array_merge([ 'title' => "Select feature", 'descriptin' => "Select feature (set attribute @selected to true)" ], $selectFeatureBase);
  85. $defaultWpsProcessList['p5:unselectFeature'] = array_merge([ 'title' => "Unselect feature", 'descriptin' => "Unselect feature (set attribute @selected to false)" ], $selectFeatureBase);
  86. $defaultWpsProcessList['p5:getSelectedFeatures'] = array_merge([ 'title' => "Get selected features", 'descriptin' => "Get list of selected features (with attribute @selected = true)" ], $selectFeatureBase);
  87. $defaultWpsProcessList['p5:unselectAllFeatures'] = array_merge([ 'title' => "Unselect all features", 'descriptin' => "Unselect all features (set attribute @selected to false)" ], $selectFeatureBase, [
  88. 'dataInputs' => [
  89. $wpsArgTypeName,
  90. ]
  91. ]);
  92. $defaultWpsProcessList['p5:selectAllFeaturesMatchingFilter'] = array_merge([ 'title' => "Select all features matching filter" ], $selectFeatureBase, [
  93. 'dataInputs' => [
  94. $wpsArgTypeName,
  95. array_merge($wpsArgPrimaryKey, [ 'minOccurs' => 0 ]), // optional arg
  96. $selectFeatureArgFilterQuery,
  97. ]
  98. ]);
  99. }
  100. { // Relations (ref's)
  101. $wpsArgTypeName = [
  102. 'minOccurs' => "1", 'maxOccurs' => "1", // 1 arg is required
  103. 'identifier' => 'typeName',
  104. 'title' => "Feature typeName",
  105. 'description' => "Feature typeName eg.: 'default_db/PROBLEMS'",
  106. 'dataType' => 'literal',
  107. 'xsdType' => 'p5:typeName',
  108. ];
  109. $relationsBase = [
  110. 'dataInputs' => [
  111. $wpsArgTypeName,
  112. array_merge($wpsArgPrimaryKey, [ 'maxOccurs' => 1 ]),
  113. $wpsArgRemoteTypeName,
  114. $wpsArgRemotePrimaryKey,
  115. ],
  116. // 'MimeType' => text/xml, 'Encoding' => base64, 'Schema' => http://foo.bar/gml/3.1.0/polygon.xsd
  117. 'defaultOutput' => [ 'MimeType' => "application/json" ],
  118. 'supportedOutput' => [
  119. [ 'MimeType' => "application/json" ],
  120. ]
  121. ];
  122. $defaultWpsProcessList['p5:getRelations'] = array_merge([ 'title' => "Fetch relations between features" ], $relationsBase, [
  123. 'dataInputs' => [
  124. $wpsArgTypeName,
  125. array_merge($wpsArgPrimaryKey, [ 'maxOccurs' => 1 ]),
  126. $wpsArgRemoteTypeName,
  127. ],
  128. ]);
  129. $defaultWpsProcessList['p5:createRelation'] = array_merge([ 'title' => "Create relation between features" ], $relationsBase);
  130. $defaultWpsProcessList['p5:deleteRelation'] = array_merge([ 'title' => "Delete relation between features" ], $relationsBase);
  131. }
  132. array_walk($defaultWpsProcessList, function (&$wpsFunction, $identifier) {
  133. $wpsFunction['identifier'] = $identifier;
  134. });
  135. return $defaultWpsProcessList;
  136. }
  137. static function validateProcessArgs($wpsProcess, $args) {
  138. foreach ($wpsProcess->dataInputs as $input) {
  139. $identifier = $input->identifier;
  140. // TODO: required? minOccurs > 0
  141. if ($input->minOccurs > 0 && empty($args[ $identifier ])) throw new Api_OwsException("Missing value for '{$identifier}'", 501, null, 'MissingParameterValue', 'request');
  142. $totalValues = count($args[ $identifier ]);
  143. if ($totalValues > 1) {
  144. if ( 'unbounded' !== $input->maxOccurs && $totalValues > $input->maxOccurs ) {
  145. throw new Api_OwsException("Too many values for '{$identifier}'", 501, null, 'TooManyParameterValues', 'request');
  146. }
  147. }
  148. // TODO: validate values with type definition - _xsdType
  149. }
  150. }
  151. static function executeProcess($wpsProcess, $args, $responseForm = 'json') {
  152. $className = implode("_", array_merge(
  153. [
  154. 'Api',
  155. 'Process',
  156. ], array_map('ucfirst', explode(':', $wpsProcess->identifier))
  157. ) );
  158. if (!Lib::tryLoadClass($className)) {
  159. throw new Api_OwsException("Api Process not found '{$wpsProcess->identifier}'", 500, null, 'ApiProcessNotFound', 'request');
  160. }
  161. $className::run($args, $responseForm);
  162. }
  163. }