-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCsv.php
211 lines (189 loc) · 6.36 KB
/
Csv.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
<?php
// +----------------------------------------------------------------------+
// | Copyright 2013 Madpixels (email : [email protected]) |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License, version 2, as |
// | published by the Free Software Foundation. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
// | MA 02110-1301 USA |
// +----------------------------------------------------------------------+
// | Author: Eugene Manuilov <[email protected]> |
// +----------------------------------------------------------------------+
/**
* Source manager for local CSV files.
*
* @category Visualizer
* @package Source
*
* @since 1.0.0
*/
class Visualizer_Source_Csv extends Visualizer_Source {
/**
* The path to the file with data.
*
* @since 1.0.0
*
* @access protected
* @var string
*/
protected $_filename;
/**
* Constructor.
*
* @since 1.0.0
*
* @access public
* @param string $filename The path to the file.
*/
public function __construct( $filename = '' ) {
$this->_filename = trim( (string) $filename );
}
/**
* Fetches series information.
*
* @since 1.0.0
*
* @access private
* @param resource $handle The file handle resource.
*/
private function _fetchSeries( &$handle ) {
// read column titles
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
$types = null;
if ( false !== strpos( $this->_filename, 'tqx=out:csv' ) ) {
$attributes = $this->_fetchSeriesForGoogleQueryLanguage( $labels );
if ( ! $attributes['abort'] ) {
$labels = $attributes['labels'];
$types = $attributes['types'];
}
}
if ( is_null( $types ) ) {
// read series types
$types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
}
$labels = array_filter( $labels );
$types = array_filter( $types );
if ( ! $labels || ! $types ) {
$this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
return false;
}
$types = array_map( 'trim', $types );
if ( ! self::_validateTypes( $types ) ) {
$this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
return false;
}
for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
$default_type = $i === 0 ? 'string' : 'number';
$labels[ $i ] = $this->toUTF8( $labels[ $i ] );
$this->_series[] = array(
'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ),
'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
);
}
return true;
}
/**
* Returns file handle to fetch data from.
*
* @since 1.4.2
*
* @access protected
* @param string $filename Optional file name to get handle. If omitted, $_filename is used.
* @return resource File handle resource on success, otherwise FALSE.
*/
protected function _get_file_handle( $filename = false ) {
// open file and return handle
return fopen( $filename ? $filename : $this->_filename, 'rb' );
}
/**
* Fetches information from source, parses it and builds series and data arrays.
*
* @since 1.0.0
*
* @access public
* @return boolean TRUE on success, otherwise FALSE.
*/
public function fetch() {
// if filename is empty return false
if ( empty( $this->_filename ) ) {
$this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
return false;
}
// read file and fill arrays
$handle = $this->_get_file_handle();
if ( $handle ) {
// fetch series
if ( ! $this->_fetchSeries( $handle ) ) {
return false;
}
// fetch data
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
$this->_data[] = $this->_normalizeData( $data );
}
// close file handle
fclose( $handle );
}
return true;
}
/**
* Returns source name.
*
* @since 1.0.0
*
* @access public
* @return string The name of source.
*/
public function getSourceName() {
return __CLASS__;
}
/**
* Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
* where the user can provide something like /gviz/tq?tq=select%20A%2C%20B%20&tqx=out:csv after the URL of the raw spreadsheet
* to get the subset of data specified by the query
* this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
* so we need to split them apart logically
* also the $types variable now contains the first row because the header and the type got conflated
*/
private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
$new_labels = array();
$new_types = array();
$abort = false;
foreach ( $labels as $label ) {
// get the index of the last space
$index = strrpos( $label, ' ' );
if ( $index === false ) {
// no space here? something has gone wrong; abort the entire process.
$abort = true;
break;
}
$type = trim( substr( $label, $index + 1 ) );
if ( ! self::_validateTypes( array( $type ) ) ) {
// some other data type? abort the entire process.
$abort = true;
break;
}
$label = substr( $label, 0, $index );
$new_labels[] = $label;
$new_types[] = $type;
}
if ( ! $abort ) {
$labels = $new_labels;
$types = $new_types;
}
return array(
'abort' => $abort,
'labels' => $labels,
'types' => $types,
);
}
}