Split the importer module out from the installer module (#468)
* Split the importer module out from the installer module * Cleanup of unused imports * Move updater into separate module #453 * Remove unused imports/formatting * Disable the install and importer modules at the end of the setup * Unused imports; update IJ style * test explicit stage for php+mysql * add more to matrix * Add different MariaDB versions * undo
This commit is contained in:
@@ -13,9 +13,6 @@ use Nwidart\Modules\Support\Stub;
|
||||
use PDO;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
/**
|
||||
* Class ConfigService
|
||||
*/
|
||||
class ConfigService extends Service
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PDO;
|
||||
|
||||
class DatabaseService extends Service
|
||||
{
|
||||
/**
|
||||
* Check the PHP version that it meets the minimum requirement
|
||||
*
|
||||
* @param $driver
|
||||
* @param $host
|
||||
* @param $port
|
||||
* @param $name
|
||||
* @param $user
|
||||
* @param $pass
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkDbConnection($driver, $host, $port, $name, $user, $pass)
|
||||
{
|
||||
Log::info('Testing Connection: '.$driver.'::'.$user.':<hidden>@'.$host.':'.$port.';'.$name);
|
||||
|
||||
// TODO: Needs testing
|
||||
if ($driver === 'postgres') {
|
||||
$dsn = "pgsql:host=$host;port=$port;dbname=$name";
|
||||
|
||||
try {
|
||||
$conn = new PDO($dsn, $user, $pass);
|
||||
} catch (\PDOException $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Default MySQL
|
||||
$dsn = "mysql:host=$host;port=$port;dbname=$name";
|
||||
Log::info('Connection string: '.$dsn);
|
||||
|
||||
try {
|
||||
$conn = new PDO($dsn, $user, $pass);
|
||||
} catch (\PDOException $e) {
|
||||
throw $e;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the database by running the migration commands
|
||||
* Only run the setup for sqlite, otherwise, we're assuming
|
||||
* that the MySQL database has already been created
|
||||
*/
|
||||
public function setupDB()
|
||||
{
|
||||
$output = '';
|
||||
|
||||
if (config('database.default') === 'sqlite') {
|
||||
Artisan::call('database:create');
|
||||
$output .= Artisan::output();
|
||||
}
|
||||
|
||||
return trim($output);
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Modules\Installer\Utils\IdMapper;
|
||||
use Modules\Installer\Utils\ImporterDB;
|
||||
use Modules\Installer\Utils\LoggerTrait;
|
||||
|
||||
abstract class BaseImporter implements ShouldQueue
|
||||
{
|
||||
use LoggerTrait, Dispatchable, InteractsWithQueue, Queueable;
|
||||
|
||||
protected $db;
|
||||
protected $idMapper;
|
||||
protected $table;
|
||||
|
||||
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);
|
||||
|
||||
/**
|
||||
* 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 = [];
|
||||
|
||||
$start = 0;
|
||||
$total_rows = $this->db->getTotalRows($this->table);
|
||||
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
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Repositories\KvpRepository;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Modules\Installer\Services\Importer\Importers\AircraftImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\AirlineImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\AirportImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\ClearDatabase;
|
||||
use Modules\Installer\Services\Importer\Importers\FinalizeImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\FlightImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\GroupImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\PirepImporter;
|
||||
use Modules\Installer\Services\Importer\Importers\RankImport;
|
||||
use Modules\Installer\Services\Importer\Importers\UserImport;
|
||||
|
||||
class ImporterService extends Service
|
||||
{
|
||||
private $CREDENTIALS_KEY = 'legacy.importer.db';
|
||||
|
||||
/**
|
||||
* @var KvpRepository
|
||||
*/
|
||||
private $kvpRepo;
|
||||
|
||||
/**
|
||||
* The list of importers, in proper order
|
||||
*/
|
||||
private $importList = [
|
||||
ClearDatabase::class,
|
||||
RankImport::class,
|
||||
GroupImporter::class,
|
||||
AirlineImporter::class,
|
||||
AircraftImporter::class,
|
||||
AirportImporter::class,
|
||||
FlightImporter::class,
|
||||
UserImport::class,
|
||||
PirepImporter::class,
|
||||
FinalizeImporter::class,
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->kvpRepo = app(KvpRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the credentials from a request
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function saveCredentialsFromRequest(Request $request)
|
||||
{
|
||||
$creds = [
|
||||
'admin_email' => $request->post('email'),
|
||||
'host' => $request->post('db_host'),
|
||||
'port' => $request->post('db_port'),
|
||||
'name' => $request->post('db_name'),
|
||||
'user' => $request->post('db_user'),
|
||||
'pass' => $request->post('db_pass'),
|
||||
'table_prefix' => $request->post('db_prefix'),
|
||||
];
|
||||
|
||||
$this->saveCredentials($creds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the given credentials
|
||||
*
|
||||
* @param array $creds
|
||||
*/
|
||||
public function saveCredentials(array $creds)
|
||||
{
|
||||
$creds = array_merge([
|
||||
'admin_email' => '',
|
||||
'host' => '',
|
||||
'port' => '',
|
||||
'name' => '',
|
||||
'user' => '',
|
||||
'pass' => 3306,
|
||||
'table_prefix' => 'phpvms_',
|
||||
], $creds);
|
||||
|
||||
$this->kvpRepo->save($this->CREDENTIALS_KEY, $creds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the saved credentials
|
||||
*/
|
||||
public function getCredentials()
|
||||
{
|
||||
return $this->kvpRepo->get($this->CREDENTIALS_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a manifest of the import. Creates an array with the importer name,
|
||||
* which then has a subarray of all of the different steps/stages it needs to run
|
||||
*/
|
||||
public function generateImportManifest()
|
||||
{
|
||||
$manifest = [];
|
||||
|
||||
foreach ($this->importList as $importerKlass) {
|
||||
/** @var \Modules\Installer\Services\Importer\BaseImporter $importer */
|
||||
$importer = new $importerKlass();
|
||||
$manifest = array_merge($manifest, $importer->getManifest());
|
||||
}
|
||||
|
||||
return $manifest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a given stage
|
||||
*
|
||||
* @param $importer
|
||||
* @param int $start
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return int|void
|
||||
*/
|
||||
public function run($importer, $start = 0)
|
||||
{
|
||||
if (!in_array($importer, $this->importList)) {
|
||||
throw new Exception('Unknown importer "'.$importer.'"');
|
||||
}
|
||||
|
||||
/** @var $importerInst \Modules\Installer\Services\Importer\BaseImporter */
|
||||
$importerInst = new $importer();
|
||||
$importerInst->run($start);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Aircraft;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Subfleet;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class AircraftImporter extends BaseImporter
|
||||
{
|
||||
public $table = 'aircraft';
|
||||
|
||||
/**
|
||||
* CONSTANTS
|
||||
*/
|
||||
public const SUBFLEET_NAME = 'Imported Aircraft';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRCRAFT IMPORT ---');
|
||||
|
||||
$subfleet = $this->getSubfleet();
|
||||
|
||||
$this->info('Subfleet ID is '.$subfleet->id);
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start) as $row) {
|
||||
$where = [
|
||||
'name' => $row->fullname,
|
||||
'registration' => $row->registration,
|
||||
];
|
||||
|
||||
$aircraft = Aircraft::firstOrCreate($where, [
|
||||
'icao' => $row->icao,
|
||||
'subfleet_id' => $subfleet->id,
|
||||
'active' => $row->enabled,
|
||||
]);
|
||||
|
||||
$this->idMapper->addMapping('aircraft', $row->id, $aircraft->id);
|
||||
|
||||
if ($aircraft->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' aircraft');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subfleet
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getSubfleet()
|
||||
{
|
||||
$airline = Airline::first();
|
||||
$subfleet = Subfleet::firstOrCreate([
|
||||
'airline_id' => $airline->id,
|
||||
'name' => self::SUBFLEET_NAME,
|
||||
], ['type' => 'PHPVMS']);
|
||||
|
||||
return $subfleet;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Airline;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class AirlineImporter extends BaseImporter
|
||||
{
|
||||
public $table = 'airlines';
|
||||
|
||||
/**
|
||||
* @param int $start
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRLINE IMPORT ---');
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start) as $row) {
|
||||
$attrs = [
|
||||
'iata' => $row->code,
|
||||
'name' => $row->name,
|
||||
'active' => $row->enabled,
|
||||
];
|
||||
|
||||
$w = ['icao' => $row->code];
|
||||
|
||||
//$airline = Airline::firstOrCreate($w, $attrs);
|
||||
$airline = Airline::create(array_merge($w, $attrs));
|
||||
|
||||
$this->idMapper->addMapping('airlines', $row->id, $airline->id);
|
||||
$this->idMapper->addMapping('airlines', $row->code, $airline->id);
|
||||
|
||||
Log::debug('Mapping '.$row->id.'/'.$row->code.' to ID '.$airline->id);
|
||||
|
||||
if ($airline->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' airlines');
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Airport;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class AirportImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'airports';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- AIRPORT IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'icao',
|
||||
'name',
|
||||
'country',
|
||||
'lat',
|
||||
'lng',
|
||||
'hub',
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
|
||||
$attrs = [
|
||||
'id' => trim($row->icao),
|
||||
'icao' => trim($row->icao),
|
||||
'name' => $row->name,
|
||||
'country' => $row->country,
|
||||
'lat' => $row->lat,
|
||||
'lon' => $row->lng,
|
||||
'hub' => $row->hub,
|
||||
];
|
||||
|
||||
$w = ['id' => $attrs['id']];
|
||||
//$airport = Airport::updateOrCreate($w, $attrs);
|
||||
|
||||
try {
|
||||
$airport = Airport::create(array_merge($w, $attrs));
|
||||
} catch (QueryException $e) {
|
||||
$sqlState = $e->errorInfo[0];
|
||||
$errorCode = $e->errorInfo[1];
|
||||
if ($sqlState === '23000' && $errorCode === 1062) {
|
||||
Log::info('Found duplicate for '.$row->icao.', ignoring');
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($airport->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' airports');
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Acars;
|
||||
use App\Models\Airline;
|
||||
use App\Models\Airport;
|
||||
use App\Models\Bid;
|
||||
use App\Models\Expense;
|
||||
use App\Models\File;
|
||||
use App\Models\Flight;
|
||||
use App\Models\FlightField;
|
||||
use App\Models\FlightFieldValue;
|
||||
use App\Models\Journal;
|
||||
use App\Models\JournalTransaction;
|
||||
use App\Models\News;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\Subfleet;
|
||||
use App\Models\User;
|
||||
use App\Models\UserAward;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class ClearDatabase extends BaseImporter
|
||||
{
|
||||
/**
|
||||
* Returns a default manifest just so this step gets run
|
||||
*/
|
||||
public function getManifest(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'importer' => get_class($this),
|
||||
'start' => 0,
|
||||
'end' => 1,
|
||||
'message' => 'Clearing database',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->cleanupDb();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup the local database of any users and other data that might conflict
|
||||
* before running the importer
|
||||
*/
|
||||
protected function cleanupDb()
|
||||
{
|
||||
$this->info('Running database cleanup/empty before starting');
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=0');
|
||||
|
||||
Bid::truncate();
|
||||
File::truncate();
|
||||
News::truncate();
|
||||
|
||||
Expense::truncate();
|
||||
JournalTransaction::truncate();
|
||||
Journal::truncate();
|
||||
|
||||
// Clear flights
|
||||
DB::table('flight_fare')->truncate();
|
||||
DB::table('flight_subfleet')->truncate();
|
||||
FlightField::truncate();
|
||||
FlightFieldValue::truncate();
|
||||
Flight::truncate();
|
||||
Subfleet::truncate();
|
||||
|
||||
// Clear permissions
|
||||
// DB::table('permission_role')->truncate();
|
||||
// DB::table('permission_user')->truncate();
|
||||
// DB::table('role_user')->truncate();
|
||||
// Role::truncate();
|
||||
|
||||
Airline::truncate();
|
||||
Airport::truncate();
|
||||
Acars::truncate();
|
||||
Pirep::truncate();
|
||||
|
||||
UserAward::truncate();
|
||||
User::truncate();
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=1');
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\UserService;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class FinalizeImporter extends BaseImporter
|
||||
{
|
||||
/**
|
||||
* Returns a default manifest just so this step gets run
|
||||
*/
|
||||
public function getManifest(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'importer' => get_class($this),
|
||||
'start' => 0,
|
||||
'end' => 1,
|
||||
'message' => 'Finalizing import',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The start method. Takes the offset to start from
|
||||
*
|
||||
* @param int $start
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->findLastPireps();
|
||||
$this->recalculateUserStats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Go through and set the last PIREP ID for the users
|
||||
*/
|
||||
protected function findLastPireps()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate all of the user stats
|
||||
*/
|
||||
protected function recalculateUserStats()
|
||||
{
|
||||
$this->comment('--- RECALCULATING USER STATS ---');
|
||||
$userSvc = app(UserService::class);
|
||||
|
||||
User::all()->each(function ($user) use ($userSvc) {
|
||||
$userSvc->recalculateStats($user);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Flight;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class FlightImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'schedules';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- FLIGHT SCHEDULE IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'id',
|
||||
'code',
|
||||
'flightnum',
|
||||
'depicao',
|
||||
'arricao',
|
||||
'route',
|
||||
'distance',
|
||||
'flightlevel',
|
||||
'deptime',
|
||||
'arrtime',
|
||||
'flightttime',
|
||||
'notes',
|
||||
'enabled',
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
|
||||
$flight_num = trim($row->flightnum);
|
||||
|
||||
$attrs = [
|
||||
'dpt_airport_id' => $row->depicao,
|
||||
'arr_airport_id' => $row->arricao,
|
||||
'route' => $row->route ?: '',
|
||||
'distance' => round($row->distance ?: 0, 2),
|
||||
'level' => $row->flightlevel ?: 0,
|
||||
'dpt_time' => $row->deptime ?: '',
|
||||
'arr_time' => $row->arrtime ?: '',
|
||||
'flight_time' => $this->convertDuration($row->flighttime) ?: '',
|
||||
'notes' => $row->notes ?: '',
|
||||
'active' => $row->enabled ?: true,
|
||||
];
|
||||
|
||||
try {
|
||||
$w = ['airline_id' => $airline_id, 'flight_number' => $flight_num];
|
||||
// $flight = Flight::updateOrCreate($w, $attrs);
|
||||
$flight = Flight::create(array_merge($w, $attrs));
|
||||
} catch (\Exception $e) {
|
||||
//$this->error($e);
|
||||
}
|
||||
|
||||
$this->idMapper->addMapping('flights', $row->id, $flight->id);
|
||||
|
||||
// TODO: deserialize route_details into ACARS table
|
||||
|
||||
if ($flight->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' flights');
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Services\RoleService;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
/**
|
||||
* Imports the groups into the permissions feature(s)
|
||||
*/
|
||||
class GroupImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'groups';
|
||||
|
||||
/**
|
||||
* Permissions in the legacy system, mapping them to the current system
|
||||
*/
|
||||
protected $legacy_permission_set = [
|
||||
'EDIT_NEWS' => 0x1,
|
||||
'EDIT_PAGES' => 0x2,
|
||||
'EDIT_DOWNLOADS' => 0x4,
|
||||
'EMAIL_PILOTS' => 0x8,
|
||||
'EDIT_AIRLINES' => 0x10, //
|
||||
'EDIT_FLEET' => 0x20, //
|
||||
'EDIT_SCHEDULES' => 0x80, //
|
||||
'IMPORT_SCHEDULES' => 0x100, //
|
||||
'MODERATE_REGISTRATIONS' => 0x200,
|
||||
'EDIT_PILOTS' => 0x400, //
|
||||
'EDIT_GROUPS' => 0x800,
|
||||
'EDIT_RANKS' => 0x1000, //
|
||||
'EDIT_AWARDS' => 0x2000, //
|
||||
'MODERATE_PIREPS' => 0x4000, //
|
||||
'VIEW_FINANCES' => 0x8000, //
|
||||
'EDIT_EXPENSES' => 0x10000, //
|
||||
'EDIT_SETTINGS' => 0x20000, //
|
||||
'EDIT_PIREPS_FIELDS' => 0x40000, //
|
||||
'EDIT_PROFILE_FIELDS' => 0x80000, //
|
||||
'EDIT_VACENTRAL' => 0x100000,
|
||||
'ACCESS_ADMIN' => 0x2000000,
|
||||
'FULL_ADMIN' => 35651519, //
|
||||
];
|
||||
|
||||
/**
|
||||
* Map a legacy value over to one of the current permission values
|
||||
*/
|
||||
protected $legacy_to_permission = [
|
||||
'FULL_ADMIN' => 'admin',
|
||||
'EDIT_AIRLINES' => 'airlines',
|
||||
'EDIT_AWARDS' => 'awards',
|
||||
'EDIT_FLEET' => 'fleet',
|
||||
'EDIT_EXPENCES' => 'finances',
|
||||
'VIEW_FINANCES' => 'finances',
|
||||
'EDIT_SCHEDULES' => 'flights',
|
||||
'EDIT_PILOTS' => 'users',
|
||||
'EDIT_PROFILE_FIELDS' => 'users',
|
||||
'EDIT_SETTINGS' => 'settings',
|
||||
'MODERATE_PIREPS' => 'pireps',
|
||||
'EDIT_PIREPS_FIELDS' => 'pireps',
|
||||
'EDIT_RANKS' => 'ranks',
|
||||
'MODERATE_REGISTRATIONS' => 'users',
|
||||
];
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- ROLES/GROUPS IMPORT ---');
|
||||
|
||||
$roleSvc = app(RoleService::class);
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start) as $row) {
|
||||
$name = str_slug($row->name);
|
||||
$role = Role::firstOrCreate(
|
||||
['name' => $name],
|
||||
['display_name' => $row->name]
|
||||
);
|
||||
|
||||
$this->idMapper->addMapping('group', $row->groupid, $role->id);
|
||||
|
||||
// See if the permission set mask contains one of the mappings above
|
||||
// Add all of the ones which apply, and then set them on the new role
|
||||
$permissions = [];
|
||||
foreach ($this->legacy_permission_set as $legacy_name => $mask) {
|
||||
if (($row->permissions & $mask) === true) {
|
||||
if (!array_key_exists($legacy_name, $this->legacy_to_permission)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$permissions[] = $this->legacy_to_permission[$legacy_name];
|
||||
}
|
||||
}
|
||||
|
||||
$roleSvc->setPermissionsForRole($role, $permissions);
|
||||
|
||||
if ($role->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' ranks');
|
||||
}
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Pirep;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class PirepImporter extends BaseImporter
|
||||
{
|
||||
protected $table = 'pireps';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- PIREP IMPORT ---');
|
||||
|
||||
$fields = [
|
||||
'pirepid',
|
||||
'pilotid',
|
||||
'code',
|
||||
'aircraft',
|
||||
'flightnum',
|
||||
'depicao',
|
||||
'arricao',
|
||||
'fuelused',
|
||||
'route',
|
||||
'source',
|
||||
'accepted',
|
||||
'submitdate',
|
||||
'distance',
|
||||
'flighttime_stamp',
|
||||
'flighttype',
|
||||
'flightlevel',
|
||||
];
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start, $fields) as $row) {
|
||||
$pirep_id = $row->pirepid;
|
||||
$user_id = $this->idMapper->getMapping('users', $row->pilotid);
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
$aircraft_id = $this->idMapper->getMapping('aircraft', $row->aircraft);
|
||||
|
||||
$attrs = [
|
||||
'user_id' => $user_id,
|
||||
'airline_id' => $airline_id,
|
||||
'aircraft_id' => $aircraft_id,
|
||||
'flight_number' => $row->flightnum ?: '',
|
||||
'dpt_airport_id' => $row->depicao,
|
||||
'arr_airport_id' => $row->arricao,
|
||||
'block_fuel' => $row->fuelused,
|
||||
'route' => $row->route ?: '',
|
||||
'source_name' => $row->source,
|
||||
'state' => $this->mapState($row->accepted),
|
||||
'created_at' => $this->parseDate($row->submitdate),
|
||||
'updated_at' => $this->parseDate($row->submitdate),
|
||||
];
|
||||
|
||||
// Set the distance
|
||||
$distance = round($row->distance ?: 0, 2);
|
||||
$attrs['distance'] = $distance;
|
||||
$attrs['planned_distance'] = $distance;
|
||||
|
||||
// Set the flight time properly
|
||||
$duration = $this->convertDuration($row->flighttime_stamp);
|
||||
$attrs['flight_time'] = $duration;
|
||||
$attrs['planned_flight_time'] = $duration;
|
||||
|
||||
// Set how it was filed
|
||||
if (strtoupper($row->source) === 'MANUAL') {
|
||||
$attrs['source'] = PirepSource::MANUAL;
|
||||
} else {
|
||||
$attrs['source'] = PirepSource::ACARS;
|
||||
}
|
||||
|
||||
// Set the flight type
|
||||
$row->flighttype = strtoupper($row->flighttype);
|
||||
if ($row->flighttype === 'P') {
|
||||
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
||||
} elseif ($row->flighttype === 'C') {
|
||||
$attrs['flight_type'] = FlightType::SCHED_CARGO;
|
||||
} else {
|
||||
$attrs['flight_type'] = FlightType::CHARTER_PAX_ONLY;
|
||||
}
|
||||
|
||||
// Set the flight level of the PIREP is set
|
||||
if (property_exists($row, 'flightlevel')) {
|
||||
$attrs['level'] = $row->flightlevel;
|
||||
} else {
|
||||
$attrs['level'] = 0;
|
||||
}
|
||||
|
||||
$w = ['id' => $pirep_id];
|
||||
|
||||
$pirep = Pirep::updateOrCreate($w, $attrs);
|
||||
//$pirep = Pirep::create(array_merge($w, $attrs));
|
||||
|
||||
//Log::debug('pirep oldid='.$pirep_id.', olduserid='.$row->pilotid
|
||||
// .'; new id='.$pirep->id.', user id='.$user_id);
|
||||
|
||||
$source = strtoupper($row->source);
|
||||
if ($source === 'SMARTCARS') {
|
||||
// TODO: Parse smartcars log into the acars table
|
||||
} elseif ($source === 'KACARS') {
|
||||
// TODO: Parse kACARS log into acars table
|
||||
} elseif ($source === 'XACARS') {
|
||||
// TODO: Parse XACARS log into acars table
|
||||
}
|
||||
|
||||
// TODO: Add extra fields in as PIREP fields
|
||||
$this->idMapper->addMapping('pireps', $row->pirepid, $pirep->id);
|
||||
|
||||
if ($pirep->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' pireps');
|
||||
}
|
||||
|
||||
/**
|
||||
* Map the old status to the current
|
||||
* https://github.com/nabeelio/phpvms_v2/blob/master/core/app.config.php#L450
|
||||
*
|
||||
* @param int $old_state
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
private function mapState($old_state)
|
||||
{
|
||||
$map = [
|
||||
0 => PirepState::PENDING,
|
||||
1 => PirepState::ACCEPTED,
|
||||
2 => PirepState::REJECTED,
|
||||
3 => PirepState::IN_PROGRESS,
|
||||
];
|
||||
|
||||
$old_state = (int) $old_state;
|
||||
if (!in_array($old_state, $map)) {
|
||||
return PirepState::PENDING;
|
||||
}
|
||||
|
||||
return $map[$old_state];
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Models\Rank;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class RankImport extends BaseImporter
|
||||
{
|
||||
protected $table = 'ranks';
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- RANK IMPORT ---');
|
||||
|
||||
$count = 0;
|
||||
foreach ($this->db->readRows($this->table, $start) as $row) {
|
||||
$rank = Rank::firstOrCreate(['name' => $row->rank], [
|
||||
'image_url' => $row->rankimage,
|
||||
'hours' => $row->minhours,
|
||||
]);
|
||||
|
||||
$this->idMapper->addMapping('ranks', $row->rankid, $rank->id);
|
||||
$this->idMapper->addMapping('ranks', $row->rank, $rank->id);
|
||||
|
||||
if ($rank->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' ranks');
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Installer\Services\Importer\Importers;
|
||||
|
||||
use App\Facades\Utils;
|
||||
use App\Models\Enums\UserState;
|
||||
use App\Models\User;
|
||||
use App\Services\UserService;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Installer\Services\Importer\BaseImporter;
|
||||
|
||||
class UserImport extends BaseImporter
|
||||
{
|
||||
protected $table = 'pilots';
|
||||
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
private $userSvc;
|
||||
|
||||
public function run($start = 0)
|
||||
{
|
||||
$this->comment('--- USER IMPORT ---');
|
||||
|
||||
$this->userSvc = app(UserService::class);
|
||||
|
||||
$count = 0;
|
||||
$first_row = true;
|
||||
foreach ($this->db->readRows($this->table, $start) as $row) {
|
||||
$pilot_id = $row->pilotid; // This isn't their actual ID
|
||||
$name = $row->firstname.' '.$row->lastname;
|
||||
|
||||
// Figure out which airline, etc, they belong to
|
||||
$airline_id = $this->idMapper->getMapping('airlines', $row->code);
|
||||
// Log::info('User airline from '.$row->code.' to ID '.$airline_id);
|
||||
|
||||
$rank_id = $this->idMapper->getMapping('ranks', $row->rank);
|
||||
$state = $this->getUserState($row->retired);
|
||||
|
||||
if ($first_row) {
|
||||
$new_password = 'admin';
|
||||
$first_row = false;
|
||||
} else {
|
||||
$new_password = Str::random(60);
|
||||
}
|
||||
|
||||
// Look for a user with that pilot ID already. If someone has it
|
||||
if ($this->userSvc->isPilotIdAlreadyUsed($pilot_id)) {
|
||||
Log::info('User with pilot id '.$pilot_id.' exists, reassigning');
|
||||
$pilot_id = $this->userSvc->getNextAvailablePilotId();
|
||||
}
|
||||
|
||||
$attrs = [
|
||||
'pilot_id' => $pilot_id,
|
||||
'name' => $name,
|
||||
'password' => Hash::make($new_password),
|
||||
'api_key' => Utils::generateApiKey(),
|
||||
'airline_id' => $airline_id,
|
||||
'rank_id' => $rank_id,
|
||||
'home_airport_id' => $row->hub,
|
||||
'curr_airport_id' => $row->hub,
|
||||
'flights' => (int) $row->totalflights,
|
||||
'flight_time' => Utils::hoursToMinutes($row->totalhours),
|
||||
'state' => $state,
|
||||
'created_at' => $this->parseDate($row->joindate),
|
||||
];
|
||||
|
||||
$user = User::updateOrCreate(['email' => $row->email], $attrs);
|
||||
$this->idMapper->addMapping('users', $row->pilotid, $user->id);
|
||||
|
||||
$this->updateUserRoles($user, $row->pilotid);
|
||||
|
||||
if ($user->wasRecentlyCreated) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Imported '.$count.' users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user's roles and add them to the proper ones
|
||||
*
|
||||
* @param User $user
|
||||
* @param string $old_pilot_id
|
||||
*/
|
||||
protected function updateUserRoles(User $user, $old_pilot_id)
|
||||
{
|
||||
// Be default add them to the user role, and then determine if they
|
||||
// belong to any other groups, and add them to that
|
||||
$this->userSvc->addUserToRole($user, 'user');
|
||||
|
||||
// Figure out what other groups they belong to
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's new state from their original state
|
||||
*
|
||||
* @param $state
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getUserState($state)
|
||||
{
|
||||
// Return active for now, let the stats/cron determine the status later
|
||||
return UserState::ACTIVE;
|
||||
/*$state = (int) $state;
|
||||
|
||||
// Declare array of classic states
|
||||
$phpvms_classic_states = [
|
||||
'ACTIVE' => 0,
|
||||
'INACTIVE' => 1,
|
||||
'BANNED' => 2,
|
||||
'ON_LEAVE' => 3,
|
||||
];
|
||||
|
||||
// Decide which state they will be in accordance with v7
|
||||
if ($state === $phpvms_classic_states['ACTIVE']) {
|
||||
return UserState::ACTIVE;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['INACTIVE']) {
|
||||
// TODO: Make an inactive state?
|
||||
return UserState::REJECTED;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['BANNED']) {
|
||||
return UserState::SUSPENDED;
|
||||
}
|
||||
|
||||
if ($state === $phpvms_classic_states['ON_LEAVE']) {
|
||||
return UserState::ON_LEAVE;
|
||||
}
|
||||
|
||||
$this->error('Unknown status: '.$state);
|
||||
|
||||
return UserState::ACTIVE;*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user