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