2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build CSV dumps of tables
6 * @package phpMyAdmin-Export-HTMLWord
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $plugin_list['htmlword'] = array(
17 'text' => __('Microsoft Word 2000'),
19 'mime_type' => 'application/vnd.ms-word',
22 /* what to dump (structure/data/both) */
23 array('type' => 'begin_group', 'name' => 'dump_what', 'text' => __('Dump table')),
24 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data'))),
25 array('type' => 'end_group'),
27 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure'),
28 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
29 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
30 array('type' => 'end_group'),
32 'options_text' => __('Options'),
39 * @param string Text of comment
41 * @return bool Whether it suceeded
43 function PMA_exportComment($text) {
48 * Outputs export footer
50 * @return bool Whether it suceeded
54 function PMA_exportFooter() {
55 return PMA_exportOutputHandler('</body></html>');
59 * Outputs export header
61 * @return bool Whether it suceeded
65 function PMA_exportHeader() {
66 global $charset, $charset_of_file;
67 return PMA_exportOutputHandler('<html xmlns:o="urn:schemas-microsoft-com:office:office"
68 xmlns:x="urn:schemas-microsoft-com:office:word"
69 xmlns="http://www.w3.org/TR/REC-html40">
71 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
74 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ?
$charset_of_file : $charset) . '" />
80 * Outputs database header
82 * @param string Database name
84 * @return bool Whether it suceeded
88 function PMA_exportDBHeader($db) {
89 return PMA_exportOutputHandler('<h1>' . __('Database') . ' ' . $db . '</h1>');
93 * Outputs database footer
95 * @param string Database name
97 * @return bool Whether it suceeded
101 function PMA_exportDBFooter($db) {
106 * Outputs create database database
108 * @param string Database name
110 * @return bool Whether it suceeded
114 function PMA_exportDBCreate($db) {
119 * Outputs the content of a table in CSV format
121 * @param string the database name
122 * @param string the table name
123 * @param string the end of line sequence
124 * @param string the url to go back in case of error
125 * @param string SQL query for obtaining data
127 * @return bool Whether it suceeded
131 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
135 if (! PMA_exportOutputHandler('<h2>' . __('Dumping data for table') . ' ' . $table . '</h2>')) {
138 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
142 // Gets the data from the database
143 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
144 $fields_cnt = PMA_DBI_num_fields($result);
146 // If required, get fields name at the first line
147 if (isset($GLOBALS['htmlword_columns'])) {
148 $schema_insert = '<tr class="print-category">';
149 for ($i = 0; $i < $fields_cnt; $i++
) {
150 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>';
152 $schema_insert .= '</tr>';
153 if (! PMA_exportOutputHandler($schema_insert)) {
159 while ($row = PMA_DBI_fetch_row($result)) {
160 $schema_insert = '<tr class="print-category">';
161 for ($j = 0; $j < $fields_cnt; $j++
) {
162 if (! isset($row[$j]) ||
is_null($row[$j])) {
163 $value = $GLOBALS[$what . '_null'];
164 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
169 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
171 $schema_insert .= '</tr>';
172 if (! PMA_exportOutputHandler($schema_insert)) {
176 PMA_DBI_free_result($result);
177 if (! PMA_exportOutputHandler('</table>')) {
184 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
188 if (! PMA_exportOutputHandler('<h2>' . __('Table structure for table') . ' ' .$table . '</h2>')) {
193 * Get the unique keys in the table
195 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
196 $keys_result = PMA_DBI_query($keys_query);
197 $unique_keys = array();
198 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
199 if ($key['Non_unique'] == 0) {
200 $unique_keys[] = $key['Column_name'];
203 PMA_DBI_free_result($keys_result);
206 * Gets fields properties
208 PMA_DBI_select_db($db);
209 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
210 $result = PMA_DBI_query($local_query);
211 $fields_cnt = PMA_DBI_num_rows($result);
213 // Check if we can use Relations (Mike Beck)
214 if ($do_relation && ! empty($cfgRelation['relation'])) {
215 // Find which tables are related with the current one and write it in
217 $res_rel = PMA_getForeigners($db, $table);
219 if ($res_rel && count($res_rel) > 0) {
229 * Displays the table structure
231 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
236 if ($do_relation && $have_rel) {
239 if ($do_comments && $cfgRelation['commwork']) {
242 if ($do_mime && $cfgRelation['mimework']) {
246 $schema_insert = '<tr class="print-category">';
247 $schema_insert .= '<th class="print">' . htmlspecialchars(__('Column')) . '</th>';
248 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Type')) . '</b></td>';
249 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Null')) . '</b></td>';
250 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Default')) . '</b></td>';
251 if ($do_relation && $have_rel) {
252 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Links to')) . '</b></td>';
255 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Comments')) . '</b></td>';
256 $comments = PMA_getComments($db, $table);
258 if ($do_mime && $cfgRelation['mimework']) {
259 $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
260 $mime_map = PMA_getMIME($db, $table, true);
262 $schema_insert .= '</tr>';
264 if (! PMA_exportOutputHandler($schema_insert)) {
268 while ($row = PMA_DBI_fetch_assoc($result)) {
270 $schema_insert = '<tr class="print-category">';
271 $type = $row['Type'];
272 // reformat mysql query output
273 // set or enum types: slashes single quotes inside options
274 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
275 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
276 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
283 $type_nowrap = ' nowrap="nowrap"';
284 $type = preg_replace('/BINARY/i', '', $type);
285 $type = preg_replace('/ZEROFILL/i', '', $type);
286 $type = preg_replace('/UNSIGNED/i', '', $type);
291 $binary = preg_match('/BINARY/i', $row['Type']);
292 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
293 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
295 $attribute = ' ';
297 $attribute = 'BINARY';
300 $attribute = 'UNSIGNED';
303 $attribute = 'UNSIGNED ZEROFILL';
305 if (! isset($row['Default'])) {
306 if ($row['Null'] != 'NO') {
307 $row['Default'] = 'NULL';
310 $row['Default'] = $row['Default'];
315 if (in_array($row['Field'], $unique_keys)) {
316 $fmt_pre = '<b>' . $fmt_pre;
317 $fmt_post = $fmt_post . '</b>';
319 if ($row['Key'] == 'PRI') {
320 $fmt_pre = '<i>' . $fmt_pre;
321 $fmt_post = $fmt_post . '</i>';
323 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($row['Field']) . $fmt_post . '</td>';
324 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
325 $schema_insert .= '<td class="print">' . htmlspecialchars(($row['Null'] == '' ||
$row['Null'] == 'NO') ?
__('No') : __('Yes')) . '</td>';
326 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($row['Default']) ?
$row['Default'] : '') . '</td>';
328 $field_name = $row['Field'];
330 if ($do_relation && $have_rel) {
331 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ?
htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
333 if ($do_comments && $cfgRelation['commwork']) {
334 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ?
htmlspecialchars($comments[$field_name]) : '') . '</td>';
336 if ($do_mime && $cfgRelation['mimework']) {
337 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ?
htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
340 $schema_insert .= '</tr>';
342 if (! PMA_exportOutputHandler($schema_insert)) {
346 PMA_DBI_free_result($result);
348 return PMA_exportOutputHandler('</table>');