Installing and managing modules from admin panel (#847)
This commit is contained in:
57
app/Utils/IdMapper.php
Normal file
57
app/Utils/IdMapper.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use Spatie\Valuestore\Valuestore;
|
||||
|
||||
class IdMapper extends Service
|
||||
{
|
||||
private $valueStore;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->valueStore = Valuestore::make(storage_path('app/legacy_migration.json'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mapping between an old ID and the new one
|
||||
*
|
||||
* @param string $entity Name of the entity (e,g table)
|
||||
* @param string $old_id
|
||||
* @param string $new_id
|
||||
*/
|
||||
public function addMapping($entity, $old_id, $new_id)
|
||||
{
|
||||
$key_name = $entity.'_'.$old_id;
|
||||
if (!$this->valueStore->has($key_name)) {
|
||||
$this->valueStore->put($key_name, $new_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ID for a mapping
|
||||
*
|
||||
* @param $entity
|
||||
* @param $old_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getMapping($entity, $old_id)
|
||||
{
|
||||
$key_name = $entity.'_'.$old_id;
|
||||
if (!$this->valueStore->has($key_name)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->valueStore->get($key_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the value store
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->valueStore->flush();
|
||||
}
|
||||
}
|
||||
256
app/Utils/ImporterDB.php
Normal file
256
app/Utils/ImporterDB.php
Normal file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
/**
|
||||
* Real basic to interface with an importer
|
||||
*/
|
||||
class ImporterDB
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $batchSize;
|
||||
|
||||
/**
|
||||
* @var PDO
|
||||
*/
|
||||
private $conn;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $dsn;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $creds;
|
||||
|
||||
public function __construct($creds)
|
||||
{
|
||||
$this->creds = $creds;
|
||||
$this->dsn = 'mysql:'.implode(';', [
|
||||
'host='.$this->creds['host'],
|
||||
'port='.$this->creds['port'],
|
||||
'dbname='.$this->creds['name'],
|
||||
]);
|
||||
|
||||
Log::info('Using DSN: '.$this->dsn);
|
||||
|
||||
$this->batchSize = config('installer.importer.batch_size', 20);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
public function connect()
|
||||
{
|
||||
try {
|
||||
$this->conn = new PDO($this->dsn, $this->creds['user'], $this->creds['pass']);
|
||||
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
||||
} catch (PDOException $e) {
|
||||
Log::error($e);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->conn) {
|
||||
$this->conn = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the table name with the prefix
|
||||
*
|
||||
* @param $table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function tableName($table)
|
||||
{
|
||||
if ($this->creds['table_prefix'] !== false) {
|
||||
return $this->creds['table_prefix'].$table;
|
||||
}
|
||||
|
||||
return $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a table exist? Try to get the column information on it.
|
||||
* The result will be 'false' if that table isn't there
|
||||
*
|
||||
* @param $table
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function tableExists($table): bool
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$sql = 'SHOW COLUMNS FROM '.$this->tableName($table);
|
||||
$result = $this->conn->query($sql);
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the names of the columns for a particular table
|
||||
*
|
||||
* @param $table
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getColumns($table)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$sql = 'SHOW COLUMNS FROM '.$this->tableName($table);
|
||||
$result = $this->conn->query($sql)->fetchAll();
|
||||
|
||||
$rows = [];
|
||||
foreach ($result as $row) {
|
||||
$rows[] = $row->Field;
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTotalRows($table)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$sql = 'SELECT COUNT(*) FROM '.$this->tableName($table);
|
||||
$rows = $this->conn->query($sql)->fetchColumn();
|
||||
|
||||
Log::info('Found '.$rows.' rows in '.$table);
|
||||
|
||||
return (int) $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read rows from a table with a given assoc array. Simple
|
||||
*
|
||||
* @param string $table
|
||||
* @param array $attrs
|
||||
*
|
||||
* @return false|\PDOStatement
|
||||
*/
|
||||
public function findBy($table, array $attrs)
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$where = [];
|
||||
foreach ($attrs as $col => $value) {
|
||||
$where[] = $col.'=\''.$value.'\'';
|
||||
}
|
||||
|
||||
$where = implode(' AND ', $where);
|
||||
|
||||
$sql = implode(' ', [
|
||||
'SELECT',
|
||||
'*',
|
||||
'FROM',
|
||||
$this->tableName($table),
|
||||
'WHERE',
|
||||
$where,
|
||||
]);
|
||||
|
||||
return $this->conn->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all the rows in a table, but read them in a batched manner
|
||||
*
|
||||
* @param string $table The name of the table
|
||||
* @param string $order_by Column to order by
|
||||
* @param int $start_offset
|
||||
* @param string $fields
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readRows($table, $order_by = 'id', $start_offset = 0, $fields = '*')
|
||||
{
|
||||
$this->connect();
|
||||
|
||||
$offset = $start_offset;
|
||||
// $total_rows = $this->getTotalRows($table);
|
||||
|
||||
$rows = [];
|
||||
$result = $this->readRowsOffset($table, $this->batchSize, $offset, $order_by, $fields);
|
||||
if ($result === false || $result === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
foreach ($result as $row) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('foreach rows error: '.$e->getMessage());
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param int $limit Number of rows to read
|
||||
* @param int $offset Where to start from
|
||||
* @param $order_by
|
||||
* @param string $fields
|
||||
*
|
||||
* @return false|\PDOStatement|void
|
||||
*/
|
||||
public function readRowsOffset($table, $limit, $offset, $order_by, $fields = '*')
|
||||
{
|
||||
if (is_array($fields)) {
|
||||
$fields = implode(',', $fields);
|
||||
}
|
||||
|
||||
$sql = implode(' ', [
|
||||
'SELECT',
|
||||
$fields,
|
||||
'FROM',
|
||||
$this->tableName($table),
|
||||
'ORDER BY '.$order_by.' ASC',
|
||||
'LIMIT '.$limit,
|
||||
'OFFSET '.$offset,
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = $this->conn->query($sql);
|
||||
if (!$result || $result->rowCount() === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $result;
|
||||
} catch (PDOException $e) {
|
||||
// Without incrementing the offset, it should re-run the same query
|
||||
Log::error('Error readRowsOffset: '.$e->getMessage());
|
||||
|
||||
if (strpos($e->getMessage(), 'server has gone away') !== false) {
|
||||
$this->connect();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error readRowsOffset: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user