1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2019 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
13
|
|
|
|
14
|
|
|
use OCP\ILogger; |
15
|
|
|
|
16
|
|
|
class ExternalFileService |
17
|
|
|
{ |
18
|
|
|
private $logger; |
19
|
|
|
private $userId; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
$userId, |
23
|
|
|
ILogger $logger |
24
|
|
|
) |
25
|
|
|
{ |
26
|
|
|
$this->userId = $userId; |
27
|
|
|
$this->logger = $logger; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the content from an external url |
32
|
|
|
* |
33
|
|
|
* @NoAdminRequired |
34
|
|
|
* @param $datasetMetadata |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function read($datasetMetadata) |
38
|
|
|
{ |
39
|
|
|
//$this->logger->error('dataset path: ' . $datasetMetadata['link']); |
40
|
|
|
$string = $datasetMetadata['link']; |
41
|
|
|
|
42
|
|
|
$ch = curl_init(); |
43
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
|
|
|
|
44
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
45
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
46
|
|
|
curl_setopt($ch, CURLOPT_URL, $string); |
47
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $string); |
48
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
49
|
|
|
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'); |
50
|
|
|
$data = curl_exec($ch); |
|
|
|
|
51
|
|
|
curl_close($ch); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$delimiter = $this->detectDelimiter($data); |
54
|
|
|
$rows = str_getcsv($data, "\n"); |
55
|
|
|
$data = array(); |
56
|
|
|
$header = array(); |
57
|
|
|
$headerrow = 0; |
58
|
|
|
|
59
|
|
|
foreach ($rows as &$row) { |
60
|
|
|
$row = str_getcsv($row, $delimiter); |
61
|
|
|
if ($headerrow === 0) { |
62
|
|
|
$header['dimension1'] = $row[0]; |
63
|
|
|
$header['dimension2'] = $row[1]; |
64
|
|
|
$header['dimension3'] = $row[2]; |
65
|
|
|
$headerrow = 1; |
66
|
|
|
} else { |
67
|
|
|
array_push($data, ['dimension1' => $row[0], 'dimension2' => $row[1], 'dimension3' => $row[2]]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$result = [ |
71
|
|
|
'header' => $header, |
72
|
|
|
'data' => $data |
73
|
|
|
]; |
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function detectDelimiter($data) |
78
|
|
|
{ |
79
|
|
|
$delimiters = ["\t", ";", "|", ","]; |
80
|
|
|
$data_2 = array(); |
81
|
|
|
$delimiter = $delimiters[0]; |
82
|
|
|
foreach ($delimiters as $d) { |
83
|
|
|
$firstRow = str_getcsv($data, "\n")[0]; |
84
|
|
|
$data_1 = str_getcsv($firstRow, $d); |
85
|
|
|
if (sizeof($data_1) > sizeof($data_2)) { |
86
|
|
|
$delimiter = $d; |
87
|
|
|
$data_2 = $data_1; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $delimiter; |
91
|
|
|
} |
92
|
|
|
} |