r86720 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86719‎ | r86720 | r86721 >
Date:18:29, 22 April 2011
Author:yaron
Status:deferred
Tags:
Comment:
Fixed parsing of data= parameter to only split on commas that aren't contained within parentheses and quotes - useful for #get_db_data
Modified paths:
  • /trunk/extensions/ExternalData/ED_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/ED_Utils.php
@@ -50,7 +50,7 @@
5151 }
5252
5353 static function parseParams( $params ) {
54 - $args = Array();
 54+ $args = array();
5555 foreach ( $params as $param ) {
5656 $param = preg_replace ( "/\s\s+/", ' ', $param ); // whitespace
5757 $param_parts = explode( "=", $param, 2 );
@@ -68,8 +68,22 @@
6969 */
7070 static function paramToArray( $arg, $lowercaseKeys = false, $lowercaseValues = false ) {
7171 $arg = preg_replace ( "/\s\s+/", ' ', $arg ); // whitespace
72 - $keyValuePairs = explode( ',', $arg );
73 - $returnArray = Array();
 72+
 73+ // Split text on commas, except for commas found within quotes
 74+ // and parentheses. Code copied from:
 75+ // http://stackoverflow.com/questions/1373735/regexp-split-string-by-commas-and-spaces-but-ignore-the-inside-quotes-and-parent#1381895
 76+ $pattern = <<<END
 77+ /
 78+ [,\s]++
 79+ (?=(?:(?:[^"]*+"){2})*+[^"]*+$)
 80+ (?=(?:(?:[^']*+'){2})*+[^']*+$)
 81+ (?=(?:[^()]*+\([^()]*+\))*+[^()]*+$)
 82+ /x
 83+END;
 84+ // " - fix for color highlighting in vi :)
 85+ $keyValuePairs = preg_split( $pattern, $arg );
 86+
 87+ $returnArray = array();
7488 foreach ( $keyValuePairs as $keyValuePair ) {
7589 $keyAndValue = explode( '=', $keyValuePair, 2 );
7690 if ( count( $keyAndValue ) == 2 ) {
@@ -214,7 +228,7 @@
215229 $rows = self::searchDB( $db, $from, $columns, $where, $options );
216230 $db->close();
217231
218 - $values = Array();
 232+ $values = array();
219233 foreach ( $rows as $row ) {
220234 foreach ( $columns as $column ) {
221235 $values[$column][] = $row[$column];
@@ -234,7 +248,7 @@
235249 echo ( wfMsgExt( "externaldata-db-invalid-query", array( 'parse', 'escape' ) ) );
236250 return false;
237251 } else {
238 - $rows = Array();
 252+ $rows = array();
239253 while ( $row = $db->fetchRow( $result ) ) {
240254 // Create a new row object, that uses the
241255 // passed-in column names as keys, so that

Follow-up revisions

RevisionCommit summaryAuthorDate
r90051Fix to r86720 - previous regexp accidentally split on spaces, in addition to ...yaron14:56, 14 June 2011

Status & tagging log