You are here

function demo_dump_db in Demonstration site (Sandbox / Snapshot) 8

Same name and namespace in other branches
  1. 5 database_mysql_dump.inc \demo_dump_db()
  2. 6 database_mysql_dump.inc \demo_dump_db()
  3. 7 database_mysql_dump.inc \demo_dump_db()

Dump active database.

Parameters

$filename: The filename including path to write the dump to.

$options: An associative array of snapshot options, as described in demo_dump().

1 call to demo_dump_db()
_demo_dump in ./demo.module
Create a new snapshot.

File

./demo.module, line 136

Code

function demo_dump_db($filename, $options = []) {

  // Make sure we have permission to save our backup file.
  $directory = dirname($filename);
  if (!\Drupal::service('file_system')
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
    return FALSE;
  }
  if ($fp = fopen($filename, 'wb')) {
    $header = [];
    $header[] = '-- Demo module database dump';
    $header[] = '-- Version ' . DEMO_DUMP_VERSION;
    $header[] = '-- http://drupal.org/project/demo';
    $header[] = '--';
    $header[] = '-- Database: ' . _demo_get_database();
    $header[] = '-- Date: ' . \Drupal::service('date.formatter')
      ->format(\Drupal::time()
      ->getRequestTime(), 'small');

    // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
    // You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
    $header[] = '-- Server version: ' . \Drupal::database()
      ->query('SELECT version()')
      ->fetchField();
    $header[] = '-- PHP version: ' . PHP_VERSION;
    $header[] = '-- Drupal version: ' . VERSION;

    // Avoid auto value for zero values (required for user id 0).
    $header[] = '';
    $header[] = 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";';

    // Temporarily disable foreign key checks for the time of import.
    $header[] = 'SET FOREIGN_KEY_CHECKS = 0;';
    $header[] = '';

    // Set collations for the import. PMA and mysqldump use conditional comments
    // to exclude MySQL <4.1, but D6 requires >=4.1.
    $header[] = 'SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;';
    $header[] = 'SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;';
    $header[] = 'SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;';
    $header[] = 'SET NAMES utf8;';
    $header[] = '';
    fwrite($fp, implode("\n", $header));
    foreach ($options['tables'] as $table => $dump_options) {
      if (!_demo_table_is_view($table)) {
        if ($dump_options['schema']) {
          _demo_dump_table_schema($fp, $table);
        }
        if ($dump_options['data']) {
          _demo_dump_table_data($fp, $table);
        }
      }
    }
    $footer = [];
    $footer[] = '';

    // Re-enable foreign key checks.
    $footer[] = 'SET FOREIGN_KEY_CHECKS = 1;';

    // Revert collations for potential subsequent database queries not belonging
    // to this module.
    // @todo Double-check this behavior according to the results of
    //   http://drupal.org/node/772678
    $footer[] = 'SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT;';
    $footer[] = 'SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS;';
    $footer[] = 'SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION;';
    $footer[] = '';
    $footer[] = '';
    fwrite($fp, implode("\n", $footer));
    fclose($fp);
    return TRUE;
  }
  return FALSE;
}