Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_upgrade / src / Commands / MigrateUpgradeCommands.php
1 <?php
2
3 namespace Drupal\migrate_upgrade\Commands;
4
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Consolidation\AnnotatedCommand\CommandData;
7 use Drupal\migrate_upgrade\MigrateUpgradeDrushRunner;
8 use Drush\Commands\DrushCommands;
9 use Drush\Exceptions\UserAbortException;
10 use Drupal\Core\State\StateInterface;
11 use Symfony\Component\Console\Command\Command;
12 use Symfony\Component\Console\Input\InputOption;
13
14 /**
15  * Migrate Upgrade drush commands.
16  */
17 class MigrateUpgradeCommands extends DrushCommands {
18
19   /**
20    * State service.
21    *
22    * @var \Drupal\Core\State\StateInterface
23    */
24   protected $state;
25
26   /**
27    * MigrateUpgradeCommands constructor.
28    *
29    * @param \Drupal\Core\State\StateInterface $state
30    *   State service.
31    */
32   public function __construct(StateInterface $state) {
33     $this->state = $state;
34   }
35
36   /**
37    * Perform one or more upgrade processes.
38    *
39    * @command migrate:upgrade
40    *
41    * @usage migrate-upgrade --legacy-db-url='mysql://root:pass@127.0.0.1/d6'
42    *   Upgrade a Drupal 6 database to Drupal 8
43    * @usage migrate-upgrade --legacy-db-key='drupal_7'
44    *   Upgrade Drupal 7 database where the connection to Drupal 7 has already
45    * been created in settings.php ($databases['drupal_7'])
46    * @usage migrate-upgrade --legacy-db-url='mysql://root:pass@127.0.0.1/d7' --configure-only --migration-prefix=d7_custom_ --legacy-root=https://www.example.com
47    *   Generate migrations for a custom migration from Drupal 7 to Drupal 8
48    *
49    * @validate-module-enabled migrate_upgrade
50    *
51    * @aliases migrate-upgrade, mup
52    *
53    * @throws \Exception
54    *   When an error occurs.
55    */
56   public function upgrade(array $options = []) {
57     $runner = new MigrateUpgradeDrushRunner($options);
58
59     $runner->configure();
60     if ($options['configure-only']) {
61       $runner->export();
62     }
63     else {
64       $runner->import();
65       $this->state->set('migrate_drupal_ui.performed', \Drupal::time()->getRequestTime());
66     }
67     // Remove the global database state.
68     $this->state->delete('migrate.fallback_state_key');
69   }
70
71   /**
72    * @hook validate migrate:upgrade
73    */
74   public function validatePassword(CommandData $commandData) {
75     $input = $commandData->input();
76     $db_url = $input->getOption('legacy-db-url');
77     $db_key = $input->getOption('legacy-db-key');
78
79     if (!$db_url && !$db_key) {
80       throw new \Exception('You must provide either a --legacy-db-url or --legacy-db-key.');
81     }
82   }
83
84   /**
85    * @hook option migrate:upgrade
86    */
87   public function legacyDatabaseUrl(Command $command, AnnotationData $annotationData) {
88     $command->addOption(
89       'legacy-db-url',
90       '',
91       InputOption::VALUE_OPTIONAL,
92       'A Drupal 6 style database URL. Required if you do not set legacy-db-key.'
93     );
94   }
95
96   /**
97    * @hook option migrate:upgrade
98    */
99   public function legacyDatabaseKey(Command $command, AnnotationData $annotationData) {
100     $command->addOption(
101       'legacy-db-key',
102       '',
103       InputOption::VALUE_OPTIONAL,
104       'A database connection key from settings.php. Use as an alternative to legacy-db-url.'
105     );
106   }
107
108   /**
109    * @hook option migrate:upgrade
110    */
111   public function legacyDatabasePrefix(Command $command, AnnotationData $annotationData) {
112     $command->addOption(
113       'legacy-db-prefix',
114       '',
115       InputOption::VALUE_OPTIONAL,
116       'Database prefix of the legacy Drupal installation.'
117     );
118   }
119
120   /**
121    * @hook option migrate:upgrade
122    */
123   public function legacyRoot(Command $command, AnnotationData $annotationData) {
124     $command->addOption(
125       'legacy-root',
126       '',
127       InputOption::VALUE_OPTIONAL,
128       'For files migrations. Site web address or file system root path (if files are local) of the legacy Drupal installation.'
129     );
130   }
131
132   /**
133    * @hook option migrate:upgrade
134    */
135   public function configureOnly(Command $command, AnnotationData $annotationData) {
136     $command->addOption(
137       'configure-only',
138       '',
139       InputOption::VALUE_NONE,
140       'Set up the appropriate upgrade processes as migrate_plus config entities but do not perform them.'
141     );
142   }
143
144   /**
145    * @hook option migrate:upgrade
146    */
147   public function migrationPrefix(Command $command, AnnotationData $annotationData) {
148     $command->addOption(
149       'migration-prefix',
150       '',
151       InputOption::VALUE_OPTIONAL,
152       'With configure-only, a prefix to apply to generated migration ids.',
153       'upgrade_'
154     );
155   }
156
157   /**
158    * Rolls back and removes upgrade migrations.
159    *
160    * @throws UserAbortException
161    *   If user chose to not perform the rollback.
162    *
163    * @command migrate:upgrade-rollback
164    * @usage migrate-upgrade-rollback
165    *   Rolls back a previously-run upgrade. It will not rollback migrations
166    *   exported as migrate_plus config entities.
167    * @validate-module-enabled migrate_upgrade
168    * @aliases migrate-upgrade-rollback, mupr
169    */
170   public function upgradeRollback() {
171     if ($date_performed = $this->state->get('migrate_drupal_ui.performed')) {
172       if ($this->io()->confirm(dt('All migrations will be rolled back. Are you sure?'))) {
173         $runner = new MigrateUpgradeDrushRunner();
174
175         $this->logger()->notice(dt('Rolling back the upgrades performed @date',
176           ['@date' => \Drupal::service('date.formatter')->format($date_performed)]));
177         $runner->rollback();
178         $this->state->delete('migrate_drupal_ui.performed');
179         $this->logger()->notice(dt('Rolled back upgrades'));
180       }
181       else {
182         throw new UserAbortException();
183       }
184     }
185     else {
186       $this->logger()->warning(dt('No upgrade operation has been performed.'));
187     }
188   }
189
190 }