From 0c462097252affa595a5244b32052cdc5aba883f Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 22 May 2020 21:01:45 -0600 Subject: [PATCH] Fix crash when a cached path doesn't actually exist in the specified datasource Fixes an issue when using databaseTemplates where if the pathCache had been generated, and then any template was removed from the database manually the pathCache being out of date would cause an exception to be thrown elsewhere. --- modules/cms/classes/AutoDatasource.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/cms/classes/AutoDatasource.php b/modules/cms/classes/AutoDatasource.php index 7c94b5356..126651138 100644 --- a/modules/cms/classes/AutoDatasource.php +++ b/modules/cms/classes/AutoDatasource.php @@ -333,7 +333,24 @@ class AutoDatasource extends Datasource implements DatasourceInterface public function selectOne(string $dirName, string $fileName, string $extension) { try { - $result = $this->getDatasourceForPath($this->makeFilePath($dirName, $fileName, $extension))->selectOne($dirName, $fileName, $extension); + $path = $this->makeFilePath($dirName, $fileName, $extension); + $result = $this->getDatasourceForPath($path)->selectOne($dirName, $fileName, $extension); + + // if result = null, this means that + // - a: The requested record doesn't exist + // - b: The requested record exists, but is marked deleted + // - c: The requested record is reported to exist in a datasource that it doesn't actually exist in + if (is_null($result)) { + foreach ($this->pathCache as $paths) { + // If the path is reported to exist here (and isn't marked deleted) even though the previous attempt + // returned nothing, then the paths cache needs to be rebuilt and we should try again + if (@$paths[$path]) { + $this->populateCache(true); + $result = $this->getDatasourceForPath($path)->selectOne($dirName, $fileName, $extension); + break; + } + } + } } catch (Exception $ex) { $result = null; }