Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Migrate / ExecuteCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Migrate\ExecuteCommand.
6  */
7
8 namespace Drupal\Console\Command\Migrate;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Core\Database\Database;
15 use Drupal\migrate\MigrateExecutable;
16 use Drupal\Console\Utils\MigrateExecuteMessageCapture;
17 use Drupal\Console\Command\Shared\MigrationTrait;
18 use Drupal\Console\Command\Shared\DatabaseTrait;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Core\Style\DrupalStyle;
21 use Drupal\migrate\Plugin\MigrationInterface;
22 use Drupal\State\StateInterface;
23 use Symfony\Component\Console\Command\Command;
24 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
25
26 class ExecuteCommand extends Command
27 {
28     use DatabaseTrait;
29     use MigrationTrait;
30     use CommandTrait;
31
32     protected $migrateConnection;
33
34     /**
35      * @var MigrationPluginManagerInterface $pluginManagerMigration
36      */
37     protected $pluginManagerMigration;
38
39     /**
40      * DebugCommand constructor.
41      *
42      * @param MigrationPluginManagerInterface $pluginManagerMigration
43      */
44     public function __construct(MigrationPluginManagerInterface $pluginManagerMigration)
45     {
46         $this->pluginManagerMigration = $pluginManagerMigration;
47         parent::__construct();
48     }
49
50     protected function configure()
51     {
52         $this
53             ->setName('migrate:execute')
54             ->setDescription($this->trans('commands.migrate.execute.description'))
55             ->addArgument('migration-ids', InputArgument::IS_ARRAY, $this->trans('commands.migrate.execute.arguments.id'))
56             ->addOption(
57                 'site-url',
58                 null,
59                 InputOption::VALUE_REQUIRED,
60                 $this->trans('commands.migrate.execute.options.site-url')
61             )
62             ->addOption(
63                 'db-type',
64                 null,
65                 InputOption::VALUE_REQUIRED,
66                 $this->trans('commands.migrate.setup.migrations.options.db-type')
67             )
68             ->addOption(
69                 'db-host',
70                 null,
71                 InputOption::VALUE_REQUIRED,
72                 $this->trans('commands.migrate.execute.options.db-host')
73             )
74             ->addOption(
75                 'db-name',
76                 null,
77                 InputOption::VALUE_REQUIRED,
78                 $this->trans('commands.migrate.execute.options.db-name')
79             )
80             ->addOption(
81                 'db-user',
82                 null,
83                 InputOption::VALUE_REQUIRED,
84                 $this->trans('commands.migrate.execute.options.db-user')
85             )
86             ->addOption(
87                 'db-pass',
88                 null,
89                 InputOption::VALUE_OPTIONAL,
90                 $this->trans('commands.migrate.execute.options.db-pass')
91             )
92             ->addOption(
93                 'db-prefix',
94                 null,
95                 InputOption::VALUE_OPTIONAL,
96                 $this->trans('commands.migrate.execute.options.db-prefix')
97             )
98             ->addOption(
99                 'db-port',
100                 null,
101                 InputOption::VALUE_REQUIRED,
102                 $this->trans('commands.migrate.execute.options.db-port')
103             )
104             ->addOption(
105                 'exclude',
106                 null,
107                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
108                 $this->trans('commands.migrate.execute.options.exclude'),
109                 []
110             )
111             ->addOption(
112                 'source-base_path',
113                 null,
114                 InputOption::VALUE_OPTIONAL,
115                 $this->trans('commands.migrate.execute.options.source-base_path')
116             );
117         ;
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     protected function interact(InputInterface $input, OutputInterface $output)
124     {
125         $io = new DrupalStyle($input, $output);
126
127         $validator_required = function ($value) {
128             if (!strlen(trim($value))) {
129                 throw new \Exception('The option can not be empty');
130             }
131
132             return $value;
133         };
134
135         // --site-url option
136         $site_url = $input->getOption('site-url');
137         if (!$site_url) {
138             $site_url = $io->ask(
139                 $this->trans('commands.migrate.execute.questions.site-url'),
140                 'http://www.example.com',
141                 $validator_required
142             );
143             $input->setOption('site-url', $site_url);
144         }
145
146         // --db-type option
147         $db_type = $input->getOption('db-type');
148         if (!$db_type) {
149             $db_type = $this->dbDriverTypeQuestion($io);
150             $input->setOption('db-type', $db_type);
151         }
152         
153         // --db-host option
154         $db_host = $input->getOption('db-host');
155         if (!$db_host) {
156             $db_host = $this->dbHostQuestion($io);
157             $input->setOption('db-host', $db_host);
158         }
159
160         // --db-name option
161         $db_name = $input->getOption('db-name');
162         if (!$db_name) {
163             $db_name = $this->dbNameQuestion($io);
164             $input->setOption('db-name', $db_name);
165         }
166
167         // --db-user option
168         $db_user = $input->getOption('db-user');
169         if (!$db_user) {
170             $db_user = $this->dbUserQuestion($io);
171             $input->setOption('db-user', $db_user);
172         }
173
174         // --db-pass option
175         $db_pass = $input->getOption('db-pass');
176         if (!$db_pass) {
177             $db_pass = $this->dbPassQuestion($io);
178             $input->setOption('db-pass', $db_pass);
179         }
180
181         // --db-prefix
182         $db_prefix = $input->getOption('db-prefix');
183         if (!$db_prefix) {
184             $db_prefix = $this->dbPrefixQuestion($io);
185             $input->setOption('db-prefix', $db_prefix);
186         }
187
188         // --db-port prefix
189         $db_port = $input->getOption('db-port');
190         if (!$db_port) {
191             $db_port = $this->dbPortQuestion($io);
192             $input->setOption('db-port', $db_port);
193         }
194         
195         $this->registerMigrateDB($input, $io);
196         $this->migrateConnection = $this->getDBConnection($io, 'default', 'upgrade');
197
198         if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
199             $io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
200             return 1;
201         }
202         
203         $database = $this->getDBInfo();
204         $version_tag = 'Drupal ' . $drupal_version;
205          
206         // Get migrations
207         $migrations_list = $this->getMigrations($version_tag);
208
209         // --migration-id prefix
210         $migration_id = $input->getArgument('migration-ids');
211
212         if (!in_array('all', $migration_id)) {
213             $migrations = $migrations_list;
214         } else {
215             $migrations = array_keys($this->getMigrations($version_tag));
216         }
217          
218         if (!$migration_id) {
219             $migrations_ids = [];
220  
221             while (true) {
222                 $migration_id = $io->choiceNoList(
223                     $this->trans('commands.migrate.execute.questions.id'),
224                     array_keys($migrations_list),
225                     'all'
226                 );
227
228                 if (empty($migration_id) || $migration_id == 'all') {
229                     // Only add all if it's the first option
230                     if (empty($migrations_ids) && $migration_id == 'all') {
231                         $migrations_ids[] = $migration_id;
232                     }
233                     break;
234                 } else {
235                     $migrations_ids[] = $migration_id;
236                 }
237             }
238
239             $input->setArgument('migration-ids', $migrations_ids);
240         }
241         
242         // --migration-id prefix
243         $exclude_ids = $input->getOption('exclude');
244         if (!$exclude_ids) {
245             unset($migrations_list['all']);
246             while (true) {
247                 $exclude_id = $io->choiceNoList(
248                     $this->trans('commands.migrate.execute.questions.exclude-id'),
249                     array_keys($migrations_list),
250                     null,
251                     true
252                 );
253
254                 if (empty($exclude_id)) {
255                     break;
256                 } else {
257                     unset($migrations_list[$exclude_id]);
258                     $exclude_ids[] = $exclude_id;
259                 }
260             }
261             $input->setOption('exclude', $exclude_ids);
262         }
263
264         // --source-base_path
265         $sourceBasepath = $input->getOption('source-base_path');
266         if (!$sourceBasepath) {
267             $sourceBasepath = $io->ask(
268                 $this->trans('commands.migrate.setup.questions.source-base_path'),
269                 ''
270             );
271             $input->setOption('source-base_path', $sourceBasepath);
272         }
273     }
274     
275     /**
276      * {@inheritdoc}
277      */
278     protected function execute(InputInterface $input, OutputInterface $output)
279     {
280         $io = new DrupalStyle($input, $output);
281         $migration_ids = $input->getArgument('migration-ids');
282         $exclude_ids = $input->getOption('exclude');
283
284         $sourceBasepath = $input->getOption('source-base_path');
285         $configuration['source']['constants']['source_base_path'] = rtrim($sourceBasepath, '/') . '/';
286
287
288         // If migrations weren't provided finish execution
289         if (empty($migration_ids)) {
290             return 1;
291         }
292
293         if (!$this->migrateConnection) {
294             $this->registerMigrateDB($input, $io);
295             $this->migrateConnection = $this->getDBConnection($io, 'default', 'upgrade');
296         }
297         
298         if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
299             $io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
300             return 1;
301         }
302         
303         $version_tag = 'Drupal ' . $drupal_version;
304         
305         if (!in_array('all', $migration_ids)) {
306             $migrations = $migration_ids;
307         } else {
308             $migrations = array_keys($this->getMigrations($version_tag));
309         }
310                 
311         if (!empty($exclude_ids)) {
312             // Remove exclude migration from migration script
313             $migrations = array_diff($migrations, $exclude_ids);
314         }
315         
316         if (count($migrations) == 0) {
317             $io->error($this->trans('commands.migrate.execute.messages.no-migrations'));
318             return 1;
319         }
320
321         foreach ($migrations as $migration_id) {
322             $io->info(
323                 sprintf(
324                     $this->trans('commands.migrate.execute.messages.processing'),
325                     $migration_id
326                 )
327             );
328
329             $migration_service = $this->pluginManagerMigration->createInstance($migration_id, $configuration);
330
331             if ($migration_service) {
332                 $messages = new MigrateExecuteMessageCapture();
333                 $executable = new MigrateExecutable($migration_service, $messages);
334                 $migration_status = $executable->import();
335                 switch ($migration_status) {
336                 case MigrationInterface::RESULT_COMPLETED:
337                     $io->info(
338                         sprintf(
339                             $this->trans('commands.migrate.execute.messages.imported'),
340                             $migration_id
341                         )
342                     );
343                     break;
344                 case MigrationInterface::RESULT_INCOMPLETE:
345                     $io->info(
346                         sprintf(
347                             $this->trans('commands.migrate.execute.messages.importing-incomplete'),
348                             $migration_id
349                         )
350                     );
351                     break;
352                 case MigrationInterface::RESULT_STOPPED:
353                     $io->error(
354                         sprintf(
355                             $this->trans('commands.migrate.execute.messages.import-stopped'),
356                             $migration_id
357                         )
358                     );
359                     break;
360                 case MigrationInterface::RESULT_FAILED:
361                     $io->error(
362                         sprintf(
363                             $this->trans('commands.migrate.execute.messages.import-fail'),
364                             $migration_id
365                         )
366                     );
367                     break;
368                 case MigrationInterface::RESULT_SKIPPED:
369                     $io->error(
370                         sprintf(
371                             $this->trans('commands.migrate.execute.messages.import-skipped'),
372                             $migration_id
373                         )
374                     );
375                     break;
376                 case MigrationInterface::RESULT_DISABLED:
377                     // Skip silently if disabled.
378                     break;
379                 }
380             } else {
381                 $io->error($this->trans('commands.migrate.execute.messages.fail-load'));
382
383                 return 1;
384             }
385         }
386
387         return 0;
388     }
389 }