. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Statistics\Repository; use Exception; use Fisharebest\Webtrees\Fact; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Header; use Fisharebest\Webtrees\Statistics\Repository\Interfaces\GedcomRepositoryInterface; use Fisharebest\Webtrees\Tree; use Illuminate\Database\Capsule\Manager as DB; use function e; use function str_contains; use function strpos; use function substr; /** * A repository providing methods for GEDCOM related statistics. */ class GedcomRepository implements GedcomRepositoryInterface { private Tree $tree; /** * @param Tree $tree */ public function __construct(Tree $tree) { $this->tree = $tree; } /** * Get information from the GEDCOM's HEAD record. * * @return array */ private function gedcomHead(): array { $title = ''; $version = ''; $source = ''; $head = Registry::headerFactory()->make('HEAD', $this->tree); if ($head instanceof Header) { $sour = $head->facts(['SOUR'])->first(); if ($sour instanceof Fact) { $source = $sour->value(); $title = $sour->attribute('NAME'); $version = $sour->attribute('VERS'); } } return [ $title, $version, $source, ]; } /** * @return string */ public function gedcomFilename(): string { return $this->tree->name(); } /** * @return int */ public function gedcomId(): int { return $this->tree->id(); } /** * @return string */ public function gedcomTitle(): string { return e($this->tree->title()); } /** * @return string */ public function gedcomCreatedSoftware(): string { return $this->gedcomHead()[0]; } /** * @return string */ public function gedcomCreatedVersion(): string { $head = $this->gedcomHead(); // Fix broken version string in Family Tree Maker if (str_contains($head[1], 'Family Tree Maker ')) { $p = strpos($head[1], '(') + 1; $p2 = strpos($head[1], ')'); $head[1] = substr($head[1], $p, $p2 - $p); } // Fix EasyTree version if ($head[2] === 'EasyTree') { $head[1] = substr($head[1], 1); } return $head[1]; } /** * @return string * @throws Exception */ public function gedcomDate(): string { $head = Registry::headerFactory()->make('HEAD', $this->tree); if ($head instanceof Header) { $fact = $head->facts(['DATE'])->first(); if ($fact instanceof Fact) { return Registry::timestampFactory()->fromString($fact->value(), 'j M Y')->isoFormat('LL'); } } return ''; } /** * @return string */ public function gedcomUpdated(): string { $row = DB::table('change') ->where('gedcom_id', '=', $this->tree->id()) ->where('status', '=', 'accepted') ->orderBy('change_id', 'DESC') ->select(['change_time']) ->first(); if ($row === null) { return $this->gedcomDate(); } return Registry::timestampFactory()->fromString($row->change_time)->isoFormat('LL'); } /** * @return string */ public function gedcomRootId(): string { return $this->tree->getPreference('PEDIGREE_ROOT_ID'); } }