2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-05-16 16:50:19 +02:00
|
|
|
|
2013-05-16 01:51:48 +02:00
|
|
|
use PDO;
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Plugin;
|
2016-05-09 08:20:26 +02:00
|
|
|
use b8\Database;
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 03:30:48 +02:00
|
|
|
/**
|
2017-01-10 18:46:19 +01:00
|
|
|
* MySQL Plugin - Provides access to a MySQL database.
|
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
|
|
|
* @author Steve Kamerman <stevekamerman@gmail.com>
|
2017-01-10 18:46:19 +01:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class Mysql extends Plugin
|
2013-05-10 13:28:43 +02:00
|
|
|
{
|
2014-05-09 18:49:20 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $host;
|
2014-05-09 18:49:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $user;
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-19 05:25:14 +02:00
|
|
|
/**
|
2014-05-09 18:49:20 +02:00
|
|
|
* @var string
|
2013-05-19 05:25:14 +02:00
|
|
|
*/
|
2014-05-09 18:49:20 +02:00
|
|
|
protected $pass;
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'mysql';
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:49:20 +02:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-05-09 18:49:20 +02:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-05-16 16:50:19 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2016-05-09 08:20:26 +02:00
|
|
|
$config = Database::getConnection('write')->getDetails();
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2016-07-21 17:20:34 +02:00
|
|
|
$this->host =(defined('DB_HOST')) ? DB_HOST : null;
|
2013-05-16 17:46:30 +02:00
|
|
|
$this->user = $config['user'];
|
|
|
|
$this->pass = $config['pass'];
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$buildSettings = $this->builder->getConfig('build_settings');
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
if (!isset($buildSettings['mysql'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($buildSettings['mysql']['host'])) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->host = $this->builder->interpolate($buildSettings['mysql']['host']);
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($buildSettings['mysql']['user'])) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->user = $this->builder->interpolate($buildSettings['mysql']['user']);
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('pass', $buildSettings['mysql'])) {
|
|
|
|
$this->pass = $buildSettings['mysql']['pass'];
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 18:17:29 +02:00
|
|
|
/**
|
|
|
|
* Connects to MySQL and runs a specified set of queries.
|
2014-05-09 18:49:20 +02:00
|
|
|
* @return boolean
|
2013-05-16 18:17:29 +02:00
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
try {
|
2016-04-21 06:58:09 +02:00
|
|
|
$opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
|
2014-05-09 18:49:20 +02:00
|
|
|
$pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
foreach ($this->options as $query) {
|
2013-05-19 05:25:14 +02:00
|
|
|
if (!is_array($query)) {
|
|
|
|
// Simple query
|
2016-07-22 09:05:34 +02:00
|
|
|
$pdo->query($this->builder->interpolate($query));
|
2013-10-10 02:01:06 +02:00
|
|
|
} elseif (isset($query['import'])) {
|
2013-05-19 05:25:14 +02:00
|
|
|
// SQL file execution
|
2013-05-19 05:33:39 +02:00
|
|
|
$this->executeFile($query['import']);
|
2013-05-22 20:21:17 +02:00
|
|
|
} else {
|
2017-02-07 13:05:44 +01:00
|
|
|
throw new \Exception('Invalid command.');
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->builder->logFailure($ex->getMessage());
|
2013-05-16 16:50:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-09 18:49:20 +02:00
|
|
|
return true;
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2014-05-09 18:49:20 +02:00
|
|
|
/**
|
|
|
|
* @param string $query
|
|
|
|
* @return boolean
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2013-05-19 05:25:14 +02:00
|
|
|
protected function executeFile($query)
|
|
|
|
{
|
2013-05-19 05:33:39 +02:00
|
|
|
if (!isset($query['file'])) {
|
2017-02-07 13:05:44 +01:00
|
|
|
throw new \Exception('Import statement must contain a \'file\' key');
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$import_file = $this->builder->buildPath . $this->builder->interpolate($query['file']);
|
2013-05-19 05:25:14 +02:00
|
|
|
if (!is_readable($import_file)) {
|
2017-02-07 13:05:44 +01:00
|
|
|
throw new \Exception(sprintf('Cannot open SQL import file: %s', $import_file));
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$database = isset($query['database']) ? $this->builder->interpolate($query['database']) : null;
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-19 05:25:14 +02:00
|
|
|
$import_command = $this->getImportCommand($import_file, $database);
|
2016-07-22 09:05:34 +02:00
|
|
|
if (!$this->builder->executeCommand($import_command)) {
|
2017-02-07 13:05:44 +01:00
|
|
|
throw new \Exception('Unable to execute SQL file');
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-19 05:25:14 +02:00
|
|
|
/**
|
|
|
|
* Builds the MySQL import command required to import/execute the specified file
|
2016-04-25 19:30:23 +02:00
|
|
|
*
|
2013-05-19 05:25:14 +02:00
|
|
|
* @param string $import_file Path to file, relative to the build root
|
|
|
|
* @param string $database If specified, this database is selected before execution
|
2016-04-25 19:30:23 +02:00
|
|
|
*
|
2013-05-19 05:25:14 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function getImportCommand($import_file, $database = null)
|
|
|
|
{
|
2016-04-21 06:58:09 +02:00
|
|
|
$decompression = [
|
2013-05-19 05:25:14 +02:00
|
|
|
'bz2' => '| bzip2 --decompress',
|
2016-04-21 06:58:09 +02:00
|
|
|
'gz' => '| gzip --decompress',
|
|
|
|
];
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-19 05:25:14 +02:00
|
|
|
$extension = strtolower(pathinfo($import_file, PATHINFO_EXTENSION));
|
|
|
|
$decomp_cmd = '';
|
|
|
|
if (array_key_exists($extension, $decompression)) {
|
|
|
|
$decomp_cmd = $decompression[$extension];
|
|
|
|
}
|
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
$args = [
|
2013-05-19 05:25:14 +02:00
|
|
|
':import_file' => escapeshellarg($import_file),
|
2016-04-21 06:58:09 +02:00
|
|
|
':decomp_cmd' => $decomp_cmd,
|
|
|
|
':host' => escapeshellarg($this->host),
|
|
|
|
':user' => escapeshellarg($this->user),
|
2016-04-25 19:30:23 +02:00
|
|
|
':pass' => (!$this->pass) ? '' : '-p' . escapeshellarg($this->pass),
|
2016-04-21 06:58:09 +02:00
|
|
|
':database' => ($database === null)? '': escapeshellarg($database),
|
|
|
|
];
|
2016-04-25 19:30:23 +02:00
|
|
|
|
|
|
|
return strtr('cat :import_file :decomp_cmd | mysql -h:host -u:user :pass :database', $args);
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|