1
0
www.mikescher.com/www/extern/egg/ExtendedGitGraph2.php

194 lines
4.4 KiB
PHP
Raw Normal View History

2019-12-28 14:00:11 +01:00
<?php
require_once 'Logger.php';
2020-04-22 11:24:48 +02:00
require_once 'EGGException.php';
2019-12-28 14:00:11 +01:00
require_once 'RemoteSource.php';
require_once 'OutputGenerator.php';
require_once 'EGGDatabase.php';
require_once 'Utils.php';
class ExtendedGitGraph2 implements ILogger
{
/** @var ILogger[] **/
private $logger;
/** @var IRemoteSource[] **/
private $sources;
/** @var IOutputGenerator[] **/
private $outputter;
/** @var EGGDatabase **/
private $db;
/**
* @param array $config
* @throws Exception
*/
public function __construct(array $config)
{
$this->logger = [];
if ($config['output_session']) $this->logger []= new SessionLogger($config['session_var']);
if ($config['output_stdout']) $this->logger []= new OutputLogger();
if ($config['output_logfile']) $this->logger []= new FileLogger($config['logfile'], $config['logfile_count']);
2019-12-29 00:26:57 +01:00
if ($config['output_file']) $this->logger []= new SingleFileLogger($config['output_filepath']);
2019-12-28 14:00:11 +01:00
$this->sources = [];
$sourcenames = [];
foreach ($config['remotes'] as $rmt)
{
$newsrc = null;
if ($rmt['type'] === 'github')
$newsrc = new GithubConnection($this, $rmt['name'], $rmt['url'], $rmt['filter'], $rmt['exclusions'], $rmt['oauth_id'], $rmt['oauth_secret'], $rmt['token_cache'] );
else if ($rmt['type'] === 'gitea')
$newsrc = new GiteaConnection($this, $rmt['name'], $rmt['url'], $rmt['filter'], $rmt['exclusions'], $rmt['username'], $rmt['password'] );
else
2020-04-22 11:24:48 +02:00
throw new EGGException("Unknown remote-type: " . $rmt['type']);
2019-12-28 14:00:11 +01:00
2020-04-22 11:24:48 +02:00
if (array_key_exists($newsrc->getName(), $sourcenames)) throw new EGGException("Duplicate source name: " . $newsrc->getName());
2019-12-28 14:00:11 +01:00
$this->sources []= $newsrc;
$sourcenames []= $newsrc->getName();
}
$this->db = new EGGDatabase($config['data_cache_file'], $this);
$this->outputter = new FullRenderer($this, $config['identities'], $config['output_cache_files']);
}
public function update()
{
try
{
$this->db->open();
$this->proclog("Start incremental data update");
$this->proclog();
foreach ($this->sources as $src)
{
$this->proclog("======= UPDATE " . $src->getName() . " =======");
$src->update($this->db);
$this->proclog();
}
$this->db->beginTransaction();
2019-12-28 14:00:11 +01:00
$this->db->deleteOldSources(array_map(function (IRemoteSource $v){ return $v->getName(); }, $this->sources));
$this->db->commitTransaction();
2019-12-28 14:00:11 +01:00
$this->proclog("Update finished.");
2020-04-22 11:24:48 +02:00
$this->proclog("Data written.");
2019-12-28 14:00:11 +01:00
$this->db->close();
2020-04-22 11:24:48 +02:00
return true;
}
catch (EGGException $exception)
{
$this->proclog("ExtendedGitGraph2::update failed:");
$this->proclog($exception->egg_message);
$this->db->abortTransactionIfExists();
return false;
2019-12-28 14:00:11 +01:00
}
catch (Exception $exception)
{
$this->proclog("(!) FATAL ERROR -- UNCAUGHT EXCEPTION THROWN");
$this->proclog();
$this->proclog($exception->getMessage());
$this->proclog();
$this->proclog($exception->getTraceAsString());
2020-04-22 11:24:48 +02:00
$this->db->abortTransactionIfExists();
return false;
2019-12-28 14:00:11 +01:00
}
}
2020-04-22 11:24:48 +02:00
public function updateCache(): ?string
2019-12-28 14:00:11 +01:00
{
try
{
2020-04-22 11:24:48 +02:00
$this->db->openReadOnly();
$this->db->beginTransaction();
2019-12-28 14:00:11 +01:00
$this->proclog("Start update cache");
$this->proclog();
$data = $this->outputter->updateCache($this->db);
$this->db->close();
$this->proclog("UpdateCache finished.");
return $data;
}
2020-04-22 11:24:48 +02:00
catch (EGGException $exception)
{
$this->proclog("ExtendedGitGraph2::updateCache failed:");
$this->proclog($exception->egg_message);
$this->db->abortTransactionIfExists();
return null;
}
2019-12-28 14:00:11 +01:00
catch (Exception $exception)
{
$this->proclog("(!) FATAL ERROR -- UNCAUGHT EXCEPTION THROWN");
$this->proclog();
$this->proclog($exception->getMessage());
$this->proclog();
$this->proclog($exception->getTraceAsString());
2020-04-22 11:24:48 +02:00
return null;
2019-12-28 14:00:11 +01:00
}
}
/**
* @return string|null
*/
public function loadFromCache()
{
try
{
2020-04-22 11:24:48 +02:00
$this->db->openReadOnly();
2019-12-28 14:00:11 +01:00
$data = $this->outputter->loadFromCache();
2020-04-22 11:24:48 +02:00
$this->db->close();
2019-12-28 14:00:11 +01:00
return $data;
}
catch (Exception $exception)
{
$this->proclog("(!) FATAL ERROR -- UNCAUGHT EXCEPTION THROWN");
$this->proclog();
$this->proclog($exception->getMessage());
$this->proclog();
$this->proclog($exception->getTraceAsString());
}
}
public function proclog($text = '')
{
foreach($this->logger as $lgr) $lgr->proclog($text);
}
/**
* @return string[]
*/
public function checkDatabase(): array
{
$this->db->open();
$r = $this->db->checkDatabase();
$this->db->close();
return $r;
}
2019-12-28 14:00:11 +01:00
}