Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Shared / MigrationTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\MigrationTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Core\Database\Connection;
11 use Drupal\Core\Database\Database;
12 use Drupal\Console\Core\Style\DrupalStyle;
13 use Symfony\Component\Console\Input\InputInterface;
14
15 /**
16  * Class MigrationTrait
17  *
18  * @package Drupal\Console\Command
19  */
20 trait MigrationTrait
21 {
22     protected $database;
23
24     /**
25      * @param bool $version_tag
26      * @param bool $flatList
27      *
28      * @return array list of migrations
29      */
30     protected function getMigrations($version_tag = false, $flatList = false, $configuration = [])
31     {
32         //Get migration definitions by tag
33         $migrations = array_filter(
34             $this->pluginManagerMigration->getDefinitions(), function ($migration) use ($version_tag) {
35                 return !empty($migration['migration_tags']) && in_array($version_tag, $migration['migration_tags']);
36             }
37         );
38
39         // Create an array to configure all migration plugins with same configuration
40         $keys = array_keys($migrations);
41         $migration_plugin_configuration = array_fill_keys($keys, $configuration);
42
43         //Create all migration instances
44         $all_migrations = $this->pluginManagerMigration->createInstances(array_keys($migrations), $migration_plugin_configuration);
45
46         $migrations = [];
47         foreach ($all_migrations as $migration) {
48             if ($flatList) {
49                 $migrations[$migration->id()] = ucwords($migration->label());
50             } else {
51                 $migrations[$migration->id()]['tags'] = implode(', ', $migration->getMigrationTags());
52                 $migrations[$migration->id()]['description'] = ucwords($migration->label());
53             }
54         }
55         return  $migrations;
56     }
57
58     protected function createDatabaseStateSettings(array $database, $drupal_version)
59     {
60         $database_state['key'] = 'upgrade';
61         $database_state['database'] = $database;
62         $database_state_key = 'migrate_drupal_' . $drupal_version;
63
64         $this->state->set($database_state_key, $database_state);
65         $this->state->set('migrate.fallback_state_key', $database_state_key);
66     }
67
68      /**
69      * @return mixed
70      */
71     protected function getDatabaseDrivers()
72     {
73         // Make sure the install API is available.
74         include_once DRUPAL_ROOT . '/core/includes/install.inc';
75         return drupal_get_database_types();
76     }
77
78     /**
79      * @param DrupalStyle $io
80      *
81      * @return mixed
82      */
83     public function dbDriverTypeQuestion(DrupalStyle $io)
84     {
85         $databases = $this->getDatabaseDrivers();
86
87         $dbType = $io->choice(
88             $this->trans('commands.migrate.setup.questions.db-type'),
89             array_keys($databases)
90         );
91
92         return $dbType;
93     }
94
95     /**
96      * @return mixed
97      */
98     protected function getDatabaseTypes()
99     {
100         $drupal = $this->get('site');
101         return $drupal->getDatabaseTypes();
102     }
103
104     /**
105      * Determine what version of Drupal the source database contains, copied from \Drupal\migrate_upgrade\MigrationCreationTrait
106      *
107      * @param \Drupal\Core\Database\Connection $connection
108      *
109      * @return int|FALSE
110      */
111     protected function getLegacyDrupalVersion(Connection $connection)
112     {
113         // Don't assume because a table of that name exists, that it has the columns
114         // we're querying. Catch exceptions and report that the source database is
115         // not Drupal.
116
117         // Druppal 5/6/7 can be detected by the schema_version in the system table.
118         if ($connection->schema()->tableExists('system')) {
119             try {
120                 $version_string = $connection->query('SELECT schema_version FROM {system} WHERE name = :module', [':module' => 'system'])
121                     ->fetchField();
122                 if ($version_string && $version_string[0] == '1') {
123                     if ((int) $version_string >= 1000) {
124                         $version_string = '5';
125                     } else {
126                         $version_string = false;
127                     }
128                 }
129             } catch (\PDOException $e) {
130                 $version_string = false;
131             }
132         }
133         // For Drupal 8 (and we're predicting beyond) the schema version is in the
134         // key_value store.
135         elseif ($connection->schema()->tableExists('key_value')) {
136             $result = $connection->query("SELECT value FROM {key_value} WHERE collection = :system_schema  and name = :module", [':system_schema' => 'system.schema', ':module' => 'system'])->fetchField();
137             $version_string = unserialize($result);
138         } else {
139             $version_string = false;
140         }
141
142         return $version_string ? substr($version_string, 0, 1) : false;
143     }
144
145     /**
146      * @return mixed
147      */
148     protected function getDBInfo()
149     {
150         return $this->database;
151     }
152
153     /**
154      * @param \Drupal\Console\Core\Style\DrupalStyle $io
155      * @param $target
156      * @param $key
157      */
158     protected function getDBConnection(DrupalStyle $io, $target, $key)
159     {
160         try {
161             return Database::getConnection($target, $key);
162         } catch (\Exception $e) {
163             $io->error(
164                 sprintf(
165                     '%s: %s',
166                     $this->trans('commands.migrate.execute.messages.destination-error'),
167                     $e->getMessage()
168                 )
169             );
170
171             return null;
172         }
173     }
174
175     /**
176      * @param InputInterface $input
177      * @param DrupalStyle    $io
178      */
179     protected function registerMigrateDB(InputInterface $input, DrupalStyle $io)
180     {
181         $dbType = $input->getOption('db-type');
182         $dbHost = $input->getOption('db-host');
183         $dbName = $input->getOption('db-name');
184         $dbUser = $input->getOption('db-user');
185         $dbPass = $input->getOption('db-pass');
186         $dbPrefix = $input->getOption('db-prefix');
187         $dbPort = $input->getOption('db-port');
188
189         $this->addDBConnection($io, 'upgrade', 'default', $dbType, $dbName, $dbUser, $dbPass, $dbPrefix, $dbPort, $dbHost);
190     }
191
192
193     /**
194      * @param DrupalStyle $io
195      * @param $key
196      * @param $target
197      * @param $dbType
198      * @param $dbName
199      * @param $dbUser
200      * @param $dbPass
201      * @param $dbPrefix
202      * @param $dbPort
203      * @param $dbHost
204      */
205     protected function addDBConnection(DrupalStyle $io, $key, $target, $dbType, $dbName, $dbUser, $dbPass, $dbPrefix, $dbPort, $dbHost)
206     {
207         $database_type = $this->getDatabaseDrivers();
208         $reflection = new \ReflectionClass($database_type[$dbType]);
209         $install_namespace = $reflection->getNamespaceName();
210         // Cut the trailing \Install from namespace.
211         $namespace = substr($install_namespace, 0, strrpos($install_namespace, '\\'));
212         $this->database = [
213             'database' => $dbName,
214             'username' => $dbUser,
215             'password' => $dbPass,
216             'prefix' => $dbPrefix,
217             'port' => $dbPort,
218             'host' => $dbHost,
219             'namespace' => $namespace,
220             'driver' => $dbType,
221         ];
222
223
224         try {
225             return Database::addConnectionInfo($key, $target, $this->database);
226         } catch (\Exception $e) {
227             $io->error(
228                 sprintf(
229                     '%s: %s',
230                     $this->trans('commands.migrate.execute.messages.source-error'),
231                     $e->getMessage()
232                 )
233             );
234
235             return null;
236         }
237     }
238 }