2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace Modules\Importer\Services;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
use App\Services\Installer\LoggerTrait;
|
2019-11-27 22:19:20 +08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2020-02-24 08:48:28 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-12-13 04:07:35 +08:00
|
|
|
use Modules\Importer\Utils\IdMapper;
|
|
|
|
use Modules\Importer\Utils\ImporterDB;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
|
|
|
abstract class BaseImporter implements ShouldQueue
|
|
|
|
{
|
2020-01-10 22:41:32 +08:00
|
|
|
use LoggerTrait;
|
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
2019-12-13 04:07:35 +08:00
|
|
|
/**
|
|
|
|
* Holds the connection to the legacy database
|
|
|
|
*
|
|
|
|
* @var \Modules\Importer\Utils\ImporterDB
|
|
|
|
*/
|
2019-11-27 22:19:20 +08:00
|
|
|
protected $db;
|
2019-12-13 04:07:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The mapper class used for old IDs to new IDs
|
|
|
|
*
|
2020-02-24 08:48:28 +08:00
|
|
|
* @var IdMapper
|
2019-12-13 04:07:35 +08:00
|
|
|
*/
|
2019-11-27 22:19:20 +08:00
|
|
|
protected $idMapper;
|
2019-12-13 04:07:35 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The legacy table this importer targets
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-12-02 22:57:35 +08:00
|
|
|
protected $table;
|
2019-11-27 22:19:20 +08:00
|
|
|
|
2020-02-24 08:48:28 +08:00
|
|
|
/**
|
|
|
|
* The column used for the ID, used for the ORDER BY
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $idField = 'id';
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$importerService = app(ImporterService::class);
|
|
|
|
$this->db = new ImporterDB($importerService->getCredentials());
|
|
|
|
$this->idMapper = app(IdMapper::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The start method. Takes the offset to start from
|
|
|
|
*
|
|
|
|
* @param int $start
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function run($start = 0);
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
/**
|
|
|
|
* Return a manifest of the import tasks to run. Returns an array of objects,
|
|
|
|
* which contain a start and end row
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getManifest(): array
|
|
|
|
{
|
|
|
|
$manifest = [];
|
|
|
|
|
2020-02-26 03:45:23 +08:00
|
|
|
// Ensure that the table exists; if it doesn't skip it from the manifest
|
|
|
|
if (!$this->db->tableExists($this->table)) {
|
|
|
|
Log::info('Table '.$this->table.' doesn\'t exist');
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
$start = 0;
|
|
|
|
$total_rows = $this->db->getTotalRows($this->table);
|
2020-02-24 08:48:28 +08:00
|
|
|
Log::info('Found '.$total_rows.' rows for '.$this->table);
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
do {
|
|
|
|
$end = $start + $this->db->batchSize;
|
|
|
|
if ($end > $total_rows) {
|
|
|
|
$end = $total_rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
$idx = $start + 1;
|
|
|
|
|
|
|
|
$manifest[] = [
|
|
|
|
'importer' => get_class($this),
|
|
|
|
'start' => $start,
|
|
|
|
'end' => $end,
|
|
|
|
'message' => 'Importing '.$this->table.' ('.$idx.' - '.$end.' of '.$total_rows.')',
|
|
|
|
];
|
|
|
|
|
|
|
|
$start += $this->db->batchSize;
|
|
|
|
} while ($start < $total_rows);
|
|
|
|
|
|
|
|
return $manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what columns exist, can be used for feature testing between v2/v5
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumns(): array
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
/**
|
|
|
|
* @param $date
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
protected function parseDate($date)
|
|
|
|
{
|
|
|
|
$carbon = Carbon::parse($date);
|
|
|
|
|
|
|
|
return $carbon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a decimal duration and convert it to minutes
|
|
|
|
*
|
|
|
|
* @param $duration
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
protected function convertDuration($duration)
|
|
|
|
{
|
|
|
|
if (strpos($duration, '.') !== false) {
|
|
|
|
$delim = '.';
|
|
|
|
} elseif (strpos($duration, ':')) {
|
|
|
|
$delim = ':';
|
|
|
|
} else {
|
|
|
|
// no delimiter, assume it's just a straight hour
|
|
|
|
return (int) $duration * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hm = explode($delim, $duration);
|
|
|
|
$hours = (int) $hm[0] * 60;
|
|
|
|
$mins = (int) $hm[1];
|
|
|
|
|
|
|
|
return $hours + $mins;
|
|
|
|
}
|
|
|
|
}
|