2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * @package phpMyAdmin-Export-CSV
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(
18 'mime_type' => 'text/comma-separated-values',
20 array('type' => 'begin_group', 'name' => 'general_opts'),
21 array('type' => 'text', 'name' => 'separator', 'text' => __('Columns separated with:')),
22 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:')),
23 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:')),
24 array('type' => 'text', 'name' => 'terminated', 'text' => __('Lines terminated with:')),
25 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
26 array('type' => 'bool', 'name' => 'removeCRLF', 'text' => __('Remove carriage return/line feed characters within columns')),
27 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
28 array('type' => 'hidden', 'name' => 'structure_or_data'),
29 array('type' => 'end_group'),
31 'options_text' => __('Options'),
38 * @param string Text of comment
40 * @return bool Whether it suceeded
42 function PMA_exportComment($text) {
47 * Outputs export footer
49 * @return bool Whether it suceeded
53 function PMA_exportFooter() {
58 * Outputs export header
60 * @return bool Whether it suceeded
64 function PMA_exportHeader() {
66 global $csv_terminated;
67 global $csv_separator;
71 // Here we just prepare some values for export
72 if ($what == 'excel') {
73 $csv_terminated = "\015\012";
74 switch($GLOBALS['excel_edition']) {
76 // as tested on Windows with Excel 2002 and Excel 2007
88 if (isset($GLOBALS['excel_columns'])) {
89 $GLOBALS['csv_columns'] = 'yes';
92 if (empty($csv_terminated) ||
strtolower($csv_terminated) == 'auto') {
93 $csv_terminated = $GLOBALS['crlf'];
95 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
96 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
97 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
99 $csv_separator = str_replace('\\t', "\011", $csv_separator);
105 * Outputs database header
107 * @param string Database name
109 * @return bool Whether it suceeded
113 function PMA_exportDBHeader($db) {
118 * Outputs database footer
120 * @param string Database name
122 * @return bool Whether it suceeded
126 function PMA_exportDBFooter($db) {
131 * Outputs create database database
133 * @param string Database name
135 * @return bool Whether it suceeded
139 function PMA_exportDBCreate($db) {
144 * Outputs the content of a table in CSV format
146 * @param string the database name
147 * @param string the table name
148 * @param string the end of line sequence
149 * @param string the url to go back in case of error
150 * @param string SQL query for obtaining data
152 * @return bool Whether it suceeded
156 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
158 global $csv_terminated;
159 global $csv_separator;
160 global $csv_enclosed;
163 // Gets the data from the database
164 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
165 $fields_cnt = PMA_DBI_num_fields($result);
167 // If required, get fields name at the first line
168 if (isset($GLOBALS['csv_columns'])) {
170 for ($i = 0; $i < $fields_cnt; $i++
) {
171 if ($csv_enclosed == '') {
172 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
174 $schema_insert .= $csv_enclosed
175 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
178 $schema_insert .= $csv_separator;
180 $schema_insert =trim(substr($schema_insert, 0, -1));
181 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
187 while ($row = PMA_DBI_fetch_row($result)) {
189 for ($j = 0; $j < $fields_cnt; $j++
) {
190 if (!isset($row[$j]) ||
is_null($row[$j])) {
191 $schema_insert .= $GLOBALS[$what . '_null'];
192 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
193 // always enclose fields
194 if ($what == 'excel') {
195 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
197 // remove CRLF characters within field
198 if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
199 $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
201 if ($csv_enclosed == '') {
202 $schema_insert .= $row[$j];
204 // also double the escape string if found in the data
205 if ('csv' == $what) {
206 $schema_insert .= $csv_enclosed
207 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
210 // for excel, avoid a problem when a field contains
212 $schema_insert .= $csv_enclosed
213 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
218 $schema_insert .= '';
220 if ($j < $fields_cnt-1) {
221 $schema_insert .= $csv_separator;
225 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
229 PMA_DBI_free_result($result);
232 } // end of the 'PMA_getTableCsv()' function