clrghouz/app/Jobs/NodelistImport.php

267 lines
6.1 KiB
PHP
Raw Normal View History

2021-06-25 11:31:57 +00:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DomainController;
use App\Models\{Address,Domain,Nodelist,System,Zone};
use App\Traits\Import as ImportTrait;
class NodelistImport implements ShouldQueue
2021-06-25 11:31:57 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ImportTrait;
protected const LOGKEY = 'JIN';
private const importkey = 'nodelist';
private string $file;
private Domain $do;
private Nodelist $no;
private bool $deletefile = FALSE;
/**
* Import Nodelist constructor.
*
* @param Domain $do
* @param Nodelist $no
* @param string $file
* @param bool $delete
*/
public function __construct(Domain $do,Nodelist $no,string $file,bool $delete=TRUE)
{
$this->do = $do;
$this->no = $no;
$this->file = $file;
if ($delete)
$no->addresses()->detach();
}
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle()
{
// Get the file from the host
$file = $this->getFileFromHost('',self::importkey,$this->file);
$lines = $this->getFileLines($file);
Log::debug(sprintf('%s:Processing [%d] lines.',static::LOGKEY,$lines));
$fh = fopen($file,'r');
2021-06-26 00:34:49 +00:00
$p = $c =0;
2021-06-25 11:31:57 +00:00
$region = NULL;
$host = NULL;
$hub_id = NULL;
2021-06-26 00:34:49 +00:00
$zo = NULL;
2021-06-25 11:31:57 +00:00
while (! feof($fh)) {
$line = stream_get_line($fh, 0, "\r\n");
// Lines beginning with a semicolon(;) are comments
if (preg_match('/^;/',$line) OR ($line == chr(0x1a)))
continue;
// Remove any embedded CR and BOM
$line = str_replace("\r",'',$line);
$line = preg_replace('/^\x{feff}/u','',$line);
$c++;
$fields = str_getcsv(trim($line));
// First field is either zone,region,host,hub,down,pvt (or blank)
if ($fields[0] AND ! in_array($fields[0],Nodelist::definitions)) {
Log::error(sprintf('%s:Invalid field zero [%s] - IGNORING record (%s)',self::LOGKEY,$fields[0],$line));
continue;
}
$node = 0;
switch ($fields[0]) {
case 'Zone': $zone = $fields[1];
2021-06-26 00:34:49 +00:00
Zone::unguard();
$zo = Zone::firstOrNew([
'zone_id'=>$zone,
'domain_id'=>$this->do->id,
'active'=>TRUE,
]);
Zone::reguard();
2021-06-25 11:31:57 +00:00
$region = 0;
$host = 0;
2021-06-25 11:31:57 +00:00
$hub_id = NULL;
$role = DomainController::NODE_ZC;
2021-06-26 00:34:49 +00:00
break;
2021-06-25 11:31:57 +00:00
case 'Region':
$region = $fields[1];
$role = DomainController::NODE_RC;
$host = 0;
$hub_id = NULL;
break;
case 'Host':
$host = $fields[1];
$hub_id = NULL;
$role = DomainController::NODE_NC;
break;
2021-06-25 11:31:57 +00:00
case 'Hub':
2021-06-26 00:34:49 +00:00
$node = $fields[1];
2021-06-25 11:31:57 +00:00
$role = DomainController::NODE_HC;
break;
case 'Pvt':
$node = $fields[1];
$role = DomainController::NODE_PVT;
break;
2021-06-26 00:34:49 +00:00
case 'Hold':
$node = $fields[1];
$role = DomainController::NODE_HOLD;
break;
2021-06-25 11:31:57 +00:00
case 'Down':
$node = $fields[1];
$role = DomainController::NODE_DOWN;
break;
case '':
$node = $fields[1];
break;
default:
Log::error(sprintf('%s:Unhandled first field [%s]',self::LOGKEY,$fields[0]));
continue 2;
}
if (! $zone) {
Log::error(sprintf('%s:Zone NOT set, ignoring record...',self::LOGKEY));
continue;
}
Address::unguard();
$ao = Address::firstOrNew([
'region_id' => $region,
'host_id' => $host,
'node_id' => $node,
'point_id' => 0,
]);
Address::reguard();
$ao->active = TRUE;
$ao->role = $role;
$ao->hub_id = $hub_id;
$role = NULL;
2021-06-26 00:34:49 +00:00
if ($ao->exists)
Log::debug(sprintf('%s:Processing existing address [%s]',self::LOGKEY,$ao->ftn));
$sysop = trim(str_replace('_',' ',$fields[4]));
$system = trim(str_replace('_',' ',$fields[2]));
$location = trim(str_replace('_',' ',$fields[3]));
2021-06-25 11:31:57 +00:00
// Get the System
2021-06-26 00:34:49 +00:00
if ($ao->system_id && (($ao->system->sysop === $sysop) || ($ao->system->name === $system))) {
2021-06-25 11:31:57 +00:00
$so = $ao->system;
// If the sysop name is different
2021-06-26 00:34:49 +00:00
if ($so->sysop !== $sysop) {
Log::debug(sprintf('%s:Sysop Name changed for BBS [%s:%s] from [%s] to [%s]',
self::LOGKEY,$so->id,$so->name,$so->sysop,$sysop));
$so->sysop = $sysop;
2021-06-25 11:31:57 +00:00
// We have the same name has changed.
2021-06-26 00:34:49 +00:00
} elseif ($so->name !== $system) {
Log::debug(sprintf('%s:System Name changed for BBS [%s:%s] to [%s]',
self::LOGKEY,$so->id,$so->name,$system));
$so->name = $system;
2021-06-25 11:31:57 +00:00
}
// We'll search and see if we already have that system
} else {
2021-06-26 00:34:49 +00:00
$so = System::where('name',$system)
->where('sysop',$sysop)
2021-06-25 11:31:57 +00:00
->firstOrNew();
2021-06-26 00:34:49 +00:00
if ($so->exists)
Log::debug(sprintf('%s:Linking address [%d:%d/%d] to [%s:%s]',self::LOGKEY,$zo->zone_id,$ao->host_id,$ao->node_id,$so->id,$so->name));
else
Log::debug(sprintf('%s:New System [%s] with address [%d:%d/%d]',self::LOGKEY,$system,$zo->zone_id,$ao->host_id,$ao->node_id));
$so->name = $system;
$so->sysop = $sysop;
2021-06-25 11:31:57 +00:00
$so->active = TRUE;
if (! $so->exists)
$so->notes = sprintf('Created by Nodelist Import: %d',$this->no->id);
}
2021-06-26 00:34:49 +00:00
$so->location = $location;
2021-06-25 11:31:57 +00:00
/*
if (! in_array($fields[5],['-Unpublished-']))
$so->phone = $fields[5];
$so->baud = $fields[6];
*/
// Save the system record
$so->save();
2021-06-26 00:34:49 +00:00
// If our zone didnt exist, we'll create it with this system
if (! $zo->exists) {
$zo->system_id = $so->id;
$zo->save();
}
$ao->zone_id = $zo->id;
if ($ao->getDirty())
2021-06-26 00:34:49 +00:00
$p++;
2021-06-25 11:31:57 +00:00
try {
$so->addresses()->save($ao);
if ($ao->role == DomainController::NODE_HC)
2021-06-25 11:31:57 +00:00
$hub_id = $ao->id;
$this->no->addresses()->attach($ao,['role'=>$ao->role]);
2021-06-25 11:31:57 +00:00
} catch (\Exception $e) {
Log::error(sprintf('%s:Error with line [%s] (%s)',self::LOGKEY,$line,$e->getMessage()),['fields'=>$fields]);
throw new \Exception($e->getMessage());
}
if (! ($c % 100)) {
Log::notice(sprintf('%s:Processed [%s] records',self::LOGKEY,$c),['memory'=>memory_get_usage(TRUE)]);
}
}
fclose($fh);
if ($this->deletefile and $c)
unlink($file);
2021-06-26 00:34:49 +00:00
Log::info(sprintf('%s:Updated %d records from %d systems',self::LOGKEY,$p,$c));
2021-06-25 11:31:57 +00:00
}
}