| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- Lib::loadClass('ApiDataSourceTodo');// TODO: @see Entity/Source/Mysql from feature-schema-install
- Lib::loadClass('Data_Source');
- class Api_Xsd {
- private $_apiUser;
- private $_dataSourceName;
- private $_tblName;
- private $_tblSchema;
- public function setUser($user) {
- $this->_apiUser = $user;
- }
- public function execute($request) {
- if (!$this->_apiUser->isAdmin()) {
- throw new HttpException("Forbidden", 403);
- }
- IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">request->segments (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->segments);echo'</pre>';}
- if (empty($request->segments) || !is_array($request->segments)) return;
- if (count($request->segments) < 1) {
- throw new HttpException("Data source and table name not specified", 400);
- }
- $this->_dataSourceName = array_shift($request->segments);
- if ('default_db' == $this->_dataSourceName) {
- $db = DB::getDB();
- $limit = 1000;
- $tbls = array();
- $sql = "show full tables";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- if ('BASE TABLE' == $r->Table_type) {
- $tblName = get_object_vars($r);
- $tblName = array_values($tblName);
- $tblName = reset($tblName);
- if ('DEALS_TABLE_2015_03_17_zest_dla_zubryka' == $tblName) {
- continue;// fields name 'grup_concat(...'
- }
- if ('KSIEG_DOKUMENTY' == $tblName) {
- continue;// fields name '201_...'
- }
- if ('KSIEG_DOKUMENTY_HIST' == $tblName) {
- continue;// fields name '201_...'
- }
- if ('Rozdzielcza_rurociag_wsg84' == $tblName) {
- continue;// fields name with space ' '
- }
- $ds = new Data_Source();
- $ds->set_table($tblName);
- $ds->get_cols();
- $tbls[$tblName] = $ds;
- if (--$limit < 0) break;
- }
- }
- $xml = <<<XMLEOF
- <?xml version="1.0"?>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:{$this->_dataSourceName}="http://biuro.biall-net.pl/xmlschema_procesy5/{$this->_dataSourceName}" targetNamespace="http://biuro.biall-net.pl/xmlschema_procesy5/{$this->_dataSourceName}">
- XMLEOF;
- Lib::loadClass('TableAcl');
- $acl = new TableAcl();
- foreach ($tbls as $tblName => $ds) {
- $xmlFields = array();
- $tblFields = $ds->_col_types;
- foreach ($tblFields as $fldName => $colType) {
- $minOccurs = 0;
- $maxOccurs = 1;
- $colDefault = null;
- $xsdType = 'string';
- //$xsdType = 'token';
- //$xsdType = 'integer';
- //$xsdType = 'double';
- $xsdRestrictions = array();
- if (false !== strpos($colType, ';')) {
- $colType = explode(';', $colType, 2);
- $colDefault = array_pop($colType);
- $colType = array_shift($colType);
- }
- //$xsdRestrictions[] = '<xs:enumeration value="WAITING"/>';
- if ($this->isIntegerField($colType)) {
- $xsdType = 'integer';
- }
- else if ($this->isDecimalField($colType)) {
- $xsdType = 'double';
- }
- else if ($this->isStringField($colType)) {
- $xsdType = 'string';
- $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType, strpos($colType, '(') + 1, -1));
- if ($maxLength > 0) {
- $xsdRestrictions[] = '<xs:maxLength value="' . $maxLength . '"/>';
- }
- }
- else if ($this->isTextField($colType)) {
- $xsdType = 'string';
- }
- else if ($this->isDateField($colType)) {
- $xsdType = 'token';
- $xsdDatePattern = '[0-9]{4}-[0-9]{2}-[0-9]{2}';
- $xsdRestrictions[] = '<xs:pattern value="' . $xsdDatePattern . '"/>';
- }
- else if ($this->isDateTimeField($colType)) {
- $xsdType = 'token';
- $xsdDatePattern = '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}';
- $xsdRestrictions[] = '<xs:pattern value="' . $xsdDatePattern . '"/>';
- }
- else if (substr($colType, 0, 4) == 'enum') {
- $xsdType = 'string';
- $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType, 5)));
- foreach ($values as $val) {
- $xsdRestrictions[] = '<xs:enumeration value="' . $val . '"/>';
- }
- }
- else if ('polygon' == $colType) {
- $xsdType = 'string';
- }// Wielokąt
- else if ('multipolygon' == $colType) {
- $xsdType = 'string';
- }// Zbiór wielokątów
- else if ('linestring' == $colType) {
- $xsdType = 'string';
- }// Krzywa z interpolacji liniowej pomiędzy punktami
- else if ('point' == $colType) {
- $xsdType = 'string';
- }// Punkt w przestrzeni 2-wymiarowej
- else if ('geometry' == $colType) {
- $xsdType = 'string';
- }// Typy, które mogą przechowywać geometrię dowolnego typu
- else if ('multipoint' == $colType) {
- $xsdType = 'string';
- }// Zbiór punktów
- else if ('multilinestring' == $colType) {
- $xsdType = 'string';
- }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
- else if ('geometrycollection' == $colType) {
- $xsdType = 'string';
- }// Zbiór obiektów geometrycznych dowolnego typu
- else if ('timestamp' == substr($colType, 0, 9)) {
- $xsdType = 'string';
- }
- else if ('time' == substr($colType, 0, 4)) {
- $xsdType = 'token';
- $xsdDatePattern = '[0-9]{2}:[0-9]{2}:[0-9]{2}';
- $xsdRestrictions[] = '<xs:pattern value="' . $xsdDatePattern . '"/>';
- }
- else if ('binary' == substr($colType, 0, 6)
- || 'varbinary' == substr($colType, 0, 9)) {
- $xsdType = 'hexBinary';
- }
- else if ('blob' == substr($colType, 0, 4)
- || 'longblob' == substr($colType, 0, 8)
- || 'mediumblob' == substr($colType, 0, 10)
- || 'tinyblob' == substr($colType, 0, 8)) {
- $xsdType = 'hexBinary';
- }
- else if (substr($colType, 0, 3) == 'set') {
- $xsdType = 'string';
- $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType, 4)));
- foreach ($values as $val) {
- $xsdRestrictions[] = '<xs:enumeration value="' . $val . '"/>';
- }
- }
- else {
- $xsdType = 'unknown-Type-'.$colType.'';
- }
- $xmlFld = '';
- $xmlFld .= '<xs:element minOccurs="' . $minOccurs . '" maxOccurs="' . $maxOccurs . '" name="' . $fldName . '">';
- $xmlFld .= "\n\t\t\t\t" . '<xs:simpleType>';
- if (!empty($xsdRestrictions)) {
- $xmlFld .= "\n\t\t\t\t\t" . '<xs:restriction base="xs:' . $xsdType . '">';
- $xmlFld .= "\n\t\t\t\t\t\t" . implode("\n\t\t\t\t\t\t", $xsdRestrictions);
- $xmlFld .= "\n\t\t\t\t\t" . '</xs:restriction>';
- } else {
- $xmlFld .= "\n\t\t\t\t\t" . '<xs:restriction base="xs:' . $xsdType . '"/>';
- }
- $xmlFld .= "\n\t\t\t\t" . '</xs:simpleType>';
- $xmlFld .= "\n\t\t\t" . '</xs:element>';
- $xmlFields[] = $xmlFld;
- }
- $xmlFields = implode("\n\t\t\t", $xmlFields);
- $xml .= <<<XMLEOF
- <xs:element name="{$tblName}" type="{$this->_dataSourceName}:{$tblName}"/>
- <xs:complexType name="{$tblName}">
- <xs:sequence>
- {$xmlFields}
- </xs:sequence>
- </xs:complexType>
- XMLEOF;
- }
- $xml .= <<<XMLEOF
- </xs:schema>
- XMLEOF;
- header('Content-Type: text/xml; charset=utf-8');
- echo $xml;
- exit;
- //echo'<pre>';print_r($tbls);echo'</pre>';
- }
- // return document tree - array of arrays
- }
- // FROM TableAcl {
- public function isIntegerField($type) {
- if (substr($type, 0, 3) == 'int'
- || substr($type, 0, 7) == 'tinyint'
- || substr($type, 0, 8) == 'smallint'
- || substr($type, 0, 9) == 'mediumint'
- || substr($type, 0, 6) == 'bigint'
- ) {
- return true;
- }
- return false;
- }
- public function isDecimalField($type) {
- if (substr($type, 0, 7) == 'decimal'
- || substr($type, 0, 7) == 'numeric'
- || substr($type, 0, 6) == 'double'
- || substr($type, 0, 5) == 'float'
- || substr($type, 0, 4) == 'real'
- ) {
- return true;
- }
- return false;
- }
- public function isDateField($type) {
- if (substr($type, 0, 4) == 'date' && substr($type, 0, 8) != 'datetime') {
- return true;
- }
- return false;
- }
- public function isDateTimeField($type) {
- if (substr($type, 0, 8) == 'datetime') {
- return true;
- }
- return false;
- }
- public function isStringField($type) {
- if (substr($type, 0, 7) == 'varchar'
- || substr($type, 0, 4) == 'char'
- ) {
- return true;
- }
- return false;
- }
- public function isTextField($type) {
- if (substr($type, 0, 4) == 'text'
- || substr($type, 0, 8) == 'tinytext'
- || substr($type, 0, 10) == 'mediumtext'
- || substr($type, 0, 8) == 'longtext'
- ) {
- return true;
- }
- return false;
- }
- // FROM } TableAcl
- private function getDataSource() {
- if (!$this->_dataSource) {
- // TODO: get data source from Factory
- {
- if ('default_db' == $this->_dataSourceName) {
- $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
- $this->_dataSource->setTable($this->_tblName);
- }
- else if ('931' == $this->_dataSourceName) {
- $this->_dataSource = new ApiDataSourceTodo($this->_dataSourceName);
- $this->_dataSource->setTable($this->_tblName);
- }
- }
- }
- return $this->_dataSource;
- }
- }
|