-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.php
343 lines (294 loc) · 10.5 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* CSV Plugin helper plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <[email protected]>
*/
/**
* Implement CSV parser and other helpers
*/
class helper_plugin_csv extends DokuWiki_Plugin
{
/**
* Returns the default options
*
* @return array
*/
public static function getDefaultOpt()
{
return array(
'hdr_rows' => 1,
'hdr_cols' => 0,
'span_empty_cols' => 0,
'maxlines' => 0,
'offset' => 0,
'file' => '',
'delim' => ',',
'enclosure' => '"',
'escape' => '"',
'content' => '',
'filter' => array(),
'output' => '',
'outc' => 0,
'outr' => 0,
);
}
/**
* Parse the options given in the syntax
*
* @param $optstr
* @return array
*/
public static function parseOptions($optstr)
{
global $INFO;
// defaults
$opt = helper_plugin_csv::getDefaultOpt();
$filters = array();
// parse options - https://regex101.com/r/tNdS9P/3/
preg_match_all(
'/([^ =\[\]]+)(?:\[(\d+)\](?:\[(\w)\])?)?(?:=((?:".*?")|(?:[^ ]+)))?/',
$optstr,
$matches,
PREG_SET_ORDER
);
foreach ($matches as $set) {
$option = $set[1];
$value = isset($set[4]) ? $set[4] : '';
$value = trim($value, '"');
if ($option == 'filter') {
$col = isset($set[2]) ? $set[2] : 1;
$typ = isset($set[3]) ? $set[3] : 'g';
$filters[$col] = array($value, $typ);
} elseif ($value === '') {
$opt['file'] = $option;
} else {
$opt[$option] = $value;
}
}
// fix tab delimiter
if ($opt['delim'] == 'tab') $opt['delim'] = "\t";
// resolve local files
if ($opt['file'] !== '' && !preg_match('/^https?:\/\//i', $opt['file'])) {
resolve_mediaid($INFO['namespace'] ?? '', $opt['file'], $exists);
}
// create regexp filters
foreach ($filters as $col => $filter) {
list($text, $type) = $filter;
if ($type != 'r') {
$text = preg_quote_cb($text);
$text = str_replace('\*', '.*?', $text);
$text = '^' . $text . '$';
}
if (@preg_match("/$text/", null) === false) {
msg("Invalid filter for column $col");
} else {
$opt['filter'][$col - 1] = $text; // use zero based index internally
}
}
// prepare the value output
list($c, $r) = array_pad(explode(',', $opt['output']), 2, 0);
$opt['outc'] = (int)$c;
$opt['outr'] = (int)$r;
if ($opt['outc']) $opt['outc'] -= 1;
if ($opt['outr']) $opt['outr'] -= 1;
unset($opt['output']);
return $opt;
}
/**
* Load CSV data from the given file or remote address
*
* @param $file
* @return string
* @throws Exception
*/
public static function loadContent($file)
{
// load file data
if (preg_match('/^https?:\/\//i', $file)) {
$http = new DokuHTTPClient();
$content = $http->get($file);
if ($content === false) throw new \Exception('Failed to fetch remote CSV data');
} else {
if (auth_quickaclcheck(getNS($file) . ':*') < AUTH_READ) {
throw new \Exception('Access denied to CSV data');
}
$file = mediaFN($file);
if (!file_exists($file)) {
throw new \Exception('requested local CSV file does not exist');
}
$content = io_readFile($file);
}
// if not valid UTF-8 is given we assume ISO-8859-1
if (!utf8_check($content)) $content = utf8_encode($content);
return $content;
}
/**
* @param string $content
* @param array $opt
* @return array
*/
public static function prepareData($content, $opt)
{
$data = array();
// get the first row - it will define the structure
$row = helper_plugin_csv::csv_explode_row($content, $opt['delim'], $opt['enclosure'], $opt['escape']);
$maxcol = count($row);
$line = 0;
while ($row !== false) {
// make sure we have enough columns
$row = array_pad($row, $maxcol, '');
if ($line < $opt['hdr_rows']) {
// if headers are wanted, always add them
$data[] = $row;
} elseif ($opt['offset'] && $line < $opt['offset'] + $opt['hdr_rows']) {
// ignore the line
} elseif ($opt['maxlines'] && $line >= $opt['maxlines'] + $opt['hdr_rows']) {
// we're done
break;
} else {
// check filters
$filterok = true;
foreach ($opt['filter'] as $col => $filter) {
if (!preg_match("/$filter/i", $row[$col])) {
$filterok = false;
break;
}
}
// add the line
if ($filterok) {
$data[] = $row;
}
}
$line++;
$row = helper_plugin_csv::csv_explode_row($content, $opt['delim'], $opt['enclosure'], $opt['escape']);
}
return $data;
}
/**
* Reads one CSV line from the given string
*
* Should handle embedded new lines, escapes, quotes and whatever else CSVs tend to have
*
* Note $delim, $enc, $esc have to be one ASCII character only! The encoding of the content is not
* handled here but is read byte by byte - if you need conversions do it on the output
*
* @param string $str Input string, first CSV line will be removed
* @param string $delim Delimiter character
* @param string $enc Enclosing character
* @param string $esc Escape character
* @return array|boolean fields found on the line, false when no more lines could be found
* @author Andreas Gohr <[email protected]>
*/
public static function csv_explode_row(&$str, $delim = ',', $enc = '"', $esc = '\\')
{
$len = strlen($str);
$infield = false;
$inenc = false;
$fields = array();
$word = '';
for ($i = 0; $i < $len; $i++) {
// convert to unix line endings
if ($str[$i] == "\015") {
if ($str[($i + 1)] != "\012") {
$str[$i] = "\012";
} else {
$i++;
if ($i >= $len) break;
}
}
// simple escape that is not an enclosure
if ($str[$i] == $esc && $esc != $enc) {
$i++; // skip this char and take next as is
$word .= $str[$i];
$infield = true; // we are obviously in a field
continue;
}
/*
* Now decide special cases depending on current field and enclosure state
*/
if (!$infield) { // not in field
// we hit a delimiter even though we're not in a field - an empty field
if ($str[$i] == $delim) {
$fields[] = $word;
$word = '';
$infield = false;
$inenc = false;
continue;
}
// a newline - an empty field as well, but we're done with this line
if ($str[$i] == "\n") {
$infield = false;
$inenc = false;
//we saw no fields or content yet? empty line! skip it.
if (!count($fields) && $word === '') continue;
// otherwise add field
$fields[] = $word;
$word = '';
break;
}
// we skip leading whitespace when we're not in a field yet
if ($str[$i] === ' ') {
continue;
}
// cell starts with an enclosure
if ($str[$i] == $enc) {
// skip this one but open an enclosed field
$infield = true;
$inenc = true;
continue;
}
// still here? whatever is here, is content and starts a field
$word .= $str[$i];
$infield = true;
$inenc = false;
} elseif ($inenc) { // in field and enclosure
// we have an escape char that is an enclosure and the next char is an enclosure, too
if ($str[$i] == $esc && $esc == $enc && isset($str[$i + 1]) && $str[$i + 1] == $esc) {
$i++; // skip this char and take next as is
$word .= $str[$i];
continue;
}
// we have an enclosure char
if ($str[$i] == $enc) {
// skip this one but close the enclosure
$infield = true;
$inenc = false;
continue;
}
// still here? just add more content
$word .= $str[$i];
} else { // in field but no enclosure
// a delimiter - next field please
if ($str[$i] == $delim) {
$fields[] = $word;
$word = '';
$infield = false;
$inenc = false;
continue;
}
// EOL - we're done with the line
if ($str[$i] == "\n") {
$infield = false;
$inenc = false;
//we saw no fields or content yet? empty line! skip it.
if (!count($fields) && $word === '') continue;
$fields[] = $word;
$word = '';
break;
}
// still here? just add more content
$word .= $str[$i];
}
}
// did we hit the end?
if ($infield && ($word || count($fields))) {
$fields[] = $word;
}
// shorten the string by the stuff we read
$str = substr($str, $i + 1);
if (!count($fields)) return false;
return $fields;
}
}