[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/database/src/Command/ -> ExportCommand.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Database Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Database\Command;
  10  
  11  use Joomla\Archive\Archive;
  12  use Joomla\Archive\Zip;
  13  use Joomla\Console\Command\AbstractCommand;
  14  use Joomla\Database\DatabaseDriver;
  15  use Joomla\Database\Exception\UnsupportedAdapterException;
  16  use Joomla\Filesystem\File;
  17  use Symfony\Component\Console\Input\InputInterface;
  18  use Symfony\Component\Console\Output\OutputInterface;
  19  use Symfony\Component\Console\Style\SymfonyStyle;
  20  use Symfony\Component\Console\Input\InputOption;
  21  
  22  /**
  23   * Console command for exporting the database
  24   *
  25   * @since  2.0.0
  26   */
  27  class ExportCommand extends AbstractCommand
  28  {
  29      /**
  30       * The default command name
  31       *
  32       * @var    string
  33       * @since  2.0.0
  34       */
  35      protected static $defaultName = 'database:export';
  36  
  37      /**
  38       * Database connector
  39       *
  40       * @var    DatabaseDriver
  41       * @since  2.0.0
  42       */
  43      private $db;
  44  
  45      /**
  46       * Instantiate the command.
  47       *
  48       * @param   DatabaseDriver  $db  Database connector
  49       *
  50       * @since   2.0.0
  51       */
  52  	public function __construct(DatabaseDriver $db)
  53      {
  54          $this->db = $db;
  55  
  56          parent::__construct();
  57      }
  58  
  59      /**
  60       * Internal function to execute the command.
  61       *
  62       * @param   InputInterface   $input   The input to inject into the command.
  63       * @param   OutputInterface  $output  The output to inject into the command.
  64       *
  65       * @return  integer  The command exit code
  66       *
  67       * @since   2.0.0
  68       */
  69  	protected function doExecute(InputInterface $input, OutputInterface $output): int
  70      {
  71          $symfonyStyle = new SymfonyStyle($input, $output);
  72  
  73          $symfonyStyle->title('Exporting Database');
  74  
  75          $totalTime = microtime(true);
  76  
  77          if (!class_exists(File::class))
  78          {
  79              $symfonyStyle->error('The "joomla/filesystem" Composer package is not installed, cannot create an export.');
  80  
  81              return 1;
  82          }
  83  
  84          // Make sure the database supports exports before we get going
  85          try
  86          {
  87              $exporter = $this->db->getExporter()
  88                  ->withStructure();
  89          }
  90          catch (UnsupportedAdapterException $e)
  91          {
  92              $symfonyStyle->error(sprintf('The "%s" database driver does not support exporting data.', $this->db->getName()));
  93  
  94              return 1;
  95          }
  96  
  97          $folderPath = $input->getOption('folder');
  98          $tableName  = $input->getOption('table');
  99          $zip        = $input->getOption('zip');
 100  
 101          $zipFile = $folderPath . '/data_exported_' . date("Y-m-d\TH-i-s") . '.zip';
 102          $tables = $this->db->getTableList();
 103          $prefix = $this->db->getPrefix();
 104  
 105          if ($tableName)
 106          {
 107              if (!\in_array($tableName, $tables))
 108              {
 109                  $symfonyStyle->error(sprintf('The %s table does not exist in the database.', $tableName));
 110  
 111                  return 1;
 112              }
 113  
 114              $tables = [$tableName];
 115          }
 116  
 117          if ($zip)
 118          {
 119              if (!class_exists(Archive::class))
 120              {
 121                  $symfonyStyle->error('The "joomla/archive" Composer package is not installed, cannot create ZIP files.');
 122  
 123                  return 1;
 124              }
 125  
 126              /** @var Zip $zipArchive */
 127              $zipArchive = (new Archive)->getAdapter('zip');
 128          }
 129  
 130          foreach ($tables as $table)
 131          {
 132              // If an empty prefix is in use then we will dump all tables, otherwise the prefix must match
 133              if (strlen($prefix) === 0 || strpos(substr($table, 0, strlen($prefix)), $prefix) !== false)
 134              {
 135                  $taskTime = microtime(true);
 136                  $filename = $folderPath . '/' . $table . '.xml';
 137  
 138                  $symfonyStyle->text(sprintf('Processing the %s table', $table));
 139  
 140                  $data = (string) $exporter->from($table)->withData(true);
 141  
 142                  if (file_exists($filename))
 143                  {
 144                      File::delete($filename);
 145                  }
 146  
 147                  File::write($filename, $data);
 148  
 149                  if ($zip)
 150                  {
 151                      $zipFilesArray[] = ['name' => $table . '.xml', 'data' => $data];
 152                      $zipArchive->create($zipFile, $zipFilesArray);
 153                      File::delete($filename);
 154                  }
 155  
 156                  $symfonyStyle->text(sprintf('Exported data for %s in %d seconds', $table, round(microtime(true) - $taskTime, 3)));
 157              }
 158          }
 159  
 160          $symfonyStyle->success(sprintf('Export completed in %d seconds', round(microtime(true) - $totalTime, 3)));
 161  
 162          return 0;
 163      }
 164  
 165      /**
 166       * Configure the command.
 167       *
 168       * @return  void
 169       *
 170       * @since   2.0.0
 171       */
 172  	protected function configure(): void
 173      {
 174          $this->setDescription('Export the database');
 175          $this->addOption('folder', null, InputOption::VALUE_OPTIONAL, 'Path to write the export files to', '.');
 176          $this->addOption('table', null, InputOption::VALUE_REQUIRED, 'The name of the database table to export');
 177          $this->addOption('zip', null, InputOption::VALUE_NONE, 'Flag indicating the export will be saved to a ZIP archive');
 178      }
 179  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer