2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / export / latex.php
blob4ea8188d6e2770256fe7669953ca002fc29464ef
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build dumps of tables
6 * @version $Id$
7 */
9 /**
12 if (isset($plugin_list)) {
13 $hide_structure = false;
14 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
15 $hide_structure = true;
17 $plugin_list['latex'] = array(
18 'text' => 'strLaTeX',
19 'extension' => 'tex',
20 'mime_type' => 'application/x-tex',
21 'options' => array(
22 array('type' => 'bool', 'name' => 'caption', 'text' => 'strLatexIncludeCaption'),
24 'options_text' => 'strOptions',
26 /* Structure options */
27 if (!$hide_structure) {
28 $plugin_list['latex']['options'][] =
29 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
30 $plugin_list['latex']['options'][] =
31 array('type' => 'text', 'name' => 'structure_caption', 'text' => 'strLatexCaption');
32 $plugin_list['latex']['options'][] =
33 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => 'strLatexContinuedCaption');
34 $plugin_list['latex']['options'][] =
35 array('type' => 'text', 'name' => 'structure_label', 'text' => 'strLatexLabel');
36 if (!empty($GLOBALS['cfgRelation']['relation'])) {
37 $plugin_list['latex']['options'][] =
38 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
40 if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
41 $plugin_list['latex']['options'][] =
42 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
44 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
45 $plugin_list['latex']['options'][] =
46 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
48 $plugin_list['latex']['options'][] =
49 array('type' => 'egroup');
51 /* Data */
52 $plugin_list['latex']['options'][] =
53 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
54 $plugin_list['latex']['options'][] =
55 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
56 $plugin_list['latex']['options'][] =
57 array('type' => 'text', 'name' => 'data_caption', 'text' => 'strLatexCaption');
58 $plugin_list['latex']['options'][] =
59 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => 'strLatexContinuedCaption');
60 $plugin_list['latex']['options'][] =
61 array('type' => 'text', 'name' => 'data_label', 'text' => 'strLatexLabel');
62 $plugin_list['latex']['options'][] =
63 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
64 $plugin_list['latex']['options'][] =
65 array('type' => 'egroup');
66 } else {
68 /**
69 * Escapes some special characters for use in TeX/LaTeX
71 * @param string the string to convert
73 * @return string the converted string with escape codes
75 * @access private
77 function PMA_texEscape($string) {
78 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
79 $cnt_escape = count($escape);
80 for ($k=0; $k < $cnt_escape; $k++) {
81 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
83 return $string;
86 /**
87 * Outputs comment
89 * @param string Text of comment
91 * @return bool Whether it suceeded
93 function PMA_exportComment($text) {
94 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
97 /**
98 * Outputs export footer
100 * @return bool Whether it suceeded
102 * @access public
104 function PMA_exportFooter() {
105 return TRUE;
109 * Outputs export header
111 * @return bool Whether it suceeded
113 * @access public
115 function PMA_exportHeader() {
116 global $crlf;
117 global $cfg;
119 $head = '% phpMyAdmin LaTeX Dump' . $crlf
120 . '% version ' . PMA_VERSION . $crlf
121 . '% http://www.phpmyadmin.net' . $crlf
122 . '%' . $crlf
123 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
124 if (!empty($cfg['Server']['port'])) {
125 $head .= ':' . $cfg['Server']['port'];
127 $head .= $crlf
128 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
129 . '% ' . $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
130 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
131 return PMA_exportOutputHandler($head);
135 * Outputs database header
137 * @param string Database name
139 * @return bool Whether it suceeded
141 * @access public
143 function PMA_exportDBHeader($db) {
144 global $crlf;
145 $head = '% ' . $crlf
146 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
147 . '% ' . $crlf;
148 return PMA_exportOutputHandler($head);
152 * Outputs database footer
154 * @param string Database name
156 * @return bool Whether it suceeded
158 * @access public
160 function PMA_exportDBFooter($db) {
161 return TRUE;
165 * Outputs create database database
167 * @param string Database name
169 * @return bool Whether it suceeded
171 * @access public
173 function PMA_exportDBCreate($db) {
174 return TRUE;
178 * Outputs the content of a table in LaTeX table/sideways table environment
180 * @param string the database name
181 * @param string the table name
182 * @param string the end of line sequence
183 * @param string the url to go back in case of error
184 * @param string SQL query for obtaining data
186 * @return bool Whether it suceeded
188 * @access public
190 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
191 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
193 $columns_cnt = PMA_DBI_num_fields($result);
194 for ($i = 0; $i < $columns_cnt; $i++) {
195 $columns[$i] = PMA_DBI_field_name($result, $i);
197 unset($i);
199 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
200 . ' \\begin{longtable}{|';
202 for ($index=0;$index<$columns_cnt;$index++) {
203 $buffer .= 'l|';
205 $buffer .= '} ' . $crlf ;
207 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
208 if (isset($GLOBALS['latex_caption'])) {
209 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
210 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
212 if (!PMA_exportOutputHandler($buffer)) {
213 return FALSE;
216 // show column names
217 if (isset($GLOBALS['latex_columns'])) {
218 $buffer = '\\hline ';
219 for ($i = 0; $i < $columns_cnt; $i++) {
220 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
223 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
224 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
225 return FALSE;
227 if (isset($GLOBALS['latex_caption'])) {
228 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
230 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
231 return FALSE;
233 } else {
234 if (!PMA_exportOutputHandler('\\\\ \hline')) {
235 return FALSE;
239 // print the whole table
240 while ($record = PMA_DBI_fetch_assoc($result)) {
242 $buffer = '';
243 // print each row
244 for ($i = 0; $i < $columns_cnt; $i++) {
245 if (isset($record[$columns[$i]])
246 && (! function_exists('is_null') || !is_null($record[$columns[$i]]))) {
247 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
248 } else {
249 $column_value = $GLOBALS['latex_null'];
252 // last column ... no need for & character
253 if ($i == ($columns_cnt - 1)) {
254 $buffer .= $column_value;
255 } else {
256 $buffer .= $column_value . " & ";
259 $buffer .= ' \\\\ \\hline ' . $crlf;
260 if (!PMA_exportOutputHandler($buffer)) {
261 return FALSE;
265 $buffer = ' \\end{longtable}' . $crlf;
266 if (!PMA_exportOutputHandler($buffer)) {
267 return FALSE;
270 PMA_DBI_free_result($result);
271 return TRUE;
273 } // end getTableLaTeX
276 * Returns $table's structure as LaTeX
278 * @param string the database name
279 * @param string the table name
280 * @param string the end of line sequence
281 * @param string the url to go back in case of error
282 * @param boolean whether to include relation comments
283 * @param boolean whether to include column comments
284 * @param boolean whether to include mime comments
285 * @param string future feature: support view dependencies
287 * @return bool Whether it suceeded
289 * @access public
291 // @@@ $strTableStructure
292 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
294 global $cfgRelation;
297 * Get the unique keys in the table
299 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
300 $keys_result = PMA_DBI_query($keys_query);
301 $unique_keys = array();
302 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
303 if ($key['Non_unique'] == 0) {
304 $unique_keys[] = $key['Column_name'];
307 PMA_DBI_free_result($keys_result);
310 * Gets fields properties
312 PMA_DBI_select_db($db);
313 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
314 $result = PMA_DBI_query($local_query);
315 $fields_cnt = PMA_DBI_num_rows($result);
317 // Check if we can use Relations (Mike Beck)
318 if ($do_relation && !empty($cfgRelation['relation'])) {
319 // Find which tables are related with the current one and write it in
320 // an array
321 $res_rel = PMA_getForeigners($db, $table);
323 if ($res_rel && count($res_rel) > 0) {
324 $have_rel = TRUE;
325 } else {
326 $have_rel = FALSE;
328 } else {
329 $have_rel = FALSE;
330 } // end if
333 * Displays the table structure
335 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
336 . ' \\begin{longtable}{';
337 if (!PMA_exportOutputHandler($buffer)) {
338 return FALSE;
341 $columns_cnt = 4;
342 $alignment = '|l|c|c|c|';
343 if ($do_relation && $have_rel) {
344 $columns_cnt++;
345 $alignment .= 'l|';
347 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
348 $columns_cnt++;
349 $alignment .= 'l|';
351 if ($do_mime && $cfgRelation['mimework']) {
352 $columns_cnt++;
353 $alignment .='l|';
355 $buffer = $alignment . '} ' . $crlf ;
357 $header = ' \\hline ';
358 $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'] . '}}';
359 if ($do_relation && $have_rel) {
360 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
362 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
363 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
364 $comments = PMA_getComments($db, $table);
366 if ($do_mime && $cfgRelation['mimework']) {
367 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
368 $mime_map = PMA_getMIME($db, $table, true);
371 $local_buffer = PMA_texEscape($table);
373 // Table caption for first page and label
374 if (isset($GLOBALS['latex_caption'])) {
375 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
376 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
377 . '} \\\\' . $crlf;
379 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
380 // Table caption on next pages
381 if (isset($GLOBALS['latex_caption'])) {
382 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
383 . '} \\\\ ' . $crlf;
385 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
387 if (!PMA_exportOutputHandler($buffer)) {
388 return FALSE;
391 while ($row = PMA_DBI_fetch_assoc($result)) {
393 $type = $row['Type'];
394 // reformat mysql query output - staybyte - 9. June 2001
395 // loic1: set or enum types: slashes single quotes inside options
396 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
397 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
398 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
399 $type_nowrap = '';
401 $binary = 0;
402 $unsigned = 0;
403 $zerofill = 0;
404 } else {
405 $type_nowrap = ' nowrap="nowrap"';
406 $type = eregi_replace('BINARY', '', $type);
407 $type = eregi_replace('ZEROFILL', '', $type);
408 $type = eregi_replace('UNSIGNED', '', $type);
409 if (empty($type)) {
410 $type = '&nbsp;';
413 $binary = eregi('BINARY', $row['Type']);
414 $unsigned = eregi('UNSIGNED', $row['Type']);
415 $zerofill = eregi('ZEROFILL', $row['Type']);
417 if (!isset($row['Default'])) {
418 if ($row['Null'] != '') {
419 $row['Default'] = 'NULL';
421 } else {
422 $row['Default'] = $row['Default'];
425 $field_name = $row['Field'];
427 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ? $row['Default'] : '');
429 if ($do_relation && $have_rel) {
430 $local_buffer .= "\000";
431 if (isset($res_rel[$field_name])) {
432 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
435 if ($do_comments && $cfgRelation['commwork']) {
436 $local_buffer .= "\000";
437 if (isset($comments[$field_name])) {
438 $local_buffer .= $comments[$field_name];
441 if ($do_mime && $cfgRelation['mimework']) {
442 $local_buffer .= "\000";
443 if (isset($mime_map[$field_name])) {
444 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
447 $local_buffer = PMA_texEscape($local_buffer);
448 if ($row['Key']=='PRI') {
449 $pos=strpos($local_buffer, "\000");
450 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
452 if (in_array($field_name, $unique_keys)) {
453 $pos=strpos($local_buffer, "\000");
454 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
456 $buffer = str_replace("\000", ' & ', $local_buffer);
457 $buffer .= ' \\\\ \\hline ' . $crlf;
459 if (!PMA_exportOutputHandler($buffer)) {
460 return FALSE;
462 } // end while
463 PMA_DBI_free_result($result);
465 $buffer = ' \\end{longtable}' . $crlf;
466 return PMA_exportOutputHandler($buffer);
467 } // end of the 'PMA_exportStructure' function
469 } // end else