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->map(function (object $row) use ($tree): ?GedcomRecord { 92 return $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type); 93 })->filter(static function (?GedcomRecord $record) use ($module, $params): bool { 94 return $record instanceof GedcomRecord && !$record->isPendingDeletion() && $module->doesRecordNeedUpdate($record, $params); 95 }); 96 97 foreach ($records as $record) { 98 $module->updateRecord($record, $params); 99 } 100 101 return response(); 102 } 103 104 /** 105 * @param Tree $tree 106 * @param ModuleDataFixInterface $module 107 * @param Collection<int,object> $rows 108 * @param array<string> $params 109 * 110 * @return ResponseInterface 111 */ 112 private function createUpdateRanges( 113 Tree $tree, 114 ModuleDataFixInterface $module, 115 Collection $rows, 116 array $params 117 ): ResponseInterface { 118 $total = $rows->count(); 119 120 $updates = $rows 121 ->chunk(self::CHUNK_SIZE) 122 ->map(static function (Collection $chunk) use ($module, $params, $tree, $total): object { 123 static $count = 0; 124 125 $count += $chunk->count(); 126 127 $start = $chunk->first()->xref; 128 $end = $chunk->last()->xref; 129 $url = route(self::class, [ 130 'tree' => $tree->name(), 131 'data_fix' => $module->name(), 132 'start' => $start, 133 'end' => $end, 134 ] + $params); 135 136 return (object) [ 137 'url' => $url, 138 'percent' => (100.0 * $count / $total) . '%', 139 'progress' => I18N::percentage($count / $total, 1), 140 ]; 141 }) 142 ->all(); 143 144 return response(json_encode($updates, JSON_THROW_ON_ERROR)); 145 } 146} 147