2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * HTML-Word export code
6 * @package PhpMyAdmin-Export
7 * @subpackage HTML-Word
9 if (! defined('PHPMYADMIN')) {
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
17 * Handles the export for the HTML-Word format
19 * @package PhpMyAdmin-Export
20 * @subpackage HTML-Word
22 class ExportHtmlword
extends ExportPlugin
27 public function __construct()
29 $this->setProperties();
33 * Sets the export HTML-Word properties
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/RadioPropertyItem.class.php";
44 include_once "$props/options/items/TextPropertyItem.class.php";
45 include_once "$props/options/items/BoolPropertyItem.class.php";
47 $exportPluginProperties = new ExportPluginProperties();
48 $exportPluginProperties->setText('Microsoft Word 2000');
49 $exportPluginProperties->setExtension('doc');
50 $exportPluginProperties->setMimeType('application/vnd.ms-word');
51 $exportPluginProperties->setForceFile(true);
52 $exportPluginProperties->setOptionsText(__('Options'));
54 // create the root group that will be the options field for
55 // $exportPluginProperties
56 // this will be shown as "Format specific options"
57 $exportSpecificOptions = new OptionsPropertyRootGroup();
58 $exportSpecificOptions->setName("Format Specific Options");
60 // what to dump (structure/data/both)
61 $dumpWhat = new OptionsPropertyMainGroup();
62 $dumpWhat->setName("dump_what");
63 $dumpWhat->setText(__('Dump table'));
64 // create primary items and add them to the group
65 $leaf = new RadioPropertyItem();
66 $leaf->setName("structure_or_data");
69 'structure' => __('structure'),
71 'structure_and_data' => __('structure and data')
74 $dumpWhat->addProperty($leaf);
75 // add the main group to the root group
76 $exportSpecificOptions->addProperty($dumpWhat);
78 // data options main group
79 $dataOptions = new OptionsPropertyMainGroup();
80 $dataOptions->setName("dump_what");
81 $dataOptions->setText(__('Data dump options'));
82 $dataOptions->setForce('structure');
83 // create primary items and add them to the group
84 $leaf = new TextPropertyItem();
85 $leaf->setName("null");
86 $leaf->setText(__('Replace NULL with:'));
87 $dataOptions->addProperty($leaf);
88 $leaf = new BoolPropertyItem();
89 $leaf->setName("columns");
90 $leaf->setText(__('Put columns names in the first row'));
91 $dataOptions->addProperty($leaf);
92 // add the main group to the root group
93 $exportSpecificOptions->addProperty($dataOptions);
95 // set the options for the export plugin property item
96 $exportPluginProperties->setOptions($exportSpecificOptions);
97 $this->properties
= $exportPluginProperties;
101 * This method is called when any PluginManager to which the observer
102 * is attached calls PluginManager::notify()
104 * @param SplSubject $subject The PluginManager notifying the observer
109 public function update (SplSubject
$subject)
114 * Outputs export header
116 * @return bool Whether it succeeded
118 public function exportHeader ()
120 global $charset_of_file;
122 return PMA_exportOutputHandler(
123 '<html xmlns:o="urn:schemas-microsoft-com:office:office"
124 xmlns:x="urn:schemas-microsoft-com:office:word"
125 xmlns="http://www.w3.org/TR/REC-html40">
127 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
128 . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
131 <meta http-equiv="Content-type" content="text/html;charset='
132 . (isset($charset_of_file) ?
$charset_of_file : 'utf-8') . '" />
139 * Outputs export footer
141 * @return bool Whether it succeeded
143 public function exportFooter ()
145 return PMA_exportOutputHandler('</body></html>');
149 * Outputs database header
151 * @param string $db Database name
153 * @return bool Whether it succeeded
155 public function exportDBHeader ($db)
157 return PMA_exportOutputHandler(
158 '<h1>' . __('Database') . ' ' . htmlspecialchars($db) . '</h1>'
163 * Outputs database footer
165 * @param string $db Database name
167 * @return bool Whether it succeeded
169 public function exportDBFooter ($db)
175 * Outputs CREATE DATABASE statement
177 * @param string $db Database name
179 * @return bool Whether it succeeded
181 public function exportDBCreate($db)
187 * Outputs the content of a table in HTML-Word format
189 * @param string $db database name
190 * @param string $table table name
191 * @param string $crlf the end of line sequence
192 * @param string $error_url the url to go back in case of error
193 * @param string $sql_query SQL query for obtaining data
195 * @return bool Whether it succeeded
197 public function exportData($db, $table, $crlf, $error_url, $sql_query)
201 if (! PMA_exportOutputHandler(
203 . __('Dumping data for table') . ' ' . htmlspecialchars($table)
208 if (! PMA_exportOutputHandler(
209 '<table class="width100" cellspacing="1">'
214 // Gets the data from the database
215 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
216 $fields_cnt = PMA_DBI_num_fields($result);
218 // If required, get fields name at the first line
219 if (isset($GLOBALS['htmlword_columns'])) {
220 $schema_insert = '<tr class="print-category">';
221 for ($i = 0; $i < $fields_cnt; $i++
) {
222 $schema_insert .= '<td class="print"><strong>'
224 stripslashes(PMA_DBI_field_name($result, $i))
228 $schema_insert .= '</tr>';
229 if (! PMA_exportOutputHandler($schema_insert)) {
235 while ($row = PMA_DBI_fetch_row($result)) {
236 $schema_insert = '<tr class="print-category">';
237 for ($j = 0; $j < $fields_cnt; $j++
) {
238 if (! isset($row[$j]) ||
is_null($row[$j])) {
239 $value = $GLOBALS[$what . '_null'];
240 } elseif ($row[$j] == '0' ||
$row[$j] != '') {
245 $schema_insert .= '<td class="print">'
246 . htmlspecialchars($value)
249 $schema_insert .= '</tr>';
250 if (! PMA_exportOutputHandler($schema_insert)) {
254 PMA_DBI_free_result($result);
255 if (! PMA_exportOutputHandler('</table>')) {
263 * Returns a stand-in CREATE definition to resolve view dependencies
265 * @param string $db the database name
266 * @param string $view the view name
267 * @param string $crlf the end of line sequence
269 * @return string resulting definition
271 public function getTableDefStandIn($db, $view, $crlf)
273 $schema_insert = '<table class="width100" cellspacing="1">'
274 . '<tr class="print-category">'
275 . '<th class="print">'
278 . '<td class="print"><strong>'
281 . '<td class="print"><strong>'
284 . '<td class="print"><strong>'
290 * Get the unique keys in the view
292 $unique_keys = array();
293 $keys = PMA_DBI_get_table_indexes($db, $view);
294 foreach ($keys as $key) {
295 if ($key['Non_unique'] == 0) {
296 $unique_keys[] = $key['Column_name'];
300 $columns = PMA_DBI_get_columns($db, $view);
301 foreach ($columns as $column) {
302 $schema_insert .= $this->formatOneColumnDefinition(
306 $schema_insert .= '</tr>';
309 $schema_insert .= '</table>';
310 return $schema_insert;
314 * Returns $table's CREATE definition
316 * @param string $db the database name
317 * @param string $table the table name
318 * @param string $crlf the end of line sequence
319 * @param string $error_url the url to go back in case of error
320 * @param bool $do_relation whether to include relation comments
321 * @param bool $do_comments whether to include the pmadb-style column
322 * comments as comments in the structure;
323 * this is deprecated but the parameter is
324 * left here because export.php calls
325 * PMA_exportStructure() also for other
326 * export types which use this parameter
327 * @param bool $do_mime whether to include mime comments
328 * @param bool $show_dates whether to include creation/update/check dates
329 * @param bool $add_semicolon whether to add semicolon and end-of-line
331 * @param bool $view whether we're handling a view
333 * @return string resulting schema
335 public function getTableDef(
344 $add_semicolon = true,
347 // set $cfgRelation here, because there is a chance that it's modified
348 // since the class initialization
354 * Gets fields properties
356 PMA_DBI_select_db($db);
358 // Check if we can use Relations
359 if ($do_relation && ! empty($cfgRelation['relation'])) {
360 // Find which tables are related with the current one and write it in
362 $res_rel = PMA_getForeigners($db, $table);
364 if ($res_rel && count($res_rel) > 0) {
374 * Displays the table structure
376 $schema_insert .= '<table class="width100" cellspacing="1">';
379 if ($do_relation && $have_rel) {
382 if ($do_comments && $cfgRelation['commwork']) {
385 if ($do_mime && $cfgRelation['mimework']) {
389 $schema_insert .= '<tr class="print-category">';
390 $schema_insert .= '<th class="print">'
393 $schema_insert .= '<td class="print"><strong>'
396 $schema_insert .= '<td class="print"><strong>'
399 $schema_insert .= '<td class="print"><strong>'
402 if ($do_relation && $have_rel) {
403 $schema_insert .= '<td class="print"><strong>'
408 $schema_insert .= '<td class="print"><strong>'
411 $comments = PMA_getComments($db, $table);
413 if ($do_mime && $cfgRelation['mimework']) {
414 $schema_insert .= '<td class="print"><strong>'
415 . htmlspecialchars('MIME')
417 $mime_map = PMA_getMIME($db, $table, true);
419 $schema_insert .= '</tr>';
421 $columns = PMA_DBI_get_columns($db, $table);
423 * Get the unique keys in the table
425 $unique_keys = array();
426 $keys = PMA_DBI_get_table_indexes($db, $table);
427 foreach ($keys as $key) {
428 if ($key['Non_unique'] == 0) {
429 $unique_keys[] = $key['Column_name'];
432 foreach ($columns as $column) {
433 $schema_insert .= $this->formatOneColumnDefinition(
437 $field_name = $column['Field'];
439 if ($do_relation && $have_rel) {
440 $schema_insert .= '<td class="print">'
441 . (isset($res_rel[$field_name])
443 $res_rel[$field_name]['foreign_table']
444 . ' (' . $res_rel[$field_name]['foreign_field']
449 if ($do_comments && $cfgRelation['commwork']) {
450 $schema_insert .= '<td class="print">'
451 . (isset($comments[$field_name])
452 ?
htmlspecialchars($comments[$field_name])
455 if ($do_mime && $cfgRelation['mimework']) {
456 $schema_insert .= '<td class="print">'
457 . (isset($mime_map[$field_name]) ?
459 str_replace('_', '/', $mime_map[$field_name]['mimetype'])
464 $schema_insert .= '</tr>';
467 $schema_insert .= '</table>';
468 return $schema_insert;
474 * @param string $db database name
475 * @param string $table table name
477 * @return string Formatted triggers list
479 protected function getTriggers($db, $table)
481 $dump = '<table class="width100" cellspacing="1">';
482 $dump .= '<tr class="print-category">';
483 $dump .= '<th class="print">' . __('Name') . '</th>';
484 $dump .= '<td class="print"><strong>' . __('Time') . '</strong></td>';
485 $dump .= '<td class="print"><strong>' . __('Event') . '</strong></td>';
486 $dump .= '<td class="print"><strong>' . __('Definition') . '</strong></td>';
489 $triggers = PMA_DBI_get_triggers($db, $table);
491 foreach ($triggers as $trigger) {
492 $dump .= '<tr class="print-category">';
493 $dump .= '<td class="print">'
494 . htmlspecialchars($trigger['name'])
496 . '<td class="print">'
497 . htmlspecialchars($trigger['action_timing'])
499 . '<td class="print">'
500 . htmlspecialchars($trigger['event_manipulation'])
502 . '<td class="print">'
503 . htmlspecialchars($trigger['definition'])
513 * Outputs table's structure
515 * @param string $db database name
516 * @param string $table table name
517 * @param string $crlf the end of line sequence
518 * @param string $error_url the url to go back in case of error
519 * @param string $export_mode 'create_table', 'triggers', 'create_view',
521 * @param string $export_type 'server', 'database', 'table'
522 * @param bool $do_relation whether to include relation comments
523 * @param bool $do_comments whether to include the pmadb-style column
524 * comments as comments in the structure;
525 * this is deprecated but the parameter is
526 * left here because export.php calls
527 * PMA_exportStructure() also for other
528 * export types which use this parameter
529 * @param bool $do_mime whether to include mime comments
530 * @param bool $dates whether to include creation/update/check dates
532 * @return bool Whether it succeeded
534 public function exportStructure(
541 $do_relation = false,
542 $do_comments = false,
548 switch($export_mode) {
551 . __('Table structure for table') . ' ' . htmlspecialchars($table)
553 $dump .= $this->getTableDef(
554 $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
560 $triggers = PMA_DBI_get_triggers($db, $table);
563 . __('Triggers') . ' ' . htmlspecialchars($table)
565 $dump .= $this->getTriggers($db, $table);
570 . __('Structure for view') . ' ' . htmlspecialchars($table)
572 $dump .= $this->getTableDef(
573 $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
579 . __('Stand-in structure for view') . ' ' . htmlspecialchars($table)
581 // export a stand-in definition to resolve view dependencies
582 $dump .= $this->getTableDefStandIn($db, $table, $crlf);
585 return PMA_exportOutputHandler($dump);
589 * Formats the definition for one column
591 * @param array $column info about this column
592 * @param array $unique_keys unique keys of the table
594 * @return string Formatted column definition
596 protected function formatOneColumnDefinition(
597 $column, $unique_keys
599 $definition = '<tr class="print-category">';
601 $extracted_columnspec
602 = PMA_Util
::extractColumnSpec($column['Type']);
604 $type = htmlspecialchars($extracted_columnspec['print_type']);
609 if (! isset($column['Default'])) {
610 if ($column['Null'] != 'NO') {
611 $column['Default'] = 'NULL';
617 if (in_array($column['Field'], $unique_keys)) {
618 $fmt_pre = '<strong>' . $fmt_pre;
619 $fmt_post = $fmt_post . '</strong>';
621 if ($column['Key'] == 'PRI') {
622 $fmt_pre = '<em>' . $fmt_pre;
623 $fmt_post = $fmt_post . '</em>';
625 $definition .= '<td class="print">' . $fmt_pre
626 . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
627 $definition .= '<td class="print">' . htmlspecialchars($type)
629 $definition .= '<td class="print">'
630 . (($column['Null'] == '' ||
$column['Null'] == 'NO')
634 $definition .= '<td class="print">'
636 isset($column['Default'])