00001 <?php
00002
00003
00004
00018 function PMA_texEscape($string) {
00019 $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
00020 $cnt_escape = count($escape);
00021 for($k=0; $k < $cnt_escape; $k++) {
00022 $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
00023 }
00024 return $string;
00025 }
00026
00034 function PMA_exportComment($text) {
00035 return PMA_exportOutputHandler('% ' . $text . $GLOBALS['crlf']);
00036 }
00037
00045 function PMA_exportHeader() {
00046 global $crlf;
00047 global $cfg;
00048
00049 $head = '% phpMyAdmin LaTeX Dump' . $crlf
00050 . '% version ' . PMA_VERSION . $crlf
00051 . '% http:
00052 . '%' . $crlf
00053 . '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
00054 if (!empty($cfg['Server']['port'])) {
00055 $head .= ':' . $cfg['Server']['port'];
00056 }
00057 $head .= $crlf
00058 . '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
00059 . '% ' . $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
00060 . '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
00061 return PMA_exportOutputHandler($head);
00062 }
00063
00073 function PMA_exportDBHeader($db) {
00074 global $crlf;
00075 $head = '% ' . $crlf
00076 . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
00077 . '% ' . $crlf;
00078 return PMA_exportOutputHandler($head);
00079 }
00080
00090 function PMA_exportDBFooter($db) {
00091 return TRUE;
00092 }
00093
00103 function PMA_exportDBCreate($db) {
00104 return TRUE;
00105 }
00106
00120 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
00121 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url);
00122
00123 $columns_cnt = mysql_num_fields($result);
00124 for ($i = 0; $i < $columns_cnt; $i++) {
00125 $columns[$i] = mysql_field_name($result, $i);
00126 }
00127 unset($i);
00128
00129 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
00130 . ' \\begin{longtable}{|';
00131
00132 for($index=0;$index<$columns_cnt;$index++) {
00133 $buffer .= 'l|';
00134 }
00135 $buffer .= '} ' . $crlf ;
00136
00137 $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
00138 if (isset($GLOBALS['latex_caption'])) {
00139 $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
00140 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
00141 }
00142 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00143
00144
00145 if (isset($GLOBALS['latex_showcolumns'])) {
00146 $buffer = '\\hline ';
00147 for ($i = 0; $i < $columns_cnt; $i++) {
00148 $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
00149 }
00150
00151 $buffer = substr($buffer,0,-2) . '\\\\ \\hline \hline ';
00152 if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) return FALSE;
00153 if (isset($GLOBALS['latex_caption'])) {
00154 if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
00155 }
00156 if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) return FALSE;
00157 } else {
00158 if (!PMA_exportOutputHandler('\\\\ \hline')) return FALSE;
00159 }
00160
00161
00162 while ($record = PMA_mysql_fetch_array($result, MYSQL_ASSOC)) {
00163
00164 $buffer = '';
00165
00166 for($i = 0; $i < $columns_cnt; $i++) {
00167 if ( isset($record[$columns[$i]]) && (!function_exists('is_null') || !is_null($record[$columns[$i]]))) {
00168 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
00169 } else {
00170 $column_value = $GLOBALS['latex_replace_null'];
00171 }
00172
00173
00174 if($i == ($columns_cnt - 1)) {
00175 $buffer .= $column_value;
00176 } else {
00177 $buffer .= $column_value . " & ";
00178 }
00179 }
00180 $buffer .= ' \\\\ \\hline ' . $crlf;
00181 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00182 }
00183
00184 $buffer = ' \\end{longtable}' . $crlf;
00185 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00186
00187 mysql_free_result($result);
00188 return TRUE;
00189
00190 }
00191
00207
00208 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
00209 {
00210 global $cfgRelation;
00211
00215 PMA_mysql_select_db($db);
00216 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
00217 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
00218 $fields_cnt = mysql_num_rows($result);
00219
00220
00221 if ($do_relation && !empty($cfgRelation['relation'])) {
00222
00223
00224 $res_rel = PMA_getForeigners($db, $table);
00225
00226 if ($res_rel && count($res_rel) > 0) {
00227 $have_rel = TRUE;
00228 } else {
00229 $have_rel = FALSE;
00230 }
00231 }
00232 else {
00233 $have_rel = FALSE;
00234 }
00235
00239 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
00240 $keys_result = PMA_mysql_query($keys_query) or PMA_mysqlDie('', $keys_query, '', $error_url);
00241 $unique_keys = array();
00242 while($key = PMA_mysql_fetch_array($keys_result)) {
00243 if ($key['Non_unique'] == 0) $unique_keys[] = $key['Column_name'];
00244 }
00245
00249 $buffer = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table . $crlf . '%' . $crlf
00250 . ' \\begin{longtable}{';
00251 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00252
00253 $columns_cnt = 4;
00254 $alignment = '|l|c|c|c|';
00255 if ($do_relation && $have_rel) {
00256 $columns_cnt++;
00257 $alignment .= 'l|';
00258 }
00259 if ($do_comments && $cfgRelation['commwork']) {
00260 $columns_cnt++;
00261 $alignment .= 'l|';
00262 }
00263 if ($do_mime && $cfgRelation['mimework']) {
00264 $columns_cnt++;
00265 $alignment .='l|';
00266 }
00267 $buffer = $alignment . '} ' . $crlf ;
00268
00269 $header .= ' \\hline ';
00270 $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'] . '}}';
00271 if ($do_relation && $have_rel) {
00272 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
00273 }
00274 if ($do_comments && $cfgRelation['commwork']) {
00275 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
00276 $comments = PMA_getComments($db, $table);
00277 }
00278 if ($do_mime && $cfgRelation['mimework']) {
00279 $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
00280 $mime_map = PMA_getMIME($db, $table, true);
00281 }
00282
00283 $local_buffer = PMA_texEscape($table);
00284
00285
00286 if (isset($GLOBALS['latex_caption'])) {
00287 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
00288 . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
00289 . '} \\\\' . $crlf;
00290 }
00291 $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
00292
00293 if (isset($GLOBALS['latex_caption'])) {
00294 $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
00295 . '} \\\\ ' . $crlf;
00296 }
00297 $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ';
00298
00299 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00300
00301 while ($row = PMA_mysql_fetch_array($result)) {
00302
00303 $type = $row['Type'];
00304
00305
00306 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
00307 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
00308 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
00309 $type_nowrap = '';
00310
00311 $binary = 0;
00312 $unsigned = 0;
00313 $zerofill = 0;
00314 } else {
00315 $type_nowrap = ' nowrap="nowrap"';
00316 $type = eregi_replace('BINARY', '', $type);
00317 $type = eregi_replace('ZEROFILL', '', $type);
00318 $type = eregi_replace('UNSIGNED', '', $type);
00319 if (empty($type)) {
00320 $type = ' ';
00321 }
00322
00323 $binary = eregi('BINARY', $row['Type'], $test);
00324 $unsigned = eregi('UNSIGNED', $row['Type'], $test);
00325 $zerofill = eregi('ZEROFILL', $row['Type'], $test);
00326 }
00327 $strAttribute = ' ';
00328 if ($binary) {
00329 $strAttribute = 'BINARY';
00330 }
00331 if ($unsigned) {
00332 $strAttribute = 'UNSIGNED';
00333 }
00334 if ($zerofill) {
00335 $strAttribute = 'UNSIGNED ZEROFILL';
00336 }
00337 if (!isset($row['Default'])) {
00338 if ($row['Null'] != '') {
00339 $row['Default'] = 'NULL';
00340 }
00341 } else {
00342 $row['Default'] = $row['Default'];
00343 }
00344
00345 $field_name = $row['Field'];
00346
00347 $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . "\000" . (isset($row['Default']) ? $row['Default'] : '');
00348
00349 if ($do_relation && $have_rel) {
00350 $local_buffer .= "\000";
00351 if (isset($res_rel[$field_name])) {
00352 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
00353 }
00354 }
00355 if ($do_comments && $cfgRelation['commwork']) {
00356 $local_buffer .= "\000";
00357 if (isset($comments[$field_name])) {
00358 $local_buffer .= $comments[$field_name];
00359 }
00360 }
00361 if ($do_mime && $cfgRelation['mimework']) {
00362 $local_buffer .= "\000";
00363 if (isset($mime_map[$field_name])) {
00364 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
00365 }
00366 }
00367 $local_buffer = PMA_texEscape($local_buffer);
00368 if ($row['Key']=='PRI') {
00369 $pos=strpos($local_buffer, "\000");
00370 $local_buffer = '\\textit{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
00371 }
00372 if (in_array($field_name, $unique_keys)) {
00373 $pos=strpos($local_buffer, "\000");
00374 $local_buffer = '\\textbf{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
00375 }
00376 $buffer = str_replace("\000", ' & ', $local_buffer);
00377 $buffer .= ' \\\\ \\hline ' . $crlf;
00378
00379 if (!PMA_exportOutputHandler($buffer)) return FALSE;
00380 }
00381 mysql_free_result($result);
00382
00383 $buffer = ' \\end{longtable}' . $crlf;
00384 return PMA_exportOutputHandler($buffer);
00385 }
00386 ?>