XML.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. Lib::loadClass('Core_XmlWriter');
  3. // $zxo = new XMLDiff\Memory;// @require xmldiff pecl package installed
  4. // TODO:TYPE_RECURCE: if type has local prefix - choose:
  5. // - find recurse typeName and restrictions
  6. // - find types and save in SystemObjectField table (if simpleType)
  7. // - ? find types and save in SystemObject table + SystemObjectField table (if complexType)
  8. class XML {
  9. public static function xmlToArray($xml) {
  10. $z = new XMLReader();
  11. if (!$z) throw new HttpException("Class XMLReader not installed", 500);
  12. if (!$z->xml($xml)) throw new Exception("Failed to parse xml", 500);
  13. return self::xmlReadToArray($z);
  14. }
  15. public static function readXmlFileToArray($filePath) {
  16. $z = new XMLReader();
  17. if (!$z) throw new HttpException("Class XMLReader not installed", 500);
  18. $fileName = basename($filePath);
  19. if (!$z->open($filePath)) throw new Exception("Failed to open ant schema file '{$fileName}'", 500);
  20. return self::xmlReadToArray($z);
  21. }
  22. public static function xmlReadToArray($z) {
  23. while ($z->read()) {
  24. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  25. return self::xmlReadRecurse($z);
  26. }
  27. }
  28. public static function xmlReadRecurse($z) {
  29. $node = [ $z->name, [], [] ];// name, attrs, childrens
  30. $depth = $z->depth;
  31. $isEmpty = $z->isEmptyElement;
  32. if ($z->hasAttributes) {
  33. while ($z->moveToNextAttribute()) {
  34. $node[1][$z->name] = $z->value;
  35. }
  36. }
  37. if ($isEmpty) {
  38. $node[2] = null;
  39. return $node;
  40. }
  41. // switch ($z->name) {
  42. // case 'xsd:complexType': $node[1]['name'] = $z->getAttribute('name'); break;
  43. // case 'xsd:simpleType': $node[1]['name'] = $z->getAttribute('name'); break;
  44. // case 'xsd:restriction': $node[1]['base'] = $z->getAttribute('base'); break;
  45. // case 'xsd:enumeration': $node[1]['value'] = $z->getAttribute('value'); break;
  46. // case 'xsd:element': $node[1] = [
  47. // 'type' => $z->getAttribute('type')
  48. // ]; break;
  49. // case 'xsd:complexContent': break;
  50. // case 'xsd:sequence': break;
  51. // case 'xsd:attribute': $node[1]['base'] = $z->getAttribute('base'); break;
  52. // case 'xsd:extension': $node[1]['base'] = $z->getAttribute('base'); break;
  53. // default: UI::alert('warning', "TODO: read attributes from: d({$z->depth}) name($z->name)");
  54. // }
  55. while ($z->read() && $z->depth > $depth) {
  56. switch ($z->nodeType) {
  57. case XMLReader::CDATA: $node[2][] = (string)$z->value; break;
  58. case XMLReader::TEXT: $node[2][] = (string)$z->value; break;
  59. case XMLReader::ELEMENT: $node[2][] = self::xmlReadRecurse($z); break;
  60. }
  61. }
  62. return $node;
  63. }
  64. public static function printXmlFromArray($xml) {
  65. $xmlWriter = new Core_XmlWriter();
  66. $xmlWriter->openUri('php://output');
  67. $xmlWriter->setIndent(true);
  68. $xmlWriter->startDocument('1.0','UTF-8');
  69. $xmlWriter->h($xml[0], $xml[1], $xml[2]);
  70. $xmlWriter->endDocument();
  71. }
  72. public static function findTargetNamespace($docArray) {
  73. return V::get('targetNamespace', '', $docArray[1]);
  74. }
  75. public static function findSimpleTypeNode($docArray, $name) {
  76. foreach ($docArray[2] as $child) {
  77. if ('simpleType' === self::getTagName($child[0])) {
  78. if ($name === $child[1]['name']) {
  79. return $child;
  80. }
  81. }
  82. }
  83. }
  84. public static function findTargetNamespacePrefix($docArray) {
  85. $tns = self::findTargetNamespace($docArray);
  86. if (!$tns) throw new Exception("targetNamespace not defined");
  87. foreach ($docArray[1] as $attr => $val) {
  88. if ('xmlns:' !== substr($attr, 0, 6)) continue;
  89. if ($tns === $val) return substr($attr, 6);
  90. }
  91. throw new Exception("Missing targetNamespace xmlns:...");
  92. }
  93. public static function findElementName($docArray, $nodeArray) {
  94. if (!empty($nodeArray[1]['name'])) return $nodeArray[1]['name'];
  95. if (!empty($nodeArray[1]['ref'])) return $nodeArray[1]['ref'];
  96. throw new Exception("Missing xsd:element name");
  97. }
  98. public static function findElementType($docArray, $nodeArray) {
  99. $fieldName = self::findElementName($docArray, $nodeArray);
  100. if (!empty($nodeArray[1]['type'])) {
  101. // TODO:TYPE_RECURCE: if local ns prefix then find correct typeName?
  102. // TODO:TYPE_RECURCE:the same for restrictions
  103. $type = $nodeArray[1]['type'];
  104. DBG::log(['is_xs:' => ('xs:' === substr($type, 0, 3)), 'is_xsd:'=>('xsd:' === substr($type, 0, 4))], 'array', "DBG: findElementType field({$fieldName}) type({$type})");
  105. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  106. return $fixedType;
  107. }
  108. list($prefix, $name) = explode(':', $type);
  109. if ($prefix === self::findTargetNamespacePrefix($docArray)) {
  110. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  111. DBG::log($simpleTypeNode, 'array', "\$simpleTypeNode \$fieldName='{$fieldName}' type='{$type}'");
  112. if (!empty($simpleTypeNode[1]['type'])) {
  113. throw new Exception("TODO: findElementType node/@type => 'xsd:simpleType/@type' = '{$simpleTypeNode[1]['type']}'");
  114. }
  115. if (!empty($simpleTypeNode[2][0]) && self::isXsdTag($simpleTypeNode[2][0][0], 'restriction')) {
  116. $restrictionNode = $simpleTypeNode[2][0];
  117. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing xsd:restriction/@base (node/@type => xsd:simpleType/[@type='{$simpleTypeNode[1]['type']}']/xsd:restriction')");
  118. $type = $restrictionNode[1]['base'];
  119. DBG::log($type, 'array', "findElementType \$fieldName='{$fieldName}' type='{$nodeArray[1]['type']}' => type='{$type}'");
  120. // check restrictions - if has enumeration then return 'p5:enum'
  121. $isEnum = false;
  122. if (!empty($restrictionNode[2])) foreach ($restrictionNode[2] as $restr) {
  123. if ('enumeration' === self::getTagName($restr[0])) {
  124. $isEnum = true;
  125. break;
  126. }
  127. }
  128. if ($isEnum) return 'p5:enum';
  129. // TODO: recurse with limit
  130. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  131. return $fixedType;
  132. }
  133. list($prefix, $name) = explode(':', $type);
  134. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  135. DBG::log($simpleTypeNode, 'array', "\$simpleTypeNode \$fieldName='{$fieldName}' ... type='{$type}'");
  136. if (!empty($simpleTypeNode[1]['type'])) {
  137. throw new Exception("TODO: findElementType node/@type => 'xsd:simpleType/@type' = '{$simpleTypeNode[1]['type']}'");
  138. }
  139. if (!empty($simpleTypeNode[2][0]) && self::isXsdTag($simpleTypeNode[2][0][0], 'restriction')) {
  140. $restrictionNode = $simpleTypeNode[2][0];
  141. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing xsd:restriction/@base (node/@type => xsd:simpleType/[@type='{$simpleTypeNode[1]['type']}']/xsd:restriction')");
  142. $type = $restrictionNode[1]['base'];
  143. DBG::log($type, 'array', "findElementType \$fieldName='{$fieldName}' ... type='{$type}'");
  144. return self::convertXsdTypeToXsdPrefix($type);
  145. }
  146. // TODO: throw...
  147. }
  148. // TODO: throw...
  149. // 0 => 'xsd:simpleType',
  150. // 1 => [
  151. // 'name' => 'PROCES_INIT_Simple',
  152. // ],
  153. // 2 => [
  154. // 0 => [
  155. // 0 => 'xsd:restriction',
  156. // 1 => [
  157. // 'base' => 'default_db__x3A__CRM_PROCES:TYPE_Simple',
  158. // ],
  159. // 2 => [
  160. // 0 => [
  161. // 0 => 'xsd:enumeration',
  162. // 1 => [
  163. // 'value' => 'PROCES_INIT',
  164. // ],
  165. // 2 => NULL,
  166. } else {
  167. return $nodeArray[1]['type'];
  168. }
  169. }
  170. if (!empty($nodeArray[1]['ref'])) return 'ref:' . $nodeArray[1]['ref'];
  171. if (empty($nodeArray[2])) throw new Exception("Missing xsd:element childrens - cannot find type");
  172. // TODO: find in loop (xsd:annotation may accure)
  173. if (empty($nodeArray[2][0][0]) || !self::isXsdTag($nodeArray[2][0][0], 'simpleType')) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'");
  174. if (empty($nodeArray[2][0][2][0]) || !self::isXsdTag($nodeArray[2][0][2][0][0], 'restriction')) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'");
  175. if (empty($nodeArray[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  176. $type = $nodeArray[2][0][2][0][1]['base'];
  177. if ($fixedType = self::tryConvertXsdTypeToXsdPrefix($type)) {
  178. return $fixedType;
  179. }
  180. DBG::log($nodeArray[2][0], 'array', "TODO:findElementType: local type with restriction field({$fieldName}) type({$type})");
  181. // TODO: element local type with restriction
  182. // <xsd:element name="TYPE">
  183. // <xsd:simpleType>
  184. // <xsd:restriction base="default_db__x3A__CRM_PROCES:TYPE_Simple">
  185. // <xsd:enumeration value="PROCES_BENEFIT_INFO"/>
  186. // </xsd:restriction>
  187. // </xsd:simpleType>
  188. // </xsd:element>
  189. return $type;
  190. }
  191. public static function tryConvertXsdTypeToXsdPrefix($type) {
  192. try {
  193. $type = self::convertXsdTypeToXsdPrefix($type);
  194. return $type;
  195. } catch (Exception $e) {
  196. DBG::log($e);
  197. }
  198. return false;
  199. }
  200. public static function convertXsdTypeToXsdPrefix($type) {
  201. // TODO: validate if type is supported in object engine and gui
  202. // TODO: prefix p5
  203. list($prefix, $name) = explode(':', $type);
  204. $prefix = ('xs' === $prefix) ? 'xsd' : $prefix;
  205. if ('xsd' === $prefix && 'int' === $name) return 'xsd:integer';
  206. if ('xsd' === $prefix && 'NCName' === $name) return 'xsd:string'; // TODO: restriction?
  207. if ('xsd' === $prefix && 'NMTOKEN' === $name) return 'xsd:string'; // TODO: restriction?
  208. if ('xsd' === $prefix) return implode(":", [ $prefix, $name ]);
  209. if ('p5' === $prefix) return implode(":", [ $prefix, $name ]);
  210. // if ('xs:' === substr($type, 0, 3)) return "xsd:" . substr($type, 3);
  211. // if ('xsd:' === substr($type, 0, 4)) return $type;
  212. throw new Exception("Not implemented type '{$type}'");
  213. }
  214. public static function findElementRestrictions($docArray, $nodeArray) {
  215. $restrictions = [];
  216. $fieldName = self::findElementName($docArray, $nodeArray);
  217. $childNodes = !empty($nodeArray[2]) ? $nodeArray[2] : null; // definition in child nodes
  218. $typeAlias = !empty($nodeArray[1]['type']) ? $nodeArray[1]['type'] : null; // definition alias - search simpleType with name = Type
  219. DBG::log([
  220. 'has $childNodes' => !empty($childNodes),
  221. 'has $typeAlias' => !empty($typeAlias),
  222. '$typeAlias' => $typeAlias,
  223. '$nodeArray' => $nodeArray
  224. ], 'array', "DBG: findElementRestrictions field({$fieldName})");
  225. if (!empty($nodeArray[1]['nillable']) && 'true' === $nodeArray[1]['nillable']) $restrictions['nillable'] = true;
  226. if ($typeAlias) {
  227. list($prefix, $name) = explode(':', $typeAlias);
  228. if ($prefix === XML::findTargetNamespacePrefix($docArray)) {
  229. $simpleTypeNode = self::findSimpleTypeNode($docArray, $name);
  230. return XML::parseSimpleTypeRestrictions($docArray, $simpleTypeNode, $fieldName);
  231. }
  232. }
  233. if ($childNodes) {
  234. foreach ($childNodes as $c) {
  235. switch (XML::getTagName($c[0])) {
  236. case 'annotation': break; // skip xsd:element/xsd:annotation @see findElementAppInfo
  237. case 'simpleType': $restrictions = XML::parseSimpleTypeRestrictions($docArray, $c, $fieldName);
  238. default: {
  239. DBG::log($c, 'array', "Not imeplemented element child '{$c[0]}'");
  240. }
  241. }
  242. }
  243. }
  244. return $restrictions;
  245. }
  246. public static function parseSimpleTypeRestrictions($docArray, $simpleTypeNode, $fieldName) { // $simpleTypeNode must be 'simpleType' === XML::getTagName($nodeArray[0]), expected 'xsd:restriction' child
  247. DBG::log($simpleTypeNode, 'array', "DBG: parseSimpleTypeRestrictions \$simpleTypeNode for field '{$fieldName}'");
  248. if (empty($simpleTypeNode[2])) return []; // Missing childrens in simpleType definition
  249. $restrictionNode = null;
  250. foreach ($simpleTypeNode[2] as $c) {
  251. if ('restriction' == XML::getTagName($c[0])) {
  252. $restrictionNode = $c;
  253. break;
  254. }
  255. }
  256. DBG::log($restrictionNode, 'array', "DBG: parseSimpleTypeRestrictions \$restrictionNode for field '{$fieldName}'");
  257. if (empty($restrictionNode)) return []; // Missing xsd:restriction in simpleType definition
  258. $restrictions = [];
  259. if (empty($restrictionNode[1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'");
  260. // xsd:simpleType/xsd:restriction/xsd:string
  261. if (!empty($restrictionNode[2])) foreach ($restrictionNode[2] as $tagRestriction) {
  262. // xsd:string/xsd:maxLength
  263. $val = $tagRestriction[1]['value'];
  264. if ('enumeration' == XML::getTagName($tagRestriction[0])) {
  265. $restrictions['enumeration'][$val] = $val;
  266. } else {
  267. $restrictions[ XML::getTagName($tagRestriction[0]) ] = $val;
  268. }
  269. }
  270. return $restrictions;
  271. }
  272. public static function findElementAppInfo($docArray, $nodeArray) {
  273. $appInfo = [];
  274. $fieldName = self::findElementName($docArray, $nodeArray);
  275. if (!empty($nodeArray[2])) {
  276. foreach ($nodeArray[2] as $c) {
  277. switch (XML::getTagName($c[0])) {
  278. case 'annotation': { // skip xsd:element/xsd:annotation
  279. DBG::log($c, 'array', "xsd:annotation/xsd:appinfo '{$fieldName}'");
  280. // <xsd:annotation>
  281. // <xsd:appinfo>
  282. // <system_cache__appinfo:flat_relation_cache>
  283. // <system_cache__appinfo:source system_cache__appinfo:name="ID"
  284. // system_cache__appinfo:xpath="default_db__x3A__CRM_WSKAZNIK:CRM_WSKAZNIK/ID_PROCES"/>
  285. $prefix = 'system_cache__appinfo';
  286. foreach ($c[2] as $cc) {
  287. if ('appinfo' == XML::getTagName($cc[0])) {
  288. foreach ($cc[2] as $appTag) {
  289. $appInfo[ XML::getTagName($appTag[0]) ] = XML::readAppInfoRecurse($appTag);
  290. }
  291. }
  292. }
  293. DBG::log($appInfo, 'array', "xsd:annotation/xsd:appinfo '{$fieldName}' \$appInfo");
  294. // <xs:annotation>
  295. // <xs:appinfo>
  296. // <system_cache__appinfo:flat_relation_cache system_cache__appinfo:backref_evaluate="true">
  297. // <system_cache__appinfo:source system_cache__appinfo:name="krs" system_cache__appinfo:xpath="default_db__x3A__BI_audit_KRS:BI_audit_KRS/krs" system_cache__appinfo:ref_engine="view"/>
  298. } break;
  299. case 'simpleType': break; // skip xsd:element/xsd:simpleType @see findElementRestrictions
  300. default: {
  301. DBG::log($c, 'array', "Not imeplemented element child '{$c[0]}'");
  302. }
  303. }
  304. }
  305. }
  306. DBG::log($fieldName, 'array', "findElementAppInfo fieldName='{$fieldName}'");
  307. return $appInfo;
  308. }
  309. public static function getTagName($xsdName) {
  310. return (false !== ($pos = strpos($xsdName, ':')))
  311. ? substr($xsdName, $pos + 1)
  312. : $xsdName;
  313. }
  314. public static function isXsdTag($xsdName, $expectedTagName) {
  315. list($xsdPrefix, $tagName) = explode(':', $xsdName);
  316. switch ($xsdPrefix) {
  317. case 'xs':
  318. case 'xsd': return ($tagName === $expectedTagName);
  319. }
  320. return false;
  321. }
  322. static function isXsdNodeAnnotation($xsdNode) { return self::isXsdTag($xsdNode[0], 'annotation'); }
  323. static function isXsdNodeAppInfo($xsdNode) { return self::isXsdTag($xsdNode[0], 'appinfo'); }
  324. public static function readAppInfoRecurse($nodeArray) {
  325. $appInfo = [];
  326. if (!empty($nodeArray[1])) foreach ($nodeArray[1] as $attrName => $attrVal) {
  327. $appInfo['@' . XML::getTagName($attrName)] = $attrVal;
  328. }
  329. if (!empty($nodeArray[2])) foreach ($nodeArray[2] as $appTag) {
  330. $appInfo[ XML::getTagName($appTag[0]) ] = XML::readAppInfoRecurse($appTag);
  331. }
  332. // TODO: text nodes
  333. return $appInfo;
  334. }
  335. public static function findFieldsFromSequence($docArray, $nodeArray) {
  336. if (!self::isXsdTag($nodeArray[0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'sequence'");
  337. $fields = [];
  338. foreach ($nodeArray[2] as $f) {
  339. if (!self::isXsdTag($f[0], 'element')) {
  340. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  341. continue;
  342. }
  343. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  344. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  345. if ('__' === substr($fieldName, 0, 2)) continue;
  346. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  347. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  348. DBG::log($f, 'array', "Skipping not implemented field structure '{$fieldName}'");
  349. continue;
  350. }
  351. $fields[$fieldName] = [
  352. 'type' => XML::findElementType($docArray, $f),
  353. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  354. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  355. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  356. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  357. ];
  358. }
  359. return $fields;
  360. }
  361. public static function findFieldsFromComplexContent($docArray, $nodeArray) {
  362. // xsd:complexType / xsd:complexContent / xsd:restriction [ @base = "default_db__x3A__CRM_PROCES:CRM_PROCES" ]
  363. // xsd:complexType / xsd:complexContent / xsd:extension [ @base = "default_db__x3A__CRM_PROCES:CRM_PROCES" ]
  364. switch (XML::getTagName($nodeArray[2][0][0])) {
  365. case 'extension': return XML::findFieldsFromExtension($docArray, $nodeArray[2][0]);
  366. case 'restriction': return XML::findFieldsFromRestriction($docArray, $nodeArray[2][0]);
  367. }
  368. // TODO:? $xsdType['extensionBase'] = V::get('base', '', $nodeArray[1]);
  369. // TODO:? $xsdType['restrictionBase'] = V::get('base', '', $nodeArray[1]);
  370. }
  371. public static function findFieldsFromExtension($docArray, $nodeArray) {
  372. $fields = [];
  373. if (!self::isXsdTag($nodeArray[2][0][0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/extension/sequence'");
  374. foreach ($nodeArray[2][0][2] as $f) {
  375. if (!self::isXsdTag($f[0], 'element')) {
  376. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  377. continue;
  378. }
  379. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  380. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  381. if ('__' === substr($fieldName, 0, 2)) continue;
  382. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  383. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  384. continue;
  385. }
  386. $fields[$fieldName] = [
  387. 'type' => XML::findElementType($docArray, $f),
  388. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  389. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  390. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  391. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  392. ];
  393. }
  394. return $fields;
  395. }
  396. public static function findFieldsFromRestriction($docArray, $nodeArray) {
  397. $fields = [];
  398. if (!self::isXsdTag($nodeArray[2][0][0], 'sequence')) throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/restriction/sequence'");
  399. foreach ($nodeArray[2][0][2] as $f) {
  400. if (!self::isXsdTag($f[0], 'element')) {
  401. DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'");
  402. continue;
  403. }
  404. $fieldName = XML::findElementName($docArray, $f); // V::get('name', '', $f[1]);
  405. if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'");
  406. if ('__' === substr($fieldName, 0, 2)) continue;
  407. if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) {
  408. UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'");
  409. continue;
  410. }
  411. $fields[$fieldName] = [
  412. 'type' => XML::findElementType($docArray, $f),
  413. 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'),
  414. 'maxOccurs' => V::get('maxOccurs', '1', $f[1]),
  415. 'restrictions' => XML::findElementRestrictions($docArray, $f),
  416. 'appInfo' => XML::findElementAppInfo($docArray, $f),
  417. ];
  418. }
  419. return $fields;
  420. }
  421. public static function getXsdTypeFromXsdSchema($xsdFilePath, $namespace, $name) {
  422. $schema = XML::readXmlFileToArray($xsdFilePath);
  423. if (empty($schema)) throw new Exception("Missing schema file for '{$namespace}'");
  424. $xsdType = [ // find xsd:element with @name = $name
  425. 'nsPrefix' => null,
  426. 'name' => null,
  427. 'nsUri' => null,
  428. 'primaryKey' => null,
  429. 'targetNsUri' => V::get('targetNamespace', '', $schema[1])
  430. ];
  431. if (!$xsdType['targetNsUri']) throw new Exception("Missing schema target namespace declaration '{$name}'");
  432. foreach ($schema[2] as $n) {
  433. if (!XML::isXsdTag($n[0], 'element')) continue;
  434. if ($name != V::get('name', '', $n[1])) continue;
  435. list($xsdType['nsPrefix'], $xsdType['name']) = explode(':', V::get('type', '', $n[1]));
  436. }
  437. if (!$xsdType['nsPrefix'] || !$xsdType['name']) throw new Exception("Missing schema root element name = '{$name}'");
  438. $xsdType['nsUri'] = V::get("xmlns:{$xsdType['nsPrefix']}", '', $schema[1]);// find xmlns:default_objects = "https://biuro.biall-net.pl/wfs/default_objects"
  439. if (!$xsdType['nsUri']) throw new Exception("Missing schema root element namespace declaration = '{$name}'");
  440. if ($xsdType['nsUri'] != $xsdType['targetNsUri']) throw new Exception("TODO: type ns is not the same as targetNamespace '{$name}'");// TODO
  441. $foundComplexTypes = array_filter($schema[2], function ($n) use ($xsdType) {
  442. return (XML::isXsdTag($n[0], 'complexType') && $xsdType['name'] === V::get('name', '', $n[1]));
  443. });
  444. if (empty($foundComplexTypes)) throw new Exception("Missing complexType node with name='{$xsdType['name']}'");
  445. $nodeComplexType = reset($foundComplexTypes);
  446. DBG::log($nodeComplexType, 'array', "found main complexType node");
  447. if (array_key_exists('system_cache__appinfo:primaryKey', $nodeComplexType[1])) {
  448. $xsdType['primaryKey'] = V::get('system_cache__appinfo:primaryKey', '', $nodeComplexType[1]);
  449. }
  450. $listAnnotations = array_filter($nodeComplexType[2], [ self, 'isXsdNodeAnnotation' ]);
  451. if (!empty($listAnnotations)) {
  452. // DBG::nicePrint($listAnnotations, "\$listAnnotations");
  453. $nodeAnnotation = reset($listAnnotations);
  454. $listAppInfo = array_filter($nodeAnnotation[2], [ self, 'isXsdNodeAppInfo' ]);
  455. if (!empty($listAppInfo)) {
  456. // DBG::nicePrint($listAppInfo, "\$listAppInfo");
  457. $nodeAppInfo = reset($listAppInfo);
  458. $xsdType['appInfo'] = XML::readAppInfoRecurse($nodeAppInfo);
  459. }
  460. }
  461. // complexType/sequence/element
  462. // complexType/complexContent/extension[base=...]/sequence/element
  463. switch ($nodeComplexType[2][0][0]) { // TODO: convert to loop
  464. case 'xs:sequence':
  465. case 'xsd:sequence': $xsdType['struct'] = XML::findFieldsFromSequence($schema, $nodeComplexType[2][0]); break;
  466. case 'xs:complexContent':
  467. case 'xsd:complexContent': $xsdType['struct'] = XML::findFieldsFromComplexContent($schema, $nodeComplexType[2][0]); break;
  468. case 'xs:annotation':
  469. case 'xsd:annotation': {
  470. switch ($nodeComplexType[2][1][0]) {
  471. case 'xs:sequence':
  472. case 'xsd:sequence': $xsdType['struct'] = XML::findFieldsFromSequence($schema, $nodeComplexType[2][1]); break;
  473. case 'xs:complexContent':
  474. case 'xsd:complexContent': $xsdType['struct'] = XML::findFieldsFromComplexContent($schema, $nodeComplexType[2][1]); break;
  475. }
  476. } break;
  477. }
  478. // if ($n[2][0][0] != 'xsd:complexContent') throw new Exception("Error Parsing Schema - expected 'complexType/complexContent'");
  479. if (empty($xsdType['primaryKey'])) {
  480. foreach ($xsdType['struct'] as $fieldName => $field) {
  481. if ('ID' === strtoupper($fieldName)) {
  482. $xsdType['primaryKey'] = $fieldName;
  483. break;
  484. }
  485. }
  486. }
  487. if (empty($xsdType['primaryKey'])) throw new Exception("Missing primaryKey for schema '{$namespace}'");
  488. return $xsdType;
  489. }
  490. }