php-censor/src/PHPCensor/Plugin/Mysql.php

161 lines
4.6 KiB
PHP
Raw Normal View History

2013-05-10 13:28:43 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
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
/**
* 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>
*/
2016-07-11 18:00:04 +02:00
class Mysql extends Plugin
2013-05-10 13:28:43 +02:00
{
/**
* @var string
*/
protected $host;
/**
* @var string
*/
protected $user;
2013-05-19 05:25:14 +02:00
/**
* @var string
2013-05-19 05:25:14 +02:00
*/
protected $pass;
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'mysql';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
2016-05-09 08:20:26 +02:00
$config = Database::getConnection('write')->getDetails();
2016-07-21 17:20:34 +02:00
$this->host =(defined('DB_HOST')) ? DB_HOST : null;
$this->user = $config['user'];
$this->pass = $config['pass'];
$buildSettings = $this->builder->getConfig('build_settings');
2013-10-10 02:01:06 +02:00
if (!isset($buildSettings['mysql'])) {
return;
}
if (!empty($buildSettings['mysql']['host'])) {
$this->host = $this->builder->interpolate($buildSettings['mysql']['host']);
2013-10-10 02:01:06 +02:00
}
if (!empty($buildSettings['mysql']['user'])) {
$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-10 13:28:43 +02:00
/**
* Connects to MySQL and runs a specified set of queries.
* @return boolean
*/
public function execute()
{
try {
2016-04-21 06:58:09 +02:00
$opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
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
$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']);
} else {
throw new \Exception('Invalid command.');
2013-05-19 05:25:14 +02:00
}
}
} catch (\Exception $ex) {
$this->builder->logFailure($ex->getMessage());
return false;
}
return true;
2013-05-19 05:25:14 +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'])) {
throw new \Exception('Import statement must contain a \'file\' key');
2013-05-19 05:25:14 +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)) {
throw new \Exception(sprintf('Cannot open SQL import file: %s', $import_file));
2013-05-19 05:25:14 +02:00
}
$database = isset($query['database']) ? $this->builder->interpolate($query['database']) : null;
2013-05-19 05:25:14 +02:00
$import_command = $this->getImportCommand($import_file, $database);
if (!$this->builder->executeCommand($import_command)) {
throw new \Exception('Unable to execute SQL file');
2013-10-10 02:01:06 +02:00
}
return true;
}
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-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
}