2 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 if (! defined('PHPMYADMIN')) {
11 * Set of functions used to build CSV dumps of tables
14 if (isset($plugin_list)) {
15 $plugin_list['csv'] = array(
16 'text' => 'strStrucCSV',
18 'mime_type' => 'text/comma-separated-values',
20 array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
21 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
22 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
23 array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
24 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
25 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
26 array('type' => 'hidden', 'name' => 'data'),
28 'options_text' => 'strOptions',
35 * @param string Text of comment
37 * @return bool Whether it suceeded
39 function PMA_exportComment($text) {
44 * Outputs export footer
46 * @return bool Whether it suceeded
50 function PMA_exportFooter() {
55 * Outputs export header
57 * @return bool Whether it suceeded
61 function PMA_exportHeader() {
63 global $csv_terminated;
64 global $csv_separator;
68 // Here we just prepare some values for export
69 if ($what == 'excel') {
70 $csv_terminated = "\015\012";
71 $csv_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ?
';' : ',';
74 if (isset($GLOBALS['excel_columns'])) {
75 $GLOBALS['csv_columns'] = 'yes';
78 if (empty($csv_terminated) ||
strtolower($csv_terminated) == 'auto') {
79 $csv_terminated = $GLOBALS['crlf'];
81 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
82 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
83 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
85 $csv_separator = str_replace('\\t', "\011", $csv_separator);
91 * Outputs database header
93 * @param string Database name
95 * @return bool Whether it suceeded
99 function PMA_exportDBHeader($db) {
104 * Outputs database footer
106 * @param string Database name
108 * @return bool Whether it suceeded
112 function PMA_exportDBFooter($db) {
117 * Outputs create database database
119 * @param string Database name
121 * @return bool Whether it suceeded
125 function PMA_exportDBCreate($db) {
130 * Outputs the content of a table in CSV format
132 * @param string the database name
133 * @param string the table name
134 * @param string the end of line sequence
135 * @param string the url to go back in case of error
136 * @param string SQL query for obtaining data
138 * @return bool Whether it suceeded
142 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
144 global $csv_terminated;
145 global $csv_separator;
146 global $csv_enclosed;
149 // Gets the data from the database
150 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
151 $fields_cnt = PMA_DBI_num_fields($result);
153 // If required, get fields name at the first line
154 if (isset($GLOBALS['csv_columns'])) {
156 for ($i = 0; $i < $fields_cnt; $i++
) {
157 if ($csv_enclosed == '') {
158 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
160 $schema_insert .= $csv_enclosed
161 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
164 $schema_insert .= $csv_separator;
166 $schema_insert =trim(substr($schema_insert, 0, -1));
167 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
173 while ($row = PMA_DBI_fetch_row($result)) {
175 for ($j = 0; $j < $fields_cnt; $j++
) {
176 if (!isset($row[$j]) ||
is_null($row[$j])) {
177 $schema_insert .= $GLOBALS[$what . '_null'];
178 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
179 // loic1 : always enclose fields
180 if ($what == 'excel') {
181 $row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
183 if ($csv_enclosed == '') {
184 $schema_insert .= $row[$j];
186 // also double the escape string if found in the data
187 if ('csv' == $what) {
188 $schema_insert .= $csv_enclosed
189 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
192 // for excel, avoid a problem when a field contains
194 $schema_insert .= $csv_enclosed
195 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
200 $schema_insert .= '';
202 if ($j < $fields_cnt-1) {
203 $schema_insert .= $csv_separator;
207 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
211 PMA_DBI_free_result($result);
214 } // end of the 'PMA_getTableCsv()' function