Skip to content

Commit

Permalink
[PHP] Get cached Polygon filter for project, layer, login and editing…
Browse files Browse the repository at this point in the history
… context

The Polygon filter is provided by QGIS Server. The request could take a long time so the response as to be cached.

Funded by [Conseil Départemental du Calvados](https://www.calvados.fr/accueil.html)
  • Loading branch information
rldhont committed Sep 29, 2023
1 parent f530e02 commit a7d34c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
46 changes: 32 additions & 14 deletions lizmap/modules/lizmap/classes/qgisVectorLayer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1396,10 +1396,12 @@ public function getRealEditionCapabilities()
*
* @param bool $editing_context If we are in a editing context or no. Default false
* @param int $ttl Cache TTL in seconds. Default 60. Use -1 to deactivate the cache.
* @param bool $get_expression If we need the expression and not the SQL
* @param bool $use_cache If we need to not use cache
*
* @return array associative array containing the keys 'expression' and 'polygon'
*/
protected function requestPolygonFilter($editing_context = false, $ttl = 60)
protected function requestPolygonFilter($editing_context = false, $ttl = 60, $get_expression = false, $use_cache = true)
{
// No filter response
$no_filter_array = array(
Expand Down Expand Up @@ -1449,17 +1451,12 @@ protected function requestPolygonFilter($editing_context = false, $ttl = 60)
}

// Get cached filter for this repository, project, layer, login and editing context
// TODO: Use the project cache tools
$use_cache = false;
// $use_cache = ($ttl > 0);
// $key = $this->project->getKey().'_'.$repository->getKey();
// $key .= '_'.$this->id.'_'.$user_key;
// $key .= '_'.(string) $editing_context;
// $cache_key = md5($key);
// $cached_filter = jCache::get($cache_key);
// if ($cached_filter && $use_cache) {
// return json_decode($cached_filter);
// }
$cache_key = session_id().'-lizmap-polygon-filter';
$cache_key .= '-'.$this->name; // layer
$cache_key .= '-'.$user_key; // login
if ($editing_context) {
$cache_key .= '-editing';
}

// Request the "polygon filter" string from QGIS Server lizmap plugin
$params = array(
Expand All @@ -1468,6 +1465,28 @@ protected function requestPolygonFilter($editing_context = false, $ttl = 60)
'map' => $this->project->getRelativeQgisPath(),
'layer' => $this->name,
);
if ($get_expression) {
$params['filter_type'] = 'expression';
$cache_key .= '-expression';
}
$cache_key = 'getsubsetstring-'.sha1($cache_key);
$cached = false;

if ($use_cache) {
try {
$cached = $this->project->getCacheHandler()->getProjectRelatedDataCache($cache_key);
} catch (\Exception $e) {
// if qgisprojects profile does not exist, or if there is an
// other error about the cache, let's log it
\jLog::logEx($e, 'error');
$use_cache = false;
}
}

// return cached data
if ($cached !== false) {
return $cached;
}

// Add user and groups in parameters
$user_and_groups = array(
Expand Down Expand Up @@ -1507,8 +1526,7 @@ protected function requestPolygonFilter($editing_context = false, $ttl = 60)
);

if ($use_cache) {
// Todo: use the project cache tools
// jCache::set($cache_key, json_encode($polygon_filter_data), $ttl);
$cached = $this->project->getCacheHandler()->setProjectRelatedDataCache($cache_key, $polygon_filter_data, 3600);
}

return $polygon_filter_data;
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,6 @@ parameters:
count: 3
path: lizmap/modules/lizmap/classes/qgisVectorLayer.class.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: lizmap/modules/lizmap/classes/qgisVectorLayer.class.php

-
message: "#^Method qgisVectorLayer\\:\\:getConstraints\\(\\) should return array\\<string\\> but returns array\\<string, int\\|false\\>\\.$#"
count: 1
Expand Down

0 comments on commit a7d34c4

Please sign in to comment.