Jelajahi Sumber

WfsDataServer add propertyName param to fetch only some fields

Piotr Labudda 10 tahun lalu
induk
melakukan
66e5ddd0ed
2 mengubah file dengan 36 tambahan dan 17 penghapusan
  1. 13 2
      SE/se-lib/Api/WfsDataServer.php
  2. 23 15
      SE/se-lib/Data_Source.php

+ 13 - 2
SE/se-lib/Api/WfsDataServer.php

@@ -51,9 +51,11 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		$startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
 		$startIndex = V::get('startIndex', 0, $_REQUEST, 'int');// sql offset
 		$ogcFilter = V::get('Filter', '', $_REQUEST);
 		$ogcFilter = V::get('Filter', '', $_REQUEST);
 		$sortBy = V::get('sortBy', '', $_REQUEST);
 		$sortBy = V::get('sortBy', '', $_REQUEST);
+		$propertyName = V::get('propertyName', '', $_REQUEST);
+		$propertyName = trim($propertyName);
 		$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
 		$srsname = V::get('SRSNAME', '', $_REQUEST);// eg. EPSG:4326
 		if (count($typeEx) == 2) {
 		if (count($typeEx) == 2) {
-			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex);
+			return $this->getFeatures($typeEx[0], $typeEx[1], $maxFeatures, $srsname, $ogcFilter, $sortBy, $startIndex, $propertyName);
 		} else {
 		} else {
 			throw new HttpException("Wrong param TYPENAME", 400);
 			throw new HttpException("Wrong param TYPENAME", 400);
 		}
 		}
@@ -76,7 +78,7 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		}
 		}
 	}
 	}
 
 
-	public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0) {
+	public function getFeatures($nsPrefix, $type, $maxFeatures, $srsname, $ogcFilter = '', $sortBy = '', $startIndex = 0, $propertyName = '') {
 		$DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
 		$DBG = (V::get('DBG_GEO', '', $_GET) > 0);// TODO: Profiler
 		$typeName = "{$nsPrefix}:{$type}";
 		$typeName = "{$nsPrefix}:{$type}";
 		$acl = $this->getAclFromTypeName($typeName);
 		$acl = $this->getAclFromTypeName($typeName);
@@ -107,6 +109,15 @@ if($DBG){echo "ogcFilter(" . strlen($ogcFilter) . "): {$ogcFilter}\n";}
 			$searchParams['order_dir'] = 'DESC';
 			$searchParams['order_dir'] = 'DESC';
 		}
 		}
 		if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
 		if (strlen($ogcFilter) > 0) $searchParams['ogc:Filter'] = $ogcFilter;
+		if (strlen($propertyName) > 0) {
+			$propertyNamesEx = explode(',', $propertyName);
+			$onlyCols = array();
+			foreach ($propertyNamesEx as $colName) {
+				$colName = trim($colName);
+				$onlyCols[] = $colName;
+			}
+			if (!empty($onlyCols)) $searchParams['cols'] = $onlyCols;
+		}
 if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
 if($DBG){echo 'getItems:';print_r($searchParams);echo "\n";}
 		$items = $acl->getItems($searchParams);
 		$items = $acl->getItems($searchParams);
 
 

+ 23 - 15
SE/se-lib/Data_Source.php

@@ -161,22 +161,27 @@ class Data_Source {
 		return $this->_fieldOwner;
 		return $this->_fieldOwner;
 	}
 	}
 
 
