Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportLatex.class.php
blob0f5c953a8b59dc0a57e6a00e349f44cf7f1b5082
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of methods used to build dumps of tables as Latex
6 * @package PhpMyAdmin-Export
7 * @subpackage Latex
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the Latex format
19 * @package PhpMyAdmin-Export
20 * @subpackage Latex
22 class ExportLatex extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 // initialize the specific export sql variables
30 $this->initSpecificVariables();
32 $this->setProperties();
35 /**
36 * Initialize the local variables that are used for export Latex
38 * @return void
40 protected function initSpecificVariables()
42 /* Messages used in default captions */
43 $GLOBALS['strLatexContent'] = __('Content of table @TABLE@');
44 $GLOBALS['strLatexContinued'] = __('(continued)');
45 $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@');
48 /**
49 * Sets the export Latex properties
51 * @return void
53 protected function setProperties()
55 global $plugin_param;
56 $hide_structure = false;
57 if ($plugin_param['export_type'] == 'table'
58 && ! $plugin_param['single_table']
59 ) {
60 $hide_structure = true;
63 $props = 'libraries/properties/';
64 include_once "$props/plugins/ExportPluginProperties.class.php";
65 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
66 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
67 include_once "$props/options/items/BoolPropertyItem.class.php";
68 include_once "$props/options/items/RadioPropertyItem.class.php";
69 include_once "$props/options/items/TextPropertyItem.class.php";
71 $exportPluginProperties = new ExportPluginProperties();
72 $exportPluginProperties->setText('LaTeX');
73 $exportPluginProperties->setExtension('tex');
74 $exportPluginProperties->setMimeType('application/x-tex');
75 $exportPluginProperties->setOptionsText(__('Options'));
77 // create the root group that will be the options field for
78 // $exportPluginProperties
79 // this will be shown as "Format specific options"
80 $exportSpecificOptions = new OptionsPropertyRootGroup();
81 $exportSpecificOptions->setName("Format Specific Options");
83 // general options main group
84 $generalOptions = new OptionsPropertyMainGroup();
85 $generalOptions->setName("general_opts");
86 // create primary items and add them to the group
87 $leaf = new BoolPropertyItem();
88 $leaf->setName("caption");
89 $leaf->setText(__('Include table caption'));
90 $generalOptions->addProperty($leaf);
91 // add the main group to the root group
92 $exportSpecificOptions->addProperty($generalOptions);
94 // what to dump (structure/data/both) main group
95 $dumpWhat = new OptionsPropertyMainGroup();
96 $dumpWhat->setName("dump_what");
97 $dumpWhat->setText(__('Dump table'));
98 // create primary items and add them to the group
99 $leaf = new RadioPropertyItem();
100 $leaf->setName("structure_or_data");
101 $leaf->setValues(
102 array(
103 'structure' => __('structure'),
104 'data' => __('data'),
105 'structure_and_data' => __('structure and data')
108 $dumpWhat->addProperty($leaf);
109 // add the main group to the root group
110 $exportSpecificOptions->addProperty($dumpWhat);
112 // structure options main group
113 if (! $hide_structure) {
114 $structureOptions = new OptionsPropertyMainGroup();
115 $structureOptions->setName("structure");
116 $structureOptions->setText(__('Object creation options'));
117 $structureOptions->setForce('data');
118 // create primary items and add them to the group
119 $leaf = new TextPropertyItem();
120 $leaf->setName("structure_caption");
121 $leaf->setText(__('Table caption'));
122 $leaf->setDoc('faq6-27');
123 $structureOptions->addProperty($leaf);
124 $leaf = new TextPropertyItem();
125 $leaf->setName("structure_continued_caption");
126 $leaf->setText(__('Table caption (continued)'));
127 $leaf->setDoc('faq6-27');
128 $structureOptions->addProperty($leaf);
129 $leaf = new TextPropertyItem();
130 $leaf->setName("structure_label");
131 $leaf->setText(__('Label key'));
132 $leaf->setDoc('faq6-27');
133 $structureOptions->addProperty($leaf);
134 if (! empty($GLOBALS['cfgRelation']['relation'])) {
135 $leaf = new BoolPropertyItem();
136 $leaf->setName("relation");
137 $leaf->setText(__('Display foreign key relationships'));
138 $structureOptions->addProperty($leaf);
140 $leaf = new BoolPropertyItem();
141 $leaf->setName("comments");
142 $leaf->setText(__('Display comments'));
143 $structureOptions->addProperty($leaf);
144 if (! empty($GLOBALS['cfgRelation']['mimework'])) {
145 $leaf = new BoolPropertyItem();
146 $leaf->setName("mime");
147 $leaf->setText(__('Display MIME types'));
148 $structureOptions->addProperty($leaf);
150 // add the main group to the root group
151 $exportSpecificOptions->addProperty($structureOptions);
154 // data options main group
155 $dataOptions = new OptionsPropertyMainGroup();
156 $dataOptions->setName("data");
157 $dataOptions->setText(__('Data dump options'));
158 $dataOptions->setForce('structure');
159 // create primary items and add them to the group
160 $leaf = new BoolPropertyItem();
161 $leaf->setName("columns");
162 $leaf->setText(__('Put columns names in the first row'));
163 $dataOptions->addProperty($leaf);
164 $leaf = new TextPropertyItem();
165 $leaf->setName("data_caption");
166 $leaf->setText(__('Table caption'));
167 $leaf->setDoc('faq6-27');
168 $dataOptions->addProperty($leaf);
169 $leaf = new TextPropertyItem();
170 $leaf->setName("data_continued_caption");
171 $leaf->setText(__('Table caption (continued)'));
172 $leaf->setDoc('faq6-27');
173 $dataOptions->addProperty($leaf);
174 $leaf = new TextPropertyItem();
175 $leaf->setName("data_label");
176 $leaf->setText(__('Label key'));
177 $leaf->setDoc('faq6-27');
178 $dataOptions->addProperty($leaf);
179 $leaf = new TextPropertyItem();
180 $leaf->setName('null');
181 $leaf->setText(__('Replace NULL with:'));
182 $dataOptions->addProperty($leaf);
183 // add the main group to the root group
184 $exportSpecificOptions->addProperty($dataOptions);
186 // set the options for the export plugin property item
187 $exportPluginProperties->setOptions($exportSpecificOptions);
188 $this->properties = $exportPluginProperties;
192 * This method is called when any PluginManager to which the observer
193 * is attached calls PluginManager::notify()
195 * @param SplSubject $subject The PluginManager notifying the observer
196 * of an update.
198 * @return void
200 public function update (SplSubject $subject)
205 * Outputs export header
207 * @return bool Whether it succeeded
209 public function exportHeader ()
211 global $crlf;
212 global $cfg;
214 $head = '% phpMyAdmin LaTeX Dump' . $crlf
215 . '% version ' . PMA_VERSION . $crlf
216 . '% http://www.phpmyadmin.net' . $crlf
217 . '%' . $crlf
218 . '% ' . __('Host') . ': ' . $cfg['Server']['host'];
219 if (! empty($cfg['Server']['port'])) {
220 $head .= ':' . $cfg['Server']['port'];
222 $head .= $crlf
223 . '% ' . __('Generation Time') . ': '
224 . PMA_Util::localisedDate() . $crlf
225 . '% ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
226 . '% ' . __('PHP Version') . ': ' . phpversion() . $crlf;
227 return PMA_exportOutputHandler($head);
231 * Outputs export footer
233 * @return bool Whether it succeeded
235 public function exportFooter ()
237 return true;
241 * Outputs database header
243 * @param string $db Database name
245 * @return bool Whether it succeeded
247 public function exportDBHeader ($db)
249 global $crlf;
250 $head = '% ' . $crlf
251 . '% ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
252 . '% ' . $crlf;
253 return PMA_exportOutputHandler($head);
257 * Outputs database footer
259 * @param string $db Database name
261 * @return bool Whether it succeeded
263 public function exportDBFooter ($db)
265 return true;
269 * Outputs CREATE DATABASE statement
271 * @param string $db Database name
273 * @return bool Whether it succeeded
275 public function exportDBCreate($db)
277 return true;
281 * Outputs the content of a table in JSON format
283 * @param string $db database name
284 * @param string $table table name
285 * @param string $crlf the end of line sequence
286 * @param string $error_url the url to go back in case of error
287 * @param string $sql_query SQL query for obtaining data
289 * @return bool Whether it succeeded
291 public function exportData($db, $table, $crlf, $error_url, $sql_query)
293 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
295 $columns_cnt = PMA_DBI_num_fields($result);
296 for ($i = 0; $i < $columns_cnt; $i++) {
297 $columns[$i] = PMA_DBI_field_name($result, $i);
299 unset($i);
301 $buffer = $crlf . '%' . $crlf . '% ' . __('Data') . ': ' . $table
302 . $crlf . '%' . $crlf . ' \\begin{longtable}{|';
304 for ($index = 0; $index < $columns_cnt; $index++) {
305 $buffer .= 'l|';
307 $buffer .= '} ' . $crlf ;
309 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
310 if (isset($GLOBALS['latex_caption'])) {
311 $buffer .= ' \\caption{'
312 . PMA_Util::expandUserString(
313 $GLOBALS['latex_data_caption'],
314 array(
315 'texEscape',
316 get_class($this),
317 'libraries/plugins/export/' . get_class($this) . ".class.php"
319 array('table' => $table, 'database' => $db)
321 . '} \\label{'
322 . PMA_Util::expandUserString(
323 $GLOBALS['latex_data_label'],
324 null,
325 array('table' => $table, 'database' => $db)
327 . '} \\\\';
329 if (! PMA_exportOutputHandler($buffer)) {
330 return false;
333 // show column names
334 if (isset($GLOBALS['latex_columns'])) {
335 $buffer = '\\hline ';
336 for ($i = 0; $i < $columns_cnt; $i++) {
337 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{'
338 . self::texEscape(stripslashes($columns[$i])) . '}} & ';
341 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
342 if (! PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
343 return false;
345 if (isset($GLOBALS['latex_caption'])) {
346 if (! PMA_exportOutputHandler(
347 '\\caption{'
348 . PMA_Util::expandUserString(
349 $GLOBALS['latex_data_continued_caption'],
350 array(
351 'texEscape',
352 get_class($this),
353 'libraries/plugins/export/'
354 . get_class($this) . ".class.php"
356 array('table' => $table, 'database' => $db)
358 . '} \\\\ '
359 )) {
360 return false;
363 if (! PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
364 return false;
366 } else {
367 if (! PMA_exportOutputHandler('\\\\ \hline')) {
368 return false;
372 // print the whole table
373 while ($record = PMA_DBI_fetch_assoc($result)) {
374 $buffer = '';
375 // print each row
376 for ($i = 0; $i < $columns_cnt; $i++) {
377 if ((! function_exists('is_null')
378 || ! is_null($record[$columns[$i]]))
379 && isset($record[$columns[$i]])
381 $column_value = self::texEscape(
382 stripslashes($record[$columns[$i]])
384 } else {
385 $column_value = $GLOBALS['latex_null'];
388 // last column ... no need for & character
389 if ($i == ($columns_cnt - 1)) {
390 $buffer .= $column_value;
391 } else {
392 $buffer .= $column_value . " & ";
395 $buffer .= ' \\\\ \\hline ' . $crlf;
396 if (! PMA_exportOutputHandler($buffer)) {
397 return false;
401 $buffer = ' \\end{longtable}' . $crlf;
402 if (! PMA_exportOutputHandler($buffer)) {
403 return false;
406 PMA_DBI_free_result($result);
407 return true;
408 } // end getTableLaTeX
411 * Outputs table's structure
413 * @param string $db database name
414 * @param string $table table name
415 * @param string $crlf the end of line sequence
416 * @param string $error_url the url to go back in case of error
417 * @param string $export_mode 'create_table', 'triggers', 'create_view',
418 * 'stand_in'
419 * @param string $export_type 'server', 'database', 'table'
420 * @param bool $do_relation whether to include relation comments
421 * @param bool $do_comments whether to include the pmadb-style column
422 * comments as comments in the structure;
423 * this is deprecated but the parameter is
424 * left here because export.php calls
425 * exportStructure() also for other
426 * export types which use this parameter
427 * @param bool $do_mime whether to include mime comments
428 * @param bool $dates whether to include creation/update/check dates
430 * @return bool Whether it succeeded
432 public function exportStructure(
433 $db,
434 $table,
435 $crlf,
436 $error_url,
437 $export_mode,
438 $export_type,
439 $do_relation = false,
440 $do_comments = false,
441 $do_mime = false,
442 $dates = false
444 global $cfgRelation;
446 /* We do not export triggers */
447 if ($export_mode == 'triggers') {
448 return true;
452 * Get the unique keys in the table
454 $unique_keys = array();
455 $keys = PMA_DBI_get_table_indexes($db, $table);
456 foreach ($keys as $key) {
457 if ($key['Non_unique'] == 0) {
458 $unique_keys[] = $key['Column_name'];
463 * Gets fields properties
465 PMA_DBI_select_db($db);
467 // Check if we can use Relations
468 if ($do_relation && ! empty($cfgRelation['relation'])) {
469 // Find which tables are related with the current one and write it in
470 // an array
471 $res_rel = PMA_getForeigners($db, $table);
473 if ($res_rel && count($res_rel) > 0) {
474 $have_rel = true;
475 } else {
476 $have_rel = false;
478 } else {
479 $have_rel = false;
480 } // end if
483 * Displays the table structure
485 $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table
486 . $crlf . '%' . $crlf . ' \\begin{longtable}{';
487 if (! PMA_exportOutputHandler($buffer)) {
488 return false;
491 $columns_cnt = 4;
492 $alignment = '|l|c|c|c|';
493 if ($do_relation && $have_rel) {
494 $columns_cnt++;
495 $alignment .= 'l|';
497 if ($do_comments) {
498 $columns_cnt++;
499 $alignment .= 'l|';
501 if ($do_mime && $cfgRelation['mimework']) {
502 $columns_cnt++;
503 $alignment .='l|';
505 $buffer = $alignment . '} ' . $crlf ;
507 $header = ' \\hline ';
508 $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column')
509 . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type')
510 . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null')
511 . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
512 if ($do_relation && $have_rel) {
513 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
515 if ($do_comments) {
516 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
517 $comments = PMA_getComments($db, $table);
519 if ($do_mime && $cfgRelation['mimework']) {
520 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
521 $mime_map = PMA_getMIME($db, $table, true);
524 // Table caption for first page and label
525 if (isset($GLOBALS['latex_caption'])) {
526 $buffer .= ' \\caption{'
527 . PMA_Util::expandUserString(
528 $GLOBALS['latex_structure_caption'],
529 array(
530 'texEscape',
531 get_class($this),
532 'libraries/plugins/export/' . get_class($this) . ".class.php"
534 array('table' => $table, 'database' => $db)
536 . '} \\label{'
537 . PMA_Util::expandUserString(
538 $GLOBALS['latex_structure_label'],
539 null,
540 array('table' => $table, 'database' => $db)
542 . '} \\\\' . $crlf;
544 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf
545 . '\\endfirsthead' . $crlf;
546 // Table caption on next pages
547 if (isset($GLOBALS['latex_caption'])) {
548 $buffer .= ' \\caption{'
549 . PMA_Util::expandUserString(
550 $GLOBALS['latex_structure_continued_caption'],
551 array(
552 'texEscape',
553 get_class($this),
554 'libraries/plugins/export/' . get_class($this) . ".class.php"
556 array('table' => $table, 'database' => $db)
558 . '} \\\\ ' . $crlf;
560 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
562 if (! PMA_exportOutputHandler($buffer)) {
563 return false;
566 $fields = PMA_DBI_get_columns($db, $table);
567 foreach ($fields as $row) {
568 $extracted_columnspec
569 = PMA_Util::extractColumnSpec(
570 $row['Type']
572 $type = $extracted_columnspec['print_type'];
573 if (empty($type)) {
574 $type = ' ';
577 if (! isset($row['Default'])) {
578 if ($row['Null'] != 'NO') {
579 $row['Default'] = 'NULL';
583 $field_name = $row['Field'];
585 $local_buffer = $field_name . "\000" . $type . "\000"
586 . (($row['Null'] == '' || $row['Null'] == 'NO')
587 ? __('No') : __('Yes'))
588 . "\000" . (isset($row['Default']) ? $row['Default'] : '');
590 if ($do_relation && $have_rel) {
591 $local_buffer .= "\000";
592 if (isset($res_rel[$field_name])) {
593 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' ('
594 . $res_rel[$field_name]['foreign_field'] . ')';
597 if ($do_comments && $cfgRelation['commwork']) {
598 $local_buffer .= "\000";
599 if (isset($comments[$field_name])) {
600 $local_buffer .= $comments[$field_name];
603 if ($do_mime && $cfgRelation['mimework']) {
604 $local_buffer .= "\000";
605 if (isset($mime_map[$field_name])) {
606 $local_buffer .= str_replace(
607 '_',
608 '/',
609 $mime_map[$field_name]['mimetype']
613 $local_buffer = self::texEscape($local_buffer);
614 if ($row['Key']=='PRI') {
615 $pos=strpos($local_buffer, "\000");
616 $local_buffer = '\\textit{'
617 . substr($local_buffer, 0, $pos)
618 . '}' . substr($local_buffer, $pos);
620 if (in_array($field_name, $unique_keys)) {
621 $pos=strpos($local_buffer, "\000");
622 $local_buffer = '\\textbf{'
623 . substr($local_buffer, 0, $pos)
624 . '}' . substr($local_buffer, $pos);
626 $buffer = str_replace("\000", ' & ', $local_buffer);
627 $buffer .= ' \\\\ \\hline ' . $crlf;
629 if (! PMA_exportOutputHandler($buffer)) {
630 return false;
632 } // end while
634 $buffer = ' \\end{longtable}' . $crlf;
635 return PMA_exportOutputHandler($buffer);
636 } // end of the 'exportStructure' method
639 * Escapes some special characters for use in TeX/LaTeX
641 * @param string $string the string to convert
643 * @return string the converted string with escape codes
645 public static function texEscape($string)
647 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
648 $cnt_escape = count($escape);
649 for ($k = 0; $k < $cnt_escape; $k++) {
650 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
652 return $string;