Files
luos/gulliver/system/class.dbMaintenance.php

544 lines
14 KiB
PHP
Raw Normal View History

2010-12-02 23:34:41 +00:00
<?php
2018-02-01 13:06:32 +00:00
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
2010-12-02 23:34:41 +00:00
/**
*
*
2010-12-02 23:34:41 +00:00
* Database Maintenance class
*
2011-01-14 11:51:34 +00:00
*
* @package gulliver.system
2010-12-02 23:34:41 +00:00
*/
class DataBaseMaintenance
2010-12-02 23:34:41 +00:00
{
2018-02-01 13:06:32 +00:00
private $host = null;
private $user = null;
private $passwd = null;
2010-12-02 23:34:41 +00:00
2018-02-01 13:06:32 +00:00
private $connect = null;
private $dbName = null;
public $result;
2018-02-01 13:06:32 +00:00
protected $tmpDir = null;
protected $outfile;
protected $infile;
2017-08-08 09:53:00 -04:00
protected $isWindows;
/**
2018-02-01 13:06:32 +00:00
* DataBaseMaintenance constructor.
*
2018-02-01 13:06:32 +00:00
* @param string $host
* @param string $user
* @param string $passwd
*
*/
2017-12-04 13:25:35 +00:00
public function __construct($host = null, $user = null, $passwd = null)
{
$this->tmpDir = './';
2018-02-01 13:06:32 +00:00
$this->setConnection(null);
$this->setDbName(null);
2017-08-08 09:53:00 -04:00
$this->isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
2018-02-01 13:06:32 +00:00
$this->setUser($user);
$this->setHost($host);
$this->setPasswd($passwd);
2010-12-02 23:34:41 +00:00
}
/**
* setUser
*
* @param string $user
*/
2017-12-04 13:25:35 +00:00
public function setUser($user)
{
$this->user = $user;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Set Password
*
* @param string $passwd
*/
2017-12-04 13:25:35 +00:00
public function setPasswd($passwd)
{
$this->passwd = $passwd;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Set Host
*
* @param string $host
*/
2017-12-04 13:25:35 +00:00
public function setHost($host)
{
$this->host = $host;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Set TempDir
*
* @param string $tmpDir
*/
2017-12-04 13:25:35 +00:00
public function setTempDir($tmpDir)
{
$this->tmpDir = $tmpDir;
2017-12-04 13:25:35 +00:00
if (!file_exists($tmpDir)) {
mkdir($this->tmpDir);
}
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Set Db Name
*
2018-02-01 13:06:32 +00:00
* @param $dbName
*/
2018-02-01 13:06:32 +00:00
public function setDbName($dbName)
{
2018-02-01 13:06:32 +00:00
$this->dbName = $dbName;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Set Connection
*
2018-02-01 13:06:32 +00:00
* @param $name
*/
2018-02-01 13:06:32 +00:00
public function setConnection($name)
{
2018-02-01 13:06:32 +00:00
$this->connect = 'DB_' . $name;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Get User
* @return string
*/
2018-02-01 13:06:32 +00:00
public function getUser()
{
2018-02-01 13:06:32 +00:00
return $this->user;
}
2018-02-01 13:06:32 +00:00
/**
* Get Password
* @return string
*/
public function getPasswd()
{
return $this->passwd;
}
2018-02-01 13:06:32 +00:00
/**
* Get Host
* @return string
*/
public function getHost()
{
return $this->host;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Get Name Connection
*
2018-02-01 13:06:32 +00:00
* @return string
*/
2018-02-01 13:06:32 +00:00
public function getConnect()
{
2018-02-01 13:06:32 +00:00
return $this->connect;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* get TempDir
*
2018-02-01 13:06:32 +00:00
* @return $this->tmpDir
*/
public function getTempDir()
{
return $this->tmpDir;
}
/**
* Get Name DB
*
2018-02-01 13:06:32 +00:00
* @return string
*/
2018-02-01 13:06:32 +00:00
public function getDbName()
{
2018-02-01 13:06:32 +00:00
return $this->dbName;
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Connect to DB
*
2018-02-01 13:06:32 +00:00
* @param string $dbName
*
2018-02-01 13:06:32 +00:00
* @throws Exception
*/
2018-02-01 13:06:32 +00:00
public function connect($dbName)
{
2018-02-01 13:06:32 +00:00
try {
$this->setConnection($dbName);
$this->setDbName($dbName);
InstallerModule::setNewConnection(
$this->getConnect(),
$this->getHost(),
$this->getUser(),
$this->getPasswd(),
$this->getDbName(),
'');
DB::connection($this->getConnect())
->statement("SET NAMES 'utf8'");
DB::connection($this->getConnect())
->statement('SET FOREIGN_KEY_CHECKS=0');
} catch (QueryException $exception) {
throw new Exception("Couldn't connect to host {$this->getHost()} with user {$this->getUser()}" . $exception->getMessage());
}
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* Query
*
* @param string $sql
*
2018-02-01 13:06:32 +00:00
* @return array
* @throws Exception
*/
2018-02-01 13:06:32 +00:00
public function query($sql)
{
2018-02-01 13:06:32 +00:00
try {
$result = DB::connection($this->getConnect())
->select($sql);
return $result;
} catch (QueryException $exception) {
throw new Exception("Couldn't connect to host {$this->getHost()} with user {$this->getUser()}" . $exception->getMessage());
}
2010-12-02 23:34:41 +00:00
}
/**
2018-02-01 13:06:32 +00:00
* get Tables List
*
2017-12-04 13:25:35 +00:00
* @return array
2018-02-01 13:06:32 +00:00
* @throws Exception
*/
2017-12-04 13:25:35 +00:00
public function getTablesList()
{
2018-02-01 13:06:32 +00:00
return $this->query('SHOW TABLES');
2010-12-02 23:34:41 +00:00
}
/**
* dumpData
*
* @param string $table
*
* @return boolean true or false
*/
2017-12-04 13:25:35 +00:00
public function dumpData($table)
{
$sql = "";
2018-02-01 13:06:32 +00:00
try {
$this->outfile = $this->tmpDir . $table . '.dump';
2018-02-01 13:06:32 +00:00
//if the file exists delete it
if (is_file($this->outfile)) {
@unlink($this->outfile);
}
$sql = "SELECT * INTO OUTFILE '{$this->outfile}' FIELDS TERMINATED BY '\t|\t' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\t\t\r\r\n' FROM $table";
2018-02-01 13:06:32 +00:00
DB::connection($this->getConnect())->raw($sql);
return true;
} catch (QueryException $exception) {
$message = $exception->getMessage();
$context = [
'sql' => $sql
];
Log::channel(':MysqlCron')->error($message, Bootstrap::context($context));
G::outRes($message . "\n");
return false;
2010-12-02 23:34:41 +00:00
}
}
/**
* restoreData
*
* @param string $backupFile
*
* @return boolean true or false
*/
2017-12-04 13:25:35 +00:00
public function restoreData($backupFile)
{
$sql = "";
2018-02-01 13:06:32 +00:00
try {
$tableName = str_replace('.dump', '', basename($backupFile));
$sql = "LOAD DATA INFILE '$backupFile' INTO TABLE $tableName FIELDS TERMINATED BY '\t|\t' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\t\t\r\r\n'";
DB::connection($this->getConnect())->raw($sql);
return true;
} catch (QueryException $exception) {
$message = $exception->getMessage();
$context = [
'sql' => $sql
];
Log::channel(':MysqlCron')->error($message, Bootstrap::context($context));
G::outRes($message . "\n");
return false;
}
2010-12-02 23:34:41 +00:00
}
/**
* restoreAllData
*
* @param string $type default value null
*
2018-02-01 13:06:32 +00:00
* @throws Exception
*/
2017-12-04 13:25:35 +00:00
public function restoreAllData($type = null)
{
2018-02-01 13:06:32 +00:00
foreach ($this->getTablesList() as $table) {
if (isset($type) && $type === 'sql') {
$this->infile = $this->tmpDir . $table . '.sql';
2017-12-04 13:25:35 +00:00
if (is_file($this->infile)) {
$queries = $this->restoreFromSql($this->infile, true);
if (!isset($queries)) {
2018-02-01 13:06:32 +00:00
$queries = 'unknown';
}
2017-12-04 13:25:35 +00:00
printf("%-59s%20s", "Restored table $table", "$queries queries\n");
}
} else {
2018-02-01 13:06:32 +00:00
$this->infile = $this->tmpDir . $table . '.dump';
2017-12-04 13:25:35 +00:00
if (is_file($this->infile)) {
$this->restoreData($this->infile);
printf("%20s %s %s\n", 'Restoring data from ', $this->infile, " in table $table");
}
}
2010-12-02 23:34:41 +00:00
}
}
/**
2018-02-01 13:06:32 +00:00
* Create DB
*
* @param string $dbname
2018-02-01 13:06:32 +00:00
* @param boolean $drop
*
2018-02-01 13:06:32 +00:00
* @return bool
* @throws Exception
*/
2017-12-04 13:25:35 +00:00
public function createDb($dbname, $drop = false)
{
2018-02-01 13:06:32 +00:00
try {
if ($drop) {
DB::connection($this->getConnect())->statement("DROP DATABASE IF EXISTS $dbname");
}
2018-02-01 13:06:32 +00:00
DB::connection($this->getConnect())->statement("CREATE DATABASE IF NOT EXISTS $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
2018-02-01 13:06:32 +00:00
return true;
} catch (QueryException $exception) {
throw new Exception($exception->getMessage());
}
2010-12-02 23:34:41 +00:00
}
/**
* backupDataBaseSchema
*
* @param string $outfile
*
* @return none
*/
2017-12-04 13:25:35 +00:00
public function backupDataBase($outfile)
{
2018-02-01 13:06:32 +00:00
$password = escapeshellarg($this->getPasswd());
2017-12-04 13:25:35 +00:00
//On Windows, escapeshellarg() instead replaces percent signs, exclamation
//marks (delayed variable substitution) and double quotes with spaces and
2017-08-08 09:53:00 -04:00
//adds double quotes around the string.
//See: http://php.net/manual/en/function.escapeshellarg.php
if ($this->isWindows) {
2018-02-01 13:06:32 +00:00
$password = $this->escapeshellargCustom($this->getPasswd());
2017-08-08 09:53:00 -04:00
}
2018-02-01 13:06:32 +00:00
$aHost = explode(':', $this->getHost());
$dbHost = $aHost[0];
if (isset($aHost[1])) {
$dbPort = $aHost[1];
$command = 'mysqldump'
2018-02-01 13:06:32 +00:00
. ' --user=' . $this->getUser()
2017-08-08 13:45:12 -04:00
. ' --password=' . $password
2017-08-02 16:06:56 -04:00
. ' --host=' . $dbHost
. ' --port=' . $dbPort
. ' --opt'
. ' --skip-comments'
. ' --no-tablespaces'
2018-02-01 13:06:32 +00:00
. ' ' . $this->getDbName()
2017-08-02 16:06:56 -04:00
. ' > ' . $outfile;
} else {
$command = 'mysqldump'
2017-08-02 16:06:56 -04:00
. ' --host=' . $dbHost
2018-02-01 13:06:32 +00:00
. ' --user=' . $this->getUser()
2017-08-02 16:06:56 -04:00
. ' --opt'
. ' --skip-comments'
. ' --no-tablespaces'
2017-08-08 13:45:12 -04:00
. ' --password=' . $password
2018-02-01 13:06:32 +00:00
. ' ' . $this->getDbName()
2017-08-02 16:06:56 -04:00
. ' > ' . $outfile;
2010-12-02 23:34:41 +00:00
}
shell_exec($command);
}
2017-08-08 09:53:00 -04:00
/**
* string escapeshellargCustom ( string $arg , character $quotes)
2017-12-04 13:25:35 +00:00
*
* escapeshellarg() adds single quotes around a string and quotes/escapes any
* existing single quotes allowing you to pass a string directly to a shell
* function and having it be treated as a single safe argument. This function
* should be used to escape individual arguments to shell functions coming
* from user input. The shell functions include exec(), system() and the
2017-08-08 09:53:00 -04:00
* backtick operator.
2017-12-04 13:25:35 +00:00
*
* On Windows, escapeshellarg() instead replaces percent signs, exclamation
* marks (delayed variable substitution) and double quotes with spaces and
2017-08-08 09:53:00 -04:00
* adds double quotes around the string.
*/
private function escapeshellargCustom($string, $quotes = "")
{
2018-02-01 13:06:32 +00:00
if ($quotes === '') {
2017-08-08 09:53:00 -04:00
$quotes = $this->isWindows ? "\"" : "'";
}
$n = strlen($string);
$special = ["!", "%", "\""];
2018-02-01 13:06:32 +00:00
$substring = '';
2017-08-08 09:53:00 -04:00
$result1 = [];
$result2 = [];
for ($i = 0; $i < $n; $i++) {
if (in_array($string[$i], $special, true)) {
$result2[] = $string[$i];
$result1[] = $substring;
2018-02-01 13:06:32 +00:00
$substring = '';
2017-08-08 09:53:00 -04:00
} else {
2018-02-01 13:06:32 +00:00
$substring .= $string[$i];
2017-08-08 09:53:00 -04:00
}
}
$result1[] = $substring;
//Rebuild the password string
$n = count($result1);
for ($i = 0; $i < $n; $i++) {
$result1[$i] = trim(escapeshellarg($result1[$i]), $quotes);
if (isset($result2[$i])) {
2018-02-01 13:06:32 +00:00
$result1[$i] .= $result2[$i];
2017-08-08 09:53:00 -04:00
}
}
//add simple quotes, see escapeshellarg function
2018-02-01 13:06:32 +00:00
$newString = $quotes . implode('', $result1) . $quotes;
2017-08-08 09:53:00 -04:00
return $newString;
}
/**
2018-02-01 13:06:32 +00:00
* Restore from sql
*
2018-02-01 13:06:32 +00:00
* @param string $sqlFile
* @param string $type
*
* @return boolean false or true
2018-02-01 13:06:32 +00:00
* @throws Exception
*/
2018-02-01 13:06:32 +00:00
public function restoreFromSql($sqlFile, $type = 'file')
{
2018-02-01 13:06:32 +00:00
if ($type == 'file' && !is_file($sqlFile)) {
throw new Exception("the $sqlFile doesn't exist!");
2010-12-02 23:34:41 +00:00
}
2018-02-01 13:06:32 +00:00
$metaFile = str_replace('.sql', '.meta', $sqlFile);
$queries = 0;
2017-12-04 13:25:35 +00:00
if (is_file($metaFile)) {
echo "Using $metaFile as metadata.\n";
2018-02-01 13:06:32 +00:00
$fp = fopen($sqlFile, 'rb');
2017-12-04 13:25:35 +00:00
$fpmd = fopen($metaFile, 'r');
while ($offset = fgets($fpmd, 1024)) {
$buffer = intval($offset); //reading the size of $oData
$query = fread($fp, $buffer); //reading string $oData
2018-02-01 13:06:32 +00:00
$queries++;
2018-02-01 13:06:32 +00:00
try {
DB::connection($this->getConnect())->raw($query);
} catch (QueryException $exception) {
$varRes = $exception->getMessage() . "\n";
2017-12-04 13:25:35 +00:00
G::outRes($varRes);
2016-08-04 14:56:58 -04:00
$varRes = "==>" . $query . "<==\n";
2017-12-04 13:25:35 +00:00
G::outRes($varRes);
}
}
2010-12-02 23:34:41 +00:00
} else {
$queries = null;
try {
2018-02-01 13:06:32 +00:00
2017-12-04 13:25:35 +00:00
if ($type === 'file') {
2018-02-01 13:06:32 +00:00
$query = file_get_contents($sqlFile);
2017-12-04 13:25:35 +00:00
} elseif ($type === 'string') {
2018-02-01 13:06:32 +00:00
$query = $sqlFile;
} else {
return false;
}
2018-02-01 13:06:32 +00:00
if (empty(trim($query))) {
return false;
}
2018-02-01 13:06:32 +00:00
try {
DB::connection($this->getConnect())->raw($query);
} catch (QueryException $exception) {
throw new Exception($exception->getMessage());
}
} catch (Exception $e) {
echo $query;
2016-08-04 14:56:58 -04:00
$token = strtotime("now");
PMException::registerErrorLog($e, $token);
2017-12-04 13:25:35 +00:00
G::outRes(G::LoadTranslation("ID_EXCEPTION_LOG_INTERFAZ", array($token)));
2010-12-02 23:34:41 +00:00
}
}
return $queries;
}
/**
* getSchemaFromTable
*
* @param string $tablename
*
* @return string $tableSchema
*/
2017-12-04 13:25:35 +00:00
public function getSchemaFromTable($tablename)
{
2018-02-01 13:06:32 +00:00
try {
$tableSchema = '';
$result = DB::connection($this->getConnect())->select("show create table `$tablename`");
if ($result) {
$tableSchema = $result['Create Table'] . ";\n\n";
2010-12-02 23:34:41 +00:00
}
2018-02-01 13:06:32 +00:00
} catch (QueryException $exception) {
G::outRes($exception->getMessage());
}
2018-02-01 13:06:32 +00:00
return $tableSchema;
}
/**
* removeCommentsIntoString
*
* @param string $str
*
* @return string $str
*/
2017-12-04 13:25:35 +00:00
public function removeCommentsIntoString($str)
{
2017-12-04 13:25:35 +00:00
$str = preg_replace('/\/\*[\w\W]*\*\//', '', $str);
$str = preg_replace("/--[\w\W]*\\n/", '', $str);
$str = preg_replace("/\/\/[\w\W]*\\n/", '', $str);
$str = preg_replace("/\#[\w\W]*\\n/", '', $str);
return $str;
2010-12-02 23:34:41 +00:00
}
2017-12-04 13:25:35 +00:00
}