Skip to content

Commit

Permalink
[PHP 8.4] fgetcsv() - The $escape parameter must be provided as its d…
Browse files Browse the repository at this point in the history
…efault value will change (#4297)

* fgetcsv - lib/Varien/Convert/Parser/Csv.php

Deprecated. The $escape parameter must be provided as its default value will change.

* fgetcsv - app/code/core/Mage/ImportExport/Model/Import/Adapter/Csv.php

Deprecated. The $escape parameter must be provided as its default value will change.

* fgetcsv - lib/Varien/Io/File.php

Deprecated. The $escape parameter must be provided as its default value will change.

* fgetcsv - lib/Varien/File/Csv.php

Deprecated. The $escape parameter must be provided as its default value will change.

* fgetcsv - app/code/core/Mage/Dataflow/Model/Session/Parser/Csv.php

Deprecated. The $escape parameter must be provided as its default value will change.

* Missing variabile fEsc in function parseTest()

* Missing $ in front of the variable

* Revert the typo

* escape parameter in function

* PHP CS Fixer for lib/Varien/Io/File.php
  • Loading branch information
ADDISON74 authored Oct 22, 2024
1 parent d34be82 commit b3d67b5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/code/core/Mage/Dataflow/Model/Session/Parser/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function parse()
{
$fDel = $this->getVar('delimiter', ',');
$fEnc = $this->getVar('enclose', '"');
$fEsc = $this->getVar('escape', '\\');

if ($fDel == '\\t') {
$fDel = "\t";
Expand All @@ -41,7 +42,7 @@ public function parse()
$sessionId = Mage::registry('current_dataflow_session_id');
$import = Mage::getModel('dataflow/import');
$map = new Varien_Convert_Mapper_Column();
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc, $fEsc); $i++) {
if ($i == 0) {
if ($this->getVar('fieldnames')) {
$fields = $line;
Expand Down
15 changes: 11 additions & 4 deletions app/code/core/Mage/ImportExport/Model/Import/Adapter/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Mage_ImportExport_Model_Import_Adapter_Csv extends Mage_ImportExport_Model
*/
protected $_enclosure = '"';

/**
* Field escape character.
*
* @var string
*/
protected $_escape = '\\';

/**
* Source file handler.
*
Expand Down Expand Up @@ -72,7 +79,7 @@ protected function _init()
#[\ReturnTypeWillChange]
public function next()
{
$this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
$this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure, $this->_escape);
$this->_currentKey = $this->_currentRow ? $this->_currentKey + 1 : null;
}

Expand All @@ -86,8 +93,8 @@ public function rewind()
{
// rewind resource, reset column names, read first row as current
rewind($this->_fileHandler);
$this->_colNames = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
$this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
$this->_colNames = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure, $this->_escape);
$this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure, $this->_escape);

if ($this->_currentRow) {
$this->_currentKey = 0;
Expand All @@ -112,7 +119,7 @@ public function seek($position)
if ($position < $this->_currentKey) {
$this->rewind();
}
while ($this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure)) {
while ($this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure, $this->_escape)) {
if (++$this->_currentKey == $position) {
return;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Varien/Convert/Parser/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function parse()
{
$fDel = $this->getVar('delimiter', ',');
$fEnc = $this->getVar('enclose', '"');
$fEsc = $this->getVar('escape', '\\');

if ($fDel == '\\t') {
$fDel = "\t";
Expand All @@ -38,7 +39,7 @@ public function parse()
fseek($fp, 0);

$data = [];
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc, $fEsc); $i++) {
if (0 == $i) {
if ($this->getVar('fieldnames')) {
$fields = $line;
Expand All @@ -65,6 +66,7 @@ public function parseTest()
{
$fDel = $this->getVar('delimiter', ',');
$fEnc = $this->getVar('enclose', '"');
$fEsc = $this->getVar('escape', '\\');

if ($fDel == '\\t') {
$fDel = "\t";
Expand All @@ -81,7 +83,7 @@ public function parseTest()
$sessionId = Mage::registry('current_dataflow_session_id');
$import = Mage::getModel('dataflow/import');
$map = new Varien_Convert_Mapper_Column();
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
for ($i = 0; $line = fgetcsv($fp, 4096, $fDel, $fEnc, $fEsc); $i++) {
if (0 == $i) {
if ($this->getVar('fieldnames')) {
$fields = $line;
Expand Down
3 changes: 2 additions & 1 deletion lib/Varien/File/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Varien_File_Csv
protected $_lineLength = 0;
protected $_delimiter = ',';
protected $_enclosure = '"';
protected $_escape = '\\';

public function __construct()
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getData($file)
}

$fh = fopen($file, 'r');
while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure, $this->_escape)) {
$data[] = $rowData;
}
fclose($fh);
Expand Down
2 changes: 1 addition & 1 deletion lib/Varien/Io/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
return false;
}

return @fputcsv($this->_streamHandler, $row, $delimiter, $enclosure);
return @fputcsv($this->_streamHandler, $row, $delimiter, $enclosure, '\\');
}

/**
Expand Down

0 comments on commit b3d67b5

Please sign in to comment.