MCPcopy Create free account
hub / github.com/Rello/analytics / readData

Method readData

lib/Datasource/ExternalCsv.php:72–161  ·  view source on GitHub ↗

* Read the Data * @param $option * @return array available options of the data source */

($option)

Source from the content-addressed store, hash-verified

70 * @return array available options of the data source
71 */
72 public function readData($option): array
73 {
74 $cache = $this->getCacheMetadata($option);
75 if ($cache['notModified'] === true) {
76 return [
77 'header' => [],
78 'dimensions' => [],
79 'data' => [],
80 'rawdata' => null,
81 'error' => 0,
82 'cache' => $cache,
83 ];
84 }
85
86 $url = htmlspecialchars_decode($option['link'], ENT_NOQUOTES);
87 $urlError = ExternalUrlValidator::validate($url);
88 if ($urlError !== null) {
89 return [
90 'header' => [],
91 'dimensions' => [],
92 'data' => [],
93 'error' => $urlError,
94 'cache' => $cache,
95 ];
96 }
97 $ch = curl_init();
98 if ($ch !== false) {
99 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
100 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
101 curl_setopt($ch, CURLOPT_HEADER, false);
102 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
103 curl_setopt($ch, CURLOPT_URL, $url);
104 curl_setopt($ch, CURLOPT_REFERER, $url);
105 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
106 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
107 $curlResult = curl_exec($ch);
108 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
109 curl_close($ch);
110 } else {
111 $curlResult = '';
112 $http_code = 0;
113 }
114
115 $rows = str_getcsv($curlResult, "\n");
116
117 // remove x number of rows from the beginning
118 if (isset($option['offset']) and is_numeric($option['offset'])) {
119 $rows = array_slice($rows, $option['offset']);
120 }
121
122 $selectedColumns = array();
123 if (isset($option['columns']) && strlen($option['columns']) > 0) {
124 $selectedColumns = str_getcsv($option['columns'], ',');
125 }
126
127 // get the delimiter by reading the first row
128 $delimiter = $this->detectDelimiter($rows[0]);
129

Callers

nothing calls this directly

Calls 4

getCacheMetadataMethod · 0.95
detectDelimiterMethod · 0.95
minimizeRowMethod · 0.95
validateMethod · 0.45

Tested by

no test coverage detected