cms/drupal/scripts/dump-database-d7.sh
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 #!/usr/bin/env php
       
     2 <?php
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Dumps a Drupal 7 database into a PHP script to test the upgrade process.
       
     7  *
       
     8  * Run this script at the root of an existing Drupal 7 installation.
       
     9  *
       
    10  * The output of this script is a PHP script that can be run inside Drupal 7
       
    11  * and recreates the Drupal 7 database as dumped. Transient data from cache,
       
    12  * session, and watchdog tables are not recorded.
       
    13  */
       
    14 
       
    15 // Define default settings.
       
    16 define('DRUPAL_ROOT', getcwd());
       
    17 $cmd = 'index.php';
       
    18 $_SERVER['HTTP_HOST']       = 'default';
       
    19 $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
       
    20 $_SERVER['SERVER_SOFTWARE'] = NULL;
       
    21 $_SERVER['REQUEST_METHOD']  = 'GET';
       
    22 $_SERVER['QUERY_STRING']    = '';
       
    23 $_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
       
    24 $_SERVER['HTTP_USER_AGENT'] = 'console';
       
    25 
       
    26 // Bootstrap Drupal.
       
    27 include_once './includes/bootstrap.inc';
       
    28 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
       
    29 
       
    30 // Include the utility drupal_var_export() function.
       
    31 include_once dirname(__FILE__) . '/../includes/utility.inc';
       
    32 
       
    33 // Output the PHP header.
       
    34 $output = <<<ENDOFHEADER
       
    35 <?php
       
    36 
       
    37 /**
       
    38  * @file
       
    39  * Filled installation of Drupal 7.0, for test purposes.
       
    40  *
       
    41  * This file was generated by the dump-database-d7.sh tool, from an
       
    42  * installation of Drupal 7, filled with data using the generate-d7-content.sh
       
    43  * tool. It has the following modules installed:
       
    44 
       
    45 ENDOFHEADER;
       
    46 
       
    47 foreach (module_list() as $module) {
       
    48   $output .= " *  - $module\n";
       
    49 }
       
    50 $output .= " */\n\n";
       
    51 
       
    52 // Get the current schema, order it by table name.
       
    53 $schema = drupal_get_schema();
       
    54 ksort($schema);
       
    55 
       
    56 // Export all the tables in the schema.
       
    57 foreach ($schema as $table => $data) {
       
    58   // Remove descriptions to save time and code.
       
    59   unset($data['description']);
       
    60   foreach ($data['fields'] as &$field) {
       
    61     unset($field['description']);
       
    62   }
       
    63 
       
    64   // Dump the table structure.
       
    65   $output .= "db_create_table('" . $table . "', " . drupal_var_export($data) . ");\n";
       
    66 
       
    67   // Don't output values for those tables.
       
    68   if (substr($table, 0, 5) == 'cache' || $table == 'sessions' || $table == 'watchdog') {
       
    69     $output .= "\n";
       
    70     continue;
       
    71   }
       
    72 
       
    73   // Prepare the export of values.
       
    74   $result = db_query('SELECT * FROM {'. $table .'}', array(), array('fetch' => PDO::FETCH_ASSOC));
       
    75   $insert = '';
       
    76   foreach ($result as $record) {
       
    77     $insert .= '->values('. drupal_var_export($record) .")\n";
       
    78   }
       
    79 
       
    80   // Dump the values if there are some.
       
    81   if ($insert) {
       
    82     $output .= "db_insert('". $table . "')->fields(". drupal_var_export(array_keys($data['fields'])) .")\n";
       
    83     $output .= $insert;
       
    84     $output .= "->execute();\n";
       
    85   }
       
    86 
       
    87   $output .= "\n";
       
    88 }
       
    89 
       
    90 print $output;