2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
2014-05-12 18:26:17 +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
|
|
|
|
|
|
|
namespace PHPCI\Plugin;
|
2013-05-16 16:50:19 +02:00
|
|
|
|
2013-05-16 01:51:48 +02:00
|
|
|
use PDO;
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Builder;
|
2014-12-04 16:48:52 +01:00
|
|
|
use PHPCI\Helper\Lang;
|
2013-10-10 02:01:06 +02:00
|
|
|
use PHPCI\Model\Build;
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 03:30:48 +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>
|
2013-05-16 03:30:48 +02:00
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Plugins
|
|
|
|
*/
|
2013-05-10 13:28:43 +02:00
|
|
|
class Mysql implements \PHPCI\Plugin
|
|
|
|
{
|
2013-05-19 05:25:14 +02:00
|
|
|
/**
|
|
|
|
* @var \PHPCI\Builder
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $phpci;
|
2014-05-09 18:49:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Model\Build
|
|
|
|
*/
|
2014-05-02 15:48:40 +02:00
|
|
|
protected $build;
|
2014-05-09 18:49:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $queries = array();
|
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
|
|
|
|
2014-05-09 18:49:20 +02:00
|
|
|
/**
|
|
|
|
* @param Builder $phpci
|
|
|
|
* @param Build $build
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
public function __construct(Builder $phpci, Build $build, array $options = array())
|
2013-05-16 16:50:19 +02:00
|
|
|
{
|
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;
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$config = \b8\Database::getConnection('write')->getDetails();
|
2013-09-18 10:56:54 +02:00
|
|
|
|
|
|
|
$this->host =(defined('PHPCI_DB_HOST')) ? PHPCI_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
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
$buildSettings = $phpci->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'])) {
|
|
|
|
$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'];
|
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 {
|
|
|
|
$opts = array(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
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
foreach ($this->queries as $query) {
|
2013-05-19 05:25:14 +02:00
|
|
|
if (!is_array($query)) {
|
|
|
|
// Simple query
|
2014-05-09 18:49:20 +02:00
|
|
|
$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']);
|
2013-05-22 20:21:17 +02:00
|
|
|
} else {
|
2014-12-04 16:48:52 +01:00
|
|
|
throw new \Exception(Lang::get('invalid_command'));
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->phpci->logFailure($ex->getMessage());
|
|
|
|
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'])) {
|
2014-12-04 16:48:52 +01:00
|
|
|
throw new \Exception(Lang::get('import_file_key'));
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
2013-09-18 10:56:54 +02:00
|
|
|
|
2013-05-22 20:21:17 +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-09-18 10:56:54 +02:00
|
|
|
|
2013-10-10 02:01:06 +02:00
|
|
|
$database = isset($query['database']) ? $this->phpci->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);
|
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
|
|
|
}
|
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
|
|
|
|
* @param string $import_file Path to file, relative to the build root
|
|
|
|
* @param string $database If specified, this database is selected before execution
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function getImportCommand($import_file, $database = null)
|
|
|
|
{
|
2013-05-19 05:25:14 +02:00
|
|
|
$decompression = array(
|
|
|
|
'bz2' => '| bzip2 --decompress',
|
|
|
|
'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];
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = array(
|
|
|
|
':import_file' => escapeshellarg($import_file),
|
|
|
|
':decomp_cmd' => $decomp_cmd,
|
|
|
|
':user' => escapeshellarg($this->user),
|
|
|
|
':pass' => escapeshellarg($this->pass),
|
|
|
|
':database' => ($database === null)? '': escapeshellarg($database),
|
|
|
|
);
|
|
|
|
return strtr('cat :import_file :decomp_cmd | mysql -u:user -p:pass :database', $args);
|
|
|
|
}
|
2013-10-10 02:01:06 +02:00
|
|
|
}
|