2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / export / csv.php
blobaee7a430315e5bcf45f601ab52950f8af46abd6f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @version $Id$
5 */
7 /**
8 * Set of functions used to build CSV dumps of tables
9 */
11 if (isset($plugin_list)) {
12 $plugin_list['csv'] = array(
13 'text' => 'strStrucCSV',
14 'extension' => 'csv',
15 'mime_type' => 'text/comma-separated-values',
16 'options' => array(
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',
27 } else {
29 /**
30 * Outputs comment
32 * @param string Text of comment
34 * @return bool Whether it suceeded
36 function PMA_exportComment($text) {
37 return TRUE;
40 /**
41 * Outputs export footer
43 * @return bool Whether it suceeded
45 * @access public
47 function PMA_exportFooter() {
48 return TRUE;
51 /**
52 * Outputs export header
54 * @return bool Whether it suceeded
56 * @access public
58 function PMA_exportHeader() {
59 global $what;
60 global $csv_terminated;
61 global $csv_separator;
62 global $csv_enclosed;
63 global $csv_escaped;
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' ? ';' : ',';
69 $csv_enclosed = '"';
70 $csv_escaped = '"';
71 if (isset($GLOBALS['excel_columns'])) {
72 $GLOBALS['csv_columns'] = 'yes';
74 } else {
75 if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
76 $csv_terminated = $GLOBALS['crlf'];
77 } else {
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);
81 } // end if
82 $csv_separator = str_replace('\\t', "\011", $csv_separator);
84 return TRUE;
87 /**
88 * Outputs database header
90 * @param string Database name
92 * @return bool Whether it suceeded
94 * @access public
96 function PMA_exportDBHeader($db) {
97 return TRUE;
101 * Outputs database footer
103 * @param string Database name
105 * @return bool Whether it suceeded
107 * @access public
109 function PMA_exportDBFooter($db) {
110 return TRUE;
114 * Outputs create database database
116 * @param string Database name
118 * @return bool Whether it suceeded
120 * @access public
122 function PMA_exportDBCreate($db) {
123 return TRUE;
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
137 * @access public
139 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
140 global $what;
141 global $csv_terminated;
142 global $csv_separator;
143 global $csv_enclosed;
144 global $csv_escaped;
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'])) {
152 $schema_insert = '';
153 for ($i = 0; $i < $fields_cnt; $i++) {
154 if ($csv_enclosed == '') {
155 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
156 } else {
157 $schema_insert .= $csv_enclosed
158 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
159 . $csv_enclosed;
161 $schema_insert .= $csv_separator;
162 } // end for
163 $schema_insert =trim(substr($schema_insert, 0, -1));
164 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
165 return FALSE;
167 } // end if
169 // Format the data
170 while ($row = PMA_DBI_fetch_row($result)) {
171 $schema_insert = '';
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];
182 } else {
183 $schema_insert .= $csv_enclosed
184 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
185 . $csv_enclosed;
187 } else {
188 $schema_insert .= '';
190 if ($j < $fields_cnt-1) {
191 $schema_insert .= $csv_separator;
193 } // end for
195 if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
196 return FALSE;
198 } // end while
199 PMA_DBI_free_result($result);
201 return TRUE;
202 } // end of the 'PMA_getTableCsv()' function