forked from TomCafferty/plugin-table2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTableData.php
105 lines (94 loc) · 3.45 KB
/
getTableData.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
<?php
function scrapeTable2Csv($dokuPageId, $fileext, $startMarker) {
$csv_data = '';
$file = wikiFN($dokuPageId);
$data = io_readWikiPage($file, $dokuPageId, $rev=false);
$raw = p_render('xhtml',p_get_instructions($data),$info);
if ($raw == false) {
msg(sprintf('Failed to read page ' . $dokuPageId));
return false;
}
$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));
$start = strpos($content,$startMarker);
$content = substr($content,$start);
$start = strpos($content,'<table ');
$end = strpos($content,'</table>',$start) + 8;
$table = substr($content,$start,$end-$start);
preg_match_all("|<tr(.*)</tr>|U",$table,$rows);
$fp = @fopen($fileext, 'w');
if ($fp === false) {
msg(sprintf('Failed to open write file ' . $fileext));
return false;
}
$row_index=0;
$numHeadings = 0;
foreach ($rows[0] as $row){
if ((strpos($row,'<th')===false))
preg_match_all("|<td(.*)</td>|U",$row,$cells);
else
$numHeadings = preg_match_all("|<t(.*)</t(.*)>|U",$row,$cells);
if ($row_index == 0)
$numCols = $numHeadings;
$cell_index=0;
foreach ($cells[0] as $cell) {
$mycells[$row_index][$cell_index] = trim(strip_tags($cell));
++$cell_index;
}
if ($mycells[$row_index] != '') {
fputcsv($fp, $mycells[$row_index]);
$csv_data .= strput2csv($mycells[$row_index], $numCols-1);
}
++$row_index;
}
fclose($fp);
return $csv_data;
}
function strput2csv($fields = array(), $numheadings, $delimiter = ',', $enclosure = '"') {
$i = 0;
$csvline = '';
$escape_char = '\\';
$field_cnt = count($fields)-1;
$enc_is_quote = in_array($enclosure, array('"',"'"));
reset($fields);
foreach( $fields AS $field ) {
/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if( is_string($field) && (
strpos($field, $delimiter)!==false ||
strpos($field, $enclosure)!==false ||
strpos($field, $escape_char)!==false ||
strpos($field, "\n")!==false ||
strpos($field, "\r")!==false ||
strpos($field, "\t")!==false ||
strpos($field, ' ')!==false ) ) {
$field_len = strlen($field);
$escaped = 0;
$csvline .= $enclosure;
for( $ch = 0; $ch < $field_len; $ch++ ) {
if( $field[$ch] == $escape_char && $field[$ch+1] == $enclosure && $enc_is_quote ) {
continue;
}elseif( $field[$ch] == $escape_char ) {
$escaped = 1;
}elseif( !$escaped && $field[$ch] == $enclosure ) {
$csvline .= $enclosure;
}else{
$escaped = 0;
}
$csvline .= $field[$ch];
}
$csvline .= $enclosure;
} else {
$csvline .= $field;
}
if( $i++ != $field_cnt ) {
$csvline .= $delimiter;
}
}
if ($field_cnt < $numheadings) {
for ($i=$field_cnt+1; $i<=$numheadings; $i++) {
$csvline .= $delimiter;
}
}
$csvline .= "\n";
return $csvline;
}