2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to export a set of queries to a MS Word document
6 * @package phpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $plugin_list['htmlword'] = array(
18 'text' => __('Microsoft Word 2000'),
20 'mime_type' => 'application/vnd.ms-word',
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'),
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'),
38 * Outputs export footer
40 * @return bool Whether it suceeded
44 function PMA_exportFooter() {
45 return PMA_exportOutputHandler('</body></html>');
49 * Outputs export header
51 * @return bool Whether it suceeded
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">
64 <meta http-equiv="Content-type" content="text/html;charset=' . (isset($charset_of_file) ?
$charset_of_file : 'utf-8') . '" />
70 * Outputs database header
72 * @param string $db Database name
73 * @return bool Whether it suceeded
77 function PMA_exportDBHeader($db) {
78 return PMA_exportOutputHandler('<h1>' . __('Database') . ' ' . htmlspecialchars($db) . '</h1>');
82 * Outputs database footer
84 * @param string $db Database name
85 * @return bool Whether it suceeded
89 function PMA_exportDBFooter($db) {
94 * Outputs CREATE DATABASE statement
96 * @param string $db Database name
97 * @return bool Whether it suceeded
101 function PMA_exportDBCreate($db) {
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
117 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
121 if (! PMA_exportOutputHandler('<h2>' . __('Dumping data for table') . ' ' . htmlspecialchars($table) . '</h2>')) {
124 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
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>';
138 $schema_insert .= '</tr>';
139 if (! PMA_exportOutputHandler($schema_insert)) {
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] != '') {
155 $schema_insert .= '<td class="print">' . htmlspecialchars($value) . '</td>';
157 $schema_insert .= '</tr>';
158 if (! PMA_exportOutputHandler($schema_insert)) {
162 PMA_DBI_free_result($result);
163 if (! PMA_exportOutputHandler('</table>')) {
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
191 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
195 if (! PMA_exportOutputHandler('<h2>' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</h2>')) {
200 * Get the unique keys in the table
202 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
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
221 $res_rel = PMA_getForeigners($db, $table);
223 if ($res_rel && count($res_rel) > 0) {
233 * Displays the table structure
235 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
240 if ($do_relation && $have_rel) {
243 if ($do_comments && $cfgRelation['commwork']) {
246 if ($do_mime && $cfgRelation['mimework']) {
250 $schema_insert = '<tr class="print-category">';
251 $schema_insert .= '<th class="print">' . __('Column') . '</th>';
252 $schema_insert .= '<td class="print"><b>' . __('Type') . '</b></td>';
253 $schema_insert .= '<td class="print"><b>' . __('Null') . '</b></td>';
254 $schema_insert .= '<td class="print"><b>' . __('Default') . '</b></td>';
255 if ($do_relation && $have_rel) {
256 $schema_insert .= '<td class="print"><b>' . __('Links to') . '</b></td>';
259 $schema_insert .= '<td class="print"><b>' . __('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)) {
272 $columns = PMA_DBI_get_columns($db, $table);
273 foreach ($columns as $column) {
275 $schema_insert = '<tr class="print-category">';
277 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
278 $type = htmlspecialchars($extracted_fieldspec['print_type']);
283 if (! isset($column['Default'])) {
284 if ($column['Null'] != 'NO') {
285 $column['Default'] = 'NULL';
291 if (in_array($column['Field'], $unique_keys)) {
292 $fmt_pre = '<b>' . $fmt_pre;
293 $fmt_post = $fmt_post . '</b>';
295 if ($column['Key'] == 'PRI') {
296 $fmt_pre = '<i>' . $fmt_pre;
297 $fmt_post = $fmt_post . '</i>';
299 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
300 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
301 $schema_insert .= '<td class="print">' . (($column['Null'] == '' ||
$column['Null'] == 'NO') ?
__('No') : __('Yes')) . '</td>';
302 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($column['Default']) ?
$column['Default'] : '') . '</td>';
304 $field_name = $column['Field'];
306 if ($do_relation && $have_rel) {
307 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ?
htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
309 if ($do_comments && $cfgRelation['commwork']) {
310 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ?
htmlspecialchars($comments[$field_name]) : '') . '</td>';
312 if ($do_mime && $cfgRelation['mimework']) {
313 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ?
htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
316 $schema_insert .= '</tr>';
318 if (! PMA_exportOutputHandler($schema_insert)) {
323 return PMA_exportOutputHandler('</table>');