Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: latex.php,v 2.16 2006/01/19 15:39:29 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Set of functions used to build dumps of tables
7 */
8  
9 /**
10 * Escapes some special characters for use in TeX/LaTeX
11 *
12 * @param string the string to convert
13 *
14 * @return string the converted string with escape codes
15 *
16 * @access private
17 */
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);
23 }
24 return $string;
25 }
26  
27 /**
28 * Outputs comment
29 *
30 * @param string Text of comment
31 *
32 * @return bool Whether it suceeded
33 */
34 function PMA_exportComment($text) {
35 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
36 }
37  
38 /**
39 * Outputs export footer
40 *
41 * @return bool Whether it suceeded
42 *
43 * @access public
44 */
45 function PMA_exportFooter() {
46 return TRUE;
47 }
48  
49 /**
50 * Outputs export header
51 *
52 * @return bool Whether it suceeded
53 *
54 * @access public
55 */
56 function PMA_exportHeader() {
57 global $crlf;
58 global $cfg;
59  
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'];
67 }
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);
73 }
74  
75 /**
76 * Outputs database header
77 *
78 * @param string Database name
79 *
80 * @return bool Whether it suceeded
81 *
82 * @access public
83 */
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);
90 }
91  
92 /**
93 * Outputs database footer
94 *
95 * @param string Database name
96 *
97 * @return bool Whether it suceeded
98 *
99 * @access public
100 */
101 function PMA_exportDBFooter($db) {
102 return TRUE;
103 }
104  
105 /**
106 * Outputs create database database
107 *
108 * @param string Database name
109 *
110 * @return bool Whether it suceeded
111 *
112 * @access public
113 */
114 function PMA_exportDBCreate($db) {
115 return TRUE;
116 }
117  
118 /**
119 * Outputs the content of a table in LaTeX table/sideways table environment
120 *
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
126 *
127 * @return bool Whether it suceeded
128 *
129 * @access public
130 */
131 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
132 $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
133  
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);
137 }
138 unset($i);
139  
140 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
141 . ' \\begin{longtable}{|';
142  
143 for ($index=0;$index<$columns_cnt;$index++) {
144 $buffer .= 'l|';
145 }
146 $buffer .= '} ' . $crlf ;
147  
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']) . '} \\\\';
152 }
153 if (!PMA_exportOutputHandler($buffer)) {
154 return FALSE;
155 }
156  
157 // show column names
158 if (isset($GLOBALS['latex_showcolumns'])) {
159 $buffer = '\\hline ';
160 for ($i = 0; $i < $columns_cnt; $i++) {
161 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
162 }
163  
164 $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
165 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
166 return FALSE;
167 }
168 if (isset($GLOBALS['latex_caption'])) {
169 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
170 }
171 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
172 return FALSE;
173 }
174 } else {
175 if (!PMA_exportOutputHandler('\\\\ \hline')) {
176 return FALSE;
177 }
178 }
179  
180 // print the whole table
181 while ($record = PMA_DBI_fetch_assoc($result)) {
182  
183 $buffer = '';
184 // print each row
185 for ($i = 0; $i < $columns_cnt; $i++) {
186 if ( isset($record[$columns[$i]]) && (!function_exists('is_null') || !is_null($record[$columns[$i]]))) {
187 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
188 } else {
189 $column_value = $GLOBALS['latex_replace_null'];
190 }
191  
192 // last column ... no need for & character
193 if ($i == ($columns_cnt - 1)) {
194 $buffer .= $column_value;
195 } else {
196 $buffer .= $column_value . " & ";
197 }
198 }
199 $buffer .= ' \\\\ \\hline ' . $crlf;
200 if (!PMA_exportOutputHandler($buffer)) {
201 return FALSE;
202 }
203 }
204  
205 $buffer = ' \\end{longtable}' . $crlf;
206 if (!PMA_exportOutputHandler($buffer)) {
207 return FALSE;
208 }
209  
210 PMA_DBI_free_result($result);
211 return TRUE;
212  
213 } // end getTableLaTeX
214  
215 /**
216 * Returns $table's structure as LaTeX
217 *
218 * @param string the database name
219 * @param string the table name
220 * @param string the end of line sequence
221 * @param string the url to go back in case of error
222 * @param boolean whether to include relation comments
223 * @param boolean whether to include column comments
224 * @param boolean whether to include mime comments
225 *
226 * @return bool Whether it suceeded
227 *
228 * @access public
229 */
230 // @@@ $strTableStructure
231 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
232 {
233 global $cfgRelation;
234  
235 /**
236 * Get the unique keys in the table
237 */
238 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
239 $keys_result = PMA_DBI_query($keys_query);
240 $unique_keys = array();
241 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
242 if ($key['Non_unique'] == 0) {
243 $unique_keys[] = $key['Column_name'];
244 }
245 }
246 PMA_DBI_free_result($keys_result);
247  
248 /**
249 * Gets fields properties
250 */
251 PMA_DBI_select_db($db);
252 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
253 $result = PMA_DBI_query($local_query);
254 $fields_cnt = PMA_DBI_num_rows($result);
255  
256 // Check if we can use Relations (Mike Beck)
257 if ($do_relation && !empty($cfgRelation['relation'])) {
258 // Find which tables are related with the current one and write it in
259 // an array
260 $res_rel = PMA_getForeigners($db, $table);
261  
262 if ($res_rel && count($res_rel) > 0) {
263 $have_rel = TRUE;
264 } else {
265 $have_rel = FALSE;
266 }
267 } else {
268 $have_rel = FALSE;
269 } // end if
270  
271 /**
272 * Displays the table structure
273 */
274 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
275 . ' \\begin{longtable}{';
276 if (!PMA_exportOutputHandler($buffer)) {
277 return FALSE;
278 }
279  
280 $columns_cnt = 4;
281 $alignment = '|l|c|c|c|';
282 if ($do_relation && $have_rel) {
283 $columns_cnt++;
284 $alignment .= 'l|';
285 }
286 if ($do_comments && $cfgRelation['commwork']) {
287 $columns_cnt++;
288 $alignment .= 'l|';
289 }
290 if ($do_mime && $cfgRelation['mimework']) {
291 $columns_cnt++;
292 $alignment .='l|';
293 }
294 $buffer = $alignment . '} ' . $crlf ;
295  
296 $header = ' \\hline ';
297 $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'] . '}}';
298 if ($do_relation && $have_rel) {
299 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
300 }
301 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
302 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
303 $comments = PMA_getComments($db, $table);
304 }
305 if ($do_mime && $cfgRelation['mimework']) {
306 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
307 $mime_map = PMA_getMIME($db, $table, true);
308 }
309  
310 $local_buffer = PMA_texEscape($table);
311  
312 // Table caption for first page and label
313 if (isset($GLOBALS['latex_caption'])) {
314 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
315 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
316 . '} \\\\' . $crlf;
317 }
318 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
319 // Table caption on next pages
320 if (isset($GLOBALS['latex_caption'])) {
321 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
322 . '} \\\\ ' . $crlf;
323 }
324 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ';
325  
326 if (!PMA_exportOutputHandler($buffer)) {
327 return FALSE;
328 }
329  
330 while ($row = PMA_DBI_fetch_assoc($result)) {
331  
332 $type = $row['Type'];
333 // reformat mysql query output - staybyte - 9. June 2001
334 // loic1: set or enum types: slashes single quotes inside options
335 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
336 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
337 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
338 $type_nowrap = '';
339  
340 $binary = 0;
341 $unsigned = 0;
342 $zerofill = 0;
343 } else {
344 $type_nowrap = ' nowrap="nowrap"';
345 $type = eregi_replace('BINARY', '', $type);
346 $type = eregi_replace('ZEROFILL', '', $type);
347 $type = eregi_replace('UNSIGNED', '', $type);
348 if (empty($type)) {
349 $type = '&nbsp;';
350 }
351  
352 $binary = eregi('BINARY', $row['Type']);
353 $unsigned = eregi('UNSIGNED', $row['Type']);
354 $zerofill = eregi('ZEROFILL', $row['Type']);
355 }
356 $strAttribute = '&nbsp;';
357 if ($binary) {
358 $strAttribute = 'BINARY';
359 }
360 if ($unsigned) {
361 $strAttribute = 'UNSIGNED';
362 }
363 if ($zerofill) {
364 $strAttribute = 'UNSIGNED ZEROFILL';
365 }
366 if (!isset($row['Default'])) {
367 if ($row['Null'] != '') {
368 $row['Default'] = 'NULL';
369 }
370 } else {
371 $row['Default'] = $row['Default'];
372 }
373  
374 $field_name = $row['Field'];
375  
376 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ? $row['Default'] : '');
377  
378 if ($do_relation && $have_rel) {
379 $local_buffer .= "\000";
380 if (isset($res_rel[$field_name])) {
381 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
382 }
383 }
384 if ($do_comments && $cfgRelation['commwork']) {
385 $local_buffer .= "\000";
386 if (isset($comments[$field_name])) {
387 $local_buffer .= $comments[$field_name];
388 }
389 }
390 if ($do_mime && $cfgRelation['mimework']) {
391 $local_buffer .= "\000";
392 if (isset($mime_map[$field_name])) {
393 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
394 }
395 }
396 $local_buffer = PMA_texEscape($local_buffer);
397 if ($row['Key']=='PRI') {
398 $pos=strpos($local_buffer, "\000");
399 $local_buffer = '\\textit{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
400 }
401 if (in_array($field_name, $unique_keys)) {
402 $pos=strpos($local_buffer, "\000");
403 $local_buffer = '\\textbf{' . substr($local_buffer, 0, $pos) . '}' . substr($local_buffer, $pos);
404 }
405 $buffer = str_replace("\000", ' & ', $local_buffer);
406 $buffer .= ' \\\\ \\hline ' . $crlf;
407  
408 if (!PMA_exportOutputHandler($buffer)) {
409 return FALSE;
410 }
411 } // end while
412 PMA_DBI_free_result($result);
413  
414 $buffer = ' \\end{longtable}' . $crlf;
415 return PMA_exportOutputHandler($buffer);
416 } // end of the 'PMA_getTableStructureLaTeX()' function
417 ?>