r90051 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90050‎ | r90051 | r90052 >
Date:14:56, 14 June 2011
Author:yaron
Status:deferred
Tags:
Comment:
Fix to r86720 - previous regexp accidentally split on spaces, in addition to commas. Also added code to skip any empty lines at the top of 'CSV with header' files
Modified paths:
  • /trunk/extensions/ExternalData/ED_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/ED_Utils.php
@@ -70,11 +70,11 @@
7171 $arg = preg_replace ( "/\s\s+/", ' ', $arg ); // whitespace
7272
7373 // Split text on commas, except for commas found within quotes
74 - // and parentheses. Code copied from:
 74+ // and parentheses. Code based on:
7575 // http://stackoverflow.com/questions/1373735/regexp-split-string-by-commas-and-spaces-but-ignore-the-inside-quotes-and-parent#1381895
7676 $pattern = <<<END
7777 /
78 - [,\s]++
 78+ [,]++
7979 (?=(?:(?:[^"]*+"){2})*+[^"]*+$)
8080 (?=(?:(?:[^']*+'){2})*+[^']*+$)
8181 (?=(?:[^()]*+\([^()]*+\))*+[^()]*+$)
@@ -319,11 +319,17 @@
320320 }
321321 fclose( $fp );
322322 //}
323 - // now "flip" the data, turning it into a column-by-column
324 - // array, instead of row-by-row
 323+ // Get header values, if this is 'csv with header'
325324 if ( $has_header ) {
326325 $header_vals = array_shift( $table );
 326+ // On the off chance that there are one or more blank
 327+ // lines at the beginning, cycle through.
 328+ while ( count( $header_vals ) == 0 ) {
 329+ $header_vals = array_shift( $table );
 330+ }
327331 }
 332+ // Now "flip" the data, turning it into a column-by-column
 333+ // array, instead of row-by-row.
328334 $values = array();
329335 foreach ( $table as $line ) {
330336 foreach ( $line as $i => $row_val ) {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86720Fixed parsing of data= parameter to only split on commas that aren't containe...yaron18:29, 22 April 2011

Status & tagging log