3.2.5
[phpmyadmin/dkf.git] / libraries / export / csv.php
blob9a8e52007477cd32940b25af86051cb61eb87817
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package phpMyAdmin-Export-CSV
5 * @version $Id$
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
12 * Set of functions used to build CSV dumps of tables
15 if (isset($plugin_list)) {
16 $plugin_list['csv'] = array(
17 'text' => 'strStrucCSV',
18 'extension' => 'csv',
19 'mime_type' => 'text/comma-separated-values',
20 'options' => array(
21 array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
22 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
23 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
24 array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
25 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
26 array('type' => 'bool', 'name' => 'removeCRLF', 'text' => 'strRemoveCRLF'),
27 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
28 array('type' => 'hidden', 'name' => 'data'),
30 'options_text' => 'strOptions',
32 } else {
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text) {
42 return TRUE;
45 /**
46 * Outputs export footer
48 * @return bool Whether it suceeded
50 * @access public
52 function PMA_exportFooter() {
53 return TRUE;
56 /**
57 * Outputs export header
59 * @return bool Whether it suceeded
61 * @access public
63 function PMA_exportHeader() {
64 global $what;
65 global $csv_terminated;
66 global $csv_separator;
67 global $csv_enclosed;
68 global $csv_escaped;
70 // Here we just prepare some values for export
71 if ($what == 'excel') {
72 $csv_terminated = "\015\012";
73 $csv_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac_excel2003' ? ';' : ',';
74 $csv_enclosed = '"';
75 $csv_escaped = '"';
76 if (isset($GLOBALS['excel_columns'])) {
77 $GLOBALS['csv_columns'] = 'yes';
79 } else {
80 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
81 $csv_terminated = $GLOBALS['crlf'];
82 } else {
83 $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
84 $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
85 $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
86 } // end if
87 $csv_separator = str_replace('\\t', "\011", $csv_separator);
89 return TRUE;
92 /**
93 * Outputs database header
95 * @param string Database name
97 * @return bool Whether it suceeded
99 * @access public
101 function PMA_exportDBHeader($db) {
102 return TRUE;
106 * Outputs database footer
108 * @param string Database name
110 * @return bool Whether it suceeded
112 * @access public
114 function PMA_exportDBFooter($db) {
115 return TRUE;
119 * Outputs create database database
121 * @param string Database name
123 * @return bool Whether it suceeded
125 * @access public
127 function PMA_exportDBCreate($db) {
128 return TRUE;
132 * Outputs the content of a table in CSV format
134 * @param string the database name
135 * @param string the table name
136 * @param string the end of line sequence
137 * @param string the url to go back in case of error
138 * @param string SQL query for obtaining data
140 * @return bool Whether it suceeded
142 * @access public
144 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
145 global $what;
146 global $csv_terminated;
147 global $csv_separator;
148 global $csv_enclosed;
149 global $csv_escaped;
151 // Gets the data from the database
152 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
153 $fields_cnt = PMA_DBI_num_fields($result);
155 // If required, get fields name at the first line
156 if (isset($GLOBALS['csv_columns'])) {
157 $schema_insert = '';
158 for ($i = 0; $i < $fields_cnt; $i++) {
159 if ($csv_enclosed == '') {
160 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
161 } else {
162 $schema_insert .= $csv_enclosed
163 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
164 . $csv_enclosed;
166 $schema_insert .= $csv_separator;
167 } // end for
168 $schema_insert =trim(substr($schema_insert, 0, -1));
169 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
170 return FALSE;
172 } // end if
174 // Format the data
175 while ($row = PMA_DBI_fetch_row($result)) {
176 $schema_insert = '';
177 for ($j = 0; $j < $fields_cnt; $j++) {
178 if (!isset($row[$j]) || is_null($row[$j])) {
179 $schema_insert .= $GLOBALS[$what . '_null'];
180 } elseif ($row[$j] == '0' || $row[$j] != '') {
181 // loic1 : always enclose fields
182 if ($what == 'excel') {
183 $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
185 // remove CRLF characters within field
186 if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
187 $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
189 if ($csv_enclosed == '') {
190 $schema_insert .= $row[$j];
191 } else {
192 // also double the escape string if found in the data
193 if ('csv' == $what) {
194 $schema_insert .= $csv_enclosed
195 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
196 . $csv_enclosed;
197 } else {
198 // for excel, avoid a problem when a field contains
199 // double quotes
200 $schema_insert .= $csv_enclosed
201 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
202 . $csv_enclosed;
205 } else {
206 $schema_insert .= '';
208 if ($j < $fields_cnt-1) {
209 $schema_insert .= $csv_separator;
211 } // end for
213 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
214 return FALSE;
216 } // end while
217 PMA_DBI_free_result($result);
219 return TRUE;
220 } // end of the 'PMA_getTableCsv()' function