Add OK and Cancel button for grid editing
[phpmyadmin/arisferyanto.git] / libraries / export / latex.php
blob1dbe21a9ab9732b59401fe4b34b5ccef28153181
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build LaTeX dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage Latex
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Messages used in default captions */
14 $GLOBALS['strLatexContent'] = __('Content of table @TABLE@');
15 $GLOBALS['strLatexContinued'] = __('(continued)');
16 $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@');
18 /**
21 if (isset($plugin_list)) {
22 $hide_structure = false;
23 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
24 $hide_structure = true;
26 $plugin_list['latex'] = array(
27 'text' => __('LaTeX'),
28 'extension' => 'tex',
29 'mime_type' => 'application/x-tex',
30 'options' => array(
31 array('type' => 'begin_group', 'name' => 'general_opts'),
32 array('type' => 'bool', 'name' => 'caption', 'text' => __('Include table caption')),
33 array('type' => 'end_group')
35 'options_text' => __('Options'),
38 /* what to dump (structure/data/both) */
39 $plugin_list['latex']['options'][] =
40 array('type' => 'begin_group', 'name' => 'dump_what', 'text' => __('Dump table'));
41 $plugin_list['latex']['options'][] =
42 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')));
43 $plugin_list['latex']['options'][] = array('type' => 'end_group');
45 /* Structure options */
46 if (!$hide_structure) {
47 $plugin_list['latex']['options'][] =
48 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options'), 'force' => 'data');
49 $plugin_list['latex']['options'][] =
50 array('type' => 'text', 'name' => 'structure_caption', 'text' => __('Table caption'), 'doc' => 'faq6_27');
51 $plugin_list['latex']['options'][] =
52 array('type' => 'text', 'name' => 'structure_continued_caption', 'text' => __('Table caption (continued)'), 'doc' => 'faq6_27');
53 $plugin_list['latex']['options'][] =
54 array('type' => 'text', 'name' => 'structure_label', 'text' => __('Label key'), 'doc' => 'faq6_27');
55 if (!empty($GLOBALS['cfgRelation']['relation'])) {
56 $plugin_list['latex']['options'][] =
57 array('type' => 'bool', 'name' => 'relation', 'text' => __('Display foreign key relationships'));
59 $plugin_list['latex']['options'][] =
60 array('type' => 'bool', 'name' => 'comments', 'text' => __('Display comments'));
61 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
62 $plugin_list['latex']['options'][] =
63 array('type' => 'bool', 'name' => 'mime', 'text' => __('Display MIME types'));
65 $plugin_list['latex']['options'][] =
66 array('type' => 'end_group');
68 /* Data */
69 $plugin_list['latex']['options'][] =
70 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure');
71 $plugin_list['latex']['options'][] =
72 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row'));
73 $plugin_list['latex']['options'][] =
74 array('type' => 'text', 'name' => 'data_caption', 'text' => __('Table caption'), 'doc' => 'faq6_27');
75 $plugin_list['latex']['options'][] =
76 array('type' => 'text', 'name' => 'data_continued_caption', 'text' => __('Table caption (continued)'), 'doc' => 'faq6_27');
77 $plugin_list['latex']['options'][] =
78 array('type' => 'text', 'name' => 'data_label', 'text' => __('Label key'), 'doc' => 'faq6_27');
79 $plugin_list['latex']['options'][] =
80 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:'));
81 $plugin_list['latex']['options'][] =
82 array('type' => 'end_group');
83 } else {
85 /**
86 * Escapes some special characters for use in TeX/LaTeX
88 * @param string the string to convert
90 * @return string the converted string with escape codes
92 * @access private
94 function PMA_texEscape($string) {
95 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
96 $cnt_escape = count($escape);
97 for ($k=0; $k < $cnt_escape; $k++) {
98 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
100 return $string;
104 * Outputs export footer
106 * @return bool Whether it suceeded
108 * @access public
110 function PMA_exportFooter() {
111 return true;
115 * Outputs export header
117 * @return bool Whether it suceeded
119 * @access public
121 function PMA_exportHeader() {
122 global $crlf;
123 global $cfg;
125 $head = '% phpMyAdmin LaTeX Dump' . $crlf
126 . '% version ' . PMA_VERSION . $crlf
127 . '% http://www.phpmyadmin.net' . $crlf
128 . '%' . $crlf
129 . '% ' . __('Host') . ': ' . $cfg['Server']['host'];
130 if (!empty($cfg['Server']['port'])) {
131 $head .= ':' . $cfg['Server']['port'];
133 $head .= $crlf
134 . '% ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
135 . '% ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
136 . '% ' . __('PHP Version') . ': ' . phpversion() . $crlf;
137 return PMA_exportOutputHandler($head);
141 * Outputs database header
143 * @param string $db Database name
144 * @return bool Whether it suceeded
146 * @access public
148 function PMA_exportDBHeader($db) {
149 global $crlf;
150 $head = '% ' . $crlf
151 . '% ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
152 . '% ' . $crlf;
153 return PMA_exportOutputHandler($head);
157 * Outputs database footer
159 * @param string $db Database name
160 * @return bool Whether it suceeded
162 * @access public
164 function PMA_exportDBFooter($db) {
165 return true;
169 * Outputs CREATE DATABASE statement
171 * @param string $db Database name
172 * @return bool Whether it suceeded
174 * @access public
176 function PMA_exportDBCreate($db) {
177 return true;
181 * Outputs the content of a table in LaTeX table/sideways table environment
183 * @param string $db database name
184 * @param string $table table name
185 * @param string $crlf the end of line sequence
186 * @param string $error_url the url to go back in case of error
187 * @param string $sql_query SQL query for obtaining data
188 * @return bool Whether it suceeded
190 * @access public
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);
199 unset($i);
201 $buffer = $crlf . '%' . $crlf . '% ' . __('Data') . ': ' . $table . $crlf . '%' . $crlf
202 . ' \\begin{longtable}{|';
204 for ($index=0;$index<$columns_cnt;$index++) {
205 $buffer .= 'l|';
207 $buffer .= '} ' . $crlf ;
209 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
210 if (isset($GLOBALS['latex_caption'])) {
211 $buffer .= ' \\caption{' . PMA_expandUserString($GLOBALS['latex_data_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
212 . '} \\label{' . PMA_expandUserString($GLOBALS['latex_data_label'], null, array('table' => $table, 'database' => $db)) . '} \\\\';
214 if (!PMA_exportOutputHandler($buffer)) {
215 return false;
218 // show column names
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)) {
227 return false;
229 if (isset($GLOBALS['latex_caption'])) {
230 if (!PMA_exportOutputHandler('\\caption{' . PMA_expandUserString($GLOBALS['latex_data_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db)) . '} \\\\ ')) return false;
232 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
233 return false;
235 } else {
236 if (!PMA_exportOutputHandler('\\\\ \hline')) {
237 return false;
241 // print the whole table
242 while ($record = PMA_DBI_fetch_assoc($result)) {
244 $buffer = '';
245 // print each row
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]]));
250 } else {
251 $column_value = $GLOBALS['latex_null'];
254 // last column ... no need for & character
255 if ($i == ($columns_cnt - 1)) {
256 $buffer .= $column_value;
257 } else {
258 $buffer .= $column_value . " & ";
261 $buffer .= ' \\\\ \\hline ' . $crlf;
262 if (!PMA_exportOutputHandler($buffer)) {
263 return false;
267 $buffer = ' \\end{longtable}' . $crlf;
268 if (!PMA_exportOutputHandler($buffer)) {
269 return false;
272 PMA_DBI_free_result($result);
273 return true;
275 } // end getTableLaTeX
278 * Outputs table's structure
280 * @param string $db database name
281 * @param string $table table name
282 * @param string $crlf the end of line sequence
283 * @param string $error_url the url to go back in case of error
284 * @param bool $do_relation whether to include relation comments
285 * @param bool $do_comments whether to include the pmadb-style column comments
286 * as comments in the structure; this is deprecated
287 * but the parameter is left here because export.php
288 * calls PMA_exportStructure() also for other export
289 * types which use this parameter
290 * @param bool $do_mime whether to include mime comments
291 * @param bool $dates whether to include creation/update/check dates
292 * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
293 * @param string $export_type 'server', 'database', 'table'
294 * @return bool Whether it suceeded
296 * @access public
298 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
300 global $cfgRelation;
303 * Get the unique keys in the table
305 $unique_keys = array();
306 $keys = PMA_DBI_get_table_indexes($db, $table);
307 foreach ($keys as $key) {
308 if ($key['Non_unique'] == 0) {
309 $unique_keys[] = $key['Column_name'];
314 * Gets fields properties
316 PMA_DBI_select_db($db);
318 // Check if we can use Relations
319 if ($do_relation && !empty($cfgRelation['relation'])) {
320 // Find which tables are related with the current one and write it in
321 // an array
322 $res_rel = PMA_getForeigners($db, $table);
324 if ($res_rel && count($res_rel) > 0) {
325 $have_rel = true;
326 } else {
327 $have_rel = false;
329 } else {
330 $have_rel = false;
331 } // end if
334 * Displays the table structure
336 $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table . $crlf . '%' . $crlf
337 . ' \\begin{longtable}{';
338 if (!PMA_exportOutputHandler($buffer)) {
339 return false;
342 $columns_cnt = 4;
343 $alignment = '|l|c|c|c|';
344 if ($do_relation && $have_rel) {
345 $columns_cnt++;
346 $alignment .= 'l|';
348 if ($do_comments) {
349 $columns_cnt++;
350 $alignment .= 'l|';
352 if ($do_mime && $cfgRelation['mimework']) {
353 $columns_cnt++;
354 $alignment .='l|';
356 $buffer = $alignment . '} ' . $crlf ;
358 $header = ' \\hline ';
359 $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null') . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
360 if ($do_relation && $have_rel) {
361 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
363 if ($do_comments) {
364 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
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 // Table caption for first page and label
373 if (isset($GLOBALS['latex_caption'])) {
374 $buffer .= ' \\caption{'. PMA_expandUserString($GLOBALS['latex_structure_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
375 . '} \\label{' . PMA_expandUserString($GLOBALS['latex_structure_label'], null, array('table' => $table, 'database' => $db))
376 . '} \\\\' . $crlf;
378 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
379 // Table caption on next pages
380 if (isset($GLOBALS['latex_caption'])) {
381 $buffer .= ' \\caption{'. PMA_expandUserString($GLOBALS['latex_structure_continued_caption'], 'PMA_texEscape', array('table' => $table, 'database' => $db))
382 . '} \\\\ ' . $crlf;
384 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
386 if (!PMA_exportOutputHandler($buffer)) {
387 return false;
390 $fields = PMA_DBI_get_columns($db, $table);
391 foreach ($fields as $row) {
392 $extracted_fieldspec = PMA_extractFieldSpec($row['Type']);
393 $type = $extracted_fieldspec['print_type'];
394 if (empty($type)) {
395 $type = ' ';
398 if (!isset($row['Default'])) {
399 if ($row['Null'] != 'NO') {
400 $row['Default'] = 'NULL';
404 $field_name = $row['Field'];
406 $local_buffer = $field_name . "\000" . $type . "\000"
407 . (($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes'))
408 . "\000" . (isset($row['Default']) ? $row['Default'] : '');
410 if ($do_relation && $have_rel) {
411 $local_buffer .= "\000";
412 if (isset($res_rel[$field_name])) {
413 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
416 if ($do_comments && $cfgRelation['commwork']) {
417 $local_buffer .= "\000";
418 if (isset($comments[$field_name])) {
419 $local_buffer .= $comments[$field_name];
422 if ($do_mime && $cfgRelation['mimework']) {
423 $local_buffer .= "\000";
424 if (isset($mime_map[$field_name])) {
425 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
428 $local_buffer = PMA_texEscape($local_buffer);
429 if ($row['Key']=='PRI') {
430 $pos=strpos($local_buffer, "\000");
431 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
433 if (in_array($field_name, $unique_keys)) {
434 $pos=strpos($local_buffer, "\000");
435 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
437 $buffer = str_replace("\000", ' & ', $local_buffer);
438 $buffer .= ' \\\\ \\hline ' . $crlf;
440 if (!PMA_exportOutputHandler($buffer)) {
441 return false;
443 } // end while
445 $buffer = ' \\end{longtable}' . $crlf;
446 return PMA_exportOutputHandler($buffer);
447 } // end of the 'PMA_exportStructure' function
449 } // end else