Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Command / DbImportCommand.php
1 <?php
2
3 namespace Drupal\Core\Command;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Database\SchemaObjectExistsException;
8 use Symfony\Component\Console\Input\InputInterface;
9 use Symfony\Component\Console\Input\InputOption;
10 use Symfony\Component\Console\Output\OutputInterface;
11
12 /**
13  * Provides a command to import the current database from a script.
14  *
15  * This script runs on databases exported using using one of the database dump
16  * commands and imports it into the current database connection.
17  *
18  * @see \Drupal\Core\Command\DbImportApplication
19  */
20 class DbImportCommand extends DbCommandBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function configure() {
26     parent::configure();
27     $this->setName('import')
28       ->setDescription('Import database from a generation script.')
29       ->addArgument('script', InputOption::VALUE_REQUIRED, 'Import script');
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function execute(InputInterface $input, OutputInterface $output) {
36     $script = $input->getArgument('script');
37     if (!is_file($script)) {
38       $output->writeln('File must exist.');
39       return;
40     }
41
42     $connection = $this->getDatabaseConnection($input);
43     $this->runScript($connection, $script);
44     $output->writeln('Import completed successfully.');
45   }
46
47   /**
48    * Run the database script.
49    *
50    * @param \Drupal\Core\Database\Connection $connection
51    *   Connection used by the script when included.
52    * @param string $script
53    *   Path to dump script.
54    */
55   protected function runScript(Connection $connection, $script) {
56     $old_key = Database::setActiveConnection($connection->getKey());
57
58     if (substr($script, -3) == '.gz') {
59       $script = "compress.zlib://$script";
60     }
61     try {
62       require $script;
63     }
64     catch (SchemaObjectExistsException $e) {
65       throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.');
66     }
67     Database::setActiveConnection($old_key);
68   }
69
70 }