* Get the data from a data source; * * @param int $datasourceId * @param $datasetMetadata * @param bool $allowCacheValidation * @return array|NotFoundException */
(int $datasourceId, $datasetMetadata, bool $allowCacheValidation = true)
| 149 | * @return array|NotFoundException |
| 150 | */ |
| 151 | #[NoAdminRequired] |
| 152 | public function read(int $datasourceId, $datasetMetadata, bool $allowCacheValidation = true) { |
| 153 | if (!$this->getDatasources()[$datasourceId]) { |
| 154 | $result['error'] = $this->l10n->t('Data source not available anymore'); |
| 155 | return $result; |
| 156 | } |
| 157 | |
| 158 | $option = array(); |
| 159 | // before 3.1.0, the options were in another format. as of 3.1.0 the standard option array is used |
| 160 | if ($datasetMetadata['link'][0] !== '{') { |
| 161 | $option['link'] = $datasetMetadata['link']; |
| 162 | } else { |
| 163 | $option = json_decode($datasetMetadata['link'], true); |
| 164 | } |
| 165 | unset($option['cacheKey']); |
| 166 | $option['user_id'] = $datasetMetadata['user_id']; |
| 167 | if (isset($option['link']) && in_array($datasourceId, [ |
| 168 | self::DATASET_TYPE_LOCAL_CSV, |
| 169 | self::DATASET_TYPE_LOCAL_SPREADSHEET, |
| 170 | self::DATASET_TYPE_LOCAL_JSON, |
| 171 | ], true)) { |
| 172 | $option['link'] = $this->VariableService->replaceDatasourceText((string)$option['link']); |
| 173 | } |
| 174 | if ($allowCacheValidation && isset($datasetMetadata['cacheKey']) && $datasetMetadata['cacheKey'] !== '') { |
| 175 | $option['cacheKey'] = $datasetMetadata['cacheKey']; |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | // read the data from the source |
| 180 | $result = $this->getDatasources()[$datasourceId]->readData($option); |
| 181 | if (isset($result['cache']['notModified']) && $result['cache']['notModified'] === true) { |
| 182 | return $result; |
| 183 | } |
| 184 | |
| 185 | // if data source should be timestamped/snapshoted |
| 186 | if (isset($option['timestamp']) && ($option['timestamp'] === 'true' || $option['timestamp'] === 'filename')) { |
| 187 | $timestamp = $this->getDatasourceTimestamp($option); |
| 188 | if ($timestamp === null) { |
| 189 | return [ |
| 190 | 'error' => $this->l10n->t('No valid timestamp found in datasource filename'), |
| 191 | 'data' => [], |
| 192 | 'status' => 'nodata', |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | $result['data'] = array_map(function ($tag) use ($timestamp) { |
| 197 | $columns = count($tag); |
| 198 | if ($columns > 1) { |
| 199 | // shift values by one dimension and stores date in second column |
| 200 | return array($tag[$columns - 2], $timestamp, $tag[$columns - 1]); |
| 201 | } else { |
| 202 | // just return 2 columns if the original data only has one column |
| 203 | return array($timestamp, $tag[$columns - 1]); |
| 204 | } |
| 205 | }, $result['data']); |
| 206 | } |
| 207 | |
| 208 | $originalFilterOptions = $datasetMetadata['filteroptions'] ?? null; |
nothing calls this directly
no test coverage detected