[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libraries/export/ -> csv.php (source)

   1  <?php
   2  /* $Id: csv.php 9549 2006-10-13 08:14:31Z nijel $ */
   3  // vim: expandtab sw=4 ts=4 sts=4:
   4  
   5  /**
   6   * Set of functions used to build CSV dumps of tables
   7   */
   8  
   9  if (isset($plugin_list)) {
  10      $plugin_list['csv'] = array(
  11          'text' => 'strStrucCSV',
  12          'extension' => 'csv',
  13          'mime_type' => 'text/comma-separated-values',
  14          'options' => array(
  15              array('type' => 'text', 'name' => 'separator', 'text' => 'strFieldsTerminatedBy'),
  16              array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy'),
  17              array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy'),
  18              array('type' => 'text', 'name' => 'terminated', 'text' => 'strLinesTerminatedBy'),
  19              array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy'),
  20              array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames'),
  21              array('type' => 'hidden', 'name' => 'data'),
  22              ),
  23          'options_text' => 'strCSVOptions',
  24          );
  25  } else {
  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 TRUE;
  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 $what;
  58      global $csv_terminated;
  59      global $csv_separator;
  60      global $csv_enclosed;
  61      global $csv_escaped;
  62  
  63      // Here we just prepare some values for export
  64      if ($what == 'excel') {
  65          $csv_terminated      = "\015\012";
  66          $csv_separator          = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ? ';' : ',';
  67          $csv_enclosed           = '"';
  68          $csv_escaped            = '"';
  69          if (isset($GLOBALS['excel_columns'])) {
  70              $GLOBALS['csv_columns'] = 'yes';
  71          }
  72      } else {
  73          if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
  74              $csv_terminated  = $GLOBALS['crlf'];
  75          } else {
  76              $csv_terminated  = str_replace('\\r', "\015", $csv_terminated);
  77              $csv_terminated  = str_replace('\\n', "\012", $csv_terminated);
  78              $csv_terminated  = str_replace('\\t', "\011", $csv_terminated);
  79          } // end if
  80          $csv_separator          = str_replace('\\t', "\011", $csv_separator);
  81      }
  82      return TRUE;
  83  }
  84  
  85  /**
  86   * Outputs database header
  87   *
  88   * @param   string      Database name
  89   *
  90   * @return  bool        Whether it suceeded
  91   *
  92   * @access  public
  93   */
  94  function PMA_exportDBHeader($db) {
  95      return TRUE;
  96  }
  97  
  98  /**
  99   * Outputs database footer
 100   *
 101   * @param   string      Database name
 102   *
 103   * @return  bool        Whether it suceeded
 104   *
 105   * @access  public
 106   */
 107  function PMA_exportDBFooter($db) {
 108      return TRUE;
 109  }
 110  
 111  /**
 112   * Outputs create database database
 113   *
 114   * @param   string      Database name
 115   *
 116   * @return  bool        Whether it suceeded
 117   *
 118   * @access  public
 119   */
 120  function PMA_exportDBCreate($db) {
 121      return TRUE;
 122  }
 123  
 124  /**
 125   * Outputs the content of a table in CSV format
 126   *
 127   * @param   string      the database name
 128   * @param   string      the table name
 129   * @param   string      the end of line sequence
 130   * @param   string      the url to go back in case of error
 131   * @param   string      SQL query for obtaining data
 132   *
 133   * @return  bool        Whether it suceeded
 134   *
 135   * @access  public
 136   */
 137  function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
 138      global $what;
 139      global $csv_terminated;
 140      global $csv_separator;
 141      global $csv_enclosed;
 142      global $csv_escaped;
 143  
 144      // Gets the data from the database
 145      $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
 146      $fields_cnt  = PMA_DBI_num_fields($result);
 147  
 148      // If required, get fields name at the first line
 149      if (isset($GLOBALS['csv_columns'])) {
 150          $schema_insert = '';
 151          for ($i = 0; $i < $fields_cnt; $i++) {
 152              if ($csv_enclosed == '') {
 153                  $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
 154              } else {
 155                  $schema_insert .= $csv_enclosed
 156                                 . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
 157                                 . $csv_enclosed;
 158              }
 159              $schema_insert     .= $csv_separator;
 160          } // end for
 161          $schema_insert  =trim(substr($schema_insert, 0, -1));
 162          if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
 163              return FALSE;
 164          }
 165      } // end if
 166  
 167      // Format the data
 168      while ($row = PMA_DBI_fetch_row($result)) {
 169          $schema_insert = '';
 170          for ($j = 0; $j < $fields_cnt; $j++) {
 171              if (!isset($row[$j]) || is_null($row[$j])) {
 172                  $schema_insert .= $GLOBALS[$what . '_null'];
 173              } elseif ($row[$j] == '0' || $row[$j] != '') {
 174                  // loic1 : always enclose fields
 175                  if ($what == 'excel') {
 176                      $row[$j]       = ereg_replace("\015(\012)?", "\012", $row[$j]);
 177                  }
 178                  if ($csv_enclosed == '') {
 179                      $schema_insert .= $row[$j];
 180                  } else {
 181                      $schema_insert .= $csv_enclosed
 182                                     . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
 183                                     . $csv_enclosed;
 184                  }
 185              } else {
 186                  $schema_insert .= '';
 187              }
 188              if ($j < $fields_cnt-1) {
 189                  $schema_insert .= $csv_separator;
 190              }
 191          } // end for
 192  
 193          if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
 194              return FALSE;
 195          }
 196      } // end while
 197      PMA_DBI_free_result($result);
 198  
 199      return TRUE;
 200  } // end of the 'PMA_getTableCsv()' function
 201  
 202  }
 203  ?>


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics