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 / odt.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  15.5 KB  |  414 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Set of functions used to build CSV dumps of tables
  5.  *
  6.  * @version $Id: odt.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['odt'] = array(
  21.         'text' => 'strOpenDocumentText',
  22.         'extension' => 'odt',
  23.         'mime_type' => 'application/vnd.oasis.opendocument.text',
  24.         'force_file' => true,
  25.         'options' => array(), /* Filled later */
  26.         'options_text' => 'strOptions',
  27.         );
  28.     /* Structure options */
  29.     if (!$hide_structure) {
  30.         $plugin_list['odt']['options'][] =
  31.             array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
  32.         if (!empty($GLOBALS['cfgRelation']['relation'])) {
  33.             $plugin_list['odt']['options'][] =
  34.                 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
  35.         }
  36.         if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
  37.             $plugin_list['odt']['options'][] =
  38.                 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
  39.         }
  40.         if (!empty($GLOBALS['cfgRelation']['mimework'])) {
  41.             $plugin_list['odt']['options'][] =
  42.                 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
  43.         }
  44.         $plugin_list['odt']['options'][] =
  45.             array('type' => 'egroup');
  46.     }
  47.     /* Data */
  48.     $plugin_list['odt']['options'][] =
  49.         array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
  50.     $plugin_list['odt']['options'][] =
  51.         array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
  52.     $plugin_list['odt']['options'][] =
  53.         array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
  54.     $plugin_list['odt']['options'][] =
  55.         array('type' => 'egroup');
  56. } else {
  57.  
  58. $GLOBALS['odt_buffer'] = '';
  59. require_once './libraries/opendocument.lib.php';
  60.  
  61. /**
  62.  * Outputs comment
  63.  *
  64.  * @param   string      Text of comment
  65.  *
  66.  * @return  bool        Whether it suceeded
  67.  */
  68. function PMA_exportComment($text) {
  69.     return TRUE;
  70. }
  71.  
  72. /**
  73.  * Outputs export footer
  74.  *
  75.  * @return  bool        Whether it suceeded
  76.  *
  77.  * @access  public
  78.  */
  79. function PMA_exportFooter() {
  80.     $GLOBALS['odt_buffer'] .= '</office:text>'
  81.         . '</office:body>'
  82.         . '</office:document-content>';
  83.     if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
  84.         return FALSE;
  85.     }
  86.     return TRUE;
  87. }
  88.  
  89. /**
  90.  * Outputs export header
  91.  *
  92.  * @return  bool        Whether it suceeded
  93.  *
  94.  * @access  public
  95.  */
  96. function PMA_exportHeader() {
  97.     $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
  98.         . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
  99.         . '<office:body>'
  100.         . '<office:text>';
  101.     return TRUE;
  102. }
  103.  
  104. /**
  105.  * Outputs database header
  106.  *
  107.  * @param   string      Database name
  108.  *
  109.  * @return  bool        Whether it suceeded
  110.  *
  111.  * @access  public
  112.  */
  113. function PMA_exportDBHeader($db) {
  114.     $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDatabase'] . ' ' . $db) . '</text:h>';
  115.     return TRUE;
  116. }
  117.  
  118. /**
  119.  * Outputs database footer
  120.  *
  121.  * @param   string      Database name
  122.  *
  123.  * @return  bool        Whether it suceeded
  124.  *
  125.  * @access  public
  126.  */
  127. function PMA_exportDBFooter($db) {
  128.     return TRUE;
  129. }
  130.  
  131. /**
  132.  * Outputs create database database
  133.  *
  134.  * @param   string      Database name
  135.  *
  136.  * @return  bool        Whether it suceeded
  137.  *
  138.  * @access  public
  139.  */
  140. function PMA_exportDBCreate($db) {
  141.     return TRUE;
  142. }
  143.  
  144. /**
  145.  * Outputs the content of a table in CSV format
  146.  *
  147.  * @param   string      the database name
  148.  * @param   string      the table name
  149.  * @param   string      the end of line sequence
  150.  * @param   string      the url to go back in case of error
  151.  * @param   string      SQL query for obtaining data
  152.  *
  153.  * @return  bool        Whether it suceeded
  154.  *
  155.  * @access  public
  156.  */
  157. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
  158.     global $what;
  159.  
  160.     // Gets the data from the database
  161.     $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  162.     $fields_cnt  = PMA_DBI_num_fields($result);
  163.     $fields_meta = PMA_DBI_get_fields_meta($result);
  164.     $field_flags = array();
  165.     for ($j = 0; $j < $fields_cnt; $j++) {
  166.         $field_flags[$j] = PMA_DBI_field_flags($result, $j);
  167.     }
  168.  
  169.     $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDumpingData'] . ' ' . $table) . '</text:h>';
  170.     $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
  171.     $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
  172.  
  173.     // If required, get fields name at the first line
  174.     if (isset($GLOBALS[$what . '_columns'])) {
  175.         $GLOBALS['odt_buffer'] .= '<table:table-row>';
  176.         for ($i = 0; $i < $fields_cnt; $i++) {
  177.             $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  178.                 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
  179.                 . '</table:table-cell>';
  180.         } // end for
  181.         $GLOBALS['odt_buffer'] .= '</table:table-row>';
  182.     } // end if
  183.  
  184.     // Format the data
  185.     while ($row = PMA_DBI_fetch_row($result)) {
  186.         $GLOBALS['odt_buffer'] .= '<table:table-row>';
  187.         for ($j = 0; $j < $fields_cnt; $j++) {
  188.             if (!isset($row[$j]) || is_null($row[$j])) {
  189.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  190.                     . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
  191.                     . '</table:table-cell>';
  192.             // ignore BLOB
  193.             } elseif (stristr($field_flags[$j], 'BINARY')
  194.                     && $fields_meta[$j]->blob) {
  195.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  196.                     . '<text:p></text:p>'
  197.                     . '</table:table-cell>';
  198.             } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
  199.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
  200.                     . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
  201.                     . '</table:table-cell>';
  202.             } else {
  203.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  204.                     . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
  205.                     . '</table:table-cell>';
  206.             }
  207.         } // end for
  208.         $GLOBALS['odt_buffer'] .= '</table:table-row>';
  209.     } // end while
  210.     PMA_DBI_free_result($result);
  211.  
  212.     $GLOBALS['odt_buffer'] .= '</table:table>';
  213.  
  214.     return TRUE;
  215. }
  216.  
  217. /**
  218.  * Returns $table's structure as Open Document Text
  219.  *
  220.  * @param   string   the database name
  221.  * @param   string   the table name
  222.  * @param   string   the end of line sequence
  223.  * @param   string   the url to go back in case of error
  224.  * @param   boolean  whether to include relation comments
  225.  * @param   boolean  whether to include column comments
  226.  * @param   boolean  whether to include mime comments
  227.  * @param   string   future feature: support view dependencies
  228.  *
  229.  * @return  bool     Whether it suceeded
  230.  *
  231.  * @access  public
  232.  */
  233.  // @@@ $strTableStructure
  234. function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
  235. {
  236.     global $cfgRelation;
  237.  
  238.     /* Heading */
  239.     $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strTableStructure'] . ' ' . $table) . '</text:h>';
  240.  
  241.     /**
  242.      * Get the unique keys in the table
  243.      */
  244.     $keys_query     = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
  245.     $keys_result    = PMA_DBI_query($keys_query);
  246.     $unique_keys    = array();
  247.     while ($key = PMA_DBI_fetch_assoc($keys_result)) {
  248.         if ($key['Non_unique'] == 0) {
  249.             $unique_keys[] = $key['Column_name'];
  250.         }
  251.     }
  252.     PMA_DBI_free_result($keys_result);
  253.  
  254.     /**
  255.      * Gets fields properties
  256.      */
  257.     PMA_DBI_select_db($db);
  258.     $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  259.     $result      = PMA_DBI_query($local_query);
  260.     $fields_cnt  = PMA_DBI_num_rows($result);
  261.  
  262.     // Check if we can use Relations (Mike Beck)
  263.     if ($do_relation && !empty($cfgRelation['relation'])) {
  264.         // Find which tables are related with the current one and write it in
  265.         // an array
  266.         $res_rel = PMA_getForeigners($db, $table);
  267.  
  268.         if ($res_rel && count($res_rel) > 0) {
  269.             $have_rel = TRUE;
  270.         } else {
  271.             $have_rel = FALSE;
  272.         }
  273.     } else {
  274.            $have_rel = FALSE;
  275.     } // end if
  276.  
  277.     /**
  278.      * Displays the table structure
  279.      */
  280.     $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
  281.     $columns_cnt = 4;
  282.     if ($do_relation && $have_rel) {
  283.         $columns_cnt++;
  284.     }
  285.     if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
  286.         $columns_cnt++;
  287.     }
  288.     if ($do_mime && $cfgRelation['mimework']) {
  289.         $columns_cnt++;
  290.     }
  291.     $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
  292.     /* Header */
  293.     $GLOBALS['odt_buffer'] .= '<table:table-row>';
  294.     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  295.         . '<text:p>' . htmlspecialchars($GLOBALS['strField']) . '</text:p>'
  296.         . '</table:table-cell>';
  297.     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  298.         . '<text:p>' . htmlspecialchars($GLOBALS['strType']) . '</text:p>'
  299.         . '</table:table-cell>';
  300.     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  301.         . '<text:p>' . htmlspecialchars($GLOBALS['strNull']) . '</text:p>'
  302.         . '</table:table-cell>';
  303.     $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  304.         . '<text:p>' . htmlspecialchars($GLOBALS['strDefault']) . '</text:p>'
  305.         . '</table:table-cell>';
  306.     if ($do_relation && $have_rel) {
  307.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  308.             . '<text:p>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</text:p>'
  309.             . '</table:table-cell>';
  310.     }
  311.     if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
  312.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  313.             . '<text:p>' . htmlspecialchars($GLOBALS['strComments']) . '</text:p>'
  314.             . '</table:table-cell>';
  315.         $comments = PMA_getComments($db, $table);
  316.     }
  317.     if ($do_mime && $cfgRelation['mimework']) {
  318.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  319.             . '<text:p>' . htmlspecialchars($GLOBALS['strMIME_MIMEtype']) . '</text:p>'
  320.             . '</table:table-cell>';
  321.         $mime_map = PMA_getMIME($db, $table, true);
  322.     }
  323.     $GLOBALS['odt_buffer'] .= '</table:table-row>';
  324.  
  325.     while ($row = PMA_DBI_fetch_assoc($result)) {
  326.  
  327.         $GLOBALS['odt_buffer'] .= '<table:table-row>';
  328.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  329.             . '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
  330.             . '</table:table-cell>';
  331.         // reformat mysql query output - staybyte - 9. June 2001
  332.         // loic1: set or enum types: slashes single quotes inside options
  333.         $field_name = $row['Field'];
  334.         $type = $row['Type'];
  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     = ' ';
  350.             }
  351.  
  352.             $binary       = eregi('BINARY', $row['Type']);
  353.             $unsigned     = eregi('UNSIGNED', $row['Type']);
  354.             $zerofill     = eregi('ZEROFILL', $row['Type']);
  355.         }
  356.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  357.             . '<text:p>' . htmlspecialchars($type) . '</text:p>'
  358.             . '</table:table-cell>';
  359.         if (!isset($row['Default'])) {
  360.             if ($row['Null'] != '') {
  361.                 $row['Default'] = 'NULL';
  362.             } else {
  363.                 $row['Default'] = '';
  364.             }
  365.         } else {
  366.             $row['Default'] = $row['Default'];
  367.         }
  368.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  369.             . '<text:p>' . htmlspecialchars(($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</text:p>'
  370.             . '</table:table-cell>';
  371.         $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  372.             . '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
  373.             . '</table:table-cell>';
  374.  
  375.         if ($do_relation && $have_rel) {
  376.             if (isset($res_rel[$field_name])) {
  377.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  378.                     . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
  379.                     . '</table:table-cell>';
  380.             }
  381.         }
  382.         if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
  383.             if (isset($comments[$field_name])) {
  384.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  385.                     . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
  386.                     . '</table:table-cell>';
  387.             } else {
  388.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  389.                     . '<text:p></text:p>'
  390.                     . '</table:table-cell>';
  391.             }
  392.         }
  393.         if ($do_mime && $cfgRelation['mimework']) {
  394.             if (isset($mime_map[$field_name])) {
  395.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  396.                     . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
  397.                     . '</table:table-cell>';
  398.             } else {
  399.                 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  400.                     . '<text:p></text:p>'
  401.                     . '</table:table-cell>';
  402.             }
  403.         }
  404.         $GLOBALS['odt_buffer'] .= '</table:table-row>';
  405.     } // end while
  406.     PMA_DBI_free_result($result);
  407.  
  408.     $GLOBALS['odt_buffer'] .= '</table:table>';
  409.     return TRUE;
  410. } // end of the 'PMA_exportStructure' function
  411.  
  412. } // end else
  413. ?>
  414.