home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / latex.php < prev    next >
Encoding:
PHP Script  |  2003-11-20  |  13.2 KB  |  386 lines

  1. <?php
  2. /* $Id: latex.php,v 2.2 2003/11/20 16:31:51 garvinhicking 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 header
  40.  *
  41.  * @return  bool        Whether it suceeded
  42.  *
  43.  * @access  public
  44.  */
  45. function PMA_exportHeader() {
  46.     global $crlf;
  47.     global $cfg;
  48.  
  49.     $head  =  '% phpMyAdmin LaTeX Dump' . $crlf
  50.            .  '% version ' . PMA_VERSION . $crlf
  51.            .  '% http://www.phpmyadmin.net' . $crlf
  52.            .  '%' . $crlf
  53.            .  '% ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
  54.     if (!empty($cfg['Server']['port'])) {
  55.          $head .= ':' . $cfg['Server']['port'];
  56.     }
  57.     $head .= $crlf
  58.            .  '% ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
  59.            .  '% ' . $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
  60.            .  '% ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf;
  61.     return PMA_exportOutputHandler($head);
  62. }
  63.  
  64. /**
  65.  * Outputs database header
  66.  *
  67.  * @param   string      Database name
  68.  *
  69.  * @return  bool        Whether it suceeded
  70.  *
  71.  * @access  public
  72.  */
  73. function PMA_exportDBHeader($db) {
  74.     global $crlf;
  75.     $head = '% ' . $crlf
  76.           . '% ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
  77.           . '% ' . $crlf;
  78.     return PMA_exportOutputHandler($head);
  79. }
  80.  
  81. /**
  82.  * Outputs database footer
  83.  *
  84.  * @param   string      Database name
  85.  *
  86.  * @return  bool        Whether it suceeded
  87.  *
  88.  * @access  public
  89.  */
  90. function PMA_exportDBFooter($db) {
  91.     return TRUE;
  92. }
  93.  
  94. /**
  95.  * Outputs create database database
  96.  *
  97.  * @param   string      Database name
  98.  *
  99.  * @return  bool        Whether it suceeded
  100.  *
  101.  * @access  public
  102.  */
  103. function PMA_exportDBCreate($db) {
  104.     return TRUE;
  105. }
  106.  
  107. /**
  108.  * Outputs the content of a table in LaTeX table/sideways table environment
  109.  *
  110.  * @param   string      the database name
  111.  * @param   string      the table name
  112.  * @param   string      the end of line sequence
  113.  * @param   string      the url to go back in case of error
  114.  * @param   string      SQL query for obtaining data
  115.  *
  116.  * @return  bool        Whether it suceeded
  117.  *
  118.  * @access  public
  119.  */
  120. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
  121.     $result      = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url);
  122.  
  123.     $columns_cnt = mysql_num_fields($result);
  124.     for ($i = 0; $i < $columns_cnt; $i++) {
  125.         $columns[$i] = mysql_field_name($result, $i);
  126.     }
  127.     unset($i);
  128.  
  129.     $buffer      = $crlf . '%' . $crlf . '% ' . $GLOBALS['strData'] . ': ' . $table . $crlf . '%' . $crlf
  130.                  . ' \\begin{longtable}{|';
  131.  
  132.     for($index=0;$index<$columns_cnt;$index++) {
  133.        $buffer .= 'l|';
  134.     }
  135.     $buffer .= '} ' . $crlf ;
  136.  
  137.     $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
  138.     if (isset($GLOBALS['latex_caption'])) {
  139.         $buffer .= ' \\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_caption'])
  140.                    . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_data_label']) . '} \\\\';
  141.     }
  142.     if (!PMA_exportOutputHandler($buffer)) return FALSE;
  143.  
  144.     // show column names
  145.     if (isset($GLOBALS['latex_showcolumns'])) {
  146.         $buffer = '\\hline ';
  147.         for ($i = 0; $i < $columns_cnt; $i++) {
  148.             $buffer .= '\\multicolumn{1}{|c|}{\\textbf{' . PMA_texEscape(stripslashes($columns[$i])) . '}} & ';
  149.           }
  150.  
  151.         $buffer = substr($buffer,0,-2) . '\\\\ \\hline \hline ';
  152.         if (!PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) return FALSE;
  153.         if (isset($GLOBALS['latex_caption'])) {
  154.             if (!PMA_exportOutputHandler('\\caption{' . str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_data_continued_caption']) . '} \\\\ ')) return FALSE;
  155.         }
  156.         if (!PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) return FALSE;
  157.     } else {
  158.         if (!PMA_exportOutputHandler('\\\\ \hline')) return FALSE;
  159.     }
  160.  
  161.     // print the whole table
  162.     while ($record = PMA_mysql_fetch_array($result, MYSQL_ASSOC)) {
  163.  
  164.         $buffer = '';
  165.         // print each row
  166.         for($i = 0; $i < $columns_cnt; $i++) {
  167.             if ( isset($record[$columns[$i]]) && (!function_exists('is_null') || !is_null($record[$columns[$i]]))) {
  168.                 $column_value = PMA_texEscape(stripslashes($record[$columns[$i]]));
  169.             } else {
  170.                 $column_value = $GLOBALS['latex_replace_null'];
  171.             }
  172.  
  173.             // last column ... no need for & character
  174.             if($i == ($columns_cnt - 1)) {
  175.                 $buffer .= $column_value;
  176.             } else {
  177.                 $buffer .= $column_value . " & ";
  178.             }
  179.         }
  180.         $buffer .= ' \\\\ \\hline ' . $crlf;
  181.         if (!PMA_exportOutputHandler($buffer)) return FALSE;
  182.     }
  183.  
  184.     $buffer = ' \\end{longtable}' . $crlf;
  185.     if (!PMA_exportOutputHandler($buffer)) return FALSE;
  186.  
  187.     mysql_free_result($result);
  188.     return TRUE;
  189.  
  190. } // end getTableLaTeX
  191.  
  192. /**
  193.  * Returns $table's structure as LaTeX
  194.  *
  195.  * @param   string   the database name
  196.  * @param   string   the table name
  197.  * @param   string   the end of line sequence
  198.  * @param   string   the url to go back in case of error
  199.  * @param   boolean  whether to include relation comments
  200.  * @param   boolean  whether to include column comments
  201.  * @param   boolean  whether to include mime comments
  202.  *
  203.  * @return  bool     Whether it suceeded
  204.  *
  205.  * @access  public
  206.  */
  207.  // @@@ $strTableStructure
  208. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false)
  209. {
  210.     global $cfgRelation;
  211.  
  212.     /**
  213.      * Gets fields properties
  214.      */
  215.     PMA_mysql_select_db($db);
  216.     $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  217.     $result      = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $error_url);
  218.     $fields_cnt  = mysql_num_rows($result);
  219.  
  220.     // Check if we can use Relations (Mike Beck)
  221.     if ($do_relation && !empty($cfgRelation['relation'])) {
  222.         // Find which tables are related with the current one and write it in
  223.         // an array
  224.         $res_rel = PMA_getForeigners($db, $table);
  225.  
  226.         if ($res_rel && count($res_rel) > 0) {
  227.             $have_rel = TRUE;
  228.         } else {
  229.             $have_rel = FALSE;
  230.         }
  231.     }
  232.     else {
  233.            $have_rel = FALSE;
  234.     } // end if
  235.  
  236.     /**
  237.      * Get the unique keys in the table
  238.      */
  239.     $keys_query     = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
  240.     $keys_result    = PMA_mysql_query($keys_query) or PMA_mysqlDie('', $keys_query, '', $error_url);
  241.     $unique_keys    = array();
  242.     while($key = PMA_mysql_fetch_array($keys_result)) {
  243.         if ($key['Non_unique'] == 0) $unique_keys[] = $key['Column_name'];
  244.     }
  245.  
  246.     /**
  247.      * Displays the table structure
  248.      */
  249.     $buffer      = $crlf . '%' . $crlf . '% ' . $GLOBALS['strStructure'] . ': ' . $table  . $crlf . '%' . $crlf
  250.                  . ' \\begin{longtable}{';
  251.     if (!PMA_exportOutputHandler($buffer)) return FALSE;
  252.  
  253.     $columns_cnt = 4;
  254.     $alignment = '|l|c|c|c|';
  255.     if ($do_relation && $have_rel) {
  256.         $columns_cnt++;
  257.         $alignment .= 'l|';
  258.     }
  259.     if ($do_comments && $cfgRelation['commwork']) {
  260.         $columns_cnt++;
  261.         $alignment .= 'l|';
  262.     }
  263.     if ($do_mime && $cfgRelation['mimework']) {
  264.         $columns_cnt++;
  265.         $alignment .='l|';
  266.     }
  267.     $buffer = $alignment . '} ' . $crlf ;
  268.  
  269.     $header .= ' \\hline ';
  270.     $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'] . '}}';
  271.     if ($do_relation && $have_rel) {
  272.         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strLinksTo'] . '}}';
  273.     }
  274.     if ($do_comments && $cfgRelation['commwork']) {
  275.         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . $GLOBALS['strComments'] . '}}';
  276.         $comments = PMA_getComments($db, $table);
  277.     }
  278.     if ($do_mime && $cfgRelation['mimework']) {
  279.         $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
  280.         $mime_map = PMA_getMIME($db, $table, true);
  281.     }
  282.  
  283.     $local_buffer = PMA_texEscape($table);
  284.  
  285.     // Table caption for first page and label
  286.     if (isset($GLOBALS['latex_caption'])) {
  287.         $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_caption'])
  288.                    . '} \\label{' . str_replace('__TABLE__', $table, $GLOBALS['latex_structure_label'])
  289.                    . '} \\\\' . $crlf;
  290.     }
  291.     $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf . '\\endfirsthead' . $crlf;
  292.     // Table caption on next pages
  293.     if (isset($GLOBALS['latex_caption'])) {
  294.         $buffer .= ' \\caption{'. str_replace('__TABLE__', PMA_texEscape($table), $GLOBALS['latex_structure_continued_caption'])
  295.                    . '} \\\\ ' . $crlf;
  296.     }
  297.     $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ';
  298.  
  299.     if (!PMA_exportOutputHandler($buffer)) return FALSE;
  300.  
  301.     while ($row = PMA_mysql_fetch_array($result)) {
  302.  
  303.         $type             = $row['Type'];
  304.         // reformat mysql query output - staybyte - 9. June 2001
  305.         // loic1: set or enum types: slashes single quotes inside options
  306.         if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
  307.             $tmp[2]       = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
  308.             $type         = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  309.             $type_nowrap  = '';
  310.  
  311.             $binary       = 0;
  312.             $unsigned     = 0;
  313.             $zerofill     = 0;
  314.         } else {
  315.             $type_nowrap  = ' nowrap="nowrap"';
  316.             $type         = eregi_replace('BINARY', '', $type);
  317.             $type         = eregi_replace('ZEROFILL', '', $type);
  318.             $type         = eregi_replace('UNSIGNED', '', $type);
  319.             if (empty($type)) {
  320.                 $type     = ' ';
  321.             }
  322.  
  323.             $binary       = eregi('BINARY', $row['Type'], $test);
  324.             $unsigned     = eregi('UNSIGNED', $row['Type'], $test);
  325.             $zerofill     = eregi('ZEROFILL', $row['Type'], $test);
  326.         }
  327.         $strAttribute     = ' ';
  328.         if ($binary) {
  329.             $strAttribute = 'BINARY';
  330.         }
  331.         if ($unsigned) {
  332.             $strAttribute = 'UNSIGNED';
  333.         }
  334.         if ($zerofill) {
  335.             $strAttribute = 'UNSIGNED ZEROFILL';
  336.         }
  337.         if (!isset($row['Default'])) {
  338.             if ($row['Null'] != '') {
  339.                 $row['Default'] = 'NULL';
  340.             }
  341.         } else {
  342.             $row['Default'] = $row['Default'];
  343.         }
  344.  
  345.         $field_name = $row['Field'];
  346.  
  347.         $local_buffer = $field_name . "\000" . $type . "\000" . (($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes'])  . "\000" . (isset($row['Default']) ? $row['Default'] : '');
  348.  
  349.         if ($do_relation && $have_rel) {
  350.             $local_buffer .= "\000";
  351.             if (isset($res_rel[$field_name])) {
  352.                 $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')';
  353.             }
  354.         }
  355.         if ($do_comments && $cfgRelation['commwork']) {
  356.             $local_buffer .= "\000";
  357.             if (isset($comments[$field_name])) {
  358.                 $local_buffer .= $comments[$field_name];
  359.             }
  360.         }
  361.         if ($do_mime && $cfgRelation['mimework']) {
  362.             $local_buffer .= "\000";
  363.             if (isset($mime_map[$field_name])) {
  364.                 $local_buffer .= str_replace('_', '/', $mime_map[$field_name]['mimetype']);
  365.             }
  366.         }
  367.         $local_buffer = PMA_texEscape($local_buffer);
  368.         if ($row['Key']=='PRI') {
  369.             $pos=strpos($local_buffer, "\000");
  370.             $local_buffer = '\\textit{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
  371.         }
  372.         if (in_array($field_name, $unique_keys)) {
  373.             $pos=strpos($local_buffer, "\000");
  374.             $local_buffer = '\\textbf{' . substr($local_buffer,0,$pos) . '}' . substr($local_buffer,$pos);
  375.         }
  376.         $buffer = str_replace("\000", ' & ', $local_buffer);
  377.         $buffer .= ' \\\\ \\hline ' . $crlf;
  378.  
  379.         if (!PMA_exportOutputHandler($buffer)) return FALSE;
  380.     } // end while
  381.     mysql_free_result($result);
  382.  
  383.     $buffer = ' \\end{longtable}' . $crlf;
  384.     return PMA_exportOutputHandler($buffer);
  385. } // end of the 'PMA_getTableStructureLaTeX()' function
  386. ?>