Pull merge.
[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 class for source plugins using a Drupal database as a source.
17  *
18  * Provides general purpose helper methods that are commonly needed
19  * when writing source plugins that use a Drupal database as a source, for
20  * example:
21  * - Check if the given module exists in the source database.
22  * - Read Drupal configuration variables from the source database.
23  *
24  * For a full list, refer to the methods of this class.
25  *
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
29  */
30 abstract class DrupalSqlBase extends SqlBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
31
32   use DependencyTrait;
33
34   /**
35    * The contents of the system table.
36    *
37    * @var array
38    */
39   protected $systemData;
40
41   /**
42    * If the source provider is missing.
43    *
44    * @var bool
45    */
46   protected $requirements = TRUE;
47
48   /**
49    * The entity manager.
50    *
51    * @var \Drupal\Core\Entity\EntityManagerInterface
52    */
53   protected $entityManager;
54
55   /**
56    * {@inheritdoc}
57    */
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;
61   }
62
63   /**
64    * Retrieves all system data information from the source Drupal database.
65    *
66    * @return array
67    *   List of system table information keyed by type and name.
68    */
69   public function getSystemData() {
70     if (!isset($this->systemData)) {
71       $this->systemData = [];
72       try {
73         $results = $this->select('system', 's')
74           ->fields('s')
75           ->execute();
76         foreach ($results as $result) {
77           $this->systemData[$result['type']][$result['name']] = $result;
78         }
79       }
80       catch (\Exception $e) {
81         // The table might not exist for example in tests.
82       }
83     }
84     return $this->systemData;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
91     return new static(
92       $configuration,
93       $plugin_id,
94       $plugin_definition,
95       $migration,
96       $container->get('state'),
97       $container->get('entity.manager')
98     );
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function checkRequirements() {
105     parent::checkRequirements();
106     if ($this->pluginDefinition['requirements_met'] === TRUE) {
107       if (isset($this->pluginDefinition['source_module'])) {
108         if ($this->moduleExists($this->pluginDefinition['source_module'])) {
109           if (isset($this->pluginDefinition['minimum_schema_version']) && !$this->getModuleSchemaVersion($this->pluginDefinition['source_module']) < $this->pluginDefinition['minimum_schema_version']) {
110             throw new RequirementsException('Required minimum schema version ' . $this->pluginDefinition['minimum_schema_version'], ['minimum_schema_version' => $this->pluginDefinition['minimum_schema_version']]);
111           }
112         }
113         else {
114           throw new RequirementsException('The module ' . $this->pluginDefinition['source_module'] . ' is not enabled in the source site.', ['source_module' => $this->pluginDefinition['source_module']]);
115         }
116       }
117     }
118   }
119
120   /**
121    * Retrieves a module schema_version from the source Drupal database.
122    *
123    * @param string $module
124    *   Name of module.
125    *
126    * @return mixed
127    *   The current module schema version on the origin system table or FALSE if
128    *   not found.
129    */
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;
133   }
134
135   /**
136    * Checks if a given module is enabled in the source Drupal database.
137    *
138    * @param string $module
139    *   Name of module to check.
140    *
141    * @return bool
142    *   TRUE if module is enabled on the origin system, FALSE if not.
143    */
144   protected function moduleExists($module) {
145     $system_data = $this->getSystemData();
146     return !empty($system_data['module'][$module]['status']);
147   }
148
149   /**
150    * Reads a variable from a source Drupal database.
151    *
152    * @param $name
153    *   Name of the variable.
154    * @param $default
155    *   The default value.
156    * @return mixed
157    */
158   protected function variableGet($name, $default) {
159     try {
160       $result = $this->select('variable', 'v')
161         ->fields('v', ['value'])
162         ->condition('name', $name)
163         ->execute()
164         ->fetchField();
165     }
166     // The table might not exist.
167     catch (\Exception $e) {
168       $result = FALSE;
169     }
170     return $result !== FALSE ? unserialize($result) : $default;
171   }
172
173   /**
174    * {@inheritdoc}
175    */
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());
180     }
181     if (isset($this->configuration['constants']['module'])) {
182       $this->addDependency('module', $this->configuration['constants']['module']);
183     }
184     return $this->dependencies;
185   }
186
187 }