3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Set of functions used to build dumps of tables
10 * Escapes some special characters for use in TeX/LaTeX
12 * @param string the string to convert
14 * @return string the converted string with escape codes
18 function PMA_texEscape($string) {
19 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
20 $cnt_escape = count($escape);
21 for ($k=0; $k < $cnt_escape; $k++
) {
22 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
30 * @param string Text of comment
32 * @return bool Whether it suceeded
34 function PMA_exportComment($text) {
35 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
39 * Outputs export footer
41 * @return bool Whether it suceeded
45 function PMA_exportFooter() {
50 * Outputs export header
52 * @return bool Whether it suceeded
56 function PMA_exportHeader() {
60 $head = '% phpMyAdmin LaTeX Dump' . $crlf
61 . '% version ' . PMA_VERSION
. $crlf
62 . '% http://www.phpmyadmin.net' . $crlf
64 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
65 if (!empty($cfg['Server']['port'])) {
66 $head .= ':' . $cfg['Server']['port'];
69 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
70 . '% ' . $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
71 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
72 return PMA_exportOutputHandler($head);
76 * Outputs database header
78 * @param string Database name
80 * @return bool Whether it suceeded
84 function PMA_exportDBHeader($db) {
87 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''). $crlf
89 return PMA_exportOutputHandler($head);
93 * Outputs database footer
95 * @param string Database name
97 * @return bool Whether it suceeded
101 function PMA_exportDBFooter($db) {
106 * Outputs create database database
108 * @param string Database name
110 * @return bool Whether it suceeded
114 function PMA_exportDBCreate($db) {
119 * Outputs the content of a table in LaTeX table/sideways table environment
121 * @param string the database name
122 * @param string the table name
123 * @param string the end of line sequence
124 * @param string the url to go back in case of error
125 * @param string SQL query for obtaining data
127 * @return bool Whether it suceeded
131 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
132 $result = PMA_DBI_try_query($sql_query, NULL, PMA_DBI_QUERY_UNBUFFERED
);
134 $columns_cnt = PMA_DBI_num_fields($result);
135 for ($i = 0; $i < $columns_cnt; $i++
) {
136 $columns[$i] = PMA_DBI_field_name($result, $i);
140 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
141 . ' \\begin{longtable}{|';
143 for ($index=0;$index<$columns_cnt;$index++
) {
146 $buffer .= '} ' . $crlf ;
148 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
149 if (isset($GLOBALS['latex_caption'])) {
150 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
151 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
153 if (!PMA_exportOutputHandler($buffer)) return FALSE;
156 if (isset($GLOBALS['latex_showcolumns'])) {
157 $buffer = '\\hline ';
158 for ($i = 0; $i < $columns_cnt; $i++
) {
159 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
162 $buffer = substr($buffer,0,-2) . '\\\\ \\hline \hline ';
163 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) return FALSE;
164 if (isset($GLOBALS['latex_caption'])) {
165 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
167 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) return FALSE;
169 if (!PMA_exportOutputHandler('\\\\ \hline')) return FALSE;
172 // print the whole table
173 while ($record = PMA_DBI_fetch_assoc($result)) {
177 for ($i = 0; $i < $columns_cnt; $i++
) {
178 if ( isset($record[$columns[$i]]) && (!function_exists('is_null') ||
!is_null($record[$columns[$i]]))) {
179 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
181 $column_value = $GLOBALS['latex_replace_null'];
184 // last column ... no need for & character
185 if ($i == ($columns_cnt - 1)) {
186 $buffer .= $column_value;
188 $buffer .= $column_value . " & ";
191 $buffer .= ' \\\\ \\hline ' . $crlf;
192 if (!PMA_exportOutputHandler($buffer)) return FALSE;
195 $buffer = ' \\end{longtable}' . $crlf;
196 if (!PMA_exportOutputHandler($buffer)) return FALSE;
198 PMA_DBI_free_result($result);
201 } // end getTableLaTeX
204 * Returns $table's structure as LaTeX
206 * @param string the database name
207 * @param string the table name
208 * @param string the end of line sequence
209 * @param string the url to go back in case of error
210 * @param boolean whether to include relation comments
211 * @param boolean whether to include column comments
212 * @param boolean whether to include mime comments
214 * @return bool Whether it suceeded
218 // @@@ $strTableStructure
219 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
224 * Get the unique keys in the table
226 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
227 $keys_result = PMA_DBI_query($keys_query);
228 $unique_keys = array();
229 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
230 if ($key['Non_unique'] == 0) $unique_keys[] = $key['Column_name'];
232 PMA_DBI_free_result($keys_result);
235 * Gets fields properties
237 PMA_DBI_select_db($db);
238 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
239 $result = PMA_DBI_query($local_query);
240 $fields_cnt = PMA_DBI_num_rows($result);
242 // Check if we can use Relations (Mike Beck)
243 if ($do_relation && !empty($cfgRelation['relation'])) {
244 // Find which tables are related with the current one and write it in
246 $res_rel = PMA_getForeigners($db, $table);
248 if ($res_rel && count($res_rel) > 0) {
259 * Displays the table structure
261 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
262 . ' \\begin{longtable}{';
263 if (!PMA_exportOutputHandler($buffer)) return FALSE;
266 $alignment = '|l|c|c|c|';
267 if ($do_relation && $have_rel) {
271 if ($do_comments && $cfgRelation['commwork']) {
275 if ($do_mime && $cfgRelation['mimework']) {
279 $buffer = $alignment . '} ' . $crlf ;
281 $header = ' \\hline ';
282 $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'] . '}}';
283 if ($do_relation && $have_rel) {
284 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
286 if ($do_comments && $cfgRelation['commwork']) {
287 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
288 $comments = PMA_getComments($db, $table);
290 if ($do_mime && $cfgRelation['mimework']) {
291 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
292 $mime_map = PMA_getMIME($db, $table, true);
295 $local_buffer = PMA_texEscape($table);
297 // Table caption for first page and label
298 if (isset($GLOBALS['latex_caption'])) {
299 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
300 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
303 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
304 // Table caption on next pages
305 if (isset($GLOBALS['latex_caption'])) {
306 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
309 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ';
311 if (!PMA_exportOutputHandler($buffer)) return FALSE;
313 while ($row = PMA_DBI_fetch_assoc($result)) {
315 $type = $row['Type'];
316 // reformat mysql query output - staybyte - 9. June 2001
317 // loic1: set or enum types: slashes single quotes inside options
318 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp = array())) {
319 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
320 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
327 $type_nowrap = ' nowrap="nowrap"';
328 $type = eregi_replace('BINARY', '', $type);
329 $type = eregi_replace('ZEROFILL', '', $type);
330 $type = eregi_replace('UNSIGNED', '', $type);
335 $binary = eregi('BINARY', $row['Type'], $test = array());
336 $unsigned = eregi('UNSIGNED', $row['Type'], $test = array());
337 $zerofill = eregi('ZEROFILL', $row['Type'], $test = array());
339 $strAttribute = ' ';
341 $strAttribute = 'BINARY';
344 $strAttribute = 'UNSIGNED';
347 $strAttribute = 'UNSIGNED ZEROFILL';
349 if (!isset($row['Default'])) {
350 if ($row['Null'] != '') {
351 $row['Default'] = 'NULL';
354 $row['Default'] = $row['Default'];
357 $field_name = $row['Field'];
359 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ?
$GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ?
$row['Default'] : '');
361 if ($do_relation && $have_rel) {
362 $local_buffer .= "\000";
363 if (isset($res_rel[$field_name])) {
364 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
367 if ($do_comments && $cfgRelation['commwork']) {
368 $local_buffer .= "\000";
369 if (isset($comments[$field_name])) {
370 $local_buffer .= $comments[$field_name];
373 if ($do_mime && $cfgRelation['mimework']) {
374 $local_buffer .= "\000";
375 if (isset($mime_map[$field_name])) {
376 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
379 $local_buffer = PMA_texEscape($local_buffer);
380 if ($row['Key']=='PRI') {
381 $pos=strpos($local_buffer, "\000");
382 $local_buffer = '\\textit{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
384 if (in_array($field_name, $unique_keys)) {
385 $pos=strpos($local_buffer, "\000");
386 $local_buffer = '\\textbf{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
388 $buffer = str_replace("\000", ' & ', $local_buffer);
389 $buffer .= ' \\\\ \\hline ' . $crlf;
391 if (!PMA_exportOutputHandler($buffer)) return FALSE;
393 PMA_DBI_free_result($result);
395 $buffer = ' \\end{longtable}' . $crlf;
396 return PMA_exportOutputHandler($buffer);
397 } // end of the 'PMA_getTableStructureLaTeX()' function