-	function _get_sql_cols() {
-		$sql_cols = "t.*";
+	function _getSqlCols($colsList = null) {
+		$sqlCols = "t.*";
 		if (!empty($this->_cols)) {
 		if (!empty($this->_cols)) {
-			$sql_cols_arr = array();
-			foreach ($this->_cols as $k_field => $v_field_label) {
-				if ($this->isGeomField($k_field)) {
-					$sqlFld = "AsWKT(t.`{$k_field}`) as {$k_field}";
+			$sqlColsList = array();
+			if (null === $colsList || empty($colsList)) {
+				$colsList = array_keys($this->_cols);
+			}
+			foreach ($colsList as $fieldName) {
+				if (!array_key_exists($fieldName, $this->_cols)) throw new Exception("Field not exists '{$fieldName}'!");
+
+				if ($this->isGeomField($fieldName)) {
+					$sqlFld = "AsWKT(t.`{$fieldName}`) as {$fieldName}";
 				} else {
 				} else {
-					$sqlFld = "t.`{$k_field}`";
+					$sqlFld = "t.`{$fieldName}`";
 				}
 				}
-				$sql_cols_arr[] = $sqlFld;
+				$sqlColsList[] = $sqlFld;
 			}
 			}
-			$sql_cols = implode(", ", $sql_cols_arr);
+			$sqlCols = implode(", ", $sqlColsList);
 		}
 		}
-if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql_cols (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql_cols);echo'</pre>'."\n\n";}
-		return $sql_cols;
+		DBG::_('DBG_DS', '>0', "sqlCols", $sqlCols, __CLASS__, __FUNCTION__, __LINE__);
+		return $sqlCols;
 	}
 	}
 
 
 	public function setAccessFltrAllowed($isAccessFltrAllowed) {
 	public function setAccessFltrAllowed($isAccessFltrAllowed) {
@@ -563,7 +568,7 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 	public function getItem($primaryKey) {
 	public function getItem($primaryKey) {
 		$primaryKeyField = $this->getPrimaryKeyField();
 		$primaryKeyField = $this->getPrimaryKeyField();
 		$ret = null;
 		$ret = null;
-		$sql_cols = $this->_get_sql_cols();
+		$sql_cols = $this->_getSqlCols();
 		$primaryKey = intval($primaryKey);// TODO: validate $primaryKey
 		$primaryKey = intval($primaryKey);// TODO: validate $primaryKey
 		$sql = "select {$sql_cols}
 		$sql = "select {$sql_cols}
 			from `{$this->_tbl}` as t
 			from `{$this->_tbl}` as t
@@ -585,10 +590,13 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 		$primaryKeyField = $this->getPrimaryKeyField();
 		$primaryKeyField = $this->getPrimaryKeyField();
 		$items = array();
 		$items = array();
 		$sql = new stdClass();
 		$sql = new stdClass();
+		$sql->_cols = V::get('cols', null, $params);
+		if (!is_array($sql->_cols) || empty($sql->_cols)) {
+			$sql->_cols = null;
+		}
 		$sql->limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
 		$sql->limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
 		$sql->offset = V::get('limitstart', 0, $params, 'int');
 		$sql->offset = V::get('limitstart', 0, $params, 'int');
 		$sql->orderBy = '';
 		$sql->orderBy = '';
-		$sql->_orderBy = V::get('order_by', '', $params);
 		$sql->_sortBy = V::get('sortBy', '', $params);
 		$sql->_sortBy = V::get('sortBy', '', $params);
 		if ($sql->_sortBy) {// ID A,COL_X D,COL_Y A,...
 		if ($sql->_sortBy) {// ID A,COL_X D,COL_Y A,...
 			$sql->_sortByList = array();
 			$sql->_sortByList = array();
@@ -629,8 +637,8 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 				}
 				}
 			}
 			}
 		}
 		}
-		$sql->cols = $this->_get_sql_cols();
 		$sql->where = $this->_parseSqlWhere($params);
 		$sql->where = $this->_parseSqlWhere($params);
+		$sql->cols = $this->_getSqlCols($sql->_cols);
 		$sql->query = "select {$sql->cols}
 		$sql->query = "select {$sql->cols}
 			from `{$this->_tbl}` t
 			from `{$this->_tbl}` t
 			where {$sql->where}
 			where {$sql->where}
@@ -652,7 +660,7 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
 	public function getHistItems($id, $params = array()) {
 	public function getHistItems($id, $params = array()) {
 		$ret = array();
 		$ret = array();
 		$sql_tbl = $this->_tbl . "_HIST";
 		$sql_tbl = $this->_tbl . "_HIST";
-		$sql_cols = $this->_get_sql_cols();
+		$sql_cols = $this->_getSqlCols();
 		$sql_where = "t.`ID_USERS2`='{$id}'";
 		$sql_where = "t.`ID_USERS2`='{$id}'";
 
 
 		$paramNotEmptyFlds = V::get('notEmptyFlds', '', $params);
 		$paramNotEmptyFlds = V::get('notEmptyFlds', '', $params);