Skip to content

Commit

Permalink
Merge pull request #447 from jjrom/develop
Browse files Browse the repository at this point in the history
Convert catalog paths internally to conform to LTREE alphanumerical constraints
  • Loading branch information
jjrom authored Oct 10, 2024
2 parents 8d11a34 + 7302094 commit 7d90fab
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/resto/core/RestoConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RestoConstants
// [IMPORTANT] Starting resto 7.x, default routes are defined in RestoRouter class

// resto version
const VERSION = '9.0.0-RC11';
const VERSION = '9.0.0-RC12';

/* ============================================================
* NEVER EVER TOUCH THESE VALUES
Expand Down
2 changes: 1 addition & 1 deletion app/resto/core/api/STACAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public function addCatalog($params, $body)
RestoLogUtil::httpError(409, 'Catalog ' . $body['id'] . ' already exists');
}
$baseUrl = $this->context->core['baseUrl'];
return RestoLogUtil::success('Catalog added', $this->catalogsFunctions->storeCatalog($body, $this->user->profile['id'], $baseUrl, null, null));
return RestoLogUtil::success('Catalog added', $this->catalogsFunctions->storeCatalog($body, $this->user->profile['id'], $baseUrl, null, null, false));

}

Expand Down
25 changes: 17 additions & 8 deletions app/resto/core/dbfunctions/CatalogsFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,20 @@ public function getCatalogItems($catalogId, $baseUrl)
* @param string $baseUrl
* @param string $collectionId
* @param string $featureId
* @param boolean $inTransaction
* @param boolean $inTransaction // True means that already in a begin/commit block
*/
public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featureId, $inTransaction = true)
public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featureId, $inTransaction)
{
// Empty catalog - do nothing
if (!isset($catalog)) {
return;
}

// [IMPORTANT] Catalog identifier should never have a trailing /
if ( substr($catalog['id'], -1) === '/' ) {
$catalog['id'] = rtrim($catalog['id'], '/');
}

$defaultCount = isset($featureId) ? 1 : 0;
$counters = array(
'total' => $defaultCount,
Expand All @@ -226,7 +231,7 @@ public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featur

try {

if ( $inTransaction ) {
if ( !$inTransaction ) {
$this->dbDriver->query('BEGIN');
}

Expand Down Expand Up @@ -276,7 +281,11 @@ public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featur
$catalogLevel = count(explode('/', $catalog['id']));
// Convert catalogId to LTREE path - first replace dot with underscore
$path = RestoUtil::path2ltree($catalog['id']);


if ( !isset($catalog['rtype']) ) {
$catalog['rtype'] = null;
}

if ( isset($featureId) && $catalog['rtype'] !== 'collection' && ($catalogLevel > 1 || $catalog['rtype'] === 'catalog') ) {
$this->insertIntoCatalogFeature($featureId, $path, $catalog['id'], $collectionId);
}
Expand All @@ -303,13 +312,13 @@ public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featur
), 500, 'Cannot update catalog feature association for child link ' . $updateCatalogs['id']);
}

if ( $inTransaction ) {
if ( !$inTransaction ) {
$this->dbDriver->query('COMMIT');
}


} catch (Exception $e) {
if ( $inTransaction ) {
if ( !$inTransaction ) {
$this->dbDriver->query('ROLLBACK');
}
RestoLogUtil::httpError(500, $e->getMessage());
Expand All @@ -329,7 +338,7 @@ public function storeCatalog($catalog, $userid, $baseUrl, $collectionId, $featur
* @param RestoCollection $collection
* @param string $featureId
*/
public function storeCatalogs($catalogs, $userid, $collection, $featureId)
public function storeCatalogs($catalogs, $userid, $collection, $featureId, $inTransaction)
{
// Empty catalogs - do nothing
if (!isset($catalogs) || count($catalogs) === 0) {
Expand All @@ -343,7 +352,7 @@ public function storeCatalogs($catalogs, $userid, $collection, $featureId)
$baseUrl = $collection->context->core['baseUrl'];
}
for ($i = count($catalogs); $i--;) {
$this->storeCatalog($catalogs[$i], $userid, $baseUrl, $collectionId, $featureId);
$this->storeCatalog($catalogs[$i], $userid, $baseUrl, $collectionId, $featureId, $inTransaction);
}

return $catalogs;
Expand Down
6 changes: 3 additions & 3 deletions app/resto/core/dbfunctions/FeaturesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function storeFeature($id, $collection, $featureArray)
/*
* Store catalogs
*/
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($keysAndValues['catalogs'], $collection->user->profile['id'], $collection, $result['id']);
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($keysAndValues['catalogs'], $collection->user->profile['id'], $collection, $result['id'], true);

/*
* Commit everything - rollback if one of the inserts failed
Expand All @@ -357,7 +357,7 @@ public function storeFeature($id, $collection, $featureArray)

} catch (Exception $e) {
pg_query($dbh, 'ROLLBACK');
RestoLogUtil::httpError(500, 'Feature ' . ($featureArray['productIdentifier'] ?? '') . ' cannot be inserted in database');
RestoLogUtil::httpError(500, 'Feature ' . ($featureArray['properties']['productIdentifier'] ?? '') . ' cannot be inserted in database');
}

return array(
Expand Down Expand Up @@ -507,7 +507,7 @@ public function updateFeature($feature, $collection, $newFeatureArray)
$feature->id
)
);
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($keysAndValues['catalogs'], $collection->user->profile['id'], $collection, $feature->id);
(new CatalogsFunctions($this->dbDriver))->storeCatalogs($keysAndValues['catalogs'], $collection->user->profile['id'], $collection, $feature->id, true);

/*
* Commit
Expand Down
27 changes: 15 additions & 12 deletions app/resto/core/dbfunctions/FiltersFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,6 @@ private function prepareFilterQueryKeywords($catalogFeatureTableName, $filterNam
$searchTerm = ltrim($searchTerm, '-');
}

/*
* Add prefix in front of all elements if needed
* See for instance [eo:instrument]
*/
if (isset($this->model->searchFilters[$filterName]['pathPrefix'])) {
$searchTerm = $this->addPathPrefix($searchTerm, $this->model->searchFilters[$filterName]['pathPrefix']);
}
else {
$searchTerm = '*.' . $searchTerm;
}

$this->terms[] = array_merge($this->processSearchTerms($searchTerm, $filters, $catalogFeatureTableName, $filterName, $exclusion));
}

Expand Down Expand Up @@ -705,7 +694,21 @@ private function processSearchTerms($searchTerm, &$filters, $catalogFeatureTable

$where = array();
for ($j = count($exploded); $j--;) {
$where[] = $catalogFeatureTableName . '.path ~ ' . '\'' . pg_escape_string($this->context->dbDriver->getConnection(), trim($exploded[$j] . '.*')) . '\'';

$ltreePath = RestoUtil::path2ltree($exploded[$j]);

/*
* Add prefix in front of all elements if needed
* See for instance [eo:instrument]
*/
if (isset($this->model->searchFilters[$filterName]['pathPrefix'])) {
$ltreePath = $this->addPathPrefix($ltreePath, $this->model->searchFilters[$filterName]['pathPrefix']);
}
else {
$ltreePath = '*.' . $ltreePath;
}

$where[] = $catalogFeatureTableName . '.path ~ ' . '\'' . pg_escape_string($this->context->dbDriver->getConnection(), trim($ltreePath . '.*')) . '\'';
}

if ($isOr) {
Expand Down
4 changes: 3 additions & 1 deletion app/resto/core/utils/RestoUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,12 @@ public static function localhostToDockerhost($url)

/**
* Convert a unix path (i.e. this/is/my/home ) to LTREE path (i.e. this.is.my.home)
*
* [IMPORTANT] LTREE path can only contains A-Za-z0-9_- characters so replace any other characters with _
*/
public static function path2ltree($path)
{
return strtolower(str_replace('/', '.', str_replace('.', '_', $path)));
return strtolower(str_replace('/', '.', preg_replace('/[^A-Za-z0-9_\- \/]/', '_', $path)));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions build/resto/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ ENV BUILD_DIR=./build/resto \
RESTO_DEBUG=1 \
S6_BEHAVIOUR_IF_STAGE2_FAILS=2

# [TODO] Should be directly removed from jjrom/nginx-fpm
# Avoid "PHP Warning: GEOSGeometry::__toString()" error at startup
RUN apt-get remove -y php-geos

# Copy NGINX configuration
COPY ${BUILD_DIR}/container_root/etc/nginx /etc/nginx

Expand Down

0 comments on commit 7d90fab

Please sign in to comment.