Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add site:export and database:dump commands #151

Merged
merged 7 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Joomlatools/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function getDefaultCommands()
$commands = array_merge($commands, array(
new Command\Database\Install(),
new Command\Database\Drop(),
new Command\Database\Dump(),
new Command\Database\Export(),

new Command\Extension\Install(),
new Command\Extension\Register(),
Expand Down
75 changes: 58 additions & 17 deletions src/Joomlatools/Console/Command/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ protected function configure()
"MySQL driver",
'mysqli'
)
->addOption(
// @TODO To be removed in 1.6
'mysql_db_prefix',
null,
InputOption::VALUE_REQUIRED,
"[DEPRECATED] MySQL database prefix"
)
;
}

Expand All @@ -82,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$db_name = $input->getOption('mysql-database');
if (empty($db_name))
{
$this->target_db_prefix = $input->getOption('mysql_db_prefix') ?: $input->getOption('mysql-db-prefix');
$this->target_db_prefix = $input->getOption('mysql-db-prefix');
$this->target_db = $this->target_db_prefix.$this->site;
}
else
Expand Down Expand Up @@ -110,21 +103,69 @@ protected function execute(InputInterface $input, OutputInterface $output)

protected function _backupDatabase($target_file)
{
$password = empty($this->mysql->password) ? '' : sprintf("-p'%s'", $this->mysql->password);
$this->_executeMysqldump(sprintf("--skip-dump-date --skip-extended-insert --no-tablespaces %s > %s", $this->target_db, $target_file));
}

exec(sprintf("mysqldump --host=%s --port=%u -u'%s' %s %s > %s", $this->mysql->host, $this->mysql->port, $this->mysql->user, $password, $this->target_db, $target_file));
protected function _executePDO($query, $database = null) {
$database = $database ?: $this->target_db;
$connectionString = "mysql:host={$this->mysql->host}:{$this->mysql->port};dbname={$database};charset=utf8mb4";
$pdoDB = new \PDO($connectionString, $this->mysql->user, $this->mysql->password);
$pdoDB->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

if (!file_exists($target_file)) {
throw new \RuntimeException(sprintf('Failed to backup database "%s"!', $this->target_db));
}
return $pdoDB->query($query);
}

protected function _executeSQL($query)
protected function _executeSQL($query, $database = '')
{
$password = empty($this->mysql->password) ? '' : sprintf("--password='%s'", $this->mysql->password);
$cmd = sprintf("echo '$query' | mysql --host=%s --port=%u --user='%s' %s", $this->mysql->host, $this->mysql->port, $this->mysql->user, $password);
return $this->_executeMysqlWithCredentials(function($path) use($query, $database) {
return "echo '$query' | mysql --defaults-extra-file=$path $database";
});
}

return exec($cmd);
protected function _executeMysql($command)
{
return $this->_executeMysqlWithCredentials(function($path) use($command) {
return "mysql --defaults-extra-file=$path $command";
});
}

protected function _executeMysqldump($command)
{
return $this->_executeMysqlWithCredentials(function($path) use($command) {
return "mysqldump --defaults-extra-file=$path $command";
});
}

/**
* Write a temporary --defaults-extra-file file and execute a Mysql command given from the callback
*
* @param callable $callback Receives a single string with the path to the --defaults-extra-file path
* @return void
*/
private function _executeMysqlWithCredentials(callable $callback)
{
try {
$file = tmpfile();
$path = stream_get_meta_data($file)['uri'];

$contents = <<<STR
[client]
user={$this->mysql->user}
password={$this->mysql->password}
host={$this->mysql->host}
port={$this->mysql->port}
STR;

fwrite($file, $contents);


return exec($callback($path));
}
finally {
if (\is_resource($file)) {
\fclose($file);
}
}
}

protected function _promptDatabaseDetails(InputInterface $input, OutputInterface $output)
Expand Down
11 changes: 0 additions & 11 deletions src/Joomlatools/Console/Command/Database/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$this->check($input, $output);

$result = $this->_executeSQL(sprintf("DROP DATABASE IF EXISTS `%s`", $this->target_db));

