2013-05-10 13:28:43 +02:00
|
|
|
<?php
|
2013-05-16 03:16:56 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2013, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link http://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-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;
|
|
|
|
protected $queries = array();
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
protected $host;
|
|
|
|
protected $user;
|
|
|
|
protected $pass;
|
2013-05-19 05:25:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database Connection
|
|
|
|
* @var PDO
|
|
|
|
*/
|
|
|
|
protected $pdo;
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
public function __construct(\PHPCI\Builder $phpci, array $options = array())
|
|
|
|
{
|
|
|
|
$this->phpci = $phpci;
|
|
|
|
$this->queries = $options;
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2013-05-16 17:46:30 +02:00
|
|
|
$config = \b8\Database::getConnection('write')->getDetails();
|
2013-05-16 16:50:19 +02:00
|
|
|
$this->host = PHPCI_DB_HOST;
|
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');
|
|
|
|
if (isset($buildSettings['mysql'])) {
|
|
|
|
$sql = $buildSettings['mysql'];
|
2013-05-16 01:51:48 +02:00
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
$this->host = !empty($sql['host']) ? $sql['host'] : $this->host;
|
|
|
|
$this->user = !empty($sql['user']) ? $sql['user'] : $this->user;
|
|
|
|
$this->pass = array_key_exists('pass', $sql) ? $sql['pass'] : $this->pass;
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2013-05-16 16:50:19 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2013-05-19 05:25:14 +02:00
|
|
|
$success = true;
|
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
try {
|
|
|
|
$opts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
|
2013-05-19 05:25:14 +02:00
|
|
|
$this->pdo = new PDO('mysql:host=' . $this->host, $this->user, $this->pass, $opts);
|
|
|
|
|
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
|
|
|
|
$this->pdo->query($query);
|
2013-05-19 05:33:39 +02:00
|
|
|
} else if (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-19 05:25:14 +02:00
|
|
|
}
|
2013-05-16 16:50:19 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->phpci->logFailure($ex->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-10 13:28:43 +02:00
|
|
|
|
2013-05-19 05:25:14 +02:00
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function executeFile($query)
|
|
|
|
{
|
2013-05-19 05:33:39 +02:00
|
|
|
if (!isset($query['file'])) {
|
|
|
|
throw new \Exception("Import statement must contiain an 'file' key");
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 05:46:26 +02:00
|
|
|
$import_file = $this->phpci->buildPath . $query['file'];
|
2013-05-19 05:25:14 +02:00
|
|
|
if (!is_readable($import_file)) {
|
2013-05-19 05:40:30 +02:00
|
|
|
throw new \Exception("Cannot open SQL import file: $import_file");
|
2013-05-19 05:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$database = isset($query['database'])? $query['database']: null;
|
|
|
|
|
|
|
|
$import_command = $this->getImportCommand($import_file, $database);
|
|
|
|
if (!$this->phpci->executeCommand($import_command)) {
|
|
|
|
throw new \Exception("Unable to execute SQL file");
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:50:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
protected function getImportCommand($import_file, $database=null) {
|
|
|
|
$decompression = array(
|
|
|
|
'bz2' => '| bzip2 --decompress',
|
|
|
|
'gz' => '| gzip --decompress',
|
|
|
|
);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|