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