* @var object file handle used during import
*/
var $_fp;
- /**
- * @var string delimiter of the records.
- */
- var $_delimiter;
- /**
- * @var string enclosure of each field.
- */
- var $_enclosure;
+
/**
* Contructor
*
* @param string $column_validation name of function for columns validation, must have one param $columns
* @return bool 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, $enclosure='"') {
+ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validation=null) {
global $USER, $CFG;
$this->close();
$content = textlib::trim_utf8_bom($content);
// Fix mac/dos newlines
$content = preg_replace('!\r\n?!', "\n", $content);
-
- $csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
-// $csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);
- $this->_delimiter = $csv_delimiter;
- $this->_enclosure = $enclosure;
-
- // create a temporary file and store the csv file there in csv file format.
- $filename = $CFG->tempdir.'/csvimport/'.$this->_type.'/'.$USER->id.'/'.$this->_iid;
- $fp = fopen($filename, 'w+');
- fwrite($fp, $content);
- fseek($fp, 0);
- // Create an array to store the imported data for error checking.
- $columns = array();
- while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure)) {
- $columns[] = $fgetdata;
+ // 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);
- $col_count = 0;
// process header - list of columns
- if (!isset($columns[0])) {
+ $columns = explode($csv_delimiter, $columns);
+ $col_count = count($columns);
+ if ($col_count === 0) {
$this->_error = get_string('csvemptyfile', 'error');
- fclose($fp);
- $this->cleanup();
return false;
- } else {
- $col_count = count($columns[0]);
}
- // column validation
+
+ foreach ($columns as $key=>$value) {
+ $columns[$key] = str_replace($csv_encode, $csv_delimiter, trim($value));
+ }
if ($column_validation) {
- $result = $column_validation($columns[0]);
+ $result = $column_validation($columns);
if ($result !== true) {
$this->_error = $result;
- fclose($fp);
- $this->cleanup();
return false;
}
}
+ $this->_columns = $columns; // cached columns
- $this->_columns = $columns[0]; // cached columns
- // check to make sure that the data columns match up with the headers.
- foreach ($columns as $rowdata) {
- if (count($rowdata) !== $col_count) {
+ // open file for writing
+ $filename = $CFG->tempdir.'/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");
}
- $datacount = count($columns);
- return $datacount;
+
+ fclose($fp);
+ return $data_count;
}
/**
return false;
}
$fp = fopen($filename, "r");
- $line = fgetcsv($fp, 0, $this->_delimiter, $this->_enclosure);
+ $line = fgets($fp);
fclose($fp);
if ($line === false) {
return false;
}
- $this->_columns = $line;
+ $this->_columns = unserialize($line);
return $this->_columns;
}
return false;
}
//skip header
- return (fgetcsv($this->_fp, 0, $this->_delimiter, $this->_enclosure) !== false);
+ return (fgets($this->_fp) !== false);
}
/**
if (empty($this->_fp) or feof($this->_fp)) {
return false;
}
- if ($ser = fgetcsv($this->_fp, 0, $this->_delimiter, $this->_enclosure)) {
- return $ser;
+ if ($ser = fgets($this->_fp)) {
+ return unserialize($ser);
} else {
return false;
}
+++ /dev/null
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * Tests csv import and export functions
- *
- * @package core
- * @category phpunit
- * @copyright 2012 Adrian Greeve
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-defined('MOODLE_INTERNAL') || die();
-
-global $CFG;
-require_once($CFG->dirroot . '/lib/csvlib.class.php');
-
-class csv_testcase extends advanced_testcase {
-
- var $testdata = array();
- var $teststring = '';
- var $teststring2 = '';
- var $teststring3 = '';
-
- protected function setUp(){
-
- $this->resetAfterTest(true);
-
- $csvdata = array();
- $csvdata[0][] = 'fullname';
- $csvdata[0][] = 'description of things';
- $csvdata[0][] = 'beer';
- $csvdata[1][] = 'William H T Macey';
- $csvdata[1][] = '<p>A field that contains "double quotes"</p>';
- $csvdata[1][] = 'Asahi';
- $csvdata[2][] = 'Phillip Jenkins';
- $csvdata[2][] = '<p>This field has </p>
-<p>Multiple lines</p>
-<p>and also contains "double quotes"</p>';
- $csvdata[2][] = 'Yebisu';
- $this->testdata = $csvdata;
-
- // Please note that each line needs a carriage return.
- $this->teststring = 'fullname,"description of things",beer
-"William H T Macey","<p>A field that contains ""double quotes""</p>",Asahi
-"Phillip Jenkins","<p>This field has </p>
-<p>Multiple lines</p>
-<p>and also contains ""double quotes""</p>",Yebisu
-';
-
- $this->teststring2 = 'fullname,"description of things",beer
-"Fred Flint","<p>Find the stone inside the box</p>",Asahi,"A fourth column"
-"Sarah Smith","<p>How are the people next door?</p>,Yebisu,"Forget the next"
-';
- }
-
- public function test_csv_functions() {
- // Testing that the content is imported correctly.
- $iid = csv_import_reader::get_new_iid('lib');
- $csvimport = new csv_import_reader($iid, 'lib');
- $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
- $csvimport->init();
- $dataset = array();
- $dataset[] = $csvimport->get_columns();
- while ($record = $csvimport->next()) {
- $dataset[] = $record;
- }
- $csvimport->cleanup();
- $csvimport->close();
- $this->assertEquals($dataset, $this->testdata);
-
- // Testing for the wrong count of columns.
- $errortext = get_string('csvweirdcolumns', 'error');
-
- $iid = csv_import_reader::get_new_iid('lib');
- $csvimport = new csv_import_reader($iid, 'lib');
- $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
- $importerror = $csvimport->get_error();
- $csvimport->cleanup();
- $csvimport->close();
- $this->assertEquals($importerror, $errortext);
-
- // Testing for empty content
- $errortext = get_string('csvemptyfile', 'error');
-
- $iid = csv_import_reader::get_new_iid('lib');
- $csvimport = new csv_import_reader($iid, 'lib');
- $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
- $importerror = $csvimport->get_error();
- $csvimport->cleanup();
- $csvimport->close();
- $this->assertEquals($importerror, $errortext);
- }
-}
\ No newline at end of file