2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build OpenDocument Text dumps of tables
6 * @package PhpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 $GLOBALS['odt_buffer'] = '';
17 require_once 'libraries/opendocument.lib.php';
20 * Handles the export for the ODT class
22 * @package PhpMyAdmin-Export
25 class ExportOdt
extends ExportPlugin
30 public function __construct()
32 $this->setProperties();
36 * Sets the export ODT properties
40 protected function setProperties()
43 $hide_structure = false;
44 if ($plugin_param['export_type'] == 'table'
45 && ! $plugin_param['single_table']
47 $hide_structure = true;
50 $props = 'libraries/properties/';
51 include_once "$props/plugins/ExportPluginProperties.class.php";
52 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
53 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
54 include_once "$props/options/items/TextPropertyItem.class.php";
55 include_once "$props/options/items/BoolPropertyItem.class.php";
56 include_once "$props/options/items/HiddenPropertyItem.class.php";
57 include_once "$props/options/items/RadioPropertyItem.class.php";
59 $exportPluginProperties = new ExportPluginProperties();
60 $exportPluginProperties->setText('OpenDocument Text');
61 $exportPluginProperties->setExtension('odt');
62 $exportPluginProperties->setMimeType(
63 'application/vnd.oasis.opendocument.text'
65 $exportPluginProperties->setForceFile(true);
66 $exportPluginProperties->setOptionsText(__('Options'));
68 // create the root group that will be the options field for
69 // $exportPluginProperties
70 // this will be shown as "Format specific options"
71 $exportSpecificOptions = new OptionsPropertyRootGroup();
72 $exportSpecificOptions->setName("Format Specific Options");
74 // what to dump (structure/data/both) main group
75 $dumpWhat = new OptionsPropertyMainGroup();
76 $dumpWhat->setName("general_opts");
77 $dumpWhat->setText(__('Dump table'));
78 // create primary items and add them to the group
79 $leaf = new RadioPropertyItem();
80 $leaf->setName("structure_or_data");
83 'structure' => __('structure'),
85 'structure_and_data' => __('structure and data')
88 $dumpWhat->addProperty($leaf);
89 // add the main group to the root group
90 $exportSpecificOptions->addProperty($dumpWhat);
93 // structure options main group
94 if (! $hide_structure) {
95 $structureOptions = new OptionsPropertyMainGroup();
96 $structureOptions->setName("structure");
97 $structureOptions->setText(__('Object creation options'));
98 $structureOptions->setForce('data');
99 // create primary items and add them to the group
100 if (! empty($GLOBALS['cfgRelation']['relation'])) {
101 $leaf = new BoolPropertyItem();
102 $leaf->setName("relation");
103 $leaf->setText(__('Display foreign key relationships'));
104 $structureOptions->addProperty($leaf);
106 $leaf = new BoolPropertyItem();
107 $leaf->setName("comments");
108 $leaf->setText(__('Display comments'));
109 $structureOptions->addProperty($leaf);
110 if (! empty($GLOBALS['cfgRelation']['mimework'])) {
111 $leaf = new BoolPropertyItem();
112 $leaf->setName("mime");
113 $leaf->setText(__('Display MIME types'));
114 $structureOptions->addProperty($leaf);
116 // add the main group to the root group
117 $exportSpecificOptions->addProperty($structureOptions);
120 // data options main group
121 $dataOptions = new OptionsPropertyMainGroup();
122 $dataOptions->setName("data");
123 $dataOptions->setText(__('Data dump options'));
124 $dataOptions->setForce('structure');
125 // create primary items and add them to the group
126 $leaf = new BoolPropertyItem();
127 $leaf->setName("columns");
128 $leaf->setText(__('Put columns names in the first row'));
129 $dataOptions->addProperty($leaf);
130 $leaf = new TextPropertyItem();
131 $leaf->setName('null');
132 $leaf->setText(__('Replace NULL with:'));
133 $dataOptions->addProperty($leaf);
134 // add the main group to the root group
135 $exportSpecificOptions->addProperty($dataOptions);
137 // set the options for the export plugin property item
138 $exportPluginProperties->setOptions($exportSpecificOptions);
139 $this->properties
= $exportPluginProperties;
143 * This method is called when any PluginManager to which the observer
144 * is attached calls PluginManager::notify()
146 * @param SplSubject $subject The PluginManager notifying the observer
151 public function update (SplSubject
$subject)
156 * Outputs export header
158 * @return bool Whether it succeeded
160 public function exportHeader ()
162 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
163 . '<office:document-content '
164 . $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
171 * Outputs export footer
173 * @return bool Whether it succeeded
175 public function exportFooter ()
177 $GLOBALS['odt_buffer'] .= '</office:text>'
179 . '</office:document-content>';
180 if (! PMA_exportOutputHandler(
181 PMA_createOpenDocument(
182 'application/vnd.oasis.opendocument.text',
183 $GLOBALS['odt_buffer']
192 * Outputs database header
194 * @param string $db Database name
196 * @return bool Whether it succeeded
198 public function exportDBHeader ($db)
200 $GLOBALS['odt_buffer'] .=
201 '<text:h text:outline-level="1" text:style-name="Heading_1"'
202 . ' text:is-list-header="true">'
203 . __('Database') . ' ' . htmlspecialchars($db)
209 * Outputs database footer
211 * @param string $db Database name
213 * @return bool Whether it succeeded
215 public function exportDBFooter ($db)
221 * Outputs CREATE DATABASE statement
223 * @param string $db Database name
225 * @return bool Whether it succeeded
227 public function exportDBCreate($db)
232 * Outputs the content of a table in NHibernate format
234 * @param string $db database name
235 * @param string $table table name
236 * @param string $crlf the end of line sequence
237 * @param string $error_url the url to go back in case of error
238 * @param string $sql_query SQL query for obtaining data
240 * @return bool Whether it succeeded
242 public function exportData($db, $table, $crlf, $error_url, $sql_query)
246 // Gets the data from the database
247 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
248 $fields_cnt = PMA_DBI_num_fields($result);
249 $fields_meta = PMA_DBI_get_fields_meta($result);
250 $field_flags = array();
251 for ($j = 0; $j < $fields_cnt; $j++
) {
252 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
255 $GLOBALS['odt_buffer'] .=
256 '<text:h text:outline-level="2" text:style-name="Heading_2"'
257 . ' text:is-list-header="true">'
258 . __('Dumping data for table') . ' ' . htmlspecialchars($table)
261 . ' table:name="' . htmlspecialchars($table) . '_structure">'
262 . '<table:table-column'
263 . ' table:number-columns-repeated="' . $fields_cnt . '"/>';
265 // If required, get fields name at the first line
266 if (isset($GLOBALS[$what . '_columns'])) {
267 $GLOBALS['odt_buffer'] .= '<table:table-row>';
268 for ($i = 0; $i < $fields_cnt; $i++
) {
269 $GLOBALS['odt_buffer'] .=
270 '<table:table-cell office:value-type="string">'
273 stripslashes(PMA_DBI_field_name($result, $i))
276 . '</table:table-cell>';
278 $GLOBALS['odt_buffer'] .= '</table:table-row>';
282 while ($row = PMA_DBI_fetch_row($result)) {
283 $GLOBALS['odt_buffer'] .= '<table:table-row>';
284 for ($j = 0; $j < $fields_cnt; $j++
) {
285 if (! isset($row[$j]) ||
is_null($row[$j])) {
286 $GLOBALS['odt_buffer'] .=
287 '<table:table-cell office:value-type="string">'
289 . htmlspecialchars($GLOBALS[$what . '_null'])
291 . '</table:table-cell>';
292 } elseif (stristr($field_flags[$j], 'BINARY')
293 && $fields_meta[$j]->blob
296 $GLOBALS['odt_buffer'] .=
297 '<table:table-cell office:value-type="string">'
298 . '<text:p></text:p>'
299 . '</table:table-cell>';
300 } elseif ($fields_meta[$j]->numeric
301 && $fields_meta[$j]->type
!= 'timestamp'
302 && ! $fields_meta[$j]->blob
304 $GLOBALS['odt_buffer'] .=
305 '<table:table-cell office:value-type="float"'
306 . ' office:value="' . $row[$j] . '" >'
308 . htmlspecialchars($row[$j])
310 . '</table:table-cell>';
312 $GLOBALS['odt_buffer'] .=
313 '<table:table-cell office:value-type="string">'
315 . htmlspecialchars($row[$j])
317 . '</table:table-cell>';
320 $GLOBALS['odt_buffer'] .= '</table:table-row>';
322 PMA_DBI_free_result($result);
324 $GLOBALS['odt_buffer'] .= '</table:table>';
330 * Returns a stand-in CREATE definition to resolve view dependencies
332 * @param string $db the database name
333 * @param string $view the view name
334 * @param string $crlf the end of line sequence
338 public function getTableDefStandIn($db, $view, $crlf)
341 * Gets fields properties
343 PMA_DBI_select_db($db);
346 * Displays the table structure
348 $GLOBALS['odt_buffer'] .=
349 '<table:table table:name="'
350 . htmlspecialchars($view) . '_data">';
352 $GLOBALS['odt_buffer'] .=
353 '<table:table-column'
354 . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
356 $GLOBALS['odt_buffer'] .= '<table:table-row>'
357 . '<table:table-cell office:value-type="string">'
358 . '<text:p>' . __('Column') . '</text:p>'
359 . '</table:table-cell>'
360 . '<table:table-cell office:value-type="string">'
361 . '<text:p>' . __('Type') . '</text:p>'
362 . '</table:table-cell>'
363 . '<table:table-cell office:value-type="string">'
364 . '<text:p>' . __('Null') . '</text:p>'
365 . '</table:table-cell>'
366 . '<table:table-cell office:value-type="string">'
367 . '<text:p>' . __('Default') . '</text:p>'
368 . '</table:table-cell>'
369 . '</table:table-row>';
371 $columns = PMA_DBI_get_columns($db, $view);
372 foreach ($columns as $column) {
373 $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition($column);
374 $GLOBALS['odt_buffer'] .= '</table:table-row>';
377 $GLOBALS['odt_buffer'] .= '</table:table>';
382 * Returns $table's CREATE definition
384 * @param string $db the database name
385 * @param string $table the table name
386 * @param string $crlf the end of line sequence
387 * @param string $error_url the url to go back in case of error
388 * @param bool $do_relation whether to include relation comments
389 * @param bool $do_comments whether to include the pmadb-style column
390 * comments as comments in the structure;
391 * this is deprecated but the parameter is
392 * left here because export.php calls
393 * PMA_exportStructure() also for other
394 * @param bool $do_mime whether to include mime comments
395 * @param bool $show_dates whether to include creation/update/check dates
396 * @param bool $add_semicolon whether to add semicolon and end-of-line at
398 * @param bool $view whether we're handling a view
402 public function getTableDef(
411 $add_semicolon = true,
417 * Gets fields properties
419 PMA_DBI_select_db($db);
421 // Check if we can use Relations
422 if ($do_relation && ! empty($cfgRelation['relation'])) {
423 // Find which tables are related with the current one and write it in
425 $res_rel = PMA_getForeigners($db, $table);
427 if ($res_rel && count($res_rel) > 0) {
437 * Displays the table structure
439 $GLOBALS['odt_buffer'] .= '<table:table table:name="'
440 . htmlspecialchars($table) . '_structure">';
442 if ($do_relation && $have_rel) {
448 if ($do_mime && $cfgRelation['mimework']) {
451 $GLOBALS['odt_buffer'] .= '<table:table-column'
452 . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
454 $GLOBALS['odt_buffer'] .= '<table:table-row>'
455 . '<table:table-cell office:value-type="string">'
456 . '<text:p>' . __('Column') . '</text:p>'
457 . '</table:table-cell>'
458 . '<table:table-cell office:value-type="string">'
459 . '<text:p>' . __('Type') . '</text:p>'
460 . '</table:table-cell>'
461 . '<table:table-cell office:value-type="string">'
462 . '<text:p>' . __('Null') . '</text:p>'
463 . '</table:table-cell>'
464 . '<table:table-cell office:value-type="string">'
465 . '<text:p>' . __('Default') . '</text:p>'
466 . '</table:table-cell>';
467 if ($do_relation && $have_rel) {
468 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
469 . '<text:p>' . __('Links to') . '</text:p>'
470 . '</table:table-cell>';
473 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
474 . '<text:p>' . __('Comments') . '</text:p>'
475 . '</table:table-cell>';
476 $comments = PMA_getComments($db, $table);
478 if ($do_mime && $cfgRelation['mimework']) {
479 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
480 . '<text:p>' . __('MIME type') . '</text:p>'
481 . '</table:table-cell>';
482 $mime_map = PMA_getMIME($db, $table, true);
484 $GLOBALS['odt_buffer'] .= '</table:table-row>';
486 $columns = PMA_DBI_get_columns($db, $table);
487 foreach ($columns as $column) {
488 $field_name = $column['Field'];
489 $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition($column);
491 if ($do_relation && $have_rel) {
492 if (isset($res_rel[$field_name])) {
493 $GLOBALS['odt_buffer'] .=
494 '<table:table-cell office:value-type="string">'
497 $res_rel[$field_name]['foreign_table']
498 . ' (' . $res_rel[$field_name]['foreign_field'] . ')'
501 . '</table:table-cell>';
505 if (isset($comments[$field_name])) {
506 $GLOBALS['odt_buffer'] .=
507 '<table:table-cell office:value-type="string">'
509 . htmlspecialchars($comments[$field_name])
511 . '</table:table-cell>';
513 $GLOBALS['odt_buffer'] .=
514 '<table:table-cell office:value-type="string">'
515 . '<text:p></text:p>'
516 . '</table:table-cell>';
519 if ($do_mime && $cfgRelation['mimework']) {
520 if (isset($mime_map[$field_name])) {
521 $GLOBALS['odt_buffer'] .=
522 '<table:table-cell office:value-type="string">'
525 str_replace('_', '/', $mime_map[$field_name]['mimetype'])
528 . '</table:table-cell>';
530 $GLOBALS['odt_buffer'] .=
531 '<table:table-cell office:value-type="string">'
532 . '<text:p></text:p>'
533 . '</table:table-cell>';
536 $GLOBALS['odt_buffer'] .= '</table:table-row>';
539 $GLOBALS['odt_buffer'] .= '</table:table>';
541 } // end of the '$this->getTableDef()' function
546 * @param string $db database name
547 * @param string $table table name
551 protected function getTriggers($db, $table)
553 $GLOBALS['odt_buffer'] .= '<table:table'
554 . ' table:name="' . htmlspecialchars($table) . '_triggers">'
555 . '<table:table-column'
556 . ' table:number-columns-repeated="4"/>'
557 . '<table:table-row>'
558 . '<table:table-cell office:value-type="string">'
559 . '<text:p>' . __('Name') . '</text:p>'
560 . '</table:table-cell>'
561 . '<table:table-cell office:value-type="string">'
562 . '<text:p>' . __('Time') . '</text:p>'
563 . '</table:table-cell>'
564 . '<table:table-cell office:value-type="string">'
565 . '<text:p>' . __('Event') . '</text:p>'
566 . '</table:table-cell>'
567 . '<table:table-cell office:value-type="string">'
568 . '<text:p>' . __('Definition') . '</text:p>'
569 . '</table:table-cell>'
570 . '</table:table-row>';
572 $triggers = PMA_DBI_get_triggers($db, $table);
574 foreach ($triggers as $trigger) {
575 $GLOBALS['odt_buffer'] .= '<table:table-row>';
576 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
578 . htmlspecialchars($trigger['name'])
580 . '</table:table-cell>';
581 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
583 . htmlspecialchars($trigger['action_timing'])
585 . '</table:table-cell>';
586 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
588 . htmlspecialchars($trigger['event_manipulation'])
590 . '</table:table-cell>';
591 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
593 . htmlspecialchars($trigger['definition'])
595 . '</table:table-cell>';
596 $GLOBALS['odt_buffer'] .= '</table:table-row>';
599 $GLOBALS['odt_buffer'] .= '</table:table>';
604 * Outputs table's structure
606 * @param string $db database name
607 * @param string $table table name
608 * @param string $crlf the end of line sequence
609 * @param string $error_url the url to go back in case of error
610 * @param string $export_mode 'create_table', 'triggers', 'create_view',
612 * @param string $export_type 'server', 'database', 'table'
613 * @param bool $do_relation whether to include relation comments
614 * @param bool $do_comments whether to include the pmadb-style column
615 * comments as comments in the structure;
616 * this is deprecated but the parameter is
617 * left here because export.php calls
618 * PMA_exportStructure() also for other
619 * @param bool $do_mime whether to include mime comments
620 * @param bool $dates whether to include creation/update/check dates
622 * @return bool Whether it succeeded
624 public function exportStructure(
631 $do_relation = false,
632 $do_comments = false,
636 switch($export_mode) {
638 $GLOBALS['odt_buffer'] .=
639 '<text:h text:outline-level="2" text:style-name="Heading_2"'
640 . ' text:is-list-header="true">'
641 . __('Table structure for table') . ' ' .
642 htmlspecialchars($table)
645 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
650 $triggers = PMA_DBI_get_triggers($db, $table);
652 $GLOBALS['odt_buffer'] .=
653 '<text:h text:outline-level="2" text:style-name="Heading_2"'
654 . ' text:is-list-header="true">'
655 . __('Triggers') . ' '
656 . htmlspecialchars($table)
658 $this->getTriggers($db, $table);
662 $GLOBALS['odt_buffer'] .=
663 '<text:h text:outline-level="2" text:style-name="Heading_2"'
664 . ' text:is-list-header="true">'
665 . __('Structure for view') . ' '
666 . htmlspecialchars($table)
669 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
670 $do_mime, $dates, true, true
674 $GLOBALS['odt_buffer'] .=
675 '<text:h text:outline-level="2" text:style-name="Heading_2"'
676 . ' text:is-list-header="true">'
677 . __('Stand-in structure for view') . ' '
678 . htmlspecialchars($table)
680 // export a stand-in definition to resolve view dependencies
681 $this->getTableDefStandIn($db, $table, $crlf);
685 } // end of the '$this->exportStructure' function
688 * Formats the definition for one column
690 * @param array $column info about this column
692 * @return string Formatted column definition
694 protected function formatOneColumnDefinition($column)
696 $field_name = $column['Field'];
697 $definition = '<table:table-row>';
698 $definition .= '<table:table-cell office:value-type="string">'
699 . '<text:p>' . htmlspecialchars($field_name) . '</text:p>'
700 . '</table:table-cell>';
702 $extracted_columnspec
703 = PMA_Util
::extractColumnSpec($column['Type']);
704 $type = htmlspecialchars($extracted_columnspec['print_type']);
709 $definition .= '<table:table-cell office:value-type="string">'
710 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
711 . '</table:table-cell>';
712 if (! isset($column['Default'])) {
713 if ($column['Null'] != 'NO') {
714 $column['Default'] = 'NULL';
716 $column['Default'] = '';
719 $column['Default'] = $column['Default'];
721 $definition .= '<table:table-cell office:value-type="string">'
723 . (($column['Null'] == '' ||
$column['Null'] == 'NO')
727 . '</table:table-cell>';
728 $definition .= '<table:table-cell office:value-type="string">'
729 . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>'
730 . '</table:table-cell>';