|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* 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 2020 Marcel Scherello |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
|
13
|
|
|
|
|
14
|
|
|
use OCP\Files\IRootFolder; |
|
15
|
|
|
use OCP\Files\NotFoundException; |
|
16
|
|
|
use OCP\IL10N; |
|
17
|
|
|
use OCP\ILogger; |
|
18
|
|
|
|
|
19
|
|
|
class File implements IDatasource |
|
20
|
|
|
{ |
|
21
|
|
|
private $logger; |
|
22
|
|
|
private $rootFolder; |
|
23
|
|
|
private $userId; |
|
24
|
|
|
private $l10n; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
$userId, |
|
28
|
|
|
IL10N $l10n, |
|
29
|
|
|
ILogger $logger, |
|
30
|
|
|
IRootFolder $rootFolder |
|
31
|
|
|
) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->userId = $userId; |
|
34
|
|
|
$this->l10n = $l10n; |
|
35
|
|
|
$this->logger = $logger; |
|
36
|
|
|
$this->rootFolder = $rootFolder; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string Display Name of the datasource |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getName(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->l10n->t('Local File'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return int digit unique datasource id |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getId(): int |
|
51
|
|
|
{ |
|
52
|
|
|
return 1; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array available options of the datasoure |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getTemplate(): array |
|
59
|
|
|
{ |
|
60
|
|
|
$template = array(); |
|
61
|
|
|
array_push($template, ['id' => 'link', 'name' => 'Filelink', 'placeholder' => 'filelink']); |
|
62
|
|
|
array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']); |
|
63
|
|
|
return $template; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Read the Data |
|
68
|
|
|
* @param $option |
|
69
|
|
|
* @return array available options of the datasoure |
|
70
|
|
|
* @throws NotFoundException |
|
71
|
|
|
* @throws \OCP\Files\NotPermittedException |
|
72
|
|
|
*/ |
|
73
|
|
|
public function readData($option): array |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($option['path'])) { |
|
76
|
|
|
$file = $this->rootFolder->getUserFolder($this->userId)->get($option['path']); |
|
77
|
|
|
} else { |
|
78
|
|
|
//$this->logger->debug('FileService 53 file content:' . $option['user_id'] . $option['link']); |
|
79
|
|
|
$file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
//$this->logger->debug('FileService 63 file content:'. $file->getContent()); |
|
83
|
|
|
$data = $file->getContent(); |
|
|
|
|
|
|
84
|
|
|
$delimiter = $this->detectDelimiter($data); |
|
85
|
|
|
$rows = str_getcsv($data, "\n"); |
|
86
|
|
|
$data = array(); |
|
87
|
|
|
$header = array(); |
|
88
|
|
|
$headerrow = $errorMessage = 0; |
|
89
|
|
|
|
|
90
|
|
|
foreach ($rows as &$row) { |
|
91
|
|
|
$row = str_getcsv($row, $delimiter); |
|
92
|
|
|
if ($headerrow === 0) { |
|
93
|
|
|
$header = $row; |
|
94
|
|
|
$headerrow = 1; |
|
95
|
|
|
} else { |
|
96
|
|
|
array_push($data, $row); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return [ |
|
100
|
|
|
'header' => $header, |
|
101
|
|
|
'data' => $data, |
|
102
|
|
|
'error' => $errorMessage, |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function detectDelimiter($data) |
|
107
|
|
|
{ |
|
108
|
|
|
$delimiters = ["\t", ";", "|", ","]; |
|
109
|
|
|
$data_2 = array(); |
|
110
|
|
|
$delimiter = $delimiters[0]; |
|
111
|
|
|
foreach ($delimiters as $d) { |
|
112
|
|
|
$firstRow = str_getcsv($data, "\n")[0]; |
|
113
|
|
|
$data_1 = str_getcsv($firstRow, $d); |
|
114
|
|
|
if (sizeof($data_1) > sizeof($data_2)) { |
|
115
|
|
|
$delimiter = $d; |
|
116
|
|
|
$data_2 = $data_1; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
return $delimiter; |
|
120
|
|
|
} |
|
121
|
|
|
} |