Skip to content
Snippets Groups Projects
Commit 0da66602 authored by Nia Kathoni's avatar Nia Kathoni Committed by Daniel Cothran
Browse files

Issue #3461109 by nikathone: Fix csv content caching after extraction from the csv file

parent 711579f2
No related branches found
No related tags found
1 merge request!8Adjust the way the fetch content method in the connection clas cache the...
Pipeline #223131 passed with warnings
......@@ -304,6 +304,7 @@ class ViewsCsvQuery extends QueryPluginBase {
if (!empty($view->live_preview)) {
$this->messenger->addError($e->getMessage());
}
$this->logger->error($e->getMessage());
}
// Avoid notices about $view->execute_time being undefined if the query
......@@ -614,7 +615,7 @@ class ViewsCsvQuery extends QueryPluginBase {
* The headers of the CSV file.
*/
public function getCsvHeader(): array {
return $this->connection->getCsvHeader($this->getCsvFileUri());
return $this->connection->getCsvHeader($this->getCsvFileUri(), $this->getQueryOptions());
}
/**
......
......@@ -44,7 +44,7 @@ class Connection {
* The event dispatcher.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param string $csvContent
* @param array $csvContent
* The content of the CSV file.
* @param string $csvUri
* The URI of the CSV file.
......@@ -55,7 +55,7 @@ class Connection {
protected TimeInterface $time,
protected ContainerAwareEventDispatcher $eventDispatcher,
ConfigFactoryInterface $config_factory,
protected string $csvContent = '',
protected array $csvContent = [],
protected string $csvUri = '',
) {
$this->config = $config_factory->get('views_csv_source.settings');
......@@ -112,10 +112,14 @@ class Connection {
* If the file is not found.
*/
public function fetchContent(string $uri, array $options = []): string {
if ($this->csvContent) {
return $this->csvContent;
$unparsed_uri = $uri;
if (!empty($this->csvContent[$unparsed_uri])) {
return $this->csvContent[$unparsed_uri];
}
// This is probably a new connection let reset the csv content container.
$this->csvContent = [];
$scheme = parse_url($options['csv_file'], PHP_URL_SCHEME);
$uri = $this->parseCsvUri($uri);
......@@ -124,14 +128,14 @@ class Connection {
if (!file_exists($uri)) {
throw new \Exception('Local file not found.');
}
$this->csvContent = file_get_contents($uri);
return $this->csvContent;
$this->csvContent[$unparsed_uri] = file_get_contents($uri);
return $this->csvContent[$unparsed_uri];
}
$cache_id = 'views_csv_source_' . md5($uri);
if ($cache = $this->cache->get($cache_id)) {
$this->csvContent = $cache->data;
return $this->csvContent;
$this->csvContent[$unparsed_uri] = $cache->data;
return $this->csvContent[$unparsed_uri];
}
$options += [
......@@ -147,6 +151,7 @@ class Connection {
}
catch (GuzzleException $e) {
$result = NULL;
\Drupal::logger('views_csv_source')->error($e->getMessage());
}
if (isset($result->error) || $result === NULL) {
......@@ -161,11 +166,11 @@ class Connection {
// Dispatch event before caching csv_content.
$event = new PreCacheEvent($options['cache_id'], $csv_content);
$this->eventDispatcher->dispatch($event, PreCacheEvent::VIEWS_CSV_SOURCE_PRE_CACHE);
$this->csvContent = $event->getData();
$this->csvContent[$unparsed_uri] = $event->getData();
$this->cache->set($cache_id, $this->csvContent, $cache_ttl);
$this->cache->set($cache_id, $this->csvContent[$unparsed_uri], $cache_ttl);
return $this->csvContent;
return $this->csvContent[$unparsed_uri];
}
/**
......@@ -177,13 +182,18 @@ class Connection {
* @return array
* The headers of the CSV file.
*/
public function getCsvHeader(string $uri = ''): array {
public function getCsvHeader(string $uri = '', array $query_options = []): array {
if (empty($uri)) {
$uri = $this->csvUri;
}
$options = $query_options += [
'delimiter' => ',',
'header_index' => 0,
];
try {
$csv = Reader::createFromString($this->fetchContent($uri));
$csv = Reader::createFromString($this->fetchContent($uri, $options));
return $csv->nth(0) ?? [];
}
catch (\Exception $e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment