Version 1
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / source / DrupalSqlBase.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\source;
4
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;
14
15 /**
16  * A base source class for Drupal migrate sources.
17  *
18  * Mainly to let children retrieve information from the origin system in an
19  * easier way.
20  */
21 abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
22
23   use DependencyTrait;
24
25   /**
26    * The contents of the system table.
27    *
28    * @var array
29    */
30   protected $systemData;
31
32   /**
33    * If the source provider is missing.
34    *
35    * @var bool
36    */
37   protected $requirements = TRUE;
38
39   /**
40    * The entity manager.
41    *
42    * @var \Drupal\Core\Entity\EntityManagerInterface
43    */
44   protected $entityManager;
45
46   /**
47    * {@inheritdoc}
48    */
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;
52   }
53
54   /**
55    * Retrieves all system data information from origin system.
56    *
57    * @return array
58    *   List of system table information keyed by type and name.
59    */
60   public function getSystemData() {
61     if (!isset($this->systemData)) {
62       $this->systemData = [];
63       try {
64         $results = $this->select('system', 's')
65           ->fields('s')
66           ->execute();
67         foreach ($results as $result) {
68           $this->systemData[$result['type']][$result['name']] = $result;
69         }
70       }
71       catch (\Exception $e) {
72         // The table might not exist for example in tests.
73       }
74     }
75     return $this->systemData;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
82     return new static(
83       $configuration,
84       $plugin_id,
85       $plugin_definition,
86       $migration,
87       $container->get('state'),
88       $container->get('entity.manager')
89     );
90   }
91
92   /**
93    * {@inheritdoc}
94    */
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']]);
101           }
102         }
103         else {
104           throw new RequirementsException('The module ' . $this->pluginDefinition['source_provider'] . ' is not enabled in the source site.', ['source_provider' => $this->pluginDefinition['source_provider']]);
105         }
106       }
107     }
108     parent::checkRequirements();
109   }
110
111   /**
112    * Get a module schema_version value in the source installation.
113    *
114    * @param string $module
115    *   Name of module.
116    *
117    * @return mixed
118    *   The current module schema version on the origin system table or FALSE if
119    *   not found.
120    */
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;
124   }
125
126   /**
127    * Check to see if a given module is enabled in the source installation.
128    *
129    * @param string $module
130    *   Name of module to check.
131    *
132    * @return bool
133    *   TRUE if module is enabled on the origin system, FALSE if not.
134    */
135   protected function moduleExists($module) {
136     $system_data = $this->getSystemData();
137     return !empty($system_data['module'][$module]['status']);
138   }
139
140   /**
141    * Read a variable from a Drupal database.
142    *
143    * @param $name
144    *   Name of the variable.
145    * @param $default
146    *   The default value.
147    * @return mixed
148    */
149   protected function variableGet($name, $default) {
150     try {
151       $result = $this->select('variable', 'v')
152         ->fields('v', ['value'])
153         ->condition('name', $name)
154         ->execute()
155         ->fetchField();
156     }
157     // The table might not exist.
158     catch (\Exception $e) {
159       $result = FALSE;
160     }
161     return $result !== FALSE ? unserialize($result) : $default;
162   }
163
164   /**
165    * {@inheritdoc}
166    */
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());
171     }
172     if (isset($this->configuration['constants']['module'])) {
173       $this->addDependency('module', $this->configuration['constants']['module']);
174     }
175     return $this->dependencies;
176   }
177
178 }