|
6 | 6 | use Tdt\Core\Pager; |
7 | 7 | use Tdt\Core\Repositories\Interfaces\TabularColumnsRepositoryInterface; |
8 | 8 | use Tdt\Core\Repositories\Interfaces\GeoPropertyRepositoryInterface; |
| 9 | +use Tdt\Core\Datasets\DatasetController; |
| 10 | + |
| 11 | +ini_set('auto_detect_line_endings', true); |
9 | 12 |
|
10 | 13 | /** |
11 | 14 | * CSV Controller |
@@ -34,6 +37,15 @@ public function readData($source_definition, $rest_parameters = array()) |
34 | 37 | { |
35 | 38 | list($limit, $offset) = Pager::calculateLimitAndOffset(); |
36 | 39 |
|
| 40 | + // Get the format, if it's CSV we allow a full read of the datasource without paging limitation |
| 41 | + list($uri, $extension) = $this->processURI(\Request::path()); |
| 42 | + |
| 43 | + $ignore_paging = false; |
| 44 | + |
| 45 | + if (strtolower($extension) == 'csv') { |
| 46 | + $ignore_paging = true; |
| 47 | + } |
| 48 | + |
37 | 49 | // Disregard the paging when rest parameters are given |
38 | 50 | if (!empty($rest_parameters)) { |
39 | 51 | $limit = PHP_INT_MAX; |
@@ -130,7 +142,7 @@ public function readData($source_definition, $rest_parameters = array()) |
130 | 142 | ); |
131 | 143 |
|
132 | 144 | if (($handle = fopen($uri, "r", false, stream_context_create($ssl_options))) !== false) { |
133 | | - while (($data = fgetcsv($handle, 2000000, $delimiter)) !== false) { |
| 145 | + while (($data = fgetcsv($handle, 0, $delimiter)) !== false) { |
134 | 146 | if ($total_rows >= $start_row) { |
135 | 147 | // Create the values array, containing the (aliased) name of the column |
136 | 148 | // to the value of a the row which $data represents |
@@ -158,12 +170,11 @@ public function readData($source_definition, $rest_parameters = array()) |
158 | 170 |
|
159 | 171 | $total_rows++; |
160 | 172 |
|
161 | | - if ($total_rows >= 10000) { |
| 173 | + if ($total_rows >= 10000 && !$ignore_paging) { |
162 | 174 | break; |
163 | 175 | } |
164 | 176 | } |
165 | 177 | fclose($handle); |
166 | | - |
167 | 178 | } else { |
168 | 179 | \App::abort(500, "Cannot retrieve any data from the CSV file on location $uri."); |
169 | 180 | } |
@@ -199,6 +210,41 @@ private function createValues($columns, $data) |
199 | 210 | return $result; |
200 | 211 | } |
201 | 212 |
|
| 213 | + /** |
| 214 | + * Parse the URI and look for the resource name and the format |
| 215 | + * |
| 216 | + * @param string $uri |
| 217 | + * |
| 218 | + * @return array |
| 219 | + */ |
| 220 | + private function processURI($uri) |
| 221 | + { |
| 222 | + $dot_position = strrpos($uri, '.'); |
| 223 | + |
| 224 | + if (!$dot_position) { |
| 225 | + return array($uri, null); |
| 226 | + } |
| 227 | + |
| 228 | + // If a dot has been found, do a couple |
| 229 | + // of checks to find out if it introduces a formatter |
| 230 | + $uri_parts = explode('.', $uri); |
| 231 | + |
| 232 | + $possible_extension = strtoupper(array_pop($uri_parts)); |
| 233 | + |
| 234 | + $uri = implode('.', $uri_parts); |
| 235 | + |
| 236 | + $formatter_class = 'Tdt\\Core\\Formatters\\' . $possible_extension . 'Formatter'; |
| 237 | + |
| 238 | + if (!class_exists($formatter_class)) { |
| 239 | + // Re-attach the dot with the latter part of the uri |
| 240 | + $uri .= '.' . strtolower($possible_extension); |
| 241 | + |
| 242 | + return array($uri, null); |
| 243 | + } |
| 244 | + |
| 245 | + return array($uri, strtolower($possible_extension)); |
| 246 | + } |
| 247 | + |
202 | 248 | /** |
203 | 249 | * Parse the columns from a CSV file and return them |
204 | 250 | * Optionally aliases can be given to columns as well as a primary key |
|
0 commit comments