sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / export / latex.php
blobf3321ce0875a787a35b666edd5be76a77078cec5
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used to build dumps of tables
7 */
9 /**
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
16 * @access private
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);
24 return $string;
27 /**
28 * Outputs comment
30 * @param string Text of comment
32 * @return bool Whether it suceeded
34 function PMA_exportComment($text) {
35 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
38 /**
39 * Outputs export footer
41 * @return bool Whether it suceeded
43 * @access public
45 function PMA_exportFooter() {
46 return TRUE;
49 /**
50 * Outputs export header
52 * @return bool Whether it suceeded
54 * @access public
56 function PMA_exportHeader() {
57 global $crlf;
58 global $cfg;
60 $head = '% phpMyAdmin LaTeX Dump' . $crlf
61 . '% version ' . PMA_VERSION . $crlf
62 . '% http://www.phpmyadmin.net' . $crlf
63 . '%' . $crlf
64 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
65 if (!empty($cfg['Server']['port'])) {
66 $head .= ':' . $cfg['Server']['port'];
68 $head .= $crlf
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);
75 /**
76 * Outputs database header
78 * @param string Database name
80 * @return bool Whether it suceeded
82 * @access public
84 function PMA_exportDBHeader($db) {
85 global $crlf;
86 $head = '% ' . $crlf
87 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
88 . '% ' . $crlf;
89 return PMA_exportOutputHandler($head);
92 /**
93 * Outputs database footer
95 * @param string Database name
97 * @return bool Whether it suceeded
99 * @access public
101 function PMA_exportDBFooter($db) {
102 return TRUE;
106 * Outputs create database database
108 * @param string Database name
110 * @return bool Whether it suceeded
112 * @access public
114 function PMA_exportDBCreate($db) {
115 return TRUE;
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
129 * @access public
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);
138 unset($i);
140 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
141 . ' \\begin{longtable}{|';
143 for ($index=0;$index<$columns_cnt;$index++) {
144 $buffer .= 'l|';
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;
155 // show column names
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;
168 } else {
169 if (!PMA_exportOutputHandler('\\\\ \hline')) return FALSE;
172 // print the whole table
173 while ($record = PMA_DBI_fetch_assoc($result)) {
175 $buffer = '';
176 // print each row
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]]));
180 } else {
181 $column_value = $GLOBALS['latex_replace_null'];
184 // last column ... no need for & character
185 if ($i == ($columns_cnt - 1)) {
186 $buffer .= $column_value;
187 } else {
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);
199 return TRUE;
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
216 * @access public
218 // @@@ $strTableStructure
219 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
221 global $cfgRelation;
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
245 // an array
246 $res_rel = PMA_getForeigners($db, $table);
248 if ($res_rel && count($res_rel) > 0) {
249 $have_rel = TRUE;
250 } else {
251 $have_rel = FALSE;
254 else {
255 $have_rel = FALSE;
256 } // end if
259 * Displays the table structure
261 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
262 . ' \\begin{longtable}{';
263 if (!PMA_exportOutputHandler($buffer)) return FALSE;
265 $columns_cnt = 4;
266 $alignment = '|l|c|c|c|';
267 if ($do_relation && $have_rel) {
268 $columns_cnt++;
269 $alignment .= 'l|';
271 if ($do_comments && $cfgRelation['commwork']) {
272 $columns_cnt++;
273 $alignment .= 'l|';
275 if ($do_mime && $cfgRelation['mimework']) {
276 $columns_cnt++;
277 $alignment .='l|';
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'])
301 . '} \\\\' . $crlf;
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'])
307 . '} \\\\ ' . $crlf;
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]) . ')';
321 $type_nowrap = '';
323 $binary = 0;
324 $unsigned = 0;
325 $zerofill = 0;
326 } else {
327 $type_nowrap = ' nowrap="nowrap"';
328 $type = eregi_replace('BINARY', '', $type);
329 $type = eregi_replace('ZEROFILL', '', $type);
330 $type = eregi_replace('UNSIGNED', '', $type);
331 if (empty($type)) {
332 $type = '&nbsp;';
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 = '&nbsp;';
340 if ($binary) {
341 $strAttribute = 'BINARY';
343 if ($unsigned) {
344 $strAttribute = 'UNSIGNED';
346 if ($zerofill) {
347 $strAttribute = 'UNSIGNED ZEROFILL';
349 if (!isset($row['Default'])) {
350 if ($row['Null'] != '') {
351 $row['Default'] = 'NULL';
353 } else {
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;
392 } // end while
393 PMA_DBI_free_result($result);
395 $buffer = ' \\end{longtable}' . $crlf;
396 return PMA_exportOutputHandler($buffer);
397 } // end of the 'PMA_getTableStructureLaTeX()' function