Просмотр исходного кода

added outputBlobFormat, fixed blob in API

Piotr Labudda 8 лет назад
Родитель
Сommit
da2eee2c43
3 измененных файлов с 71 добавлено и 33 удалено
  1. 56 7
      SE/se-lib/Api/Wfs/GetFeature.php
  2. 14 26
      SE/se-lib/Api/WfsDataServer.php
  3. 1 0
      SE/se-lib/Api/WfsServerBase.php

+ 56 - 7
SE/se-lib/Api/Wfs/GetFeature.php

@@ -96,10 +96,28 @@ class Api_Wfs_GetFeature {
 	}
 
 	// TODO: add $contextAcl and context xpath to check for special perms by contextAcl
-	public static function printXmlFeatureRecurse($xmlWriter, $acl, $item, $showFields, $tagName, $attrs = [], $showAdvancedAttrs = false, $schemaCache = [], $printedFidLog = []) {
+	/** @example:
+	 * 		Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, [
+	 * 			'fields' => $searchParams['cols'],
+	 * 			'tagName' => "{$wfsNs}:{$type}",
+	 * 			'showAdvancedAttrs' => !$simple,
+	 * 			'xsdAttributes' => array_merge(
+	 * 				[ 'fid' => "{$type}.{$itemKey}" ],
+	 * 				(!$simple)
+	 * 				?	[ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
+	 * 				:	[]
+	 * 			)
+	 * 		], $schemaCache, $printedFidLog);
+	 */
+	public static function printXmlFeatureRecurse($xmlWriter, $acl, $item, $params = [], $schemaCache = [], $printedFidLog = []) {
+		$showFields = V::get('fields', [], $params, 'array'); // TODO: if empty $showFields ?
+		$tagName = V::get('tagName', '', $params);
+		$attrs = V::get('xsdAttributes', [], $params, 'array');
+		$showAdvancedAttrs = V::get('showAdvancedAttrs', false, $params, 'bool');
 		$dbgFid = V::get('fid', 0, $attrs);
+		$outputBlobFormat = V::get('outputBlobFormat', '', $params, 'word');
 		if(V::get('DBG_XML', '', $_GET))$xmlWriter->writeComment("DBG: print Xml Feature Recurse... '{$tagName}'" . ( $dbgFid ? " fid='{$dbgFid}'" : "" )); // TODO: DBG
-		DBG::log($acl, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$acl)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$acl");
+		// DBG::log($acl, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$acl)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$acl");
 		DBG::log($item, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$item");
 		DBG::log($showFields, 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$item)" . ( $dbgFid ? " fid='{$dbgFid}'" : "" ) . " \$showFields");
 		DBG::log([$attrs, $showAdvancedAttrs, array_keys($schemaCache), $printedFidLog], 'array', "DBG: print Xml Feature Recurse( ... {$tagName}, \$attrs, \$showAdvancedAttrs, keys(\$schemaCache), \$printedFidLog)");
@@ -157,7 +175,6 @@ class Api_Wfs_GetFeature {
 		});
 		DBG::log($fldList, 'array', ">>> loop start filtered fields(".count($fldList).")");
 		foreach ($fldList as $idZasob => $fldName) {
-			// TODO: show only fields from $showFields
 			DBG::log(">>> loop {$idZasob} => {$fldName}...");
 			$fldType = $acl->getXsdFieldType($fldName);
 			DBG::log(">>> loop '{$fldName}' xsdType: '{$fldType}'");
@@ -189,7 +206,13 @@ class Api_Wfs_GetFeature {
 						$firstItem = reset($item[$fldName]);
 						if (1 === count($firstItem) && !empty($firstItem['xlink'])) { // TODO: $schemaCache[$fieldNs] must exists for xlinks - xlmns is required
 							foreach ($item[$fldName] as $childItem) {
-								self::printXmlFeatureRecurse($xmlWriter, $childAcl = null, $childItem, $showChildFields = [], $fldName, [], $showAdvancedAttrs, $schemaCache, $printedFidLog);
+								self::printXmlFeatureRecurse($xmlWriter, $childAcl = null, $childItem, [
+									'fields' => [],
+									'tagName' => $fldName,
+									'xsdAttributes' => [],
+									'showAdvancedAttrs' => $showAdvancedAttrs,
+									'outputBlobFormat' => $outputBlobFormat,
+								], $schemaCache, $printedFidLog);
 							}
 						} else {
 							DBG::log($schemaCache, 'array', "Error Processing Request - field is not ref or missing acl ".$acl->getName().".{$itemKey}/{$fldName}");
@@ -210,7 +233,13 @@ class Api_Wfs_GetFeature {
 							}, $showChildFields);
 							// DBG::log($showChildFields, 'array', "DBG: fld({$fldName}) \$showChildFields 2");
 							$childPK = V::get($childAcl->getPrimaryKeyField(), '', $childItem);
-							self::printXmlFeatureRecurse($xmlWriter, $childAcl, $childItem, $showChildFields, $fldName, ($childPK) ? [ 'fid' => "{$childName}.{$childPK}" ] : [], $showAdvancedAttrs, $schemaCache, $printedFidLog);
+							self::printXmlFeatureRecurse($xmlWriter, $childAcl, $childItem, [
+								'fields' => $showChildFields,
+								'tagName' => $fldName,
+								'xsdAttributes' => ($childPK) ? [ 'fid' => "{$childName}.{$childPK}" ] : [],
+								'showAdvancedAttrs' => $showAdvancedAttrs,
+								'outputBlobFormat' => $outputBlobFormat,
+							], $schemaCache, $printedFidLog);
 						}
 						// foreach ($item[$fldName] as $refItem) {
 						// 	DBG::log($refItem, 'array', "\$refItem fld({$fldName})");
@@ -243,7 +272,7 @@ class Api_Wfs_GetFeature {
 					}
 				}
 			} else if ('xsd:base64Binary' === $acl->getXsdFieldType($fldName)) {
-				if (empty($item[$fldName]) && '0' !== $item[$fldName]) continue;
+				if (empty($item[$fldName])) continue; //  && '0' !== $item[$fldName]
 				$xmlWriter->startElement("{$itemPrefix}:{$fldName}");
 					if ($showAdvancedAttrs && !$acl->canReadObjectField($fldName, (object)$item)) {
 						$xmlWriter->writeAttribute("p5:allow_read", "false");
@@ -251,7 +280,27 @@ class Api_Wfs_GetFeature {
 					if ($showAdvancedAttrs && $acl->canWriteObjectField($fldName, (object)$item)) {
 						$xmlWriter->writeAttribute("p5:allow_write", "true");
 					}
-					$xmlWriter->text(base64_encode($item[$fldName]));
+					$pk = V::get($acl->getPrimaryKeyField(), '', $item);
+					if ($showAdvancedAttrs) {
+						$xmlWriter->writeAttribute("p5:blobUrl", Request::getPathUri() . "wfs-data.php?" . implode("&", [
+							"SERVICE=WFS", "VERSION=1.0.0", "REQUEST=GetBlob",
+							"namespace=" . $acl->getNamespace(),
+							"primaryKey={$pk}",
+							"fieldName={$fldName}",
+						]));
+					}
+					if (in_array($item[$fldName], ['0', '1'])) {
+						if ('base64' === $outputBlobFormat) {
+							$content = DB::getPDO( $acl->getDatabaseID() )->getBlob( $acl->getRootTableName(), $fldName, $acl->getPrimaryKeyField(), $pk );
+							$xmlWriter->text(base64_encode($content));
+						} else if ('link' === $outputBlobFormat) {
+							$xmlWriter->text($item[$fldName]);
+						} else { // hide
+							throw new Exception("Not implemented outputBlobFormat - expected base64 or link");
+						}
+					} else {
+						$xmlWriter->text(base64_encode($item[$fldName]));
+					}
 				$xmlWriter->endElement();// {$itemPrefix}:{$fldName}
 			} else {
 				$value = str_replace('&', '&', $item[$fldName]);

+ 14 - 26
SE/se-lib/Api/WfsDataServer.php

@@ -214,7 +214,7 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		$xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
 		$xlmns = [];
 		$xlmns[ $wfsNs ] = $wfsNsUri;
-		if (!$simple) $xlmns[ $rootWfsNs ] = $rootWfsNsUri;
+		if (!$simple) $xlmns[ $rootWfsNs ] = $rootWfsNsUri; // $xmlWriter->writeAttribute('xmlns:p5', Api_WfsNs::getBaseWfsUri());
 		foreach ($schemaCache as $childSchema) {
 			$xlmns[ $childSchema['nsPrefix'] ] = "{$rootWfsNsUri}/" . str_replace('__x3A__', '/', $childSchema['nsPrefix']);
 		}
@@ -236,14 +236,18 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 			DBG::log($item, 'array' , ">>> loop({$itemKey})");
 			DBG::log($searchParams['cols'], 'array' , "\$searchParams['cols']");
 			$xmlWriter->startElement('gml:featureMember');
-			Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, $searchParams['cols'], $tagName = "{$wfsNs}:{$type}", array_merge(
-				[
-					'fid' => "{$type}.{$itemKey}",
-				],
-				(!$simple)
-				?	[ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
-				:	[]
-			), $showAdvancedAttrs = !$simple, $schemaCache);
+			Api_Wfs_GetFeature::printXmlFeatureRecurse($xmlWriter, $acl, $item, [
+				'fields' => $searchParams['cols'],
+				'tagName' => "{$wfsNs}:{$type}",
+				'xsdAttributes' => array_merge(
+					[ 'fid' => "{$type}.{$itemKey}" ],
+					(!$simple)
+					?	[ "{$rootWfsNs}:web_link" => Request::getPathUri() . "index.php?_route=ViewTableAjax&namespace=" . $acl->getNamespace() . "#EDIT/{$itemKey}" ]
+					:	[]
+				),
+				'showAdvancedAttrs' => !$simple,
+				'outputBlobFormat' => $args['outputBlobFormat'],
+			], $schemaCache);
 			$xmlWriter->endElement();// gml:featureMember
 		}
 		$xmlWriter->endElement();// wfs:FeatureCollection
@@ -337,23 +341,7 @@ class Api_WfsDataServer extends Api_WfsServerBase {
 		if (!$acl->canReadObjectField($fieldName, $item)) throw new Exception("Access Denied for field '{$fieldName}'");
 
 
-		$pdo = DB::getPDO($idDatabase);
-		switch ($pdo->getType()) {
-			case 'mysql': {
-				$sql = "
-					select {$sqlFieldName}
-					from {$rootTableName}
-					where `{$sqlPkField}` = :pk
-					limit 1
-				";
-				$sth = $pdo->prepare($sql);
-				$sth->bindValue(':pk', $primaryKey, PDO::PARAM_STR);
-				$sth->execute();
-				$sth->bindColumn(1, $content, PDO::PARAM_LOB);
-				$sth->fetch();
-			} break;
-			default: throw new Exception("Not implemented database type '" . $pdo->getType() . "' in GetBlob");
-		}
+		$content = DB::getPDO($idDatabase)->getBlob($rootTableName, $sqlFieldName, $sqlPkField, $primaryKey);
 		if (empty($content)) throw new Exception("Brak danych");
 		$finfo = finfo_open(FILEINFO_MIME);
 		$mime = finfo_buffer($finfo, $content);

+ 1 - 0
SE/se-lib/Api/WfsServerBase.php

@@ -108,6 +108,7 @@ class Api_WfsServerBase {
 		$args['bbox'] = null;// @from: BBOX (bbox)
 		$args['wfs:featureID'] = null;// @from: featureID, featureId (featureid)
 		$args['primaryKey'] = null;// primaryKey type - @from: featureID or $req[primaryKey]
+		$args['outputBlobFormat'] = V::get('outputBlobFormat', 'base64', $rawArgs); // how to print blob fields ('base64', 'link') - default base64
 
 		$lowerArgs = array(); foreach ($rawArgs as $name => $value) $lowerArgs[ strtolower($name) ] = trim($value);