forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvlib.class.php
268 lines (240 loc) · 7.69 KB
/
csvlib.class.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
<?php
/*
* General csv import library.
* @author Petr Skoda
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
/**
* Utitily class for importing of CSV files.
*/
class csv_import_reader {
var $_iid;
var $_type;
var $_error;
var $_columns; //cached columns
var $_fp;
/**
* Contructor
* @param int $iid import identifier
* @param string $type which script imports?
*/
function csv_import_reader($iid, $type) {
$this->_iid = $iid;
$this->_type = $type;
}
/**
* Parse this content
* @param string &$content passed by ref for memory reasons, unset after return
* @param string $encoding content encoding
* @param string $delimiter_name separator (comma, semicolon, colon, cfg)
* @param string $column_validation name of function for columns validation, must have one param $columns
* @return false if error, count of data lines if ok; use get_error() to get error string
*/
function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation=null) {
global $USER, $CFG;
$this->close();
$this->_error = null;
$textlib = textlib_get_instance();
$content = $textlib->convert($content, $encoding, 'utf-8');
// remove Unicode BOM from first line
$content = $textlib->trim_utf8_bom($content);
// Fix mac/dos newlines
$content = preg_replace('!\r\n?!', "\n", $content);
// is there anyting in file?
$columns = strtok($content, "\n");
if ($columns === false) {
$this->_error = get_string('csvemptyfile', 'error');
return false;
}
$csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
$csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);
// process header - list of columns
$columns = explode($csv_delimiter, $columns);
$col_count = count($columns);
if ($col_count === 0) {
$this->_error = get_string('csvemptyfile', 'error');
return false;
}
foreach ($columns as $key=>$value) {
$columns[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
}
if ($column_validation) {
$result = $column_validation($columns);
if ($result !== true) {
$this->_error = $result;
return false;
}
}
$this->_columns = $columns; // cached columns
// open file for writing
$filename = $CFG->dataroot.'/temp/csvimport/'.$this->_type.'/'.$USER->id.'/'.$this->_iid;
$fp = fopen($filename, "w");
fwrite($fp, serialize($columns)."\n");
// again - do we have any data for processing?
$line = strtok("\n");
$data_count = 0;
while ($line !== false) {
$line = explode($csv_delimiter, $line);
foreach ($line as $key=>$value) {
$line[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
}
if (count($line) !== $col_count) {
// this is critical!!
$this->_error = get_string('csvweirdcolumns', 'error');
fclose($fp);
$this->cleanup();
return false;
}
fwrite($fp, serialize($line)."\n");
$data_count++;
$line = strtok("\n");
}
fclose($fp);
return $data_count;
}
/**
* Returns list of columns
* @return array
*/
function get_columns() {
if (isset($this->_columns)) {
return $this->_columns;
}
global $USER, $CFG;
$filename = $CFG->dataroot.'/temp/csvimport/'.$this->_type.'/'.$USER->id.'/'.$this->_iid;
if (!file_exists($filename)) {
return false;
}
$fp = fopen($filename, "r");
$line = fgets($fp);
fclose($fp);
if ($line === false) {
return false;
}
$this->_columns = unserialize($line);
return $this->_columns;
}
/**
* Init iterator.
*/
function init() {
global $CFG, $USER;
if (!empty($this->_fp)) {
$this->close();
}
$filename = $CFG->dataroot.'/temp/csvimport/'.$this->_type.'/'.$USER->id.'/'.$this->_iid;
if (!file_exists($filename)) {
return false;
}
if (!$this->_fp = fopen($filename, "r")) {
return false;
}
//skip header
return (fgets($this->_fp) !== false);
}
/**
* Get next line
* @return array of values
*/
function next() {
if (empty($this->_fp) or feof($this->_fp)) {
return false;
}
if ($ser = fgets($this->_fp)) {
return unserialize($ser);
} else {
return false;
}
}
/**
* Release iteration related resources
*/
function close() {
if (!empty($this->_fp)) {
fclose($this->_fp);
$this->_fp = null;
}
}
/**
* Get last error
* @return string error text of null if none
*/
function get_error() {
return $this->_error;
}
/**
* Cleanup temporary data
* @static if $full=true
* @param boolean true menasdo a full cleanup - all sessions for current user, false only the active iid
*/
function cleanup($full=false) {
global $USER, $CFG;
if ($full) {
@remove_dir($CFG->dataroot.'/temp/csvimport/'.$this->_type.'/'.$USER->id);
} else {
@unlink($CFG->dataroot.'/temp/csvimport/'.$this->_type.'/'.$USER->id.'/'.$this->_iid);
}
}
/**
* Get list of cvs delimiters
* @static
* @return array suitable for selection box
*/
function get_delimiter_list() {
$delimiters = array('comma'=>',', 'semicolon'=>';', 'colon'=>':', 'tab'=>'\\t');
if (isset($CFG->CSV_DELIMITER) and strlen($CFG->CSV_DELIMITER) === 1 and !in_array($CFG->CSV_DELIMITER, $delimiters)) {
$delimiters['cfg'] = $CFG->CSV_DELIMITER;
}
return $delimiters;
}
/**
* Get delimiter character
* @static
* @param string separator name
* @return char delimiter char
*/
function get_delimiter($delimiter_name) {
switch ($delimiter_name) {
case 'colon': return ':';
case 'semicolon': return ';';
case 'tab': return "\t";
case 'cfg': if (isset($CFG->CSV_DELIMITER)) { return $CFG->CSV_DELIMITER; } // no break; fall back to comma
case 'comma': return ',';
}
}
/**
* Get encoded delimiter character
* @static
* @param string separator name
* @return char encoded delimiter char
*/
function get_encoded_delimiter($delimiter_name) {
global $CFG;
if ($delimiter_name == 'cfg' and isset($CFG->CSV_ENCODE)) {
return $CFG->CSV_ENCODE;
}
$delimiter = csv_import_reader::get_delimiter($delimiter_name);
return '&#'.ord($delimiter);
}
/**
* Create new import id
* @static
* @param string who imports?
* @return int iid
*/
function get_new_iid($type) {
global $USER;
if (!$filename = make_upload_directory('temp/csvimport/'.$type.'/'.$USER->id, false)) {
print_error('Can not create temporary upload directory - verify moodledata permissions!');
}
// use current (non-conflicting) time stamp
$iiid = time();
while (file_exists($filename.'/'.$iiid)) {
$iiid--;
}
return $iiid;
}
}
?>