Plot functionality using Hihcharts
[phpmyadmin/ammaryasirr.git] / libraries / export / latex.php
blob9709dc4607ba8e97e6aca2ea8e63ddc0ad15b93e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build dumps of tables
6 * @package phpMyAdmin-Export-Latex
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /* Messages used in default captions */
13 $GLOBALS['strLatexContent'] = __('Content of table @TABLE@');
14 $GLOBALS['strLatexContinued'] = __('(continued)');
15 $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@');
17 /**
20 if (isset($plugin_list)) {
21 $hide_structure = false;
22 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
23 $hide_structure = true;
25 $plugin_list['latex'] = array(
26 'text' => __('LaTeX'),
27 'extension' => 'tex',
28 'mime_type' => 'application/x-tex',
29 'options' => array(
30 array('type' => 'begin_group', 'name' => 'general_opts'),
31 array('type' => 'bool', 'name' => 'caption', 'text' => __('Include table caption')),
32 array('type' => 'end_group')
34 'options_text' => __('Options'),
37 /* what to dump (structure/data/both) */
38 $plugin_list['latex']['options'][] =
39 array('type' => 'begin_group', 'name' => 'dump_what', 'text' => __('Dump table'));
40 $plugin_list['latex']['options'][] =
41 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')));
42 $plugin_list['latex']['options'][] = array('type' => 'end_group');
44 /* Structure options */
45 if (!$hide_structure) {
46 $plugin_list['latex']['options'][] =
47 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options'), 'force' => 'data');
48 $plugin_list['latex']['options'][] =
49 array('type' => 'text', 'name' => 'structure_caption', 'text' => __('Table caption'), 'doc' => 'faq6_27');
50 $plugin_list['latex']['options'][] =
51 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => __('Table caption (continued)'), 'doc' => 'faq6_27');
52 $plugin_list['latex']['options'][] =
53 array('type' => 'text', 'name' => 'structure_label', 'text' => __('Label key'), 'doc' => 'faq6_27');
54 if (!empty($GLOBALS['cfgRelation']['relation'])) {
55 $plugin_list['latex']['options'][] =
56 array('type' => 'bool', 'name' => 'relation', 'text' => __('Display foreign key relationships'));
58 $plugin_list['latex']['options'][] =
59 array('type' => 'bool', 'name' => 'comments', 'text' => __('Display comments'));
60 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
61 $plugin_list['latex']['options'][] =
62 array('type' => 'bool', 'name' => 'mime', 'text' => __('Display MIME types'));
64 $plugin_list['latex']['options'][] =
65 array('type' => 'end_group');
67 /* Data */
68 $plugin_list['latex']['options'][] =
69 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure');
70 $plugin_list['latex']['options'][] =
71 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row'));
72 $plugin_list['latex']['options'][] =
73 array('type' => 'text', 'name' => 'data_caption', 'text' => __('Table caption'), 'doc' => 'faq6_27');
74 $plugin_list['latex']['options'][] =
75 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => __('Table caption (continued)'), 'doc' => 'faq6_27');
76 $plugin_list['latex']['options'][] =
77 array('type' => 'text', 'name' => 'data_label', 'text' => __('Label key'), 'doc' => 'faq6_27');
78 $plugin_list['latex']['options'][] =
79 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:'));
80 $plugin_list['latex']['options'][] =
81 array('type' => 'end_group');
82 } else {
84 /**
85 * Escapes some special characters for use in TeX/LaTeX
87 * @param string the string to convert
89 * @return string the converted string with escape codes
91 * @access private
93 function PMA_texEscape($string) {
94 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
95 $cnt_escape = count($escape);
96 for ($k=0; $k < $cnt_escape; $k++) {
97 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
99 return $string;
103 * Outputs comment
105 * @param string Text of comment
107 * @return bool Whether it suceeded
109 function PMA_exportComment($text) {
110 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
114 * Outputs export footer
116 * @return bool Whether it suceeded
118 * @access public
120 function PMA_exportFooter() {
121 return true;
125 * Outputs export header
127 * @return bool Whether it suceeded
129 * @access public
131 function PMA_exportHeader() {
132 global $crlf;
133 global $cfg;
135 $head = '% phpMyAdmin LaTeX Dump' . $crlf
136 . '% version ' . PMA_VERSION . $crlf
137 . '% http://www.phpmyadmin.net' . $crlf
138 . '%' . $crlf
139 . '% ' . __('Host') . ': ' . $cfg['Server']['host'];
140 if (!empty($cfg['Server']['port'])) {
141 $head .= ':' . $cfg['Server']['port'];
143 $head .= $crlf
144 . '% ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
145 . '% ' . __('Server version') . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
146 . '% ' . __('PHP Version') . ': ' . phpversion() . $crlf;
147 return PMA_exportOutputHandler($head);
151 * Outputs database header
153 * @param string Database name
155 * @return bool Whether it suceeded
157 * @access public
159 function PMA_exportDBHeader($db) {
160 global $crlf;
161 $head = '% ' . $crlf
162 . '% ' . __('Database') . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
163 . '% ' . $crlf;
164 return PMA_exportOutputHandler($head);
168 * Outputs database footer
170 * @param string Database name
172 * @return bool Whether it suceeded
174 * @access public
176 function PMA_exportDBFooter($db) {
177 return true;
181 * Outputs create database database
183 * @param string Database name
185 * @return bool Whether it suceeded
187 * @access public
189 function PMA_exportDBCreate($db) {
190 return true;
194 * Outputs the content of a table in LaTeX table/sideways table environment
196 * @param string the database name
197 * @param string the table name
198 * @param string the end of line sequence
199 * @param string the url to go back in case of error
200 * @param string SQL query for obtaining data
202 * @return bool Whether it suceeded
204 * @access public
206 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
207 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
209 $columns_cnt = PMA_DBI_num_fields($result);
210 for ($i = 0; $i < $columns_cnt; $i++) {
211 $columns[$i] = PMA_DBI_field_name($result, $i);
213 unset($i);
215 $buffer = $crlf . '%' . $crlf . '% ' . __('Data') . ': ' . $table . $crlf . '%' . $crlf
216 . ' \\begin{longtable}{|';
218 for ($index=0;$index<$columns_cnt;$index++) {
219 $buffer .= 'l|';
221 $buffer .= '} ' . $crlf ;
223 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
224 if (isset($GLOBALS['latex_caption'])) {
225 $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_data_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
226 . '} \\label{' . PMA_expandUserString($GLOBALS['latex_data_label'], NULL, array('table' => $table, 'database' => $db)) . '} \\\\';
228 if (!PMA_exportOutputHandler($buffer)) {
229 return false;
232 // show column names
233 if (isset($GLOBALS['latex_columns'])) {
234 $buffer = '\\hline ';
235 for ($i = 0; $i < $columns_cnt; $i++) {
236 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
239 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
240 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
241 return false;
243 if (isset($GLOBALS['latex_caption'])) {
244 if (!PMA_exportOutputHandler('\\caption{' . PMA_expandUserString($GLOBALS['latex_data_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\\\ ')) return false;
246 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
247 return false;
249 } else {
250 if (!PMA_exportOutputHandler('\\\\ \hline')) {
251 return false;
255 // print the whole table
256 while ($record = PMA_DBI_fetch_assoc($result)) {
258 $buffer = '';
259 // print each row
260 for ($i = 0; $i < $columns_cnt; $i++) {
261 if (isset($record[$columns[$i]])
262 && (! function_exists('is_null') || !is_null($record[$columns[$i]]))) {
263 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
264 } else {
265 $column_value = $GLOBALS['latex_null'];
268 // last column ... no need for & character
269 if ($i == ($columns_cnt - 1)) {
270 $buffer .= $column_value;
271 } else {
272 $buffer .= $column_value . " & ";
275 $buffer .= ' \\\\ \\hline ' . $crlf;
276 if (!PMA_exportOutputHandler($buffer)) {
277 return false;
281 $buffer = ' \\end{longtable}' . $crlf;
282 if (!PMA_exportOutputHandler($buffer)) {
283 return false;
286 PMA_DBI_free_result($result);
287 return true;
289 } // end getTableLaTeX
292 * Returns $table's structure as LaTeX
294 * @param string the database name
295 * @param string the table name
296 * @param string the end of line sequence
297 * @param string the url to go back in case of error
298 * @param boolean whether to include relation comments
299 * @param boolean whether to include column comments
300 * @param boolean whether to include mime comments
301 * @param string future feature: support view dependencies
303 * @return bool Whether it suceeded
305 * @access public
307 // @@@ Table structure
308 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
310 global $cfgRelation;
313 * Get the unique keys in the table
315 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
316 $keys_result = PMA_DBI_query($keys_query);
317 $unique_keys = array();
318 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
319 if ($key['Non_unique'] == 0) {
320 $unique_keys[] = $key['Column_name'];
323 PMA_DBI_free_result($keys_result);
326 * Gets fields properties
328 PMA_DBI_select_db($db);
329 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
330 $result = PMA_DBI_query($local_query);
331 $fields_cnt = PMA_DBI_num_rows($result);
333 // Check if we can use Relations
334 if ($do_relation && !empty($cfgRelation['relation'])) {
335 // Find which tables are related with the current one and write it in
336 // an array
337 $res_rel = PMA_getForeigners($db, $table);
339 if ($res_rel && count($res_rel) > 0) {
340 $have_rel = true;
341 } else {
342 $have_rel = false;
344 } else {
345 $have_rel = false;
346 } // end if
349 * Displays the table structure
351 $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table . $crlf . '%' . $crlf
352 . ' \\begin{longtable}{';
353 if (!PMA_exportOutputHandler($buffer)) {
354 return false;
357 $columns_cnt = 4;
358 $alignment = '|l|c|c|c|';
359 if ($do_relation && $have_rel) {
360 $columns_cnt++;
361 $alignment .= 'l|';
363 if ($do_comments) {
364 $columns_cnt++;
365 $alignment .= 'l|';
367 if ($do_mime && $cfgRelation['mimework']) {
368 $columns_cnt++;
369 $alignment .='l|';
371 $buffer = $alignment . '} ' . $crlf ;
373 $header = ' \\hline ';
374 $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
375 if ($do_relation && $have_rel) {
376 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
378 if ($do_comments) {
379 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
380 $comments = PMA_getComments($db, $table);
382 if ($do_mime && $cfgRelation['mimework']) {
383 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
384 $mime_map = PMA_getMIME($db, $table, true);
387 $local_buffer = PMA_texEscape($table);
389 // Table caption for first page and label
390 if (isset($GLOBALS['latex_caption'])) {
391 $buffer .= ' \\caption{'. PMA_expandUserString($GLOBALS['latex_structure_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
392 . '} \\label{' . PMA_expandUserString($GLOBALS['latex_structure_label'], NULL, array('table' => $table, 'database' => $db))
393 . '} \\\\' . $crlf;
395 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
396 // Table caption on next pages
397 if (isset($GLOBALS['latex_caption'])) {
398 $buffer .= ' \\caption{'. PMA_expandUserString($GLOBALS['latex_structure_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
399 . '} \\\\ ' . $crlf;
401 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
403 if (!PMA_exportOutputHandler($buffer)) {
404 return false;
407 while ($row = PMA_DBI_fetch_assoc($result)) {
409 $type = $row['Type'];
410 // reformat mysql query output
411 // set or enum types: slashes single quotes inside options
412 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
413 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
414 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
415 $type_nowrap = '';
417 $binary = 0;
418 $unsigned = 0;
419 $zerofill = 0;
420 } else {
421 $type_nowrap = ' nowrap="nowrap"';
422 $type = preg_replace('/BINARY/i', '', $type);
423 $type = preg_replace('/ZEROFILL/i', '', $type);
424 $type = preg_replace('/UNSIGNED/i', '', $type);
425 if (empty($type)) {
426 $type = '&nbsp;';
429 $binary = preg_match('/BINARY/i', $row['Type']);
430 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
431 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
433 if (!isset($row['Default'])) {
434 if ($row['Null'] != 'NO') {
435 $row['Default'] = 'NULL';
437 } else {
438 $row['Default'] = $row['Default'];
441 $field_name = $row['Field'];
443 $local_buffer = $field_name . "\000" . $type . "\000"
444 . (($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'))
445 . "\000" . (isset($row['Default']) ? $row['Default'] : '');
447 if ($do_relation && $have_rel) {
448 $local_buffer .= "\000";
449 if (isset($res_rel[$field_name])) {
450 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
453 if ($do_comments && $cfgRelation['commwork']) {
454 $local_buffer .= "\000";
455 if (isset($comments[$field_name])) {
456 $local_buffer .= $comments[$field_name];
459 if ($do_mime && $cfgRelation['mimework']) {
460 $local_buffer .= "\000";
461 if (isset($mime_map[$field_name])) {
462 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
465 $local_buffer = PMA_texEscape($local_buffer);
466 if ($row['Key']=='PRI') {
467 $pos=strpos($local_buffer, "\000");
468 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
470 if (in_array($field_name, $unique_keys)) {
471 $pos=strpos($local_buffer, "\000");
472 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
474 $buffer = str_replace("\000", ' & ', $local_buffer);
475 $buffer .= ' \\\\ \\hline ' . $crlf;
477 if (!PMA_exportOutputHandler($buffer)) {
478 return false;
480 } // end while
481 PMA_DBI_free_result($result);
483 $buffer = ' \\end{longtable}' . $crlf;
484 return PMA_exportOutputHandler($buffer);
485 } // end of the 'PMA_exportStructure' function
487 } // end else