fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Http\RequestHandlers; |
||
| 21 | |||
| 22 | use Fisharebest\Webtrees\GedcomRecord; |
||
| 23 | use Fisharebest\Webtrees\I18N; |
||
| 24 | use Fisharebest\Webtrees\Module\ModuleDataFixInterface; |
||
| 25 | use Fisharebest\Webtrees\Services\DataFixService; |
||
| 26 | use Fisharebest\Webtrees\Services\ModuleService; |
||
| 27 | use Fisharebest\Webtrees\Tree; |
||
| 28 | use Fisharebest\Webtrees\Validator; |
||
| 29 | use Illuminate\Support\Collection; |
||
| 30 | use Psr\Http\Message\ResponseInterface; |
||
| 31 | use Psr\Http\Message\ServerRequestInterface; |
||
| 32 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 33 | |||
| 34 | use function assert; |
||
| 35 | use function json_encode; |
||
| 36 | use function response; |
||
| 37 | |||
| 38 | use const JSON_THROW_ON_ERROR; |
||
| 39 | |||
| 40 | final class DataFixUpdateAll implements RequestHandlerInterface |
||
| 41 | { |
||
| 42 | // Process this number of records in each HTTP request |
||
| 43 | private const int CHUNK_SIZE = 250; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 44 | |||
| 45 | private DataFixService $data_fix_service; |
||
| 46 | |||
| 47 | private ModuleService $module_service; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param DataFixService $data_fix_service |
||
| 51 | * @param ModuleService $module_service |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 54 | DataFixService $data_fix_service, |
||
| 55 | ModuleService $module_service |
||
| 56 | ) { |
||
| 57 | $this->data_fix_service = $data_fix_service; |
||
| 58 | $this->module_service = $module_service; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 62 | { |
||
| 63 | $tree = Validator::attributes($request)->tree(); |
||
| 64 | $data_fix = Validator::attributes($request)->string('data_fix', ''); |
||
| 65 | $module = $this->module_service->findByName($data_fix); |
||
| 66 | assert($module instanceof ModuleDataFixInterface); |
||
| 67 | |||
| 68 | $params = $request->getQueryParams(); |
||
| 69 | $rows = $module->recordsToFix($tree, $params); |
||
| 70 | |||
| 71 | if ($rows->isEmpty()) { |
||
| 72 | return response([]); |
||
| 73 | } |
||
| 74 | |||
| 75 | $start = Validator::queryParams($request)->string('start', ''); |
||
| 76 | $end = Validator::queryParams($request)->string('end', ''); |
||
| 77 | |||
| 78 | if ($start === '' || $end === '') { |
||
| 79 | return $this->createUpdateRanges($tree, $module, $rows, $params); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** @var Collection<int,GedcomRecord> $records */ |
||
| 83 | $records = $rows |
||
| 84 | ->map(fn (object $row): GedcomRecord|null => $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type)) |
||
| 85 | ->filter(static fn (GedcomRecord|null $record): bool => $record instanceof GedcomRecord && !$record->isPendingDeletion() && $module->doesRecordNeedUpdate($record, $params)); |
||
| 86 | |||
| 87 | foreach ($records as $record) { |
||
| 88 | $module->updateRecord($record, $params); |
||
| 89 | } |
||
| 90 | |||
| 91 | return response(); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Collection<int,object{xref:string,type:string}> $rows |
||
| 96 | * @param array<string> $params |
||
| 97 | */ |
||
| 98 | private function createUpdateRanges( |
||
| 99 | Tree $tree, |
||
| 100 | ModuleDataFixInterface $module, |
||
| 101 | Collection $rows, |
||
| 102 | array $params |
||
| 103 | ): ResponseInterface { |
||
| 104 | $total = $rows->count(); |
||
| 105 | |||
| 106 | $updates = $rows |
||
| 107 | ->chunk(self::CHUNK_SIZE) |
||
| 108 | ->map(static function (Collection $chunk) use ($module, $params, $tree, $total): object { |
||
| 109 | static $count = 0; |
||
| 110 | |||
| 111 | $count += $chunk->count(); |
||
| 112 | |||
| 113 | $start = $chunk->first()->xref; |
||
| 114 | $end = $chunk->last()->xref; |
||
| 115 | $url = route(self::class, [ |
||
| 116 | 'tree' => $tree->name(), |
||
| 117 | 'data_fix' => $module->name(), |
||
| 118 | 'start' => $start, |
||
| 119 | 'end' => $end, |
||
| 120 | ] + $params); |
||
| 121 | |||
| 122 | return (object) [ |
||
| 123 | 'url' => $url, |
||
| 124 | 'percent' => (100.0 * $count / $total) . '%', |
||
| 125 | 'progress' => I18N::percentage($count / $total, 1), |
||
| 126 | ]; |
||
| 127 | }) |
||
| 128 | ->all(); |
||
| 129 | |||
| 130 | return response(json_encode($updates, JSON_THROW_ON_ERROR)); |
||
| 131 | } |
||
| 132 | } |
||
| 133 |