diff --git a/core/composer.json b/core/composer.json index dbe1db9..f88d372 100644 --- a/core/composer.json +++ b/core/composer.json @@ -101,6 +101,7 @@ "drupal/menu_ui": "self.version", "drupal/migrate": "self.version", "drupal/migrate_drupal": "self.version", + "drupal/migrate_drupal_ui": "self.version", "drupal/node": "self.version", "drupal/options": "self.version", "drupal/page_cache": "self.version", diff --git a/core/modules/migrate_drupal/src/MigrationCreationTrait.php b/core/modules/migrate_drupal/src/MigrationCreationTrait.php new file mode 100644 index 0000000..fa08e4b --- /dev/null +++ b/core/modules/migrate_drupal/src/MigrationCreationTrait.php @@ -0,0 +1,225 @@ +getConnection($database); + $system_data = []; + try { + $results = $connection->select('system', 's', [ + 'fetch' => \PDO::FETCH_ASSOC, + ]) + ->fields('s') + ->execute(); + foreach ($results as $result) { + $system_data[$result['type']][$result['name']] = $result; + } + } + catch (\Exception $e) { + // The table might not exist for example in tests. + } + return $system_data; + } + + /** + * Sets up the relevant migrations for import from a database connection. + * + * @param array $database + * Database array representing the source Drupal database. + * @param string $source_base_path + * (Optional) Address of the source Drupal site (e.g., http://example.com/). + * + * @return array + * An array of the migration templates (parsed YAML config arrays) that were + * tagged for the identified source Drupal version. The templates are + * populated with database state key and file source base path information + * for execution. The array is keyed by migration IDs. + * + * @throws \Exception + */ + protected function getMigrationTemplates(array $database, $source_base_path = '') { + // Set up the connection. + $connection = $this->getConnection($database); + if (!$drupal_version = $this->getLegacyDrupalVersion($connection)) { + throw new \Exception('Source database does not contain a recognizable Drupal version.'); + } + $database_state['key'] = 'upgrade'; + $database_state['database'] = $database; + $database_state_key = 'migrate_drupal_' . $drupal_version; + \Drupal::state()->set($database_state_key, $database_state); + + $version_tag = 'Drupal ' . $drupal_version; + + $template_storage = \Drupal::service('migrate.template_storage'); + $migration_templates = $template_storage->findTemplatesByTag($version_tag); + foreach ($migration_templates as $id => $template) { + $migration_templates[$id]['source']['database_state_key'] = $database_state_key; + // Configure file migrations so they can find the files. + if ($template['destination']['plugin'] == 'entity:file') { + if ($source_base_path) { + // Make sure we have a single trailing slash. + $source_base_path = rtrim($source_base_path, '/') . '/'; + $migration_templates[$id]['destination']['source_base_path'] = $source_base_path; + } + } + } + return $migration_templates; + } + + /** + * Gets the migrations for import. + * + * Uses the migration template connection to ensure that only the relevant + * migrations are returned. + * + * @param array $migration_templates + * Migration templates (parsed YAML config arrays), keyed by the ID. + * + * @return \Drupal\migrate\Entity\MigrationInterface[] + * The migrations for import. + */ + protected function getMigrations(array $migration_templates) { + // Let the builder service create our migration configuration entities from + // the templates, expanding them to multiple entities where necessary. + /** @var \Drupal\migrate\MigrationBuilder $builder */ + $builder = \Drupal::service('migrate.migration_builder'); + $initial_migrations = $builder->createMigrations($migration_templates); + $migrations = []; + foreach ($initial_migrations as $migration) { + try { + $source_plugin = $migration->getSourcePlugin(); + if ($source_plugin instanceof RequirementsInterface) { + $source_plugin->checkRequirements(); + } + $destination_plugin = $migration->getDestinationPlugin(); + if ($destination_plugin instanceof RequirementsInterface) { + $destination_plugin->checkRequirements(); + } + $migrations[] = $migration; + } + // Migrations which are not applicable given the source and destination + // site configurations (e.g., what modules are enabled) will be silently + // ignored. + catch (RequirementsException $e) { + } + catch (PluginNotFoundException $e) { + } + } + + return $migrations; + } + + /** + * Saves the migrations for import from the provided template connection. + * + * @param array $migration_templates + * Migration template. + * + * @return array + * The migration IDs sorted in dependency order. + */ + protected function createMigrations(array $migration_templates) { + $migration_ids = []; + $migrations = $this->getMigrations($migration_templates); + foreach ($migrations as $migration) { + // Don't try to resave migrations that already exist. + if (!Migration::load($migration->id())) { + $migration->save(); + } + $migration_ids[] = $migration->id(); + } + // loadMultiple will sort the migrations in dependency order. + return array_keys(Migration::loadMultiple($migration_ids)); + } + + /** + * Determines what version of Drupal the source database contains. + * + * @param \Drupal\Core\Database\Connection $connection + * The database connection object. + * + * @return int|FALSE + * An integer representing the major branch of Drupal core (e.g. '6' for + * Drupal 6.x), or FALSE if no valid version is matched. + */ + protected function getLegacyDrupalVersion(Connection $connection) { + // Don't assume because a table of that name exists, that it has the columns + // we're querying. Catch exceptions and report that the source database is + // not Drupal. + // Drupal 5/6/7 can be detected by the schema_version in the system table. + if ($connection->schema()->tableExists('system')) { + try { + $version_string = $connection + ->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system']) + ->fetchField(); + if ($version_string && $version_string[0] == '1') { + if ((int) $version_string >= 1000) { + $version_string = '5'; + } + else { + $version_string = FALSE; + } + } + } + catch (\PDOException $e) { + $version_string = FALSE; + } + } + // For Drupal 8 (and we're predicting beyond) the schema version is in the + // key_value store. + elseif ($connection->schema()->tableExists('key_value')) { + $result = $connection + ->query("SELECT value FROM {key_value} WHERE collection = :system_schema and name = :module", [':system_schema' => 'system.schema', ':module' => 'system']) + ->fetchField(); + $version_string = unserialize($result); + } + else { + $version_string = FALSE; + } + + return $version_string ? substr($version_string, 0, 1) : FALSE; + } + +} diff --git a/core/modules/migrate_drupal_ui/migrate_drupal_ui.info.yml b/core/modules/migrate_drupal_ui/migrate_drupal_ui.info.yml new file mode 100644 index 0000000..4a9c448 --- /dev/null +++ b/core/modules/migrate_drupal_ui/migrate_drupal_ui.info.yml @@ -0,0 +1,11 @@ +name: 'Drupal Upgrade UI' +type: module +description: 'UI for direct upgrades from older Drupal versions.' +package: 'Core (Experimental)' +version: VERSION +core: 8.x +configure: migrate_drupal_ui.upgrade +dependencies: + - migrate + - migrate_drupal + - dblog diff --git a/core/modules/migrate_drupal_ui/migrate_drupal_ui.install b/core/modules/migrate_drupal_ui/migrate_drupal_ui.install new file mode 100644 index 0000000..8fb0783 --- /dev/null +++ b/core/modules/migrate_drupal_ui/migrate_drupal_ui.install @@ -0,0 +1,16 @@ +toString(); + drupal_set_message(t('The Drupal Upgrade module has been enabled. Proceed to the upgrade form.', [':url' => $url])); +} diff --git a/core/modules/migrate_drupal_ui/migrate_drupal_ui.module b/core/modules/migrate_drupal_ui/migrate_drupal_ui.module new file mode 100644 index 0000000..a3454e9 --- /dev/null +++ b/core/modules/migrate_drupal_ui/migrate_drupal_ui.module @@ -0,0 +1,19 @@ +' . t('The Drupal Upgrade module provides a one-click upgrade from an earlier version of Drupal. For details, see the online documentation for the Drupal Upgrade module in the handbook on upgrading from previous versions.', [':migrate' => 'https://www.drupal.org/upgrade/migrate']) . '
'; + return $output; + } +} diff --git a/core/modules/migrate_drupal_ui/migrate_drupal_ui.routing.yml b/core/modules/migrate_drupal_ui/migrate_drupal_ui.routing.yml new file mode 100644 index 0000000..4b4bd08 --- /dev/null +++ b/core/modules/migrate_drupal_ui/migrate_drupal_ui.routing.yml @@ -0,0 +1,18 @@ +migrate_drupal_ui.upgrade: + path: '/upgrade' + defaults: + _form: '\Drupal\migrate_drupal_ui\Form\MigrateUpgradeForm' + _title: 'Upgrade' + requirements: + _permission: 'administer software updates' + options: + _admin_route: TRUE + +migrate_drupal_ui.log: + path: '/upgrade/log' + defaults: + _controller: '\Drupal\migrate_drupal_ui\Controller\MigrateController::showLog' + requirements: + _permission: 'administer software updates' + options: + _admin_route: TRUE diff --git a/core/modules/migrate_drupal_ui/src/Controller/MigrateController.php b/core/modules/migrate_drupal_ui/src/Controller/MigrateController.php new file mode 100644 index 0000000..5d4a885 --- /dev/null +++ b/core/modules/migrate_drupal_ui/src/Controller/MigrateController.php @@ -0,0 +1,29 @@ + 'migrate_drupal_ui']; + return $this->redirect('dblog.overview'); + } + +} diff --git a/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php new file mode 100644 index 0000000..36e1e50 --- /dev/null +++ b/core/modules/migrate_drupal_ui/src/Form/MigrateUpgradeForm.php @@ -0,0 +1,1232 @@ + [ + 'source_module' => 'system', + 'destination_module' => 'action', + ], + 'd6_aggregator_feed' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd6_aggregator_item' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd6_aggregator_settings' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd7_aggregator_feed' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd7_aggregator_item' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd7_aggregator_settings' => [ + 'source_module' => 'aggregator', + 'destination_module' => 'aggregator', + ], + 'd7_blocked_ips' => [ + 'source_module' => 'system', + 'destination_module' => 'ban', + ], + 'd6_block' => [ + 'source_module' => 'block', + 'destination_module' => 'block', + ], + 'd7_block' => [ + 'source_module' => 'block', + 'destination_module' => 'block', + ], + 'block_content_body_field' => [ + 'source_module' => 'block', + 'destination_module' => 'block_content', + ], + 'block_content_type' => [ + 'source_module' => 'block', + 'destination_module' => 'block_content', + ], + 'd6_custom_block' => [ + 'source_module' => 'block', + 'destination_module' => 'block_content', + ], + 'd7_custom_block' => [ + 'source_module' => 'block', + 'destination_module' => 'block_content', + ], + 'd6_book' => [ + 'source_module' => 'book', + 'destination_module' => 'book', + ], + 'd6_book_settings' => [ + 'source_module' => 'book', + 'destination_module' => 'book', + ], + 'd6_comment' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_entity_display' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_entity_form_display' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_entity_form_display_subject' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_field' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_field_instance' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd6_comment_type' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_entity_display' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_entity_form_display' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_entity_form_display_subject' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_field' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_field_instance' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'd7_comment_type' => [ + 'source_module' => 'comment', + 'destination_module' => 'comment', + ], + 'contact_category' => [ + 'source_module' => 'contact', + 'destination_module' => 'contact', + ], + 'd6_contact_settings' => [ + 'source_module' => 'contact', + 'destination_module' => 'contact', + ], + 'd7_contact_settings' => [ + 'source_module' => 'contact', + 'destination_module' => 'contact', + ], + 'd6_dblog_settings' => [ + 'source_module' => 'dblog', + 'destination_module' => 'dblog', + ], + 'd7_dblog_settings' => [ + 'source_module' => 'dblog', + 'destination_module' => 'dblog', + ], + 'd6_field' => [ + 'source_module' => 'content', + 'destination_module' => 'field', + ], + 'd6_field_formatter_settings' => [ + 'source_module' => 'content', + 'destination_module' => 'field', + ], + 'd6_field_instance' => [ + 'source_module' => 'content', + 'destination_module' => 'field', + ], + 'd6_field_instance_widget_settings' => [ + 'source_module' => 'content', + 'destination_module' => 'field', + ], + 'd7_field' => [ + 'source_module' => 'field', + 'destination_module' => 'field', + ], + 'd7_field_formatter_settings' => [ + 'source_module' => 'field', + 'destination_module' => 'field', + ], + 'd7_field_instance' => [ + 'source_module' => 'field', + 'destination_module' => 'field', + ], + 'd7_field_instance_widget_settings' => [ + 'source_module' => 'field', + 'destination_module' => 'field', + ], + 'd7_view_modes' => [ + 'source_module' => 'field', + 'destination_module' => 'field', + ], + 'd6_file' => [ + 'source_module' => 'system', + 'destination_module' => 'file', + ], + 'd6_file_settings' => [ + 'source_module' => 'system', + 'destination_module' => 'file', + ], + 'd6_upload' => [ + 'source_module' => 'upload', + 'destination_module' => 'file', + ], + 'd6_upload_entity_display' => [ + 'source_module' => 'upload', + 'destination_module' => 'file', + ], + 'd6_upload_entity_form_display' => [ + 'source_module' => 'upload', + 'destination_module' => 'file', + ], + 'd6_upload_field' => [ + 'source_module' => 'upload', + 'destination_module' => 'file', + ], + 'd6_upload_field_instance' => [ + 'source_module' => 'upload', + 'destination_module' => 'file', + ], + 'd7_file' => [ + 'source_module' => 'file', + 'destination_module' => 'file', + ], + 'd6_filter_format' => [ + 'source_module' => 'filter', + 'destination_module' => 'filter', + ], + 'd7_filter_format' => [ + 'source_module' => 'filter', + 'destination_module' => 'filter', + ], + 'd6_forum_settings' => [ + 'source_module' => 'forum', + 'destination_module' => 'forum', + ], + 'd7_forum_settings' => [ + 'source_module' => 'forum', + 'destination_module' => 'forum', + ], + 'd6_imagecache_presets' => [ + 'source_module' => 'imagecache', + 'destination_module' => 'image', + ], + 'd7_image_settings' => [ + 'source_module' => 'image', + 'destination_module' => 'image', + ], + 'd7_image_styles' => [ + 'source_module' => 'image', + 'destination_module' => 'image', + ], + 'd7_language_negotiation_settings' => [ + 'source_module' => 'locale', + 'destination_module' => 'language', + ], + 'language' => [ + 'source_module' => 'locale', + 'destination_module' => 'language', + ], + 'locale_settings' => [ + 'source_module' => 'locale', + 'destination_module' => 'locale', + ], + 'menu_links' => [ + 'source_module' => 'menu', + 'destination_module' => 'menu_link_content', + ], + 'menu_settings' => [ + 'source_module' => 'menu', + 'destination_module' => 'menu_ui', + ], + 'd6_node' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_revision' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_setting_promote' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_setting_status' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_setting_sticky' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_settings' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_node_type' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_view_modes' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd7_node' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd7_node_revision' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd7_node_settings' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd7_node_title_label' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd7_node_type' => [ + 'source_module' => 'node', + 'destination_module' => 'node', + ], + 'd6_url_alias' => [ + 'source_module' => 'path', + 'destination_module' => 'path', + ], + 'd7_url_alias' => [ + 'source_module' => 'path', + 'destination_module' => 'path', + ], + 'search_page' => [ + 'source_module' => 'search', + 'destination_module' => 'search', + ], + 'd6_search_settings' => [ + 'source_module' => 'search', + 'destination_module' => 'search', + ], + 'd7_search_settings' => [ + 'source_module' => 'search', + 'destination_module' => 'search', + ], + 'd7_shortcut' => [ + 'source_module' => 'shortcut', + 'destination_module' => 'shortcut', + ], + 'd7_shortcut_set' => [ + 'source_module' => 'shortcut', + 'destination_module' => 'shortcut', + ], + 'd7_shortcut_set_users' => [ + 'source_module' => 'shortcut', + 'destination_module' => 'shortcut', + ], + 'd6_simpletest_settings' => [ + 'source_module' => 'simpletest', + 'destination_module' => 'simpletest', + ], + 'd7_simpletest_settings' => [ + 'source_module' => 'simpletest', + 'destination_module' => 'simpletest', + ], + 'd6_statistics_settings' => [ + 'source_module' => 'statistics', + 'destination_module' => 'statistics', + ], + 'd6_syslog_settings' => [ + 'source_module' => 'syslog', + 'destination_module' => 'syslog', + ], + 'd7_syslog_settings' => [ + 'source_module' => 'syslog', + 'destination_module' => 'syslog', + ], + 'd6_date_formats' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_cron' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_date' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_file' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_image' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_image_gd' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_logging' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_maintenance' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_performance' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_rss' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'd6_system_site' => [ + 'source_module' => 'system', + 'destination_module' => 'system', + ], + 'menu' => [ + 'source_module' => 'menu', + 'destination_module' => 'system', + ], + 'taxonomy_settings' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_taxonomy_term' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_taxonomy_vocabulary' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_term_node' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_term_node_revision' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_vocabulary_entity_display' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_vocabulary_entity_form_display' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_vocabulary_field' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd6_vocabulary_field_instance' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd7_taxonomy_term' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'd7_taxonomy_vocabulary' => [ + 'source_module' => 'taxonomy', + 'destination_module' => 'taxonomy', + ], + 'text_settings' => [ + 'source_module' => 'text', + 'destination_module' => 'text', + ], + 'd7_tracker_node' => [ + 'source_module' => 'tracker', + 'destination_module' => 'tracker', + ], + 'd7_tracker_settings' => [ + 'source_module' => 'tracker', + 'destination_module' => 'tracker', + ], + 'd7_tracker_user' => [ + 'source_module' => 'tracker', + 'destination_module' => 'tracker', + ], + 'update_settings' => [ + 'source_module' => 'update', + 'destination_module' => 'update', + ], + 'd6_profile_values' => [ + 'source_module' => 'profile', + 'destination_module' => 'user', + ], + 'd6_user' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd6_user_contact_settings' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd6_user_mail' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd6_user_picture_file' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd6_user_role' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd6_user_settings' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd7_user' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd7_user_flood' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd7_user_mail' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'd7_user_role' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'user_picture_entity_display' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'user_picture_entity_form_display' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'user_picture_field' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'user_picture_field_instance' => [ + 'source_module' => 'user', + 'destination_module' => 'user', + ], + 'user_profile_entity_display' => [ + 'source_module' => 'profile', + 'destination_module' => 'user', + ], + 'user_profile_entity_form_display' => [ + 'source_module' => 'profile', + 'destination_module' => 'user', + ], + 'user_profile_field' => [ + 'source_module' => 'profile', + 'destination_module' => 'user', + ], + 'user_profile_field_instance' => [ + 'source_module' => 'profile', + 'destination_module' => 'user', + ], + ]; + + /** + * The state service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** + * The date formatter service. + * + * @var \Drupal\Core\Datetime\DateFormatterInterface + */ + protected $dateFormatter; + + /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** + * The migration entity storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ + protected $entityStorage; + + /** + * Constructs the MigrateUpgradeForm. + * + * @param \Drupal\Core\State\StateInterface $state + * The state service. + * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter + * The date formatter service. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. + * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage + * The migration entity storage. + */ + public function __construct(StateInterface $state, DateFormatterInterface $date_formatter, RendererInterface $renderer, EntityStorageInterface $entity_storage) { + $this->state = $state; + $this->dateFormatter = $date_formatter; + $this->renderer = $renderer; + $this->entityStorage = $entity_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('state'), + $container->get('date.formatter'), + $container->get('renderer'), + $container->get('entity_type.manager')->getStorage('migration') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'migrate_drupal_ui_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $step = $form_state->getValue('step', 'overview'); + switch ($step) { + case 'overview': + return $this->buildOverviewForm($form, $form_state); + + case 'credentials': + return $this->buildCredentialForm($form, $form_state); + + case 'confirm': + return $this->buildConfirmForm($form, $form_state); + + default: + drupal_set_message($this->t('Unrecognized form step @step', ['@step' => $step]), 'error'); + return []; + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // This method is intentionally empty, see the specific submit methods for + // each form step. + } + + /** + * Builds the form presenting an overview of the migration process. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return array + * The form structure. + */ + public function buildOverviewForm(array $form, FormStateInterface $form_state) { + $form['#title'] = $this->t('Drupal Upgrade'); + + if ($date_performed = $this->state->get('migrate_drupal_ui.performed')) { + $form['upgrade_option_item'] = [ + '#type' => 'item', + '#prefix' => '' . $this->t('An upgrade has already been performed on this site.') . '
', + '#description' => '' . $this->t('Last upgrade: @date', ['@date' => $this->dateFormatter->format($date_performed)]) . '
', + ]; + $form['upgrade_option'] = array( + '#type' => 'radios', + '#title' => $this->t('You have two options:'), + '#default_value' => static::MIGRATE_UPGRADE_INCREMENTAL, + '#options' => [ + static::MIGRATE_UPGRADE_INCREMENTAL => $this->t('Rerun: Import additional configuration and content that was not available when running the upgrade previously.'), + static::MIGRATE_UPGRADE_ROLLBACK => $this->t('Rollback: Remove content and configuration entities (such as fields and node types). Default values of other configuration will not be reverted (such as site name).'), + ], + ); + $validate = ['::validateCredentialForm']; + } + else { + $form['info_header'] = [ + '#markup' => '' . $this->t('Upgrade a Drupal site by importing it into a clean and empty new install of Drupal 8. You will lose any existing configuration once you import your site into it. See the upgrading handbook for more detailed information.', [ + ':url' => 'https://www.drupal.org/upgrade/migrate', + ]), + ]; + + $info[] = $this->t('Back up the database for this site. Upgrade will change the database for this site.'); + $info[] = $this->t('Make sure that the host this site is on has access to the database for your previous site.'); + $info[] = $this->t('If your previous site has private files to be migrated, a copy of your files directory must be accessible on the host this site is on.'); + $info[] = $this->t('In general, enable all modules on this site that are enabled on the previous site. For example, if you have used the book module on the previous site then you must enable the book module on this site for that data to be available on this site.'); + $info[] = $this->t('Put this site into maintenance mode.', [ + ':url' => Url::fromRoute('system.site_maintenance_mode')->toString(TRUE)->getGeneratedUrl(), + ]); + + $form['info'] = [ + '#theme' => 'item_list', + '#list_type' => 'ol', + '#items' => $info, + ]; + + $form['info_footer'] = [ + '#markup' => '
' . $this->t('This upgrade can take a long time. It is better to import a local copy of your site instead of directly importing from your live site.'), + ]; + + $validate = []; + } + + $form['actions'] = ['#type' => 'actions']; + $form['actions']['save'] = [ + '#type' => 'submit', + '#value' => $this->t('Continue'), + '#button_type' => 'primary', + '#validate' => $validate, + '#submit' => ['::submitOverviewForm'], + ]; + return $form; + } + + /** + * Form submission handler for the overview form. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function submitOverviewForm(array &$form, FormStateInterface $form_state) { + switch ($form_state->getValue('upgrade_option')) { + case static::MIGRATE_UPGRADE_INCREMENTAL: + $form_state->setValue('step', 'confirm'); + break; + + case static::MIGRATE_UPGRADE_ROLLBACK: + $form_state->setValue('step', 'confirm'); + break; + + default: + $form_state->setValue('step', 'credentials'); + break; + } + $form_state->setRebuild(); + } + + /** + * Builds the database credential form and adds file location information. + * + * This is largely borrowed from \Drupal\Core\Installer\Form\SiteSettingsForm. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return array + * The form structure. + * + * @todo Private files directory not yet implemented, depends on + * https://www.drupal.org/node/2547125. + */ + public function buildCredentialForm(array $form, FormStateInterface $form_state) { + $form['#title'] = $this->t('Drupal Upgrade'); + + $drivers = $this->getDatabaseTypes(); + $drivers_keys = array_keys($drivers); + // @todo https://www.drupal.org/node/2678510 Because this is a multi-step + // form, the form is not rebuilt during submission. Ideally we would get + // the chosen driver from form input, if available, in order to use + // #limit_validation_errors in the same way + // \Drupal\Core\Installer\Form\SiteSettingsForm does. + $default_driver = current($drivers_keys); + + $default_options = []; + + $form['database'] = [ + '#type' => 'details', + '#title' => $this->t('Source database'), + '#description' => $this->t('Provide credentials for the database of the Drupal site you want to upgrade.'), + '#open' => TRUE, + ]; + + $form['database']['driver'] = [ + '#type' => 'radios', + '#title' => $this->t('Database type'), + '#required' => TRUE, + '#default_value' => $default_driver, + ]; + if (count($drivers) == 1) { + $form['database']['driver']['#disabled'] = TRUE; + } + + // Add driver-specific configuration options. + foreach ($drivers as $key => $driver) { + $form['database']['driver']['#options'][$key] = $driver->name(); + + $form['database']['settings'][$key] = $driver->getFormOptions($default_options); + // @todo https://www.drupal.org/node/2678510 Using + // #limit_validation_errors in the submit does not work so it is not + // possible to require the database and username for mysql and pgsql. + // This is because this is a multi-step form. + $form['database']['settings'][$key]['database']['#required'] = FALSE; + $form['database']['settings'][$key]['username']['#required'] = FALSE; + $form['database']['settings'][$key]['#prefix'] = '
' . $this->t('Upgrade analysis report') . '
'; + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Perform upgrade'); + } + +} diff --git a/core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php b/core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php new file mode 100644 index 0000000..9e220f1 --- /dev/null +++ b/core/modules/migrate_drupal_ui/src/MigrateMessageCapture.php @@ -0,0 +1,48 @@ +messages[] = $message; + } + + /** + * Clears out any captured messages. + */ + public function clear() { + $this->messages = []; + } + + /** + * Returns any captured messages. + * + * @return array + * The captured messages. + */ + public function getMessages() { + return $this->messages; + } + +} diff --git a/core/modules/migrate_drupal_ui/src/MigrateUpgradeRunBatch.php b/core/modules/migrate_drupal_ui/src/MigrateUpgradeRunBatch.php new file mode 100644 index 0000000..669ec5e --- /dev/null +++ b/core/modules/migrate_drupal_ui/src/MigrateUpgradeRunBatch.php @@ -0,0 +1,371 @@ +addListener(MigrateEvents::POST_ROW_SAVE, [static::class, 'onPostRowSave']); + $event_dispatcher->addListener(MigrateEvents::MAP_SAVE, [static::class, 'onMapSave']); + $event_dispatcher->addListener(MigrateEvents::IDMAP_MESSAGE, [static::class, 'onIdMapMessage']); + } + else { + $event_dispatcher->addListener(MigrateEvents::POST_ROW_DELETE, [static::class, 'onPostRowDelete']); + $event_dispatcher->addListener(MigrateEvents::MAP_DELETE, [static::class, 'onMapDelete']); + } + static::$maxExecTime = ini_get('max_execution_time'); + if (static::$maxExecTime <= 0) { + static::$maxExecTime = 60; + } + // Set an arbitrary threshold of 3 seconds (e.g., if max_execution_time is + // 45 seconds, we will quit at 42 seconds so a slow item or cleanup + // overhead don't put us over 45). + static::$maxExecTime -= 3; + static::$listenersAdded = TRUE; + } + if (!isset($context['sandbox']['migration_ids'])) { + $context['sandbox']['max'] = count($initial_ids); + $context['sandbox']['current'] = 1; + // Total number processed for this migration. + $context['sandbox']['num_processed'] = 0; + // migration_ids will be the list of IDs remaining to run. + $context['sandbox']['migration_ids'] = $initial_ids; + $context['sandbox']['messages'] = []; + $context['results']['failures'] = 0; + $context['results']['successes'] = 0; + $context['results']['operation'] = $operation; + } + + // Number processed in this batch. + static::$numProcessed = 0; + + $migration_id = reset($context['sandbox']['migration_ids']); + /** @var \Drupal\migrate\Entity\Migration $migration */ + $migration = Migration::load($migration_id); + if ($migration) { + static::$messages = new MigrateMessageCapture(); + $executable = new MigrateExecutable($migration, static::$messages); + + $migration_name = $migration->label() ? $migration->label() : $migration_id; + + try { + if ($operation == 'import') { + $migration_status = $executable->import(); + } + else { + $migration_status = $executable->rollback(); + } + } + catch (\Exception $e) { + static::logger()->error($e->getMessage()); + $migration_status = MigrationInterface::RESULT_FAILED; + } + + switch ($migration_status) { + case MigrationInterface::RESULT_COMPLETED: + // Store the number processed in the sandbox. + $context['sandbox']['num_processed'] += static::$numProcessed; + if ($operation == 'import') { + $message = static::getTranslation()->formatPlural( + $context['sandbox']['num_processed'], 'Upgraded @migration (processed 1 item total)', 'Upgraded @migration (processed @num_processed items total)', + ['@migration' => $migration_name, '@num_processed' => $context['sandbox']['num_processed']]); + } + else { + $message = static::getTranslation()->formatPlural( + $context['sandbox']['num_processed'], 'Rolled back @migration (processed 1 item total)', 'Rolled back @migration (processed @num_processed items total)', + ['@migration' => $migration_name, '@num_processed' => $context['sandbox']['num_processed']]); + $migration->delete(); + } + $context['sandbox']['messages'][] = $message; + static::logger()->notice($message); + $context['sandbox']['num_processed'] = 0; + $context['results']['successes']++; + break; + + case MigrationInterface::RESULT_INCOMPLETE: + $context['sandbox']['messages'][] = static::getTranslation()->formatPlural( + static::$numProcessed, 'Continuing with @migration (processed 1 item)', 'Continuing with @migration (processed @num_processed items)', + ['@migration' => $migration_name, '@num_processed' => static::$numProcessed]); + $context['sandbox']['num_processed'] += static::$numProcessed; + break; + + case MigrationInterface::RESULT_STOPPED: + $context['sandbox']['messages'][] = t('Operation stopped by request'); + break; + + case MigrationInterface::RESULT_FAILED: + $context['sandbox']['messages'][] = t('Operation on @migration failed', ['@migration' => $migration_name]); + $context['results']['failures']++; + static::logger()->error('Operation on @migration failed', ['@migration' => $migration_name]); + break; + + case MigrationInterface::RESULT_SKIPPED: + $context['sandbox']['messages'][] = t('Operation on @migration skipped due to unfulfilled dependencies', ['@migration' => $migration_name]); + static::logger()->error('Operation on @migration skipped due to unfulfilled dependencies', ['@migration' => $migration_name]); + break; + + case MigrationInterface::RESULT_DISABLED: + // Skip silently if disabled. + break; + } + + // Unless we're continuing on with this migration, take it off the list. + if ($migration_status != MigrationInterface::RESULT_INCOMPLETE) { + array_shift($context['sandbox']['migration_ids']); + $context['sandbox']['current']++; + } + + // Add and log any captured messages. + foreach (static::$messages->getMessages() as $message) { + $context['sandbox']['messages'][] = $message; + static::logger()->error($message); + } + + // Only display the last MESSAGE_LENGTH messages, in reverse order. + $message_count = count($context['sandbox']['messages']); + $context['message'] = ''; + for ($index = max(0, $message_count - self::MESSAGE_LENGTH); $index < $message_count; $index++) { + $context['message'] = $context['sandbox']['messages'][$index] . "