Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportOdt.class.php
blobbb608f0e69983336ea457ae343cc400f62b1e109
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build OpenDocument Text dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage ODT
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 $GLOBALS['odt_buffer'] = '';
17 require_once 'libraries/opendocument.lib.php';
19 /**
20 * Handles the export for the ODT class
22 * @package PhpMyAdmin-Export
23 * @subpackage ODT
25 class ExportOdt extends ExportPlugin
27 /**
28 * Constructor
30 public function __construct()
32 $this->setProperties();
35 /**
36 * Sets the export ODT properties
38 * @return void
40 protected function setProperties()
42 global $plugin_param;
43 $hide_structure = false;
44 if ($plugin_param['export_type'] == 'table'
45 && ! $plugin_param['single_table']
46 ) {
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");
81 $leaf->setValues(
82 array(
83 'structure' => __('structure'),
84 'data' => __('data'),
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
147 * of an update.
149 * @return void
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">'
165 . '<office:body>'
166 . '<office:text>';
167 return true;
171 * Outputs export footer
173 * @return bool Whether it succeeded
175 public function exportFooter ()
177 $GLOBALS['odt_buffer'] .= '</office:text>'
178 . '</office:body>'
179 . '</office:document-content>';
180 if (! PMA_exportOutputHandler(
181 PMA_createOpenDocument(
182 'application/vnd.oasis.opendocument.text',
183 $GLOBALS['odt_buffer']
185 )) {
186 return false;
188 return true;
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)
204 . '</text:h>';
205 return true;
209 * Outputs database footer
211 * @param string $db Database name
213 * @return bool Whether it succeeded
215 public function exportDBFooter ($db)
217 return true;
221 * Outputs CREATE DATABASE statement
223 * @param string $db Database name
225 * @return bool Whether it succeeded
227 public function exportDBCreate($db)
229 return true;
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)
244 global $what;
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)
259 . '</text:h>'
260 . '<table: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">'
271 . '<text:p>'
272 . htmlspecialchars(
273 stripslashes(PMA_DBI_field_name($result, $i))
275 . '</text:p>'
276 . '</table:table-cell>';
277 } // end for
278 $GLOBALS['odt_buffer'] .= '</table:table-row>';
279 } // end if
281 // Format the data
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">'
288 . '<text:p>'
289 . htmlspecialchars($GLOBALS[$what . '_null'])
290 . '</text:p>'
291 . '</table:table-cell>';
292 } elseif (stristr($field_flags[$j], 'BINARY')
293 && $fields_meta[$j]->blob
295 // ignore 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] . '" >'
307 . '<text:p>'
308 . htmlspecialchars($row[$j])
309 . '</text:p>'
310 . '</table:table-cell>';
311 } else {
312 $GLOBALS['odt_buffer'] .=
313 '<table:table-cell office:value-type="string">'
314 . '<text:p>'
315 . htmlspecialchars($row[$j])
316 . '</text:p>'
317 . '</table:table-cell>';
319 } // end for
320 $GLOBALS['odt_buffer'] .= '</table:table-row>';
321 } // end while
322 PMA_DBI_free_result($result);
324 $GLOBALS['odt_buffer'] .= '</table:table>';
326 return true;
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
336 * @return bool true
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">';
351 $columns_cnt = 4;
352 $GLOBALS['odt_buffer'] .=
353 '<table:table-column'
354 . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
355 /* Header */
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>';
375 } // end foreach
377 $GLOBALS['odt_buffer'] .= '</table:table>';
378 return true;
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
397 * the end
398 * @param bool $view whether we're handling a view
400 * @return bool true
402 public function getTableDef(
403 $db,
404 $table,
405 $crlf,
406 $error_url,
407 $do_relation,
408 $do_comments,
409 $do_mime,
410 $show_dates = false,
411 $add_semicolon = true,
412 $view = false
414 global $cfgRelation;
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
424 // an array
425 $res_rel = PMA_getForeigners($db, $table);
427 if ($res_rel && count($res_rel) > 0) {
428 $have_rel = true;
429 } else {
430 $have_rel = false;
432 } else {
433 $have_rel = false;
434 } // end if
437 * Displays the table structure
439 $GLOBALS['odt_buffer'] .= '<table:table table:name="'
440 . htmlspecialchars($table) . '_structure">';
441 $columns_cnt = 4;
442 if ($do_relation && $have_rel) {
443 $columns_cnt++;
445 if ($do_comments) {
446 $columns_cnt++;
448 if ($do_mime && $cfgRelation['mimework']) {
449 $columns_cnt++;
451 $GLOBALS['odt_buffer'] .= '<table:table-column'
452 . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
453 /* Header */
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>';
472 if ($do_comments) {
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">'
495 . '<text:p>'
496 . htmlspecialchars(
497 $res_rel[$field_name]['foreign_table']
498 . ' (' . $res_rel[$field_name]['foreign_field'] . ')'
500 . '</text:p>'
501 . '</table:table-cell>';
504 if ($do_comments) {
505 if (isset($comments[$field_name])) {
506 $GLOBALS['odt_buffer'] .=
507 '<table:table-cell office:value-type="string">'
508 . '<text:p>'
509 . htmlspecialchars($comments[$field_name])
510 . '</text:p>'
511 . '</table:table-cell>';
512 } else {
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">'
523 . '<text:p>'
524 . htmlspecialchars(
525 str_replace('_', '/', $mime_map[$field_name]['mimetype'])
527 . '</text:p>'
528 . '</table:table-cell>';
529 } else {
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>';
537 } // end foreach
539 $GLOBALS['odt_buffer'] .= '</table:table>';
540 return true;
541 } // end of the '$this->getTableDef()' function
544 * Outputs triggers
546 * @param string $db database name
547 * @param string $table table name
549 * @return bool true
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">'
577 . '<text:p>'
578 . htmlspecialchars($trigger['name'])
579 . '</text:p>'
580 . '</table:table-cell>';
581 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
582 . '<text:p>'
583 . htmlspecialchars($trigger['action_timing'])
584 . '</text:p>'
585 . '</table:table-cell>';
586 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
587 . '<text:p>'
588 . htmlspecialchars($trigger['event_manipulation'])
589 . '</text:p>'
590 . '</table:table-cell>';
591 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
592 . '<text:p>'
593 . htmlspecialchars($trigger['definition'])
594 . '</text:p>'
595 . '</table:table-cell>';
596 $GLOBALS['odt_buffer'] .= '</table:table-row>';
599 $GLOBALS['odt_buffer'] .= '</table:table>';
600 return true;
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',
611 * 'stand_in'
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(
625 $db,
626 $table,
627 $crlf,
628 $error_url,
629 $export_mode,
630 $export_type,
631 $do_relation = false,
632 $do_comments = false,
633 $do_mime = false,
634 $dates = false
636 switch($export_mode) {
637 case 'create_table':
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)
643 . '</text:h>';
644 $this->getTableDef(
645 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
646 $do_mime, $dates
648 break;
649 case 'triggers':
650 $triggers = PMA_DBI_get_triggers($db, $table);
651 if ($triggers) {
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)
657 . '</text:h>';
658 $this->getTriggers($db, $table);
660 break;
661 case 'create_view':
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)
667 . '</text:h>';
668 $this->getTableDef(
669 $db, $table, $crlf, $error_url, $do_relation, $do_comments,
670 $do_mime, $dates, true, true
672 break;
673 case 'stand_in':
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)
679 . '</text:h>';
680 // export a stand-in definition to resolve view dependencies
681 $this->getTableDefStandIn($db, $table, $crlf);
682 } // end switch
684 return true;
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']);
705 if (empty($type)) {
706 $type = '&nbsp;';
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';
715 } else {
716 $column['Default'] = '';
718 } else {
719 $column['Default'] = $column['Default'];
721 $definition .= '<table:table-cell office:value-type="string">'
722 . '<text:p>'
723 . (($column['Null'] == '' || $column['Null'] == 'NO')
724 ? __('No')
725 : __('Yes'))
726 . '</text:p>'
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>';
731 return $definition;