1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Fact; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\DataFixService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Query\Builder; 28use Illuminate\Support\Collection; 29 30use function str_contains; 31 32use const PHP_INT_MAX; 33 34/** 35 * Class FixPrimaryTag 36 */ 37class FixWtObjeSortTag extends AbstractModule implements ModuleDataFixInterface 38{ 39 use ModuleDataFixTrait; 40 41 private DataFixService $data_fix_service; 42 43 /** 44 * @param DataFixService $data_fix_service 45 */ 46 public function __construct(DataFixService $data_fix_service) 47 { 48 $this->data_fix_service = $data_fix_service; 49 } 50 51 /** 52 * How should this module be identified in the control panel, etc.? 53 * 54 * @return string 55 */ 56 public function title(): string 57 { 58 /* I18N: Name of a module */ 59 return I18N::translate('Convert %s tags to GEDCOM 5.5.1', 'INDI:_WT_OBJE_SORT'); 60 } 61 62 public function description(): string 63 { 64 /* I18N: Description of a “Data fix” module */ 65 return I18N::translate('_WT_OBJE_SORT tags were used by old versions of webtrees to indicate the preferred image for an individual. An alternative is to re-order the images so that the preferred one is listed first.'); 66 } 67 68 /** 69 * XREFs of media records that might need fixing. 70 * 71 * @param Tree $tree 72 * @param array<string,string> $params 73 * 74 * @return Collection<int,string> 75 */ 76 public function individualsToFix(Tree $tree, array $params): Collection 77 { 78 return $this->individualsToFixQuery($tree, $params) 79 ->where('i_file', '=', $tree->id()) 80 ->where(function (Builder $query): void { 81 $query 82 ->where('i_gedcom', 'LIKE', "%\n1 _WT_OBJE_SORT %") 83 ->orWhere('i_gedcom', 'LIKE', "%\n1 _WT_OBJE_SORT %"); 84 }) 85 ->pluck('i_id'); 86 } 87 88 /** 89 * Does a record need updating? 90 * 91 * @param GedcomRecord $record 92 * @param array<string,string> $params 93 * 94 * @return bool 95 */ 96 public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool 97 { 98 return str_contains($record->gedcom(), "\n1 _WT_OBJE_SORT ") || str_contains($record->gedcom(), "\n1 _PGV_OBJE_SORT "); 99 } 100 101 /** 102 * Show the changes we would make 103 * 104 * @param GedcomRecord $record 105 * @param array<string,string> $params 106 * 107 * @return string 108 */ 109 public function previewUpdate(GedcomRecord $record, array $params): string 110 { 111 $old = $record->gedcom(); 112 $new = $this->reorderMediaLinks($record); 113 114 return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); 115 } 116 117 /** 118 * Fix a record 119 * 120 * @param GedcomRecord $record 121 * @param array<string,string> $params 122 * 123 * @return void 124 */ 125 public function updateRecord(GedcomRecord $record, array $params): void 126 { 127 $record->updateRecord($this->reorderMediaLinks($record), false); 128 } 129 130 /** 131 * @param GedcomRecord $record 132 * 133 * @return string 134 */ 135 private function reorderMediaLinks(GedcomRecord $record): string 136 { 137 // Sort the level 1 media links in this order 138 $wt_obje_sort = $record->facts(['_PGV_OBJE_SORT', '_WT_OBJE_SORT'], false, null, true) 139 ->map(static fn (Fact $fact): string => $fact->value()); 140 141 $callback = static function (Fact $x, Fact $y) use ($wt_obje_sort): int { 142 $sort1 = $wt_obje_sort->search($x->value(), true); 143 $sort2 = $wt_obje_sort->search($y->value(), true); 144 $sort1 = $sort1 === false ? PHP_INT_MAX : $sort1; 145 $sort2 = $sort2 === false ? PHP_INT_MAX : $sort2; 146 147 return $sort1 <=> $sort2; 148 }; 149 150 $obje = $record 151 ->facts(['OBJE'], false, null, true) 152 ->sort($callback); 153 154 $gedcom = '0 @' . $record->xref() . "@ INDI\n" . $record->facts([], false, null, true) 155 ->filter(static fn (Fact $fact): bool => $fact->tag() !== 'INDI:OBJE' && $fact->tag() !== 'INDI:_WT_OBJE_SORT' && $fact->tag() !== '_PGV_OBJE_SORT:OBJE') 156 ->map(static fn (Fact $fact): string => $fact->gedcom()) 157 ->implode("\n"); 158 159 return $gedcom . $obje->map(static fn (Fact $fact): string => "\n" . $fact->gedcom())->implode(''); 160 } 161} 162