Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: csv.php,v 2.9 2006/01/17 17:03:02 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Set of functions used to build CSV dumps of tables
7 */
8  
9 /**
10 * Outputs comment
11 *
12 * @param string Text of comment
13 *
14 * @return bool Whether it suceeded
15 */
16 function PMA_exportComment($text) {
17 return TRUE;
18 }
19  
20 /**
21 * Outputs export footer
22 *
23 * @return bool Whether it suceeded
24 *
25 * @access public
26 */
27 function PMA_exportFooter() {
28 return TRUE;
29 }
30  
31 /**
32 * Outputs export header
33 *
34 * @return bool Whether it suceeded
35 *
36 * @access public
37 */
38 function PMA_exportHeader() {
39 global $what;
40 global $add_character;
41 global $export_separator;
42 global $enclosed;
43 global $escaped;
44  
45 // Here we just prepare some values for export
46 if ($what == 'excel') {
47 $add_character = "\015\012";
48 $export_separator = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ? ';' : ',';
49 $enclosed = '"';
50 $escaped = '"';
51 if (isset($GLOBALS['showexcelnames']) && $GLOBALS['showexcelnames'] == 'yes') {
52 $GLOBALS['showcsvnames'] = 'yes';
53 }
54 } else {
55 if (empty($add_character)) {
56 $add_character = $GLOBALS['crlf'];
57 } else {
58 $add_character = str_replace('\\r', "\015", $add_character);
59 $add_character = str_replace('\\n', "\012", $add_character);
60 $add_character = str_replace('\\t', "\011", $add_character);
61 } // end if
62 $export_separator = str_replace('\\t', "\011", $export_separator);
63 }
64 return TRUE;
65 }
66  
67 /**
68 * Outputs database header
69 *
70 * @param string Database name
71 *
72 * @return bool Whether it suceeded
73 *
74 * @access public
75 */
76 function PMA_exportDBHeader($db) {
77 return TRUE;
78 }
79  
80 /**
81 * Outputs database footer
82 *
83 * @param string Database name
84 *
85 * @return bool Whether it suceeded
86 *
87 * @access public
88 */
89 function PMA_exportDBFooter($db) {
90 return TRUE;
91 }
92  
93 /**
94 * Outputs create database database
95 *
96 * @param string Database name
97 *
98 * @return bool Whether it suceeded
99 *
100 * @access public
101 */
102 function PMA_exportDBCreate($db) {
103 return TRUE;
104 }
105  
106 /**
107 * Outputs the content of a table in CSV format
108 *
109 * @param string the database name
110 * @param string the table name
111 * @param string the end of line sequence
112 * @param string the url to go back in case of error
113 * @param string SQL query for obtaining data
114 *
115 * @return bool Whether it suceeded
116 *
117 * @access public
118 */
119 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
120 global $what;
121 global $add_character;
122 global $export_separator;
123 global $enclosed;
124 global $escaped;
125  
126 // Gets the data from the database
127 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
128 $fields_cnt = PMA_DBI_num_fields($result);
129  
130 // If required, get fields name at the first line
131 if (isset($GLOBALS['showcsvnames']) && $GLOBALS['showcsvnames'] == 'yes') {
132 $schema_insert = '';
133 for ($i = 0; $i < $fields_cnt; $i++) {
134 if ($enclosed == '') {
135 $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
136 } else {
137 $schema_insert .= $enclosed
138 . str_replace($enclosed, $escaped . $enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
139 . $enclosed;
140 }
141 $schema_insert .= $export_separator;
142 } // end for
143 $schema_insert =trim(substr($schema_insert, 0, -1));
144 if (!PMA_exportOutputHandler($schema_insert . $add_character)) {
145 return FALSE;
146 }
147 } // end if
148  
149 // Format the data
150 while ($row = PMA_DBI_fetch_row($result)) {
151 $schema_insert = '';
152 for ($j = 0; $j < $fields_cnt; $j++) {
153 if (!isset($row[$j]) || is_null($row[$j])) {
154 $schema_insert .= $GLOBALS[$what . '_replace_null'];
155 } elseif ($row[$j] == '0' || $row[$j] != '') {
156 // loic1 : always enclose fields
157 if ($what == 'excel') {
158 $row[$j] = ereg_replace("\015(\012)?", "\012", $row[$j]);
159 }
160 if ($enclosed == '') {
161 $schema_insert .= $row[$j];
162 } else {
163 $schema_insert .= $enclosed
164 . str_replace($enclosed, $escaped . $enclosed, $row[$j])
165 . $enclosed;
166 }
167 } else {
168 $schema_insert .= '';
169 }
170 if ($j < $fields_cnt-1) {
171 $schema_insert .= $export_separator;
172 }
173 } // end for
174  
175 if (!PMA_exportOutputHandler($schema_insert . $add_character)) {
176 return FALSE;
177 }
178 } // end while
179 PMA_DBI_free_result($result);
180  
181 return TRUE;
182 } // end of the 'PMA_getTableCsv()' function
183 ?>