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 $unique_keys = array();
203 $keys = PMA_DBI_get_table_indexes($db, $table);
204 foreach ($keys as $key) {
205 if ($key['Non_unique'] == 0) {
206 $unique_keys[] = $key['Column_name'];
211 * Gets fields properties
213 PMA_DBI_select_db($db);
215 // Check if we can use Relations
216 if ($do_relation && ! empty($cfgRelation['relation'])) {
217 // Find which tables are related with the current one and write it in
219 $res_rel = PMA_getForeigners($db, $table);
221 if ($res_rel && count($res_rel) > 0) {
231 * Displays the table structure
233 if (! PMA_exportOutputHandler('<table class="width100" cellspacing="1">')) {
238 if ($do_relation && $have_rel) {
241 if ($do_comments && $cfgRelation['commwork']) {
244 if ($do_mime && $cfgRelation['mimework']) {
248 $schema_insert = '<tr class="print-category">';
249 $schema_insert .= '<th class="print">' . __('Column') . '</th>';
250 $schema_insert .= '<td class="print"><b>' . __('Type') . '</b></td>';
251 $schema_insert .= '<td class="print"><b>' . __('Null') . '</b></td>';
252 $schema_insert .= '<td class="print"><b>' . __('Default') . '</b></td>';
253 if ($do_relation && $have_rel) {
254 $schema_insert .= '<td class="print"><b>' . __('Links to') . '</b></td>';
257 $schema_insert .= '<td class="print"><b>' . __('Comments') . '</b></td>';
258 $comments = PMA_getComments($db, $table);
260 if ($do_mime && $cfgRelation['mimework']) {
261 $schema_insert .= '<td class="print"><b>' . htmlspecialchars('MIME') . '</b></td>';
262 $mime_map = PMA_getMIME($db, $table, true);
264 $schema_insert .= '</tr>';
266 if (! PMA_exportOutputHandler($schema_insert)) {
270 $columns = PMA_DBI_get_columns($db, $table);
271 foreach ($columns as $column) {
273 $schema_insert = '<tr class="print-category">';
275 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
276 $type = htmlspecialchars($extracted_fieldspec['print_type']);
281 if (! isset($column['Default'])) {
282 if ($column['Null'] != 'NO') {
283 $column['Default'] = 'NULL';
289 if (in_array($column['Field'], $unique_keys)) {
290 $fmt_pre = '<b>' . $fmt_pre;
291 $fmt_post = $fmt_post . '</b>';
293 if ($column['Key'] == 'PRI') {
294 $fmt_pre = '<i>' . $fmt_pre;
295 $fmt_post = $fmt_post . '</i>';
297 $schema_insert .= '<td class="print">' . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
298 $schema_insert .= '<td class="print">' . htmlspecialchars($type) . '</td>';
299 $schema_insert .= '<td class="print">' . (($column['Null'] == '' ||
$column['Null'] == 'NO') ?
__('No') : __('Yes')) . '</td>';
300 $schema_insert .= '<td class="print">' . htmlspecialchars(isset($column['Default']) ?
$column['Default'] : '') . '</td>';
302 $field_name = $column['Field'];
304 if ($do_relation && $have_rel) {
305 $schema_insert .= '<td class="print">' . (isset($res_rel[$field_name]) ?
htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') : '') . '</td>';
307 if ($do_comments && $cfgRelation['commwork']) {
308 $schema_insert .= '<td class="print">' . (isset($comments[$field_name]) ?
htmlspecialchars($comments[$field_name]) : '') . '</td>';
310 if ($do_mime && $cfgRelation['mimework']) {
311 $schema_insert .= '<td class="print">' . (isset($mime_map[$field_name]) ?
htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) : '') . '</td>';
314 $schema_insert .= '</tr>';
316 if (! PMA_exportOutputHandler($schema_insert)) {
321 return PMA_exportOutputHandler('</table>');