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 source class for Drupal migrate sources.
18 * Mainly to let children retrieve information from the origin system in an
21 abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
26 * The contents of the system table.
30 protected $systemData;
33 * If the source provider is missing.
37 protected $requirements = TRUE;
42 * @var \Drupal\Core\Entity\EntityManagerInterface
44 protected $entityManager;
49 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
50 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);
51 $this->entityManager = $entity_manager;
55 * Retrieves all system data information from origin system.
58 * List of system table information keyed by type and name.
60 public function getSystemData() {
61 if (!isset($this->systemData)) {
62 $this->systemData = [];
64 $results = $this->select('system', 's')
67 foreach ($results as $result) {
68 $this->systemData[$result['type']][$result['name']] = $result;
71 catch (\Exception $e) {
72 // The table might not exist for example in tests.
75 return $this->systemData;
81 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
87 $container->get('state'),
88 $container->get('entity.manager')
95 public function checkRequirements() {
96 if ($this->pluginDefinition['requirements_met'] === TRUE) {
97 if (isset($this->pluginDefinition['source_provider'])) {
98 if ($this->moduleExists($this->pluginDefinition['source_provider'])) {
99 if (isset($this->pluginDefinition['minimum_schema_version']) && !$this->getModuleSchemaVersion($this->pluginDefinition['source_provider']) < $this->pluginDefinition['minimum_schema_version']) {
100 throw new RequirementsException('Required minimum schema version ' . $this->pluginDefinition['minimum_schema_version'], ['minimum_schema_version' => $this->pluginDefinition['minimum_schema_version']]);
104 throw new RequirementsException('The module ' . $this->pluginDefinition['source_provider'] . ' is not enabled in the source site.', ['source_provider' => $this->pluginDefinition['source_provider']]);
108 parent::checkRequirements();
112 * Get a module schema_version value in the source installation.
114 * @param string $module
118 * The current module schema version on the origin system table or FALSE if
121 protected function getModuleSchemaVersion($module) {
122 $system_data = $this->getSystemData();
123 return isset($system_data['module'][$module]['schema_version']) ? $system_data['module'][$module]['schema_version'] : FALSE;
127 * Check to see if a given module is enabled in the source installation.
129 * @param string $module
130 * Name of module to check.
133 * TRUE if module is enabled on the origin system, FALSE if not.
135 protected function moduleExists($module) {
136 $system_data = $this->getSystemData();
137 return !empty($system_data['module'][$module]['status']);
141 * Read a variable from a Drupal database.
144 * Name of the variable.
149 protected function variableGet($name, $default) {
151 $result = $this->select('variable', 'v')
152 ->fields('v', ['value'])
153 ->condition('name', $name)
157 // The table might not exist.
158 catch (\Exception $e) {
161 return $result !== FALSE ? unserialize($result) : $default;
167 public function calculateDependencies() {
168 // Generic handling for Drupal source plugin constants.
169 if (isset($this->configuration['constants']['entity_type'])) {
170 $this->addDependency('module', $this->entityManager->getDefinition($this->configuration['constants']['entity_type'])->getProvider());
172 if (isset($this->configuration['constants']['module'])) {
173 $this->addDependency('module', $this->configuration['constants']['module']);
175 return $this->dependencies;