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