2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build dumps of tables
6 * @package phpMyAdmin-Export-Latex
9 if (! defined('PHPMYADMIN')) {
16 if (isset($plugin_list)) {
17 $hide_structure = false;
18 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
19 $hide_structure = true;
21 $plugin_list['latex'] = array(
24 'mime_type' => 'application/x-tex',
26 array('type' => 'bool', 'name' => 'caption', 'text' => 'strLatexIncludeCaption'),
28 'options_text' => 'strOptions',
30 /* Structure options */
31 if (!$hide_structure) {
32 $plugin_list['latex']['options'][] =
33 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
34 $plugin_list['latex']['options'][] =
35 array('type' => 'text', 'name' => 'structure_caption', 'text' => 'strLatexCaption');
36 $plugin_list['latex']['options'][] =
37 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => 'strLatexContinuedCaption');
38 $plugin_list['latex']['options'][] =
39 array('type' => 'text', 'name' => 'structure_label', 'text' => 'strLatexLabel');
40 if (!empty($GLOBALS['cfgRelation']['relation'])) {
41 $plugin_list['latex']['options'][] =
42 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
44 $plugin_list['latex']['options'][] =
45 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
46 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
47 $plugin_list['latex']['options'][] =
48 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
50 $plugin_list['latex']['options'][] =
51 array('type' => 'egroup');
54 $plugin_list['latex']['options'][] =
55 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
56 $plugin_list['latex']['options'][] =
57 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
58 $plugin_list['latex']['options'][] =
59 array('type' => 'text', 'name' => 'data_caption', 'text' => 'strLatexCaption');
60 $plugin_list['latex']['options'][] =
61 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => 'strLatexContinuedCaption');
62 $plugin_list['latex']['options'][] =
63 array('type' => 'text', 'name' => 'data_label', 'text' => 'strLatexLabel');
64 $plugin_list['latex']['options'][] =
65 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
66 $plugin_list['latex']['options'][] =
67 array('type' => 'egroup');
71 * Escapes some special characters for use in TeX/LaTeX
73 * @param string the string to convert
75 * @return string the converted string with escape codes
79 function PMA_texEscape($string) {
80 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
81 $cnt_escape = count($escape);
82 for ($k=0; $k < $cnt_escape; $k++
) {
83 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
91 * @param string Text of comment
93 * @return bool Whether it suceeded
95 function PMA_exportComment($text) {
96 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
100 * Outputs export footer
102 * @return bool Whether it suceeded
106 function PMA_exportFooter() {
111 * Outputs export header
113 * @return bool Whether it suceeded
117 function PMA_exportHeader() {
121 $head = '% phpMyAdmin LaTeX Dump' . $crlf
122 . '% version ' . PMA_VERSION
. $crlf
123 . '% http://www.phpmyadmin.net' . $crlf
125 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
126 if (!empty($cfg['Server']['port'])) {
127 $head .= ':' . $cfg['Server']['port'];
130 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
131 . '% ' . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 3) . $crlf
132 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
133 return PMA_exportOutputHandler($head);
137 * Outputs database header
139 * @param string Database name
141 * @return bool Whether it suceeded
145 function PMA_exportDBHeader($db) {
148 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''). $crlf
150 return PMA_exportOutputHandler($head);
154 * Outputs database footer
156 * @param string Database name
158 * @return bool Whether it suceeded
162 function PMA_exportDBFooter($db) {
167 * Outputs create database database
169 * @param string Database name
171 * @return bool Whether it suceeded
175 function PMA_exportDBCreate($db) {
180 * Outputs the content of a table in LaTeX table/sideways table environment
182 * @param string the database name
183 * @param string the table name
184 * @param string the end of line sequence
185 * @param string the url to go back in case of error
186 * @param string SQL query for obtaining data
188 * @return bool Whether it suceeded
192 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
193 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
195 $columns_cnt = PMA_DBI_num_fields($result);
196 for ($i = 0; $i < $columns_cnt; $i++
) {
197 $columns[$i] = PMA_DBI_field_name($result, $i);
201 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
202 . ' \\begin{longtable}{|';
204 for ($index=0;$index<$columns_cnt;$index++
) {
207 $buffer .= '} ' . $crlf ;
209 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
210 if (isset($GLOBALS['latex_caption'])) {
211 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
212 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
214 if (!PMA_exportOutputHandler($buffer)) {
219 if (isset($GLOBALS['latex_columns'])) {
220 $buffer = '\\hline ';
221 for ($i = 0; $i < $columns_cnt; $i++
) {
222 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
225 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
226 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
229 if (isset($GLOBALS['latex_caption'])) {
230 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
232 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
236 if (!PMA_exportOutputHandler('\\\\ \hline')) {
241 // print the whole table
242 while ($record = PMA_DBI_fetch_assoc($result)) {
246 for ($i = 0; $i < $columns_cnt; $i++
) {
247 if (isset($record[$columns[$i]])
248 && (! function_exists('is_null') ||
!is_null($record[$columns[$i]]))) {
249 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
251 $column_value = $GLOBALS['latex_null'];
254 // last column ... no need for & character
255 if ($i == ($columns_cnt - 1)) {
256 $buffer .= $column_value;
258 $buffer .= $column_value . " & ";
261 $buffer .= ' \\\\ \\hline ' . $crlf;
262 if (!PMA_exportOutputHandler($buffer)) {
267 $buffer = ' \\end{longtable}' . $crlf;
268 if (!PMA_exportOutputHandler($buffer)) {
272 PMA_DBI_free_result($result);
275 } // end getTableLaTeX
278 * Returns $table's structure as LaTeX
280 * @param string the database name
281 * @param string the table name
282 * @param string the end of line sequence
283 * @param string the url to go back in case of error
284 * @param boolean whether to include relation comments
285 * @param boolean whether to include column comments
286 * @param boolean whether to include mime comments
287 * @param string future feature: support view dependencies
289 * @return bool Whether it suceeded
293 // @@@ $strTableStructure
294 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
299 * Get the unique keys in the table
301 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
302 $keys_result = PMA_DBI_query($keys_query);
303 $unique_keys = array();
304 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
305 if ($key['Non_unique'] == 0) {
306 $unique_keys[] = $key['Column_name'];
309 PMA_DBI_free_result($keys_result);
312 * Gets fields properties
314 PMA_DBI_select_db($db);
315 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
316 $result = PMA_DBI_query($local_query);
317 $fields_cnt = PMA_DBI_num_rows($result);
319 // Check if we can use Relations (Mike Beck)
320 if ($do_relation && !empty($cfgRelation['relation'])) {
321 // Find which tables are related with the current one and write it in
323 $res_rel = PMA_getForeigners($db, $table);
325 if ($res_rel && count($res_rel) > 0) {
335 * Displays the table structure
337 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
338 . ' \\begin{longtable}{';
339 if (!PMA_exportOutputHandler($buffer)) {
344 $alignment = '|l|c|c|c|';
345 if ($do_relation && $have_rel) {
353 if ($do_mime && $cfgRelation['mimework']) {
357 $buffer = $alignment . '} ' . $crlf ;
359 $header = ' \\hline ';
360 $header .= '\\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strField'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strType'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strNull'] . '}} & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strDefault'] . '}}';
361 if ($do_relation && $have_rel) {
362 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
365 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
366 $comments = PMA_getComments($db, $table);
368 if ($do_mime && $cfgRelation['mimework']) {
369 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
370 $mime_map = PMA_getMIME($db, $table, true);
373 $local_buffer = PMA_texEscape($table);
375 // Table caption for first page and label
376 if (isset($GLOBALS['latex_caption'])) {
377 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
378 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
381 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
382 // Table caption on next pages
383 if (isset($GLOBALS['latex_caption'])) {
384 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
387 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
389 if (!PMA_exportOutputHandler($buffer)) {
393 while ($row = PMA_DBI_fetch_assoc($result)) {
395 $type = $row['Type'];
396 // reformat mysql query output - staybyte - 9. June 2001
397 // loic1: set or enum types: slashes single quotes inside options
398 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
399 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
400 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
407 $type_nowrap = ' nowrap="nowrap"';
408 $type = preg_replace('/BINARY/i', '', $type);
409 $type = preg_replace('/ZEROFILL/i', '', $type);
410 $type = preg_replace('/UNSIGNED/i', '', $type);
415 $binary = preg_match('/BINARY/i', $row['Type']);
416 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
417 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
419 if (!isset($row['Default'])) {
420 if ($row['Null'] != 'NO') {
421 $row['Default'] = 'NULL';
424 $row['Default'] = $row['Default'];
427 $field_name = $row['Field'];
429 $local_buffer = $field_name . "\000" . $type . "\000"
430 . (($row['Null'] == '' ||
$row['Null'] == 'NO') ?
$GLOBALS['strNo'] : $GLOBALS['strYes'])
431 . "\000" . (isset($row['Default']) ?
$row['Default'] : '');
433 if ($do_relation && $have_rel) {
434 $local_buffer .= "\000";
435 if (isset($res_rel[$field_name])) {
436 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
439 if ($do_comments && $cfgRelation['commwork']) {
440 $local_buffer .= "\000";
441 if (isset($comments[$field_name])) {
442 $local_buffer .= $comments[$field_name];
445 if ($do_mime && $cfgRelation['mimework']) {
446 $local_buffer .= "\000";
447 if (isset($mime_map[$field_name])) {
448 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
451 $local_buffer = PMA_texEscape($local_buffer);
452 if ($row['Key']=='PRI') {
453 $pos=strpos($local_buffer, "\000");
454 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
456 if (in_array($field_name, $unique_keys)) {
457 $pos=strpos($local_buffer, "\000");
458 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
460 $buffer = str_replace("\000", ' & ', $local_buffer);
461 $buffer .= ' \\\\ \\hline ' . $crlf;
463 if (!PMA_exportOutputHandler($buffer)) {
467 PMA_DBI_free_result($result);
469 $buffer = ' \\end{longtable}' . $crlf;
470 return PMA_exportOutputHandler($buffer);
471 } // end of the 'PMA_exportStructure' function