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' => 'columns', 'name' => $this->l10n->t('Select columns'), 'placeholder' => $this->l10n->t('e.g. 1,2,4 or leave empty')]); |
63
|
|
|
array_push($template, ['id' => 'offset', 'name' => $this->l10n->t('Ignore leading rows'), 'placeholder' => $this->l10n->t('Number of rows')]); |
64
|
|
|
return $template; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Read the Data |
69
|
|
|
* @param $option |
70
|
|
|
* @return array available options of the datasoure |
71
|
|
|
* @throws NotFoundException |
72
|
|
|
* @throws \OCP\Files\NotPermittedException |
73
|
|
|
*/ |
74
|
|
|
public function readData($option): array |
75
|
|
|
{ |
76
|
|
|
$data = array(); |
77
|
|
|
$header = array(); |
78
|
|
|
$headerrow = $errorMessage = 0; |
79
|
|
|
$selectedColumns = array(); |
80
|
|
|
|
81
|
|
|
if (isset($option['path'])) { |
82
|
|
|
$file = $this->rootFolder->getUserFolder($this->userId)->get($option['path']); |
83
|
|
|
} else { |
84
|
|
|
//$this->logger->debug('FileService 53 file content:' . $option['user_id'] . $option['link']); |
85
|
|
|
$file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$rows = str_getcsv($file->getContent(), "\n"); |
|
|
|
|
89
|
|
|
|
90
|
|
|
// remove x number of rows from the beginning |
91
|
|
|
if (isset($option['offset']) and is_numeric($option['offset'])) { |
92
|
|
|
$rows = array_slice($rows, $option['offset']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// ensure that all values are integers |
96
|
|
|
if (isset($option['columns'])) { |
97
|
|
|
$new = array(); |
98
|
|
|
$selectedColumns = str_getcsv($option['columns'], ','); |
99
|
|
|
foreach ($selectedColumns as $value) { |
100
|
|
|
if (is_numeric($value)) { |
101
|
|
|
array_push($new, $value); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$selectedColumns = $new; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$delimiter = $this->detectDelimiter($rows[0]); |
108
|
|
|
|
109
|
|
|
foreach ($rows as &$row) { |
110
|
|
|
$row = str_getcsv($row, $delimiter); |
111
|
|
|
$rowMinimized = array(); |
112
|
|
|
|
113
|
|
|
if (count($selectedColumns) !== 0) { |
114
|
|
|
foreach ($selectedColumns as $selectedColumn) { |
115
|
|
|
array_push($rowMinimized, $row[$selectedColumn - 1]); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
$rowMinimized = $row; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($headerrow === 0) { |
122
|
|
|
$header = $rowMinimized; |
123
|
|
|
$headerrow = 1; |
124
|
|
|
} else { |
125
|
|
|
array_push($data, $rowMinimized); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return [ |
129
|
|
|
'header' => $header, |
130
|
|
|
'data' => $data, |
131
|
|
|
'error' => $errorMessage, |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function detectDelimiter($data) |
136
|
|
|
{ |
137
|
|
|
$delimiters = ["\t", ";", "|", ","]; |
138
|
|
|
$data_2 = array(); |
139
|
|
|
$delimiter = $delimiters[0]; |
140
|
|
|
foreach ($delimiters as $d) { |
141
|
|
|
//$firstRow = str_getcsv($data, "\n")[0]; |
142
|
|
|
$data_1 = str_getcsv($data, $d); |
143
|
|
|
if (sizeof($data_1) > sizeof($data_2)) { |
144
|
|
|
$delimiter = $d; |
145
|
|
|
$data_2 = $data_1; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
return $delimiter; |
149
|
|
|
} |
150
|
|
|
} |