3 namespace Drupal\migrate_drupal\Plugin\migrate\source;
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Entity\DependencyTrait;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Exception\RequirementsException;
12 use Drupal\migrate\Plugin\migrate\source\SqlBase;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * A base class for source plugins using a Drupal database as a source.
18 * Provides general purpose helper methods that are commonly needed
19 * when writing source plugins that use a Drupal database as a source, for
21 * - Check if the given module exists in the source database.
22 * - Read Drupal configuration variables from the source database.
24 * For a full list, refer to the methods of this class.
26 * For available configuration keys, refer to the parent classes:
27 * @see \Drupal\migrate\Plugin\migrate\source\SqlBase
28 * @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
30 abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
35 * The contents of the system table.
39 protected $systemData;
42 * If the source provider is missing.
46 protected $requirements = TRUE;
51 * @var \Drupal\Core\Entity\EntityManagerInterface
53 protected $entityManager;
58 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
59 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);
60 $this->entityManager = $entity_manager;
64 * Retrieves all system data information from the source Drupal database.
67 * List of system table information keyed by type and name.
69 public function getSystemData() {
70 if (!isset($this->systemData)) {
71 $this->systemData = [];
73 $results = $this->select('system', 's')
76 foreach ($results as $result) {
77 $this->systemData[$result['type']][$result['name']] = $result;
80 catch (\Exception $e) {
81 // The table might not exist for example in tests.
84 return $this->systemData;
90 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
96 $container->get('state'),
97 $container->get('entity.manager')
104 public function checkRequirements() {
105 if ($this->pluginDefinition['requirements_met'] === TRUE) {
106 if (isset($this->pluginDefinition['source_module'])) {
107 if ($this->moduleExists($this->pluginDefinition['source_module'])) {
108 if (isset($this->pluginDefinition['minimum_schema_version']) && !$this->getModuleSchemaVersion($this->pluginDefinition['source_module']) < $this->pluginDefinition['minimum_schema_version']) {
109 throw new RequirementsException('Required minimum schema version ' . $this->pluginDefinition['minimum_schema_version'], ['minimum_schema_version' => $this->pluginDefinition['minimum_schema_version']]);
113 throw new RequirementsException('The module ' . $this->pluginDefinition['source_module'] . ' is not enabled in the source site.', ['source_module' => $this->pluginDefinition['source_module']]);
117 parent::checkRequirements();
121 * Retrieves a module schema_version from the source Drupal database.
123 * @param string $module
127 * The current module schema version on the origin system table or FALSE if
130 protected function getModuleSchemaVersion($module) {
131 $system_data = $this->getSystemData();
132 return isset($system_data['module'][$module]['schema_version']) ? $system_data['module'][$module]['schema_version'] : FALSE;
136 * Checks if a given module is enabled in the source Drupal database.
138 * @param string $module
139 * Name of module to check.
142 * TRUE if module is enabled on the origin system, FALSE if not.
144 protected function moduleExists($module) {
145 $system_data = $this->getSystemData();
146 return !empty($system_data['module'][$module]['status']);
150 * Reads a variable from a source Drupal database.
153 * Name of the variable.
158 protected function variableGet($name, $default) {
160 $result = $this->select('variable', 'v')
161 ->fields('v', ['value'])
162 ->condition('name', $name)
166 // The table might not exist.
167 catch (\Exception $e) {
170 return $result !== FALSE ? unserialize($result) : $default;
176 public function calculateDependencies() {
177 // Generic handling for Drupal source plugin constants.
178 if (isset($this->configuration['constants']['entity_type'])) {
179 $this->addDependency('module', $this->entityManager->getDefinition($this->configuration['constants']['entity_type'])->getProvider());
181 if (isset($this->configuration['constants']['module'])) {
182 $this->addDependency('module', $this->configuration['constants']['module']);
184 return $this->dependencies;