Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-36365 - lib: The csv class now reads tab separated files with tab…

…s at the end of the file.
  • Loading branch information...
commit 1222dc52e1c3aea4274dd9888cead58a3a638edf 1 parent 6109f21
Adrian Greeve abgreeve authored
7 lib/csvlib.class.php
View
@@ -91,7 +91,12 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
// Fix mac/dos newlines
$content = preg_replace('!\r\n?!', "\n", $content);
// Remove any spaces or new lines at the end of the file.
- $content = trim($content);
+ if ($delimiter_name == 'tab') {
+ // trim() by default removes tabs from the end of content which is undesirable in a tab separated file.
+ $content = trim($content, chr(0x20) . chr(0x0A) . chr(0x0D) . chr(0x00) . chr(0x0B));
+ } else {
+ $content = trim($content);
+ }
$csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
// $csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);
13 lib/tests/csvclass_test.php
View
@@ -68,6 +68,7 @@ protected function setUp(){
}
public function test_csv_functions() {
+ global $CFG;
$csvexport = new csv_export_writer();
$csvexport->set_filename('unittest');
foreach ($this->testdata as $data) {
@@ -113,5 +114,17 @@ public function test_csv_functions() {
$csvimport->cleanup();
$csvimport->close();
$this->assertEquals($importerror, $errortext);
+
+ // Testing for a tab separated file.
+ // The tab separated file has a trailing tab and extra blank lines at the end of the file.
+ $filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
+ $fp = fopen($filename, 'r');
+ $tabdata = fread($fp, filesize($filename));
+ fclose($fp);
+ $iid = csv_import_reader::get_new_iid('tab');
+ $csvimport = new csv_import_reader($iid, 'tab');
+ $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
+ // This should import four rows including the headings.
+ $this->assertEquals($contentcount, 4);
}
}
6 lib/tests/fixtures/tabfile.csv
View
@@ -0,0 +1,6 @@
+name url "Description of map" Grade
+"George Fitz" "http://www.myblog.com" "This is a small map, that could be used in anything."
+"Jim Brown" "http://www.blank.ne.jp" "Difficult to follow for the first five km.
+Then things get easier." 1
+"Tanya Keisel" "www.google.com" "No joke, it's just all burn't up. You can't ""understand"" any of it."
+
Please sign in to comment.
Something went wrong with that request. Please try again.