if (!empty($result)) {
Expand All @@ -36,13 +34,4 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

public function check(InputInterface $input, OutputInterface $output)
{
$result = $this->_executeSQL(sprintf("SHOW DATABASES LIKE \"%s\"", $this->target_db));

if (empty($result)) {
throw new \RuntimeException(sprintf('Database %s does not exist', $this->target_db));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* @author Steven Rombauts <https://github.com/stevenrombauts>
* @package Joomlatools\Console
*/
class Dump extends AbstractDatabase
class Export extends AbstractDatabase
{
protected function configure()
{
parent::configure();

$this->setName('database:dump')
$this->setName('database:export')
->addOption(
'folder',
null,
Expand All @@ -41,7 +41,13 @@ protected function configure()
"File name for the backup. Defaults to sitename_date.format",
null
)
->setDescription('Dump the database of a site');
->addOption(
'per-table',
null,
InputOption::VALUE_NONE,
"If set, each table will be exported into a separate file",
)
->setDescription('Export the database of a site');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -50,10 +56,30 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->check();

$path = $input->getOption('folder') ?? $this->target_dir;
$path .= '/'.($input->getOption('filename') ?? $this->site.'_database_'.date('Y-m-d').'.sql');
$folder = $input->getOption('folder') ?? $this->target_dir;

if (!\is_dir($folder)) {
@mkdir($folder, 0755, true);

if (!\is_dir($folder)) {
throw new \RuntimeException("Folder $folder doesn't exist.");
}
}

if ($input->getOption('per-table'))
{
$statement = $this->_executePDO('show tables');

$this->_backupDatabase($path);
while (($table = $statement->fetchColumn()) !== false) {

$this->_executeMysqldump(sprintf("--skip-dump-date --skip-comments --skip-extended-insert --no-tablespaces %s %s > %s", $this->target_db, $table, $folder.'/'.$table.'.sql'));
}

} else {
$path = $folder.'/'.($input->getOption('filename') ?? $this->site.'_database_'.date('Y-m-d').'.sql');

$this->_backupDatabase($path);
}

return 0;
}
Expand All @@ -63,5 +89,12 @@ public function check()
if (!file_exists($this->target_dir)) {
throw new \RuntimeException(sprintf('The site %s does not exist', $this->site));
}

$result = $this->_executeSQL(sprintf("SHOW DATABASES LIKE \"%s\"", $this->target_db));

if (empty($result)) {
throw new \RuntimeException(sprintf('Database %s does not exist', $this->target_db));
}

}
}
10 changes: 3 additions & 7 deletions src/Joomlatools/Console/Command/Database/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$password = empty($this->mysql->password) ? '' : sprintf("-p'%s'", $this->mysql->password);

$this->drop = $input->getOption('drop');
$this->skip_check = $input->getOption('skip-exists-check');
$this->skip_create_statement = $input->getOption('skip-create-statement');
Expand Down Expand Up @@ -119,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

file_put_contents($tmp, $contents);

$result = exec(sprintf("mysql --host=%s --port=%u --user='%s' %s %s < %s", $this->mysql->host, $this->mysql->port, $this->mysql->user, $password, $this->target_db, $tmp));
$result = $this->_executeMysql(sprintf("%s < %s", $this->target_db, $tmp));

unlink($tmp);

Expand All @@ -143,10 +141,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$version = Util::getJoomlaVersion($this->target_dir);

$executeQuery = function ($sql) use ($password) {
$command = sprintf("mysql --host=%s --port=%u --user='%s' %s %s -e %s", $this->mysql->host, $this->mysql->port, $this->mysql->user, $password, $this->target_db, escapeshellarg($sql));

exec ($command);
$executeQuery = function($sql) {
$this->_executeMysql(sprintf("%s -e %s", $this->target_db, escapeshellarg($sql)));
};

$executeQuery("REPLACE INTO j_schemas (extension_id, version_id) VALUES (700, '$schema');");
Expand Down
2 changes: 1 addition & 1 deletion src/Joomlatools/Console/Command/Site/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'--www' => $this->www
);

$optionalArgs = array('sample-data', 'symlink', 'projects-dir', 'interactive', 'mysql-login', 'mysql_db_prefix', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'options', 'skip-create-statement', 'use-webroot-dir');
$optionalArgs = array('sample-data', 'symlink', 'projects-dir', 'interactive', 'mysql-login', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'options', 'skip-create-statement', 'use-webroot-dir');
foreach ($optionalArgs as $optionalArg)
{
$value = $input->getOption($optionalArg);
Expand Down
6 changes: 1 addition & 5 deletions src/Joomlatools/Console/Command/Site/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public function check(InputInterface $input, OutputInterface $output)
if (getcwd() === $this->target_dir && getcwd() !== $this->www) {
throw new \RuntimeException('You are currently in the directory you are trying to delete. Aborting');
}

if (!is_dir($this->target_dir)) {
throw new \RuntimeException(sprintf('The site %s does not exist!', $this->site));
}
}

public function deleteDirectory(InputInterface $input, OutputInterface $output)
Expand All @@ -78,7 +74,7 @@ public function deleteDatabase(InputInterface $input, OutputInterface $output)
'site' => $this->site
);

$optionalArgs = array('mysql-login', 'mysql_db_prefix', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database');
$optionalArgs = array('mysql-login', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database');
foreach ($optionalArgs as $optionalArg)
{
$value = $input->getOption($optionalArg);
Expand Down
63 changes: 5 additions & 58 deletions src/Joomlatools/Console/Command/Site/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

use Joomlatools\Console\Command;
use Joomlatools\Console\Command\Database;
use Joomlatools\Console\Command\Vhost;
use Joomlatools\Console\Joomla\Util;

class Install extends Database\AbstractDatabase
{
Expand Down Expand Up @@ -115,11 +113,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->installExtensions($input, $output);
}

$this->_enableWebInstaller($input, $output);

$output->writeln("Your new Joomla site has been configured.");
$output->writeln("Your new Joomla site has been created.");
$output->writeln("It was installed using the domain name <info>$this->site.test</info>.");
$output->writeln("You can login using the following username and password combination: <info>admin</info>/<info>admin</info>.");


return 0;
}

Expand All @@ -138,7 +136,7 @@ public function importdb(InputInterface $input, OutputInterface $output)
'--www' => $this->www
);

$optionalArgs = array('sample-data', 'drop', 'mysql-login', 'mysql_db_prefix', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'skip-exists-check', 'skip-create-statement', 'www', 'use-webroot-dir');
$optionalArgs = array('sample-data', 'drop', 'mysql-login', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'skip-exists-check', 'skip-create-statement', 'www', 'use-webroot-dir');
foreach ($optionalArgs as $optionalArg)
{
$value = $input->getOption($optionalArg);
Expand All @@ -163,7 +161,7 @@ public function createConfig(InputInterface $input, OutputInterface $output)
'--www' => $this->www
);

$optionalArgs = array('overwrite', 'mysql-login', 'mysql_db_prefix', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'mysql-driver', 'interactive', 'options', 'www', 'use-webroot-dir');
$optionalArgs = array('overwrite', 'mysql-login', 'mysql-db-prefix', 'mysql-host', 'mysql-port', 'mysql-database', 'mysql-driver', 'interactive', 'options', 'www', 'use-webroot-dir');
foreach ($optionalArgs as $optionalArg)
{
$value = $input->getOption($optionalArg);
Expand Down Expand Up @@ -204,55 +202,4 @@ public function installExtensions(InputInterface $input, OutputInterface $output

$installer->run($extension_input, $output);
}

protected function _enableWebInstaller(InputInterface $input, OutputInterface $output)
{
$version = Util::getJoomlaVersion($this->target_dir);

if (version_compare($version->release, '3.2.0', '<')) {
return;
}

$xml = simplexml_load_file('http://appscdn.joomla.org/webapps/jedapps/webinstaller.xml');

if(!$xml)
{
$output->writeln('<warning>Failed to install web installer</warning>');

return;
}

$url = '';
foreach($xml->update->downloads->children() as $download)
{
$attributes = $download->attributes();
if($attributes->type == 'full' && $attributes->format == 'zip')
{
$url = (string) $download;
break;
}
}

if(empty($url)) {
return;
}

$filename = Util::getWritablePath().'/cache/'.basename($url);
if(!file_exists($filename))
{
$bytes = file_put_contents($filename, fopen($url, 'r'));
if($bytes === false || $bytes == 0) {
return;
}
}

`mkdir -p $this->target_dir/plugins/installer`;
`cd $this->target_dir/plugins/installer/ && unzip -o $filename`;

$sql = "INSERT INTO `j_extensions` (`name`, `type`, `element`, `folder`, `enabled`, `access`, `manifest_cache`) VALUES ('plg_installer_webinstaller', 'plugin', 'webinstaller', 'installer', 1, 1, '{\"name\":\"plg_installer_webinstaller\",\"type\":\"plugin\",\"version\":\"".$xml->update->version."\",\"description\":\"Web Installer\"}');";
$sql = escapeshellarg($sql);

$password = empty($this->mysql->password) ? '' : sprintf("-p'%s'", $this->mysql->password);
exec(sprintf("mysql --host=%s --port=%u -u'%s' %s %s -e %s", $this->mysql->host, $this->mysql->port, $this->mysql->user, $password, $this->target_db, $sql));
}
}
6 changes: 0 additions & 6 deletions src/Joomlatools/Console/Command/Vhost/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Joomlatools\Console\Command\Vhost;

use Joomlatools\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -49,10 +47,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

if (!file_exists($this->target_dir)) {
throw new \RuntimeException(sprintf('Site not found: %s', $this->site));
}

$file = $this->_getVhostPath($input);

if (is_file($file))
Expand Down