dump-database-d7.sh 2.6 KB

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