[ '@namespace' => 'default_objects/SystemObjectField', '@primaryKey' => 'fieldNamespace', 'idZasob' => [ '@type' => 'xsd:integer' ], 'idDatabase' => [ '@type' => 'xsd:integer' ], '_rootTableName' => [ '@type' => 'xsd:string' ], 'objectNamespace' => [ '@type' => 'xsd:string' ], 'fieldNamespace' => [ '@type' => 'xsd:string' ], 'xsdType' => [ '@type' => 'xsd:string' ], 'xsdRestrictions' => [ '@type' => 'xsd:string' ], 'minOccurs' => [ '@type' => 'xsd:string' ], 'maxOccurs' => [ '@type' => 'xsd:string' ], // '0..unbounded', 'isActive' => [ '@type' => 'xsd:integer' ], // installed 'description' => [ '@type' => 'xsd:string' ], // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ], // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ], // 'A_RECORD_UPDATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'zaktualizowaƂ' ], // 'A_RECORD_UPDATE_DATE' => [ '@type' => 'xsd:date', '@label' => 'zaktualizowano' ], ] ]; // public $_rootTableName = 'CRM_LISTA_ZASOBOW'; public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT_FIELD'; public $_version = '1'; public function updateCache($namespace = null) { DBG::simpleLog('schema', "SystemObjectField::updateCache({$namespace})..."); // DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG DB::getPDO()->execSql(" create table if not exists `{$this->_rootTableName}` ( `fieldNamespace` varchar(255) DEFAULT '', `idZasob` int(11) DEFAULT NULL, `idDatabase` int(11) NOT NULL, `_rootTableName` varchar(255) DEFAULT '', `objectNamespace` varchar(255) DEFAULT '', `xsdType` varchar(255) DEFAULT '', `xsdRestrictions` varchar(1000) DEFAULT '', `minOccurs` int(11) DEFAULT '0', `maxOccurs` varchar(11) DEFAULT '1' COMMENT '0..unbounded', `isActive` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'installed', `description` varchar(255) DEFAULT '', UNIQUE KEY `idZasob` (idZasob), UNIQUE KEY `namespace` (objectNamespace, fieldNamespace), KEY `isActive` (isActive) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 "); DB::getPDO()->update($this->_rootTableName, 'objectNamespace', $namespace, ['isActive' => 0]); $sysObjectStorage = SchemaFactory::loadDefaultObject('SystemObject'); if (!$namespace) throw new Exception("Missing namespace '{$namespace}'"); $objectItem = $sysObjectStorage->getItem($namespace); DBG::nicePrint($objectItem, '$objectItem'); DBG::log($objectItem, 'array', '$objectItem'); switch ($objectItem['_type']) { case 'AntAcl': $this->updateCacheAntAcl($objectItem); break; case 'TableAcl': $this->updateCacheTableAcl($objectItem); break; default: throw new Exception("TODO: Not Implemented type '{$objectItem['_type']}'"); } // TODO: mv from methods: SchemaFactory::loadDefaultObject('SystemObject')->updateItem([ // 'namespace' => $item['namespace'], // 'isStructInstalled' => 1 // ]); } public function updateCacheAntAcl($item) { Lib::loadClass('AntAclBase'); $antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']); if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists"); // if (file_exists("{$antAclPath}/{$item['name']}.xsd")) { // DBG::log("{$antAclPath}/{$item['name']}.xsd", 'string', 'xsd file exists'); // $outputType = 'XML'; // $antOutput = file_get_contents("{$antAclPath}/{$item['name']}.xsd"); // } else { // DBG::log($antAclPath, 'string', '$antAclPath'); // $antBin = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant'; // $cmd = "cd {$antAclPath} && {$antBin} DescribeFeatureType 2>&1"; // V::exec($cmd, $out, $ret); // DBG::log($out, 'array', "DescribeFeatureType ret({$ret})"); // $outputType = 'XML'; // $antOutput = []; $startRead = false; // foreach ($out as $line) { // // $line = " [echo] OUTPUT__TYPE__XML" // if ('[echo]' == substr(trim($line), 0, 6)) { // $line = trim(substr(trim($line), 7)); // } // // if (!$startRead) { // if ('OUTPUT__TYPE__XML' == $line) $outputType = 'XML'; // if ('OUTPUT__TYPE__HTML' == $line) $outputType = 'HTML'; // if ('OUTPUT__START' == $line) { // $startRead = true; // continue; // } // } else { // if (' 'max-height:400px; overflow:scroll'], htmlspecialchars($antOutput)); // } else if ('HTML' == $outputType) { // echo UI::h('div', ['class'=>"container", 'style'=>"padding:12px; border:1px solid #ddd"], $antOutput); // } // } Lib::loadClass('XML'); $schema = XML::readXmlFileToArray("{$antAclPath}/{$item['name']}.xsd"); if (empty($schema)) throw new Exception("Missing schema file for '{$item['namespace']}'"); $xsdType = [ // find xsd:element with @name = $item['name'] 'nsPrefix' => null, 'name' => null, 'nsUri' => null, 'targetNsUri' => V::get('targetNamespace', '', $schema[1]) ]; if (!$xsdType['targetNsUri']) throw new Exception("Missing schema target namespace declaration '{$item['name']}'"); foreach ($schema[2] as $n) { if ('xsd:element' != $n[0]) continue; if ($item['name'] != V::get('name', '', $n[1])) continue; list($xsdType['nsPrefix'], $xsdType['name']) = explode(':', V::get('type', '', $n[1])); } if (!$xsdType['nsPrefix'] || !$xsdType['name']) throw new Exception("Missing schema root element name = '{$item['name']}'"); $xsdType['nsUri'] = V::get("xmlns:{$xsdType['nsPrefix']}", '', $schema[1]);// find xmlns:default_objects = "https://biuro.biall-net.pl/wfs/default_objects" if (!$xsdType['nsUri']) throw new Exception("Missing schema root element namespace declaration = '{$item['name']}'"); if ($xsdType['nsUri'] != $xsdType['targetNsUri']) throw new Exception("TODO: type ns is not the same as targetNamespace '{$item['name']}'");// TODO foreach ($schema[2] as $n) { if ('xsd:complexType' != $n[0]) continue; if ($xsdType['name'] != V::get('name', '', $n[1])) continue; DBG::nicePrint($n, 'TODO: parse complexType'); // complexType/complexContent/extension[base=...]/sequence/element if ($n[2][0][0] != 'xsd:complexContent') throw new Exception("Error Parsing Schema - expected 'complexType/complexContent'"); if ($n[2][0][2][0][0] != 'xsd:extension') throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/extension'"); $xsdType['extensionBase'] = V::get('base', '', $n[2][0][2][0][1]); if ($n[2][0][2][0][2][0][0] != 'xsd:sequence') throw new Exception("Error Parsing Schema - expected 'complexType/complexContent/extension/sequence'"); foreach ($n[2][0][2][0][2][0][2] as $f) { if ($f[0] !== 'xsd:element') { DBG::log($n, 'array', "Schema xsd parse error - Not implemented node type '{$f[0]}'"); continue; } $fieldName = V::get('name', '', $f[1]); if (!$fieldName) throw new Exception("Error Parsing Schema - expected 'element[@name]'"); if ('__' === substr($fieldName, 0, 2)) continue; if (!V::get('type', '', $f[1]) && !V::get('ref', '', $f[1]) && empty($f[2])) { UI::alert('danger', "Skipping not implemented field structure '{$fieldName}'"); continue; } $xsdType['struct'][$fieldName] = [ 'type' => V::get('type', '', $f[1]), 'ref' => V::get('ref', '', $f[1]), 'minOccurs' => V::get('minOccurs', 0, $f[1], 'int'), 'maxOccurs' => V::get('maxOccurs', '1', $f[1]), 'restrictions' => [], ]; if (!empty($f[1]['nillable']) && 'true' === $f[1]['nillable']) $xsdType['struct'][$fieldName]['restrictions']['nillable'] = true; if (!empty($f[2])) { if (empty($f[2][0][0]) || 'xsd:simpleType' != $f[2][0][0]) throw new Exception("Missing 'xsd:simpleType' for field '{$fieldName}'"); if (empty($f[2][0][2][0]) || 'xsd:restriction' != $f[2][0][2][0][0]) throw new Exception("Missing 'xsd:restriction' for field '{$fieldName}'"); if (empty($f[2][0][2][0][1]['base'])) throw new Exception("Missing 'xsd:restriction/@base' for field '{$fieldName}'"); // [2] => Array: // [0] => Array: // [0] => xsd:simpleType // [1] => Array: // [2] => Array: // [0] => Array: // [0] => xsd:restriction // [1] => Array: // [base] => xsd:string // [2] => Array: foreach ($f[2][0][2][0][2] as $tagRestriction) { // [0] => Array: // [0] => xsd:maxLength // [1] => Array: // [value] => 20 // [2] => $val = $tagRestriction[1]['value']; if ('xsd:enumeration' == $tagRestriction[0]) { $restrictions['enumeration'][$val] = $val; } else { $restrictions[substr($tagRestriction[0], 4)] = $val; } } DBG::nicePrint(['f'=>$f, '$restr'=>$restrictions], 'TODO: field childrens f[2]'); if (!empty($restrictions)) $xsdType['struct'][$fieldName]['restrictions'] = $restrictions; } } } DBG::nicePrint($xsdType, '$xsdType'); if (empty($xsdType['struct'])) throw new Exception("Field list not found for '{$item['namespace']}'"); foreach ($xsdType['struct'] as $fieldName => $x) { DB::getPDO()->insertOrUpdate($this->_rootTableName, [ 'objectNamespace' => $item['namespace'], 'idDatabase' => $item['idDatabase'], '_rootTableName' => $item['_rootTableName'], 'fieldNamespace' => $fieldName, 'xsdType' => (!empty($x['type'])) ? $x['type'] : "ref:{$x['ref']}", 'xsdRestrictions' => json_encode($x['restrictions']), 'minOccurs' => $x['minOccurs'], 'maxOccurs' => $x['maxOccurs'], 'isActive' => 1 ]); } DB::getPDO()->execSql(" update `{$this->_rootTableName}` t join CRM_LISTA_ZASOBOW zp on(zp.PARENT_ID = {$item['idDatabase']} and zp.`DESC` = '{$item['_rootTableName']}') join CRM_LISTA_ZASOBOW z on(z.PARENT_ID = zp.ID and z.`DESC` = t.fieldNamespace) set t.idZasob = z.ID where t.objectNamespace = '{$item['namespace']}' "); {// TODO: DBG DBG::nicePrint($schema, '$schema'); Lib::loadClass('Core_XmlWriter'); ob_start(); $xmlWriter = new Core_XmlWriter(); $xmlWriter->openUri('php://output'); $xmlWriter->setIndent(true); $xmlWriter->startDocument('1.0','UTF-8'); $xmlWriter->h($schema[0], $schema[1], $schema[2]); $xmlWriter->endDocument(); echo UI::h('pre', ['style' => 'max-height:400px; overflow:scroll'], htmlspecialchars(ob_get_clean())); } } public function updateCacheTableAcl($item) { // TODO: xsdType - convert mysql type to xsdType, xsdRestrictions $xsdInfo = []; Lib::loadClass('SchemaHelper'); $dbName = DB::getPDO()->getDatabaseName(); $mysqlTypes = DB::getPDO()->fetchAll(" select c.COLUMN_NAME, c.COLUMN_TYPE, c.DATA_TYPE, c.IS_NULLABLE, c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, c.NUMERIC_SCALE from `information_schema`.`COLUMNS` c where c.TABLE_SCHEMA = '{$dbName}' and c.TABLE_NAME = '{$item['_rootTableName']}' "); foreach ($mysqlTypes as $mysqlType) { $xsdInfo[ $mysqlType['COLUMN_NAME'] ] = SchemaHelper::convertMysqlTypeToSchemaXsd($mysqlType); } DBG::nicePrint($xsdInfo, '$xsdInfo'); foreach ($xsdInfo as $fieldName => $x) { DB::getPDO()->insertOrUpdate($this->_rootTableName, [ 'fieldNamespace' => $fieldName, 'isActive' => 1, 'idDatabase' => $item['idDatabase'], '_rootTableName' => $item['_rootTableName'], 'objectNamespace' => $item['namespace'], 'xsdType' => $x['type'], 'xsdRestrictions' => json_encode($x['restrictions']), ]); } SchemaFactory::loadDefaultObject('SystemObject')->updateItem([ 'namespace' => $item['namespace'], 'isStructInstalled' => 1 ]); $struct = $this->getItems([ '__backRef' => [ 'namespace' => 'default_objects/SystemObject', 'primaryKey' => $item['namespace'] ] ]); DBG::nicePrint($struct, '$struct'); {// TODO: DBG $schema = ['xsd:schema', [ 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", 'xmlns:gml' => "http://www.opengis.net/gml", 'xmlns:p5' => "https://biuro.biall-net.pl/wfs", 'xmlns:default_db' => "https://biuro.biall-net.pl/wfs/default_db", 'xmlns:default_objects' => "https://biuro.biall-net.pl/wfs/default_objects", 'elementFormDefault' => "qualified", 'version' => "1.0.0", ], []]; Lib::loadClass('Api_WfsNs'); $tnsUri = Api_WfsNs::getNsUri($item['nsPrefix']); $schema[1]['xmlns:' . Api_WfsNs::getNsPrefix($tnsUri)] = $tnsUri; $schema[1]['targetNamespace'] = $tnsUri; $schema[2][] = ['xsd:import', ['namespace'=>"http://www.opengis.net/gml", 'schemaLocation'=>"https://biuro.biall-net.pl/dev-pl/se-master/schema/gml/2.1.2/feature.xsd"], null]; $schema[2][] = ['xsd:complexType', [ 'name'=> $item['name'] . "Type" ], [ [ 'xsd:complexContent', [], [ [ 'xsd:extension', ['base'=>"gml:AbstractFeatureType"], [ [ 'xsd:sequence', [], array_map(function ($field) { $attrs = [ 'name' => $field['fieldNamespace'], // 'minOccurs' => $field['minOccurs'], 'maxOccurs' => $field['maxOccurs'], ]; $childrens = null; $restrictions = []; foreach ($this->getXsdRestrictionsValue($field) as $k => $v) { if ('enumeration' == $k) { foreach ($v as $enumValue) { $restrictions[] = [ 'xsd:enumeration', [ 'value' => $enumValue ], null ]; } } else if ('maxLength' == $k) { $restrictions[] = [ 'xsd:maxLength', [ 'value' => $v ], null ]; } else if ('totalDigits' == $k) { $restrictions[] = [ 'xsd:totalDigits', [ 'value' => $v ], null ]; } else if ('fractionDigits' == $k) { $restrictions[] = [ 'xsd:fractionDigits', [ 'value' => $v ], null ]; } else if ('nillable' == $k) { if ($v) $attrs['nillable'] = "true"; } else { DBG::log(['TODO' => $k, $v]); } } if (!empty($restrictions)) { $childrens = [ [ 'xsd:simpleType', [], [ [ 'xsd:restriction', [ 'base' => $field['xsdType'] ], $restrictions ] ]] ]; } else { $attrs['type'] = $field['xsdType']; } return [ 'xsd:element', $attrs, $childrens ]; }, $struct)] ]] ]] ]]; $schema[2][] = ['xsd:element', [ 'name' => $item['name'], 'type' => "{$item['nsPrefix']}:{$item['name']}Type", 'substitutionGroup' => "gml:_Feature" ], null]; DBG::nicePrint($schema, '$schema'); Lib::loadClass('Core_XmlWriter'); ob_start(); $xmlWriter = new Core_XmlWriter(); $xmlWriter->openUri('php://output'); $xmlWriter->setIndent(true); $xmlWriter->startDocument('1.0','UTF-8'); $xmlWriter->h($schema[0], $schema[1], $schema[2]); $xmlWriter->endDocument(); echo UI::h('pre', ['style' => 'max-height:400px; overflow:scroll'], htmlspecialchars(ob_get_clean())); } } public function _parseWhere($params = []) { $sqlWhere = []; DBG::log($params, 'array', "SystemObject::_parseWhere"); if (!empty($params['__backRef'])) { // '__backRef' => [ // 'namespace' => 'default_objects/SystemObject', // 'primaryKey' => 'default_db/TEST_PERMS', // ), if (empty($params['__backRef']['namespace'])) throw new Exception("Missing refFrom/namespace"); if (empty($params['__backRef']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey"); if ('default_objects/SystemObject' != $params['__backRef']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['__backRef']['namespace']}'"); $sqlWhere[] = "t.objectNamespace = " . DB::getPDO()->quote($params['__backRef']['primaryKey'], PDO::PARAM_INT); } { $filterParams = []; $xsdFields = $this->getXsdTypes(); DBG::log($xsdFields); foreach ($params as $k => $v) { if ('f_' != substr($k, 0, 2)) continue; $fieldName = substr($k, 2); if (!array_key_exists($fieldName, $xsdFields)) { // TODO: check query by xpath or use different param prefix throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'"); } if ('p5:' == substr($xsdFields[$fieldName]['xsdType'], 0, 3)) { continue; } $filterParams[$fieldName] = $v; } } if (!empty($filterParams)) { DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams"); foreach ($filterParams as $fieldName => $value) { if (is_array($value)) { DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]"); } else if (is_scalar($value)) { if ('=' == substr($value, 0, 1)) { $sqlWhere[] = "t.{$fieldName} = " . DB::getPDO()->quote(substr($value, 1), PDO::PARAM_STR); } else { $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR); } } else { DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]"); } } } return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : ''; } public function getTotal($params = []) { $sqlWhere = $this->_parseWhere($params); return DB::getPDO()->fetchValue(" select count(1) as cnt from `{$this->_rootTableName}` t {$sqlWhere} "); } public function getItems($params = []) { $sqlWhere = $this->_parseWhere($params); $currSortCol = V::get('order_by', 'idZasob', $params); $currSortFlip = strtolower(V::get('order_dir', 'desc', $params)); // TODO: validate $currSortCol is in field list // TODO: validate $currSortFlip ('asc' or 'desc') $xsdFields = $this->getXsdTypes(); if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'"); if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed"); $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}"; $limit = V::get('limit', 0, $params, 'int'); $limit = ($limit < 0) ? 0 : $limit; $offset = V::get('limitstart', 0, $params, 'int'); $offset = ($offset < 0) ? 0 : $offset; $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : ''; return array_map(array($this, 'buildFeatureFromSqlRow'), DB::getPDO()->fetchAll(" select t.* from `{$this->_rootTableName}` t {$sqlWhere} {$sqlOrderBy} {$sqlLimit} ")); } public function getXsdRestrictionsValue($item) { $xsdRestrictions = @json_decode($item['xsdRestrictions'], $assoc = true); if (empty($xsdRestrictions)) $xsdRestrictions = []; if (array_key_exists('__DBG__', $xsdRestrictions)) unset($xsdRestrictions['__DBG__']); return $xsdRestrictions; } public function buildFeatureFromSqlRow($item) { return $item; } }