2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build dumps of tables
8 if (! defined('PHPMYADMIN')) {
15 if (isset($plugin_list)) {
16 $hide_structure = false;
17 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
18 $hide_structure = true;
20 $plugin_list['latex'] = array(
23 'mime_type' => 'application/x-tex',
25 array('type' => 'bool', 'name' => 'caption', 'text' => 'strLatexIncludeCaption'),
27 'options_text' => 'strOptions',
29 /* Structure options */
30 if (!$hide_structure) {
31 $plugin_list['latex']['options'][] =
32 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
33 $plugin_list['latex']['options'][] =
34 array('type' => 'text', 'name' => 'structure_caption', 'text' => 'strLatexCaption');
35 $plugin_list['latex']['options'][] =
36 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => 'strLatexContinuedCaption');
37 $plugin_list['latex']['options'][] =
38 array('type' => 'text', 'name' => 'structure_label', 'text' => 'strLatexLabel');
39 if (!empty($GLOBALS['cfgRelation']['relation'])) {
40 $plugin_list['latex']['options'][] =
41 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
43 $plugin_list['latex']['options'][] =
44 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
45 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
46 $plugin_list['latex']['options'][] =
47 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
49 $plugin_list['latex']['options'][] =
50 array('type' => 'egroup');
53 $plugin_list['latex']['options'][] =
54 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
55 $plugin_list['latex']['options'][] =
56 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
57 $plugin_list['latex']['options'][] =
58 array('type' => 'text', 'name' => 'data_caption', 'text' => 'strLatexCaption');
59 $plugin_list['latex']['options'][] =
60 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => 'strLatexContinuedCaption');
61 $plugin_list['latex']['options'][] =
62 array('type' => 'text', 'name' => 'data_label', 'text' => 'strLatexLabel');
63 $plugin_list['latex']['options'][] =
64 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
65 $plugin_list['latex']['options'][] =
66 array('type' => 'egroup');
70 * Escapes some special characters for use in TeX/LaTeX
72 * @param string the string to convert
74 * @return string the converted string with escape codes
78 function PMA_texEscape($string) {
79 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
80 $cnt_escape = count($escape);
81 for ($k=0; $k < $cnt_escape; $k++
) {
82 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
90 * @param string Text of comment
92 * @return bool Whether it suceeded
94 function PMA_exportComment($text) {
95 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
99 * Outputs export footer
101 * @return bool Whether it suceeded
105 function PMA_exportFooter() {
110 * Outputs export header
112 * @return bool Whether it suceeded
116 function PMA_exportHeader() {
120 $head = '% phpMyAdmin LaTeX Dump' . $crlf
121 . '% version ' . PMA_VERSION
. $crlf
122 . '% http://www.phpmyadmin.net' . $crlf
124 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
125 if (!empty($cfg['Server']['port'])) {
126 $head .= ':' . $cfg['Server']['port'];
129 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
130 . '% ' . $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
131 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
132 return PMA_exportOutputHandler($head);
136 * Outputs database header
138 * @param string Database name
140 * @return bool Whether it suceeded
144 function PMA_exportDBHeader($db) {
147 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''). $crlf
149 return PMA_exportOutputHandler($head);
153 * Outputs database footer
155 * @param string Database name
157 * @return bool Whether it suceeded
161 function PMA_exportDBFooter($db) {
166 * Outputs create database database
168 * @param string Database name
170 * @return bool Whether it suceeded
174 function PMA_exportDBCreate($db) {
179 * Outputs the content of a table in LaTeX table/sideways table environment
181 * @param string the database name
182 * @param string the table name
183 * @param string the end of line sequence
184 * @param string the url to go back in case of error
185 * @param string SQL query for obtaining data
187 * @return bool Whether it suceeded
191 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
192 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
194 $columns_cnt = PMA_DBI_num_fields($result);
195 for ($i = 0; $i < $columns_cnt; $i++
) {
196 $columns[$i] = PMA_DBI_field_name($result, $i);
200 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
201 . ' \\begin{longtable}{|';
203 for ($index=0;$index<$columns_cnt;$index++
) {
206 $buffer .= '} ' . $crlf ;
208 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
209 if (isset($GLOBALS['latex_caption'])) {
210 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
211 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
213 if (!PMA_exportOutputHandler($buffer)) {
218 if (isset($GLOBALS['latex_columns'])) {
219 $buffer = '\\hline ';
220 for ($i = 0; $i < $columns_cnt; $i++
) {
221 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
224 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
225 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
228 if (isset($GLOBALS['latex_caption'])) {
229 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
231 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
235 if (!PMA_exportOutputHandler('\\\\ \hline')) {
240 // print the whole table
241 while ($record = PMA_DBI_fetch_assoc($result)) {
245 for ($i = 0; $i < $columns_cnt; $i++
) {
246 if (isset($record[$columns[$i]])
247 && (! function_exists('is_null') ||
!is_null($record[$columns[$i]]))) {
248 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
250 $column_value = $GLOBALS['latex_null'];
253 // last column ... no need for & character
254 if ($i == ($columns_cnt - 1)) {
255 $buffer .= $column_value;
257 $buffer .= $column_value . " & ";
260 $buffer .= ' \\\\ \\hline ' . $crlf;
261 if (!PMA_exportOutputHandler($buffer)) {
266 $buffer = ' \\end{longtable}' . $crlf;
267 if (!PMA_exportOutputHandler($buffer)) {
271 PMA_DBI_free_result($result);
274 } // end getTableLaTeX
277 * Returns $table's structure as LaTeX
279 * @param string the database name
280 * @param string the table name
281 * @param string the end of line sequence
282 * @param string the url to go back in case of error
283 * @param boolean whether to include relation comments
284 * @param boolean whether to include column comments
285 * @param boolean whether to include mime comments
286 * @param string future feature: support view dependencies
288 * @return bool Whether it suceeded
292 // @@@ $strTableStructure
293 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
298 * Get the unique keys in the table
300 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
301 $keys_result = PMA_DBI_query($keys_query);
302 $unique_keys = array();
303 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
304 if ($key['Non_unique'] == 0) {
305 $unique_keys[] = $key['Column_name'];
308 PMA_DBI_free_result($keys_result);
311 * Gets fields properties
313 PMA_DBI_select_db($db);
314 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
315 $result = PMA_DBI_query($local_query);
316 $fields_cnt = PMA_DBI_num_rows($result);
318 // Check if we can use Relations (Mike Beck)
319 if ($do_relation && !empty($cfgRelation['relation'])) {
320 // Find which tables are related with the current one and write it in
322 $res_rel = PMA_getForeigners($db, $table);
324 if ($res_rel && count($res_rel) > 0) {
334 * Displays the table structure
336 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
337 . ' \\begin{longtable}{';
338 if (!PMA_exportOutputHandler($buffer)) {
343 $alignment = '|l|c|c|c|';
344 if ($do_relation && $have_rel) {
352 if ($do_mime && $cfgRelation['mimework']) {
356 $buffer = $alignment . '} ' . $crlf ;
358 $header = ' \\hline ';
359 $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'] . '}}';
360 if ($do_relation && $have_rel) {
361 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
364 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
365 $comments = PMA_getComments($db, $table);
367 if ($do_mime && $cfgRelation['mimework']) {
368 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
369 $mime_map = PMA_getMIME($db, $table, true);
372 $local_buffer = PMA_texEscape($table);
374 // Table caption for first page and label
375 if (isset($GLOBALS['latex_caption'])) {
376 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
377 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
380 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
381 // Table caption on next pages
382 if (isset($GLOBALS['latex_caption'])) {
383 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
386 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
388 if (!PMA_exportOutputHandler($buffer)) {
392 while ($row = PMA_DBI_fetch_assoc($result)) {
394 $type = $row['Type'];
395 // reformat mysql query output - staybyte - 9. June 2001
396 // loic1: set or enum types: slashes single quotes inside options
397 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
398 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
399 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
406 $type_nowrap = ' nowrap="nowrap"';
407 $type = eregi_replace('BINARY', '', $type);
408 $type = eregi_replace('ZEROFILL', '', $type);
409 $type = eregi_replace('UNSIGNED', '', $type);
414 $binary = eregi('BINARY', $row['Type']);
415 $unsigned = eregi('UNSIGNED', $row['Type']);
416 $zerofill = eregi('ZEROFILL', $row['Type']);
418 if (!isset($row['Default'])) {
419 if ($row['Null'] != '') {
420 $row['Default'] = 'NULL';
423 $row['Default'] = $row['Default'];
426 $field_name = $row['Field'];
428 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ?
$GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ?
$row['Default'] : '');
430 if ($do_relation && $have_rel) {
431 $local_buffer .= "\000";
432 if (isset($res_rel[$field_name])) {
433 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
436 if ($do_comments && $cfgRelation['commwork']) {
437 $local_buffer .= "\000";
438 if (isset($comments[$field_name])) {
439 $local_buffer .= $comments[$field_name];
442 if ($do_mime && $cfgRelation['mimework']) {
443 $local_buffer .= "\000";
444 if (isset($mime_map[$field_name])) {
445 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
448 $local_buffer = PMA_texEscape($local_buffer);
449 if ($row['Key']=='PRI') {
450 $pos=strpos($local_buffer, "\000");
451 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
453 if (in_array($field_name, $unique_keys)) {
454 $pos=strpos($local_buffer, "\000");
455 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
457 $buffer = str_replace("\000", ' & ', $local_buffer);
458 $buffer .= ' \\\\ \\hline ' . $crlf;
460 if (!PMA_exportOutputHandler($buffer)) {
464 PMA_DBI_free_result($result);
466 $buffer = ' \\end{longtable}' . $crlf;
467 return PMA_exportOutputHandler($buffer);
468 } // end of the 'PMA_exportStructure' function