2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 * Set of functions used to build CSV dumps of tables
11 if (isset($plugin_list)) {
12 $plugin_list['csv'] = array(
13 'text' => 'strStrucCSV',
15 'mime_type' => 'text/comma-separated-values',
17 array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
18 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
19 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
20 array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
21 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
22 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
23 array('type' => 'hidden', 'name' => 'data'),
25 'options_text' => 'strOptions',
32 * @param string Text of comment
34 * @return bool Whether it suceeded
36 function PMA_exportComment($text) {
41 * Outputs export footer
43 * @return bool Whether it suceeded
47 function PMA_exportFooter() {
52 * Outputs export header
54 * @return bool Whether it suceeded
58 function PMA_exportHeader() {
60 global $csv_terminated;
61 global $csv_separator;
65 // Here we just prepare some values for export
66 if ($what == 'excel') {
67 $csv_terminated = "\015\012";
68 $csv_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ?
';' : ',';
71 if (isset($GLOBALS['excel_columns'])) {
72 $GLOBALS['csv_columns'] = 'yes';
75 if (empty($csv_terminated) ||
strtolower($csv_terminated) == 'auto') {
76 $csv_terminated = $GLOBALS['crlf'];
78 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
79 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
80 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
82 $csv_separator = str_replace('\\t', "\011", $csv_separator);
88 * Outputs database header
90 * @param string Database name
92 * @return bool Whether it suceeded
96 function PMA_exportDBHeader($db) {
101 * Outputs database footer
103 * @param string Database name
105 * @return bool Whether it suceeded
109 function PMA_exportDBFooter($db) {
114 * Outputs create database database
116 * @param string Database name
118 * @return bool Whether it suceeded
122 function PMA_exportDBCreate($db) {
127 * Outputs the content of a table in CSV format
129 * @param string the database name
130 * @param string the table name
131 * @param string the end of line sequence
132 * @param string the url to go back in case of error
133 * @param string SQL query for obtaining data
135 * @return bool Whether it suceeded
139 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
141 global $csv_terminated;
142 global $csv_separator;
143 global $csv_enclosed;
146 // Gets the data from the database
147 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
148 $fields_cnt = PMA_DBI_num_fields($result);
150 // If required, get fields name at the first line
151 if (isset($GLOBALS['csv_columns'])) {
153 for ($i = 0; $i < $fields_cnt; $i++
) {
154 if ($csv_enclosed == '') {
155 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
157 $schema_insert .= $csv_enclosed
158 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
161 $schema_insert .= $csv_separator;
163 $schema_insert =trim(substr($schema_insert, 0, -1));
164 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
170 while ($row = PMA_DBI_fetch_row($result)) {
172 for ($j = 0; $j < $fields_cnt; $j++
) {
173 if (!isset($row[$j]) ||
is_null($row[$j])) {
174 $schema_insert .= $GLOBALS[$what . '_null'];
175 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
176 // loic1 : always enclose fields
177 if ($what == 'excel') {
178 $row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
180 if ($csv_enclosed == '') {
181 $schema_insert .= $row[$j];
183 $schema_insert .= $csv_enclosed
184 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
188 $schema_insert .= '';
190 if ($j < $fields_cnt-1) {
191 $schema_insert .= $csv_separator;
195 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
199 PMA_DBI_free_result($result);
202 } // end of the 'PMA_getTableCsv()' function