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