2
/* $Id: csv.php 9549 2006-10-13 08:14:31Z nijel $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
6
* Set of functions used to build CSV dumps of tables
9
if (isset($plugin_list)) {
10
$plugin_list['csv'] = array(
11
'text' => 'strStrucCSV',
13
'mime_type' => 'text/comma-separated-values',
15
array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
16
array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
17
array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
18
array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
19
array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
20
array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
21
array('type' => 'hidden', 'name' => 'data'),
23
'options_text' => 'strCSVOptions',
30
* @param string Text of comment
32
* @return bool Whether it suceeded
34
function PMA_exportComment($text) {
39
* Outputs export footer
41
* @return bool Whether it suceeded
45
function PMA_exportFooter() {
50
* Outputs export header
52
* @return bool Whether it suceeded
56
function PMA_exportHeader() {
58
global $csv_terminated;
59
global $csv_separator;
63
// Here we just prepare some values for export
64
if ($what == 'excel') {
65
$csv_terminated = "\015\012";
66
$csv_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ? ';' : ',';
69
if (isset($GLOBALS['excel_columns'])) {
70
$GLOBALS['csv_columns'] = 'yes';
73
if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
74
$csv_terminated = $GLOBALS['crlf'];
76
$csv_terminated = str_replace('\\r', "\015", $csv_terminated);
77
$csv_terminated = str_replace('\\n', "\012", $csv_terminated);
78
$csv_terminated = str_replace('\\t', "\011", $csv_terminated);
80
$csv_separator = str_replace('\\t', "\011", $csv_separator);
86
* Outputs database header
88
* @param string Database name
90
* @return bool Whether it suceeded
94
function PMA_exportDBHeader($db) {
99
* Outputs database footer
101
* @param string Database name
103
* @return bool Whether it suceeded
107
function PMA_exportDBFooter($db) {
112
* Outputs create database database
114
* @param string Database name
116
* @return bool Whether it suceeded
120
function PMA_exportDBCreate($db) {
125
* Outputs the content of a table in CSV format
127
* @param string the database name
128
* @param string the table name
129
* @param string the end of line sequence
130
* @param string the url to go back in case of error
131
* @param string SQL query for obtaining data
133
* @return bool Whether it suceeded
137
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
139
global $csv_terminated;
140
global $csv_separator;
141
global $csv_enclosed;
144
// Gets the data from the database
145
$result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
146
$fields_cnt = PMA_DBI_num_fields($result);
148
// If required, get fields name at the first line
149
if (isset($GLOBALS['csv_columns'])) {
151
for ($i = 0; $i < $fields_cnt; $i++) {
152
if ($csv_enclosed == '') {
153
$schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
155
$schema_insert .= $csv_enclosed
156
. str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
159
$schema_insert .= $csv_separator;
161
$schema_insert =trim(substr($schema_insert, 0, -1));
162
if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
168
while ($row = PMA_DBI_fetch_row($result)) {
170
for ($j = 0; $j < $fields_cnt; $j++) {
171
if (!isset($row[$j]) || is_null($row[$j])) {
172
$schema_insert .= $GLOBALS[$what . '_null'];
173
} elseif ($row[$j] == '0' || $row[$j] != '') {
174
// loic1 : always enclose fields
175
if ($what == 'excel') {
176
$row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
178
if ($csv_enclosed == '') {
179
$schema_insert .= $row[$j];
181
$schema_insert .= $csv_enclosed
182
. str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
186
$schema_insert .= '';
188
if ($j < $fields_cnt-1) {
189
$schema_insert .= $csv_separator;
193
if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
197
PMA_DBI_free_result($result);
200
} // end of the 'PMA_getTableCsv()' function