You are here

function dba_export_table in Database Administration 7

1 call to dba_export_table()
dba_export_tables_submit in ./dba.admin.inc
Perform export of one or more tables.

File

./dba.admin.inc, line 382

Code

function dba_export_table($table, $options = array()) {

  // Determine which fields are currently active
  $checked = NULL;
  if (isset($_SESSION["dba_view_fields_{$table}"])) {
    $checked = $_SESSION["dba_view_fields_{$table}"];
    if (!count($checked)) {
      $checked = NULL;
    }
  }
  $unprefixed_table = dba_strip_prefix($table);
  $fields = drupal_schema_fields_sql($unprefixed_table);
  if (count($checked)) {
    $active = array();
    foreach ($fields as $key => $field) {
      $index = $key + 1;
      if (isset($checked[$index]) && $checked[$index] == $index) {
        $active[] = $field;
      }
    }
    $fields = $active;
  }
  $num_fields = sizeof($fields);
  $output = '';
  switch ($options['format']) {
    case DBA_EXPORT_MYSQLDUMP:
      $output = "--\n";
      $output .= "-- Table structure for table '{$table}'\n";
      $output .= "--\n\n";
      $return = dba_show_create_table($table);
      if ($return !== FALSE) {
        if ($options['droptable']) {
          $output .= "DROP TABLE IF EXISTS {$table};\n";
        }
        $output .= $return;
        $output .= ";\n\n";
      }
      else {
        $driver = db_driver();
        $output .= "--\n";
        $output .= "-- ERROR: Unable to dump table structure for {$driver} tables\n";
        $output .= "--\n\n";
      }
      $output .= "--\n";
      $output .= "-- Dumping data for table '{$table}'\n";
      $output .= "--\n\n";
      break;
    case DBA_EXPORT_CSV:
      if ($options['header']) {
        $output = implode(',', $fields) . "\r\n";
      }
      break;
    case DBA_EXPORT_HTML:
    case DBA_EXPORT_OOCALC:
    case DBA_EXPORT_OOWRITE:
    case DBA_EXPORT_MSEXCEL:
    case DBA_EXPORT_MSWORD:
      if ($options['tablename']) {
        echo "<h2>{$table}</h2>\r\n";
      }
      $variables = array();
      $variables['header'] = $fields;
      break;
  }
  echo $output;

  // SELECT * FROM $table;
  $query = db_select($unprefixed_table)
    ->fields($unprefixed_table);

  // Add tags to allow query altering
  $rows = $query
    ->addTag('dba')
    ->addTag('export')
    ->execute();
  foreach ($rows as $row) {
    switch ($options['format']) {
      case DBA_EXPORT_MYSQLDUMP:
        $line = "INSERT INTO {$table} VALUES(";
        $i = 0;
        foreach ($row as $field => $value) {
          $value = addslashes($value);
          $value = preg_replace("/\n/", "\\n", $value);
          $line .= isset($value) ? "\"{$value}\"" : "\"\"";
          $line .= ++$i < $num_fields ? ',' : ");\n";
        }
        echo $line;
        break;
      case DBA_EXPORT_CSV:
        $values = '';
        foreach ($row as $field => $value) {
          $values[] = '"' . str_replace('"', '""', decode_entities(strip_tags($value))) . '"';
        }
        echo implode(',', $values) . "\r\n";
        break;
      case DBA_EXPORT_HTML:
      case DBA_EXPORT_OOCALC:
      case DBA_EXPORT_OOWRITE:
      case DBA_EXPORT_MSEXCEL:
      case DBA_EXPORT_MSWORD:
        $r = array();
        foreach ($row as $field => $value) {
          $r[]['data'] = decode_entities(strip_tags($value));
        }
        $variables['rows'][] = $r;
    }
  }
  switch ($options['format']) {
    case DBA_EXPORT_MYSQLDUMP:
    case DBA_EXPORT_CSV:
      echo "\n";
      break;
    case DBA_EXPORT_HTML:
    case DBA_EXPORT_OOCALC:
    case DBA_EXPORT_OOWRITE:
    case DBA_EXPORT_MSEXCEL:
    case DBA_EXPORT_MSWORD:
      echo theme('table', $variables) . "\n";
      break;
  }
}