2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This is a one-line short description of the file
20 * You can have a rather longer description of the file as well,
21 * if you like, and it can span multiple lines.
25 * @copyright Petr Skoda
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
32 * Utitily class for importing of CSV files.
33 * @copyright Petr Skoda
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class csv_import_reader
{
39 * @var int import identifier
43 * @var string which script imports?
47 * @var string|null Null if ok, error msg otherwise
51 * @var array cached columns
55 * @var object file handle used during import
62 * @param int $iid import identifier
63 * @param string $type which script imports?
65 function csv_import_reader($iid, $type) {
75 * @param string $content passed by ref for memory reasons, unset after return
76 * @param string $encoding content encoding
77 * @param string $delimiter_name separator (comma, semicolon, colon, cfg)
78 * @param string $column_validation name of function for columns validation, must have one param $columns
79 * @return bool false if error, count of data lines if ok; use get_error() to get error string
81 function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation=null) {
87 $textlib = textlib_get_instance();
89 $content = $textlib->convert($content, $encoding, 'utf-8');
90 // remove Unicode BOM from first line
91 $content = $textlib->trim_utf8_bom($content);
92 // Fix mac/dos newlines
93 $content = preg_replace('!\r\n?!', "\n", $content);
94 // is there anyting in file?
95 $columns = strtok($content, "\n");
96 if ($columns === false) {
97 $this->_error
= get_string('csvemptyfile', 'error');
100 $csv_delimiter = csv_import_reader
::get_delimiter($delimiter_name);
101 $csv_encode = csv_import_reader
::get_encoded_delimiter($delimiter_name);
103 // process header - list of columns
104 $columns = explode($csv_delimiter, $columns);
105 $col_count = count($columns);
106 if ($col_count === 0) {
107 $this->_error
= get_string('csvemptyfile', 'error');
111 foreach ($columns as $key=>$value) {
112 $columns[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
114 if ($column_validation) {
115 $result = $column_validation($columns);
116 if ($result !== true) {
117 $this->_error
= $result;
121 $this->_columns
= $columns; // cached columns
123 // open file for writing
124 $filename = $CFG->dataroot
.'/temp/csvimport/'.$this->_type
.'/'.$USER->id
.'/'.$this->_iid
;
125 $fp = fopen($filename, "w");
126 fwrite($fp, serialize($columns)."\n");
128 // again - do we have any data for processing?
129 $line = strtok("\n");
131 while ($line !== false) {
132 $line = explode($csv_delimiter, $line);
133 foreach ($line as $key=>$value) {
134 $line[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
136 if (count($line) !== $col_count) {
137 // this is critical!!
138 $this->_error
= get_string('csvweirdcolumns', 'error');
143 fwrite($fp, serialize($line)."\n");
145 $line = strtok("\n");
153 * Returns list of columns
157 function get_columns() {
158 if (isset($this->_columns
)) {
159 return $this->_columns
;
164 $filename = $CFG->dataroot
.'/temp/csvimport/'.$this->_type
.'/'.$USER->id
.'/'.$this->_iid
;
165 if (!file_exists($filename)) {
168 $fp = fopen($filename, "r");
171 if ($line === false) {
174 $this->_columns
= unserialize($line);
175 return $this->_columns
;
183 * @return bool Success
188 if (!empty($this->_fp
)) {
191 $filename = $CFG->dataroot
.'/temp/csvimport/'.$this->_type
.'/'.$USER->id
.'/'.$this->_iid
;
192 if (!file_exists($filename)) {
195 if (!$this->_fp
= fopen($filename, "r")) {
199 return (fgets($this->_fp
) !== false);
205 * @return mixed false, or an array of values
208 if (empty($this->_fp
) or feof($this->_fp
)) {
211 if ($ser = fgets($this->_fp
)) {
212 return unserialize($ser);
219 * Release iteration related resources
224 if (!empty($this->_fp
)) {
233 * @return string error text of null if none
235 function get_error() {
236 return $this->_error
;
240 * Cleanup temporary data
244 * @param boolean $full true means do a full cleanup - all sessions for current user, false only the active iid
246 function cleanup($full=false) {
250 @remove_dir
($CFG->dataroot
.'/temp/csvimport/'.$this->_type
.'/'.$USER->id
);
252 @unlink
($CFG->dataroot
.'/temp/csvimport/'.$this->_type
.'/'.$USER->id
.'/'.$this->_iid
);
257 * Get list of cvs delimiters
259 * @return array suitable for selection box
261 static function get_delimiter_list() {
263 $delimiters = array('comma'=>',', 'semicolon'=>';', 'colon'=>':', 'tab'=>'\\t');
264 if (isset($CFG->CSV_DELIMITER
) and strlen($CFG->CSV_DELIMITER
) === 1 and !in_array($CFG->CSV_DELIMITER
, $delimiters)) {
265 $delimiters['cfg'] = $CFG->CSV_DELIMITER
;
271 * Get delimiter character
273 * @param string separator name
274 * @return string delimiter char
276 static function get_delimiter($delimiter_name) {
278 switch ($delimiter_name) {
279 case 'colon': return ':';
280 case 'semicolon': return ';';
281 case 'tab': return "\t";
282 case 'cfg': if (isset($CFG->CSV_DELIMITER
)) { return $CFG->CSV_DELIMITER
; } // no break; fall back to comma
283 case 'comma': return ',';
288 * Get encoded delimiter character
291 * @param string separator name
292 * @return string encoded delimiter char
294 function get_encoded_delimiter($delimiter_name) {
296 if ($delimiter_name == 'cfg' and isset($CFG->CSV_ENCODE
)) {
297 return $CFG->CSV_ENCODE
;
299 $delimiter = csv_import_reader
::get_delimiter($delimiter_name);
300 return '&#'.ord($delimiter);
304 * Create new import id
307 * @param string who imports?
310 function get_new_iid($type) {
313 $filename = make_upload_directory('temp/csvimport/'.$type.'/'.$USER->id
);
315 // use current (non-conflicting) time stamp
317 while (file_exists($filename.'/'.$iiid)) {