Merge branch 'integration'
[phpmyadmin-regexreplace.git] / libraries / export / htmlword.php
blobf5425bc4e0222a10da274a6c78714c047c1e9a22
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build CSV dumps of tables
6 * @package phpMyAdmin-Export-HTMLWord
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['htmlword'] = array(
17 'text' => __('Microsoft Word 2000'),
18 'extension' => 'doc',
19 'mime_type' => 'application/vnd.ms-word',
20 'force_file' => true,
21 'options' => array(
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'),
26 /* data options */
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'),
34 } else {
36 /**
37 * Outputs comment
39 * @param string Text of comment
41 * @return bool Whether it suceeded
43 function PMA_exportComment($text) {
44 return TRUE;
47 /**
48 * Outputs export footer
50 * @return bool Whether it suceeded
52 * @access public
54 function PMA_exportFooter() {
55 return PMA_exportOutputHandler('</body></html>');
58 /**
59 * Outputs export header
61 * @return bool Whether it suceeded
63 * @access public
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">
72 <html>
73 <head>
74 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ? $charset_of_file : $charset) . '" />
75 </head>
76 <body>');
79 /**
80 * Outputs database header
82 * @param string Database name
84 * @return bool Whether it suceeded
86 * @access public
88 function PMA_exportDBHeader($db) {
89 return PMA_exportOutputHandler('<h1>' . __('Database') . ' ' . $db . '</h1>');
92 /**
93 * Outputs database footer
95 * @param string Database name
97 * @return bool Whether it suceeded
99 * @access public
101 function PMA_exportDBFooter($db) {
102 return TRUE;
106 * Outputs create database database
108 * @param string Database name
110 * @return bool Whether it suceeded
112 * @access public
114 function PMA_exportDBCreate($db) {
115 return TRUE;
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
129 * @access public
131 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
133 global $what;
135 if (! PMA_exportOutputHandler('<h2>' . __('Dumping data for table') . ' ' . $table . '</h2>')) {
136 return FALSE;
138 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
139 return FALSE;
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>';
151 } // end for
152 $schema_insert .= '</tr>';
153 if (! PMA_exportOutputHandler($schema_insert)) {
154 return FALSE;
156 } // end if
158 // Format the data
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] != '') {
165 $value = $row[$j];
166 } else {
167 $value = '';
169 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
170 } // end for
171 $schema_insert .= '</tr>';
172 if (! PMA_exportOutputHandler($schema_insert)) {
173 return FALSE;
175 } // end while
176 PMA_DBI_free_result($result);
177 if (! PMA_exportOutputHandler('</table>')) {
178 return FALSE;
181 return TRUE;
184 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
186 global $cfgRelation;
188 if (! PMA_exportOutputHandler('<h2>' . __('Table structure for table') . ' ' .$table . '</h2>')) {
189 return FALSE;
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
216 // an array
217 $res_rel = PMA_getForeigners($db, $table);
219 if ($res_rel && count($res_rel) > 0) {
220 $have_rel = TRUE;
221 } else {
222 $have_rel = FALSE;
224 } else {
225 $have_rel = FALSE;
226 } // end if
229 * Displays the table structure
231 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
232 return FALSE;
235 $columns_cnt = 4;
236 if ($do_relation && $have_rel) {
237 $columns_cnt++;
239 if ($do_comments && $cfgRelation['commwork']) {
240 $columns_cnt++;
242 if ($do_mime && $cfgRelation['mimework']) {
243 $columns_cnt++;
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>';
254 if ($do_comments) {
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)) {
265 return FALSE;
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]) . ')';
277 $type_nowrap = '';
279 $binary = 0;
280 $unsigned = 0;
281 $zerofill = 0;
282 } else {
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);
287 if (empty($type)) {
288 $type = '&nbsp;';
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 = '&nbsp;';
296 if ($binary) {
297 $attribute = 'BINARY';
299 if ($unsigned) {
300 $attribute = 'UNSIGNED';
302 if ($zerofill) {
303 $attribute = 'UNSIGNED ZEROFILL';
305 if (! isset($row['Default'])) {
306 if ($row['Null'] != 'NO') {
307 $row['Default'] = 'NULL';
309 } else {
310 $row['Default'] = $row['Default'];
313 $fmt_pre = '';
314 $fmt_post = '';
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)) {
343 return FALSE;
345 } // end while
346 PMA_DBI_free_result($result);
348 return PMA_exportOutputHandler('</table>');