PHP-BRIEF   15

dump-database-d6.sh

Guest on 10th August 2021 04:52:56 PM

  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. /**
  5.  * Dump a Drupal 6 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 6 installation.
  9.  *
  10.  * The output of this script is a PHP script that can be ran inside Drupal 7
  11.  * and recreates the Drupal 6 database as dumped. Transient data from cache
  12.  * session and watchdog tables are not recorded.
  13.  */
  14.  
  15. // Define default settings.
  16. $cmd = 'index.php';
  17. $_SERVER['HTTP_HOST']       = 'default';
  18. $_SERVER['PHP_SELF']        = '/index.php';
  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 __DIR__ . '/../includes/utility.inc';
  32.  
  33. // Output the PHP header.
  34. $output = <<<ENDOFHEADER
  35. <?php
  36.  
  37. /**
  38.  * @file
  39.  * Filled installation of Drupal 6.17, for test purposes.
  40.  *
  41.  * This file was generated by the dump-database-d6.sh tool, from an
  42.  * installation of Drupal 6, filled with data using the generate-d6-content.sh
  43.  * tool. It has the following modules installed:
  44.  
  45. ENDOFHEADER;
  46.  
  47. foreach (\Drupal::moduleHandler()->getModuleList() as $module => $filename) {
  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. // Override the field type of the filename primary key to bypass the
  57. // InnoDB 191 character limitation.
  58. if (isset($schema['system']['primary key']) && $schema['system']['primary key'] == 'filename' && isset($schema['system']['fields']['filename']['type']) && $schema['system']['fields']['filename']['type'] == 'varchar') {
  59.   $schema['system']['fields']['filename']['type'] = 'varchar_ascii';
  60. }
  61. // Export all the tables in the schema.
  62. foreach ($schema as $table => $data) {
  63.   // Remove descriptions to save time and code.
  64.   unset($data['description']);
  65.   foreach ($data['fields'] as &$field) {
  66.     unset($field['description']);
  67.   }
  68.  
  69.   // Dump the table structure.
  70.   $output .= "db_create_table('" . $table . "', " . drupal_var_export($data) . ");\n";
  71.  
  72.   // Don't output values for those tables.
  73.   if (substr($table, 0, 5) == 'cache' || $table == 'sessions' || $table == 'watchdog') {
  74.     $output .= "\n";
  75.     continue;
  76.   }
  77.  
  78.   // Prepare the export of values.
  79.   $result = db_query('SELECT * FROM {'. $table .'}');
  80.   $insert = '';
  81.   while ($record = db_fetch_array($result)) {
  82.     // users.uid is a serial and inserting 0 into a serial can break MySQL.
  83.     // So record uid + 1 instead of uid for every uid and once all records
  84.     // are in place, fix them up.
  85.     if ($table == 'users') {
  86.       $record['uid']++;
  87.     }
  88.     $insert .= '->values('. drupal_var_export($record) .")\n";
  89.   }
  90.  
  91.   // Dump the values if there are some.
  92.   if ($insert) {
  93.     $output .= "db_insert('". $table . "')->fields(". drupal_var_export(array_keys($data['fields'])) .")\n";
  94.     $output .= $insert;
  95.     $output .= "->execute();\n";
  96.   }
  97.  
  98.   // Add the statement fixing the serial in the user table.
  99.   if ($table == 'users') {
  100.     $output .= "db_query('UPDATE {users} SET uid = uid - 1');\n";
  101.   }
  102.  
  103.   $output .= "\n";
  104. }
  105.  
  106. print $output;

Raw Paste


Login or Register to edit or fork this paste. It's free.