Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / export / htmlword.php
blobd1b9cf3a18895bb186d09d024648fe96209db416
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build HTML dumps of tables
6 * @package phpMyAdmin-Export
7 * @subpackage HTMLWord
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $plugin_list['htmlword'] = array(
18 'text' => __('Microsoft Word 2000'),
19 'extension' => 'doc',
20 'mime_type' => 'application/vnd.ms-word',
21 'force_file' => true,
22 'options' => array(
23 /* what to dump (structure/data/both) */
24 array('type' => 'begin_group', 'name' => 'dump_what', 'text' => __('Dump table')),
25 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data'))),
26 array('type' => 'end_group'),
27 /* data options */
28 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure'),
29 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
30 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
31 array('type' => 'end_group'),
33 'options_text' => __('Options'),
35 } else {
37 /**
38 * Outputs export footer
40 * @return bool Whether it suceeded
42 * @access public
44 function PMA_exportFooter() {
45 return PMA_exportOutputHandler('</body></html>');
48 /**
49 * Outputs export header
51 * @return bool Whether it suceeded
53 * @access public
55 function PMA_exportHeader() {
56 global $charset_of_file;
57 return PMA_exportOutputHandler('<html xmlns:o="urn:schemas-microsoft-com:office:office"
58 xmlns:x="urn:schemas-microsoft-com:office:word"
59 xmlns="http://www.w3.org/TR/REC-html40">
61 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
62 <html>
63 <head>
64 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ? $charset_of_file : 'utf-8') . '" />
65 </head>
66 <body>');
69 /**
70 * Outputs database header
72 * @param string $db Database name
73 * @return bool Whether it suceeded
75 * @access public
77 function PMA_exportDBHeader($db) {
78 return PMA_exportOutputHandler('<h1>' . __('Database') . ' ' . htmlspecialchars($db) . '</h1>');
81 /**
82 * Outputs database footer
84 * @param string $db Database name
85 * @return bool Whether it suceeded
87 * @access public
89 function PMA_exportDBFooter($db) {
90 return true;
93 /**
94 * Outputs CREATE DATABASE statement
96 * @param string $db Database name
97 * @return bool Whether it suceeded
99 * @access public
101 function PMA_exportDBCreate($db) {
102 return true;
106 * Outputs the content of a table in HTML (Microsoft Word) format
108 * @param string $db database name
109 * @param string $table table name
110 * @param string $crlf the end of line sequence
111 * @param string $error_url the url to go back in case of error
112 * @param string $sql_query SQL query for obtaining data
113 * @return bool Whether it suceeded
115 * @access public
117 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
119 global $what;
121 if (! PMA_exportOutputHandler('<h2>' . __('Dumping data for table') . ' ' . htmlspecialchars($table) . '</h2>')) {
122 return false;
124 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
125 return false;
128 // Gets the data from the database
129 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
130 $fields_cnt = PMA_DBI_num_fields($result);
132 // If required, get fields name at the first line
133 if (isset($GLOBALS['htmlword_columns'])) {
134 $schema_insert = '<tr class="print-category">';
135 for ($i = 0; $i < $fields_cnt; $i++) {
136 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</b></td>';
137 } // end for
138 $schema_insert .= '</tr>';
139 if (! PMA_exportOutputHandler($schema_insert)) {
140 return false;
142 } // end if
144 // Format the data
145 while ($row = PMA_DBI_fetch_row($result)) {
146 $schema_insert = '<tr class="print-category">';
147 for ($j = 0; $j < $fields_cnt; $j++) {
148 if (! isset($row[$j]) || is_null($row[$j])) {
149 $value = $GLOBALS[$what . '_null'];
150 } elseif ($row[$j] == '0' || $row[$j] != '') {
151 $value = $row[$j];
152 } else {
153 $value = '';
155 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
156 } // end for
157 $schema_insert .= '</tr>';
158 if (! PMA_exportOutputHandler($schema_insert)) {
159 return false;
161 } // end while
162 PMA_DBI_free_result($result);
163 if (! PMA_exportOutputHandler('</table>')) {
164 return false;
167 return true;
171 * Outputs table's structure
173 * @param string $db database name
174 * @param string $table table name
175 * @param string $crlf the end of line sequence
176 * @param string $error_url the url to go back in case of error
177 * @param bool $do_relation whether to include relation comments
178 * @param bool $do_comments whether to include the pmadb-style column comments
179 * as comments in the structure; this is deprecated
180 * but the parameter is left here because export.php
181 * calls PMA_exportStructure() also for other export
182 * types which use this parameter
183 * @param bool $do_mime whether to include mime comments
184 * @param bool $dates whether to include creation/update/check dates
185 * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
186 * @param string $export_type 'server', 'database', 'table'
187 * @return bool Whether it suceeded
189 * @access public
191 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
193 global $cfgRelation;
195 if (! PMA_exportOutputHandler('<h2>' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</h2>')) {
196 return false;
200 * Get the unique keys in the table
202 $keys_query = PMA_DBI_get_table_indexes_sql($db, $table);
203 $keys_result = PMA_DBI_query($keys_query);
204 $unique_keys = array();
205 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
206 if ($key['Non_unique'] == 0) {
207 $unique_keys[] = $key['Column_name'];
210 PMA_DBI_free_result($keys_result);
213 * Gets fields properties
215 PMA_DBI_select_db($db);
217 // Check if we can use Relations
218 if ($do_relation && ! empty($cfgRelation['relation'])) {
219 // Find which tables are related with the current one and write it in
220 // an array
221 $res_rel = PMA_getForeigners($db, $table);
223 if ($res_rel && count($res_rel) > 0) {
224 $have_rel = true;
225 } else {
226 $have_rel = false;
228 } else {
229 $have_rel = false;
230 } // end if
233 * Displays the table structure
235 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
236 return false;
239 $columns_cnt = 4;
240 if ($do_relation && $have_rel) {
241 $columns_cnt++;
243 if ($do_comments && $cfgRelation['commwork']) {
244 $columns_cnt++;
246 if ($do_mime && $cfgRelation['mimework']) {
247 $columns_cnt++;
250 $schema_insert = '<tr class="print-category">';
251 $schema_insert .= '<th class="print">' . htmlspecialchars(__('Column')) . '</th>';
252 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Type')) . '</b></td>';
253 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Null')) . '</b></td>';
254 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Default')) . '</b></td>';
255 if ($do_relation && $have_rel) {
256 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Links to')) . '</b></td>';
258 if ($do_comments) {
259 $schema_insert .= '<td class="print"><b>' . htmlspecialchars(__('Comments')) . '</b></td>';
260 $comments = PMA_getComments($db, $table);
262 if ($do_mime && $cfgRelation['mimework']) {
263 $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
264 $mime_map = PMA_getMIME($db, $table, true);
266 $schema_insert .= '</tr>';
268 if (! PMA_exportOutputHandler($schema_insert)) {
269 return false;
272 $columns = PMA_DBI_get_columns($db, $table);
273 foreach ($columns as $column) {
275 $schema_insert = '<tr class="print-category">';
276 $type = $column['Type'];
277 // reformat mysql query output
278 // set or enum types: slashes single quotes inside options
279 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
280 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
281 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
282 $type_nowrap = '';
284 $binary = 0;
285 $unsigned = 0;
286 $zerofill = 0;
287 } else {
288 $type_nowrap = ' nowrap="nowrap"';
289 $type = preg_replace('/BINARY/i', '', $type);
290 $type = preg_replace('/ZEROFILL/i', '', $type);
291 $type = preg_replace('/UNSIGNED/i', '', $type);
292 if (empty($type)) {
293 $type = '&nbsp;';
296 $binary = preg_match('/BINARY/i', $column['Type']);
297 $unsigned = preg_match('/UNSIGNED/i', $column['Type']);
298 $zerofill = preg_match('/ZEROFILL/i', $column['Type']);
300 $attribute = '&nbsp;';
301 if ($binary) {
302 $attribute = 'BINARY';
304 if ($unsigned) {
305 $attribute = 'UNSIGNED';
307 if ($zerofill) {
308 $attribute = 'UNSIGNED ZEROFILL';
310 if (! isset($column['Default'])) {
311 if ($column['Null'] != 'NO') {
312 $column['Default'] = 'NULL';
316 $fmt_pre = '';
317 $fmt_post = '';
318 if (in_array($column['Field'], $unique_keys)) {
319 $fmt_pre = '<b>' . $fmt_pre;
320 $fmt_post = $fmt_post . '</b>';
322 if ($column['Key'] == 'PRI') {
323 $fmt_pre = '<i>' . $fmt_pre;
324 $fmt_post = $fmt_post . '</i>';
326 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
327 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
328 $schema_insert .= '<td class="print">' . htmlspecialchars(($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes')) . '</td>';
329 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($column['Default']) ? $column['Default'] : '') . '</td>';
331 $field_name = $column['Field'];
333 if ($do_relation && $have_rel) {
334 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ? htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
336 if ($do_comments && $cfgRelation['commwork']) {
337 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ? htmlspecialchars($comments[$field_name]) : '') . '</td>';
339 if ($do_mime && $cfgRelation['mimework']) {
340 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ? htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
343 $schema_insert .= '</tr>';
345 if (! PMA_exportOutputHandler($schema_insert)) {
346 return false;
348 } // end while
350 return PMA_exportOutputHandler('</table>');