Ejegg has uploaded a new change for review.
https://gerrit.wikimedia.org/r/242587
Change subject: Store CSV column indexes by name
......................................................................
Store CSV column indexes by name
Avoid doing an array_search for every data item.
Change-Id: I6ae964861d6f97afea21af10f47233deab14ca31
---
M Core/DataFiles/HeadedCsvReader.php
1 file changed, 8 insertions(+), 4 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig
refs/changes/87/242587/1
diff --git a/Core/DataFiles/HeadedCsvReader.php
b/Core/DataFiles/HeadedCsvReader.php
index 1e84730..22ff008 100644
--- a/Core/DataFiles/HeadedCsvReader.php
+++ b/Core/DataFiles/HeadedCsvReader.php
@@ -8,12 +8,16 @@
class HeadedCsvReader extends CsvReader {
/** @var string[] */
protected $colNames;
+ protected $colIndexes;
public function __construct( $file, $delimiter = ',', $maxRowLength =
4098 ) {
parent::__construct( $file, $delimiter, $maxRowLength );
// Extract the header information
$this->colNames = parent::current();
+ foreach( $this->colNames as $index => $name ) {
+ $this->colIndexes[$name] = $index;
+ }
parent::next();
}
@@ -30,11 +34,11 @@
* @return string Contents of the column
*/
public function extractCol( $colName, &$row ) {
- $col = array_search( $colName, $this->colNames );
- if ( $col === false ) {
- throw new DataFileException( "Column name
{$this->colNames} not found!" );
+ if ( !isset( $this->colIndexes[$colName] ) ) {
+ throw new DataFileException( "Column name {$colName}
not found!" );
}
- return $row[$col];
+ $index = $this->colIndexes[$colName];
+ return $row[$index];
}
/**
--
To view, visit https://gerrit.wikimedia.org/r/242587
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6ae964861d6f97afea21af10f47233deab14ca31
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits