XML.php 23 KB

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