MDL-55126 files: Deprecate get_records_csv and put_records_csv.

Initial deprecation of the above functions.
This commit is contained in:
Adrian Greeve 2016-07-13 13:57:59 +08:00
parent 5a1728df39
commit f6f0ba2d45
3 changed files with 134 additions and 121 deletions

View File

@ -4490,3 +4490,134 @@ function external_function_info($function, $strictness=MUST_EXIST) {
return external_api::external_function_info($function, $strictness);
}
/**
* Retrieves an array of records from a CSV file and places
* them into a given table structure
* This function is deprecated. Please use csv_import_reader() instead.
*
* @deprecated since Moodle 3.2 MDL-55126
* @todo MDL-55195 for final deprecation in Moodle 3.6
* @see csv_import_reader::load_csv_content()
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The path to a CSV file
* @param string $table The table to retrieve columns from
* @return bool|array Returns an array of CSV records or false
*/
function get_records_csv($file, $table) {
global $CFG, $DB;
debugging('get_records_csv() is deprecated. Please use lib/csvlib.class.php csv_import_reader() instead.');
if (!$metacolumns = $DB->get_columns($table)) {
return false;
}
if(!($handle = @fopen($file, 'r'))) {
print_error('get_records_csv failed to open '.$file);
}
$fieldnames = fgetcsv($handle, 4096);
if(empty($fieldnames)) {
fclose($handle);
return false;
}
$columns = array();
foreach($metacolumns as $metacolumn) {
$ord = array_search($metacolumn->name, $fieldnames);
if(is_int($ord)) {
$columns[$metacolumn->name] = $ord;
}
}
$rows = array();
while (($data = fgetcsv($handle, 4096)) !== false) {
$item = new stdClass;
foreach($columns as $name => $ord) {
$item->$name = $data[$ord];
}
$rows[] = $item;
}
fclose($handle);
return $rows;
}
/**
* Create a file with CSV contents
* This function is deprecated. Please use download_as_dataformat() instead.
*
* @deprecated since Moodle 3.2 MDL-55126
* @todo MDL-55195 for final deprecation in Moodle 3.6
* @see download_as_dataformat (lib/dataformatlib.php)
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The file to put the CSV content into
* @param array $records An array of records to write to a CSV file
* @param string $table The table to get columns from
* @return bool success
*/
function put_records_csv($file, $records, $table = NULL) {
global $CFG, $DB;
debugging('put_records_csv() is deprecated. Please use lib/dataformatlib.php download_as_dataformat()');
if (empty($records)) {
return true;
}
$metacolumns = NULL;
if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
return false;
}
echo "x";
if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
print_error('put_records_csv failed to open '.$file);
}
$proto = reset($records);
if(is_object($proto)) {
$fields_records = array_keys(get_object_vars($proto));
}
else if(is_array($proto)) {
$fields_records = array_keys($proto);
}
else {
return false;
}
echo "x";
if(!empty($metacolumns)) {
$fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
$fields = array_intersect($fields_records, $fields_table);
}
else {
$fields = $fields_records;
}
fwrite($fp, implode(',', $fields));
fwrite($fp, "\r\n");
foreach($records as $record) {
$array = (array)$record;
$values = array();
foreach($fields as $field) {
if(strpos($array[$field], ',')) {
$values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
}
else {
$values[] = $array[$field];
}
}
fwrite($fp, implode(',', $values)."\r\n");
}
fclose($fp);
@chmod($CFG->tempdir.'/'.$file, $CFG->filepermissions);
return true;
}

View File

@ -2367,127 +2367,6 @@ function send_stored_file($stored_file, $lifetime=null, $filter=0, $forcedownloa
die; //no more chars to output!!!
}
/**
* Retrieves an array of records from a CSV file and places
* them into a given table structure
*
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The path to a CSV file
* @param string $table The table to retrieve columns from
* @return bool|array Returns an array of CSV records or false
*/
function get_records_csv($file, $table) {
global $CFG, $DB;
if (!$metacolumns = $DB->get_columns($table)) {
return false;
}
if(!($handle = @fopen($file, 'r'))) {
print_error('get_records_csv failed to open '.$file);
}
$fieldnames = fgetcsv($handle, 4096);
if(empty($fieldnames)) {
fclose($handle);
return false;
}
$columns = array();
foreach($metacolumns as $metacolumn) {
$ord = array_search($metacolumn->name, $fieldnames);
if(is_int($ord)) {
$columns[$metacolumn->name] = $ord;
}
}
$rows = array();
while (($data = fgetcsv($handle, 4096)) !== false) {
$item = new stdClass;
foreach($columns as $name => $ord) {
$item->$name = $data[$ord];
}
$rows[] = $item;
}
fclose($handle);
return $rows;
}
/**
* Create a file with CSV contents
*
* @global stdClass $CFG
* @global moodle_database $DB
* @param string $file The file to put the CSV content into
* @param array $records An array of records to write to a CSV file
* @param string $table The table to get columns from
* @return bool success
*/
function put_records_csv($file, $records, $table = NULL) {
global $CFG, $DB;
if (empty($records)) {
return true;
}
$metacolumns = NULL;
if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) {
return false;
}
echo "x";
if(!($fp = @fopen($CFG->tempdir.'/'.$file, 'w'))) {
print_error('put_records_csv failed to open '.$file);
}
$proto = reset($records);
if(is_object($proto)) {
$fields_records = array_keys(get_object_vars($proto));
}
else if(is_array($proto)) {
$fields_records = array_keys($proto);
}
else {
return false;
}
echo "x";
if(!empty($metacolumns)) {
$fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
$fields = array_intersect($fields_records, $fields_table);
}
else {
$fields = $fields_records;
}
fwrite($fp, implode(',', $fields));
fwrite($fp, "\r\n");
foreach($records as $record) {
$array = (array)$record;
$values = array();
foreach($fields as $field) {
if(strpos($array[$field], ',')) {
$values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
}
else {
$values[] = $array[$field];
}
}
fwrite($fp, implode(',', $values)."\r\n");
}
fclose($fp);
@chmod($CFG->tempdir.'/'.$file, $CFG->filepermissions);
return true;
}
/**
* Recursively delete the file or folder with path $location. That is,
* if it is a file delete it. If it is a folder, delete all its content

View File

@ -13,6 +13,9 @@ information provided here is intended especially for developers.
- get_user_max_upload_file_size()
* The following functions have been removed and should not be used any more:
- file_modify_html_header() - See MDL-29738 for more information.
* The following functions have been deprecated and are not used any more:
- get_records_csv() Please use csv_import_reader::load_csv_content() instead.
- put_records_csv() Please use download_as_dataformat (lib/dataformatlib.php) instead.
=== 3.1 ===