Permalink
Browse files

Revert "MDL-34075 - lib - Alteration to the csv import lib to include…

… rfc-4180 compliance"

This reverts commit 92dfce6.
  • Loading branch information...
danpoltawski committed Aug 8, 2012
1 parent 25cba7d commit 40a37bf9743baf089ef7c7f9d00d733f32b14244
Showing with 44 additions and 152 deletions.
  1. +44 −45 lib/csvlib.class.php
  2. +0 −107 lib/tests/csvtest.php
View
@@ -55,14 +55,7 @@ class csv_import_reader {
* @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
*
@@ -85,7 +78,7 @@ function csv_import_reader($iid, $type) {
* @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();
@@ -96,56 +89,62 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
$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;
}
/**
@@ -165,12 +164,12 @@ function get_columns() {
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;
}
@@ -195,7 +194,7 @@ function init() {
return false;
}
//skip header
return (fgetcsv($this->_fp, 0, $this->_delimiter, $this->_enclosure) !== false);
return (fgets($this->_fp) !== false);
}
/**
@@ -207,8 +206,8 @@ function next() {
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;
}
View

This file was deleted.

Oops, something went wrong.

0 comments on commit 40a37bf

Please sign in to comment.