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

184 lines
5.1 KiB
PHP
Raw Normal View History

2013-05-10 13:28:43 +02:00
<?php
2013-05-16 03:16:56 +02:00
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2013-05-10 13:28:43 +02:00
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\Helper\Lang;
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.
* @author Dan Cryer <dan@block8.co.uk>
2013-05-19 05:25:14 +02:00
* @author Steve Kamerman <stevekamerman@gmail.com>
* @package PHPCI
* @subpackage Plugins
*/
2016-05-09 08:20:26 +02:00
class Mysql implements Plugin
2013-05-10 13:28:43 +02:00
{
2013-05-19 05:25:14 +02:00
/**
* @var \PHPCensor\Builder
2013-05-19 05:25:14 +02:00
*/
protected $phpci;
/**
* @var \PHPCensor\Model\Build
*/
2014-05-02 15:48:40 +02:00
protected $build;
/**
* @var array
*/
2016-04-21 06:58:09 +02:00
protected $queries = [];
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;
/**
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
2016-04-21 06:58:09 +02:00
public function __construct(Builder $phpci, Build $build, array $options = [])
{
2013-10-10 02:01:06 +02:00
$this->phpci = $phpci;
2014-05-02 15:48:40 +02:00
$this->build = $build;
2013-10-10 02:01:06 +02:00
$this->queries = $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 = $phpci->getConfig('build_settings');
2013-10-10 02:01:06 +02:00
if (!isset($buildSettings['mysql'])) {
return;
}
if (!empty($buildSettings['mysql']['host'])) {
$this->host = $this->phpci->interpolate($buildSettings['mysql']['host']);
}
if (!empty($buildSettings['mysql']['user'])) {
$this->user = $this->phpci->interpolate($buildSettings['mysql']['user']);
}
if (array_key_exists('pass', $buildSettings['mysql'])) {
$this->pass = $buildSettings['mysql']['pass'];
}
$this->phpci->logDebug('Plugin options: ' . json_encode($options));
}
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);
foreach ($this->queries as $query) {
2013-05-19 05:25:14 +02:00
if (!is_array($query)) {
// Simple query
$pdo->query($this->phpci->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 {
2014-12-04 16:48:52 +01:00
throw new \Exception(Lang::get('invalid_command'));
2013-05-19 05:25:14 +02:00
}
}
} catch (\Exception $ex) {
$this->phpci->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'])) {
2014-12-04 16:48:52 +01:00
throw new \Exception(Lang::get('import_file_key'));
2013-05-19 05:25:14 +02:00
}
$import_file = $this->phpci->buildPath . $this->phpci->interpolate($query['file']);
2013-05-19 05:25:14 +02:00
if (!is_readable($import_file)) {
2014-12-04 16:48:52 +01:00
throw new \Exception(Lang::get('cannot_open_import', $import_file));
2013-05-19 05:25:14 +02:00
}
2013-10-10 02:01:06 +02:00
$database = isset($query['database']) ? $this->phpci->interpolate($query['database']) : null;
2013-05-19 05:25:14 +02:00
$import_command = $this->getImportCommand($import_file, $database);
2013-10-10 02:01:06 +02:00
if (!$this->phpci->executeCommand($import_command)) {
2014-12-04 16:48:52 +01:00
throw new \Exception(Lang::get('unable_to_execute'));
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
}