dump-database-d7.sh 2.5 KB

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