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