home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / export / latex.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  16.1 KB  |  474 lines

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