MDL-83335 core: Explicitly specify escape with fgetcsv/fputcsv

PHP 8.4 requires that a value be provided for the `$escape` parameter to
both `fgetcsv()` and `fputcsv()`.
This commit is contained in:
Andrew Nicols 2024-10-02 09:10:50 +08:00
parent ab5692acdf
commit b29615a6f0
No known key found for this signature in database
GPG key ID: 6D1E3157C8CFBF14
9 changed files with 26 additions and 26 deletions

View file

@ -85,7 +85,7 @@ class brickfield_accessibility_guideline {
$csv = fopen(dirname(__FILE__) .'/guidelines/translations/'. $domain .'.txt', 'r'); $csv = fopen(dirname(__FILE__) .'/guidelines/translations/'. $domain .'.txt', 'r');
if ($csv) { if ($csv) {
while ($translation = fgetcsv($csv)) { while ($translation = fgetcsv($csv, escape: '\\')) {
if (count($translation) == 4) { if (count($translation) == 4) {
$this->translations[$translation[0]] = [ $this->translations[$translation[0]] = [
'title' => $translation[1], 'title' => $translation[1],

View file

@ -85,9 +85,9 @@ if ($options['replace']) {
$results = $urlfinder->http_link_stats(); $results = $urlfinder->http_link_stats();
asort($results); asort($results);
$fp = fopen('php://stdout', 'w'); $fp = fopen('php://stdout', 'w');
fputcsv($fp, ['clientsite', 'httpdomain', 'urlcount']); fputcsv($fp, ['clientsite', 'httpdomain', 'urlcount'], escape: '\\');
foreach ($results as $domain => $count) { foreach ($results as $domain => $count) {
fputcsv($fp, [$SITE->shortname, $domain, $count]); fputcsv($fp, [$SITE->shortname, $domain, $count], escape: '\\');
} }
fclose($fp); fclose($fp);
} }

View file

@ -148,7 +148,7 @@ class dataset_manager {
return false; return false;
} }
foreach ($data as $line) { foreach ($data as $line) {
fputcsv($fh, $line); fputcsv($fh, $line, escape: '\\');
} }
fclose($fh); fclose($fh);
@ -290,12 +290,12 @@ class dataset_manager {
$rh = $file->get_content_file_handle(); $rh = $file->get_content_file_handle();
// Copy the var names as they are, all files should have the same var names. // Copy the var names as they are, all files should have the same var names.
$varnames = fgetcsv($rh); $varnames = fgetcsv($rh, escape: '\\');
$analysablesvalues[] = fgetcsv($rh); $analysablesvalues[] = fgetcsv($rh, escape: '\\');
// Copy the columns as they are, all files should have the same columns. // Copy the columns as they are, all files should have the same columns.
$columns = fgetcsv($rh); $columns = fgetcsv($rh, escape: '\\');
} }
// Merge analysable values skipping the ones that are the same in all analysables. // Merge analysable values skipping the ones that are the same in all analysables.
@ -316,9 +316,9 @@ class dataset_manager {
throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath); throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath);
} }
fputcsv($wh, $varnames); fputcsv($wh, $varnames, escape: '\\');
fputcsv($wh, $values); fputcsv($wh, $values, escape: '\\');
fputcsv($wh, $columns); fputcsv($wh, $columns, escape: '\\');
// Iterate through all files and add them to the tmp one. We don't want file contents in memory. // Iterate through all files and add them to the tmp one. We don't want file contents in memory.
foreach ($files as $file) { foreach ($files as $file) {
@ -402,11 +402,11 @@ class dataset_manager {
$calculations = array(); $calculations = array();
$headers = fgetcsv($rh); $headers = fgetcsv($rh, escape: '\\');
// Get rid of the sampleid column name. // Get rid of the sampleid column name.
array_shift($headers); array_shift($headers);
while ($columns = fgetcsv($rh)) { while ($columns = fgetcsv($rh, escape: '\\')) {
$uniquesampleid = array_shift($columns); $uniquesampleid = array_shift($columns);
// Unfortunately fgetcsv does not respect line's var types. // Unfortunately fgetcsv does not respect line's var types.

View file

@ -209,7 +209,7 @@ class behat_core_competency_generator extends behat_generator_base {
*/ */
protected function preprocess_plan(array $data): array { protected function preprocess_plan(array $data): array {
if (isset($data['competencies'])) { if (isset($data['competencies'])) {
$competencies = array_map('trim', str_getcsv($data['competencies'])); $competencies = array_map('trim', str_getcsv($data['competencies'], escape: '\\'));
$data['competencyids'] = array_map([$this, 'get_competency_id'], $competencies); $data['competencyids'] = array_map([$this, 'get_competency_id'], $competencies);
unset($data['competencies']); unset($data['competencies']);

View file

@ -112,7 +112,7 @@ if ($handle = fopen($imported_file, 'r')) {
// data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0 // data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
// or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line. // or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line.
while ( $csv_data = fgetcsv($handle, 8192, ';', '"')) { // if the line is over 8k, it won't work... while ( $csv_data = fgetcsv($handle, 8192, ';', '"', '\\')) { // if the line is over 8k, it won't work...
$line++; $line++;
// be tolerant on input, as fgetcsv returns "an array comprising a single null field" on blank lines // be tolerant on input, as fgetcsv returns "an array comprising a single null field" on blank lines

View file

@ -126,7 +126,7 @@ class csv_import_reader {
$columns = array(); $columns = array();
// str_getcsv doesn't iterate through the csv data properly. It has // str_getcsv doesn't iterate through the csv data properly. It has
// problems with line returns. // problems with line returns.
while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure)) { while ($fgetdata = fgetcsv($fp, 0, $csv_delimiter, $enclosure, '\\')) {
// Check to see if we have an empty line. // Check to see if we have an empty line.
if (count($fgetdata) == 1) { if (count($fgetdata) == 1) {
if ($fgetdata[0] !== null) { if ($fgetdata[0] !== null) {
@ -204,7 +204,7 @@ class csv_import_reader {
return false; return false;
} }
$fp = fopen($filename, "r"); $fp = fopen($filename, "r");
$line = fgetcsv($fp); $line = fgetcsv($fp, escape: '\\');
fclose($fp); fclose($fp);
if ($line === false) { if ($line === false) {
return false; return false;
@ -234,7 +234,7 @@ class csv_import_reader {
return false; return false;
} }
//skip header //skip header
return (fgetcsv($this->_fp) !== false); return (fgetcsv($this->_fp, escape: '\\') !== false);
} }
/** /**
@ -246,7 +246,7 @@ class csv_import_reader {
if (empty($this->_fp) or feof($this->_fp)) { if (empty($this->_fp) or feof($this->_fp)) {
return false; return false;
} }
if ($ser = fgetcsv($this->_fp)) { if ($ser = fgetcsv($this->_fp, escape: '\\')) {
return $ser; return $ser;
} else { } else {
return false; return false;
@ -449,7 +449,7 @@ class csv_export_writer {
} }
} }
$delimiter = csv_import_reader::get_delimiter($this->delimiter); $delimiter = csv_import_reader::get_delimiter($this->delimiter);
fputcsv($this->fp, $row, $delimiter, $this->csvenclosure); fputcsv($this->fp, $row, $delimiter, $this->csvenclosure, '\\');
} }
/** /**

View file

@ -126,7 +126,7 @@ class processor implements \core_analytics\classifier, \core_analytics\regressor
$samples = array(); $samples = array();
$targets = array(); $targets = array();
while (($data = fgetcsv($fh)) !== false) { while (($data = fgetcsv($fh, escape: '\\')) !== false) {
$sampledata = array_map('floatval', $data); $sampledata = array_map('floatval', $data);
$samples[] = array_slice($sampledata, 0, $metadata['nfeatures']); $samples[] = array_slice($sampledata, 0, $metadata['nfeatures']);
$targets[] = intval($data[$metadata['nfeatures']]); $targets[] = intval($data[$metadata['nfeatures']]);
@ -189,7 +189,7 @@ class processor implements \core_analytics\classifier, \core_analytics\regressor
$sampleids = array(); $sampleids = array();
$samples = array(); $samples = array();
$predictions = array(); $predictions = array();
while (($data = fgetcsv($fh)) !== false) { while (($data = fgetcsv($fh, escape: '\\')) !== false) {
$sampledata = array_map('floatval', $data); $sampledata = array_map('floatval', $data);
$sampleids[] = $data[0]; $sampleids[] = $data[0];
$samples[] = array_slice($sampledata, 1, $metadata['nfeatures']); $samples[] = array_slice($sampledata, 1, $metadata['nfeatures']);
@ -270,7 +270,7 @@ class processor implements \core_analytics\classifier, \core_analytics\regressor
$samples = array(); $samples = array();
$targets = array(); $targets = array();
while (($data = fgetcsv($fh)) !== false) { while (($data = fgetcsv($fh, escape: '\\')) !== false) {
$sampledata = array_map('floatval', $data); $sampledata = array_map('floatval', $data);
$samples[] = array_slice($sampledata, 0, $metadata['nfeatures']); $samples[] = array_slice($sampledata, 0, $metadata['nfeatures']);
@ -535,8 +535,8 @@ class processor implements \core_analytics\classifier, \core_analytics\regressor
* @return array * @return array
*/ */
protected function extract_metadata($fh) { protected function extract_metadata($fh) {
$metadata = fgetcsv($fh); $metadata = fgetcsv($fh, escape: '\\');
return array_combine($metadata, fgetcsv($fh)); return array_combine($metadata, fgetcsv($fh, escape: '\\'));
} }
/** /**

View file

@ -282,7 +282,7 @@ class phpunit_dataset {
rewind($fh); rewind($fh);
// We just accept default, delimiter = comma, enclosure = double quote. // We just accept default, delimiter = comma, enclosure = double quote.
while ( ($row = fgetcsv($fh) ) !== false ) { while ( ($row = fgetcsv($fh, escape: '\\') ) !== false ) {
if (empty($this->columns[$tablename])) { if (empty($this->columns[$tablename])) {
$this->columns[$tablename] = $row; $this->columns[$tablename] = $row;
} else { } else {

View file

@ -115,7 +115,7 @@ class recording_row_playback implements renderable, templatable {
$issafeformat = false; $issafeformat = false;
// Now check the list of safe formats. // Now check the list of safe formats.
if ($safeformats = config::get('recording_safe_formats')) { if ($safeformats = config::get('recording_safe_formats')) {
$safeformatarray = str_getcsv($safeformats); $safeformatarray = str_getcsv($safeformats, escape: '\\');
$issafeformat = in_array($playback['type'], $safeformatarray); $issafeformat = in_array($playback['type'], $safeformatarray);
} }
return ($canmanagerecordings && $canviewallformats) || $issafeformat; return ($canmanagerecordings && $canviewallformats) || $issafeformat;