From 1920d5b4b6edb25fdf6b316e74bff3ee1926c049 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Thu, 22 Nov 2018 12:04:41 -0600 Subject: [PATCH] Fix bug where updating records that didn't exist in the DB yet would cause both the original and new records to display --- modules/cms/classes/AutoDatasource.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/modules/cms/classes/AutoDatasource.php b/modules/cms/classes/AutoDatasource.php index 173ad4833..42d3b6594 100644 --- a/modules/cms/classes/AutoDatasource.php +++ b/modules/cms/classes/AutoDatasource.php @@ -10,7 +10,8 @@ use October\Rain\Halcyon\Datasource\DatasourceInterface; /** * Datasource that loads from other data sources automatically * - * @Todo: Need to prevent softdeleted DB records from appearing, even if they exist in the filesystem + * @package october\cms + * @author Luke Towers */ class AutoDatasource extends Datasource implements DatasourceInterface { @@ -24,6 +25,11 @@ class AutoDatasource extends Datasource implements DatasourceInterface */ protected $pathCache = []; + /** + * @var boolean Flag on whether the cache should respect refresh requests + */ + protected $allowCacheRefreshes = true; + /** * Create a new datasource instance. * @@ -50,7 +56,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface $pathCache = []; foreach ($this->datasources as $datasource) { // Remove any existing cache data - if ($refresh) { + if ($refresh && $this->allowCacheRefreshes) { Cache::forget($datasource->getPathsCacheKey()); } @@ -134,7 +140,7 @@ class AutoDatasource extends Datasource implements DatasourceInterface $fnMatch = !empty($options['fileMatch']) ? fnmatch($options['fileMatch'], str_after($path, $basePath)) : true; // Check the extension if provided as an option - $validExt = is_array($options['extensions']) && !empty($options['extensions']) ? in_array(pathinfo($path, PATHINFO_EXTENSION), $options['extensions']) : true; + $validExt = !empty($options['extensions']) && is_array($options['extensions']) ? in_array(pathinfo($path, PATHINFO_EXTENSION), $options['extensions']) : true; return $inPath && $fnMatch && $validExt; }, ARRAY_FILTER_USE_KEY)); @@ -267,6 +273,14 @@ class AutoDatasource extends Datasource implements DatasourceInterface $searchFileName = $oldFileName ?: $fileName; $searchExt = $oldExtension ?: $extension; + // Ensure that files that are being renamed have their old names marked as deleted prior to inserting the renamed file + // Also ensure that the cache only gets updated at the end of this operation instead of twice, once here and again at the end + if ($searchFileName !== $fileName || $searchExt !== $extension) { + $this->allowCacheRefreshes = false; + $this->delete($dirName, $searchFileName, $searchExt); + $this->allowCacheRefreshes = true; + } + if (!empty($this->datasources[0]->selectOne($dirName, $searchFileName, $searchExt))) { $result = $this->datasources[0]->update($dirName, $fileName, $extension, $content, $oldFileName, $oldExtension); } else {