xref: /webtrees/app/Http/RequestHandlers/DataFixUpdateAll.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify
7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by
8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ce42304aSGreg Roach * (at your option) any later version.
10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful,
11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ce42304aSGreg Roach * GNU General Public License for more details.
14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ce42304aSGreg Roach
22ce42304aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
24ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
26ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
27ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29ce42304aSGreg Roachuse Illuminate\Support\Collection;
30ce42304aSGreg Roachuse Psr\Http\Message\ResponseInterface;
31ce42304aSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32ce42304aSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33ce42304aSGreg Roach
34ce42304aSGreg Roachuse function assert;
35ce42304aSGreg Roachuse function json_encode;
36ce42304aSGreg Roachuse function response;
37ce42304aSGreg Roach
3808b5db2aSGreg Roachuse const JSON_THROW_ON_ERROR;
3908b5db2aSGreg Roach
40ce42304aSGreg Roach/**
41ce42304aSGreg Roach * Run a data-fix.
42ce42304aSGreg Roach */
43ce42304aSGreg Roachclass DataFixUpdateAll implements RequestHandlerInterface
44ce42304aSGreg Roach{
45ce42304aSGreg Roach    // Process this number of records in each HTTP request
467684867eSGreg Roach    private const CHUNK_SIZE = 250;
47ce42304aSGreg Roach
48c4943cffSGreg Roach    private DataFixService $data_fix_service;
49ce42304aSGreg Roach
50c4943cffSGreg Roach    private ModuleService $module_service;
51ce42304aSGreg Roach
52ce42304aSGreg Roach    /**
53ce42304aSGreg Roach     * @param DataFixService $data_fix_service
54ce42304aSGreg Roach     * @param ModuleService  $module_service
55ce42304aSGreg Roach     */
56ce42304aSGreg Roach    public function __construct(
57ce42304aSGreg Roach        DataFixService $data_fix_service,
58de1b1aedSGreg Roach        ModuleService $module_service
59ce42304aSGreg Roach    ) {
60ce42304aSGreg Roach        $this->data_fix_service = $data_fix_service;
61ce42304aSGreg Roach        $this->module_service   = $module_service;
62ce42304aSGreg Roach    }
63ce42304aSGreg Roach
64ce42304aSGreg Roach    /**
65ce42304aSGreg Roach     * @param ServerRequestInterface $request
66ce42304aSGreg Roach     *
67ce42304aSGreg Roach     * @return ResponseInterface
68ce42304aSGreg Roach     */
69ce42304aSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
70ce42304aSGreg Roach    {
71b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
72b55cbc6bSGreg Roach        $data_fix = Validator::attributes($request)->string('data_fix', '');
73ce42304aSGreg Roach        $module   = $this->module_service->findByName($data_fix);
74ce42304aSGreg Roach        assert($module instanceof ModuleDataFixInterface);
75ce42304aSGreg Roach
7637653836SGreg Roach        $params = $request->getQueryParams();
77ce42304aSGreg Roach        $rows   = $module->recordsToFix($tree, $params);
78ce42304aSGreg Roach
79ce42304aSGreg Roach        if ($rows->isEmpty()) {
80e4d7d4c5SGreg Roach            return response([]);
81ce42304aSGreg Roach        }
82ce42304aSGreg Roach
832791fd84SGreg Roach        $start = Validator::queryParams($request)->string('start', '');
842791fd84SGreg Roach        $end   = Validator::queryParams($request)->string('end', '');
85ce42304aSGreg Roach
86ce42304aSGreg Roach        if ($start === '' || $end === '') {
87ce42304aSGreg Roach            return $this->createUpdateRanges($tree, $module, $rows, $params);
88ce42304aSGreg Roach        }
89ce42304aSGreg Roach
9036779af1SGreg Roach        /** @var Collection<int,GedcomRecord> $records */
91f25fc0f9SGreg Roach        $records = $rows
92*1ff45046SGreg Roach            ->map(fn (object $row): GedcomRecord|null => $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type))
93*1ff45046SGreg Roach            ->filter(static fn (GedcomRecord|null $record): bool => $record instanceof GedcomRecord && !$record->isPendingDeletion() && $module->doesRecordNeedUpdate($record, $params));
94ce42304aSGreg Roach
95ce42304aSGreg Roach        foreach ($records as $record) {
96ce42304aSGreg Roach            $module->updateRecord($record, $params);
97ce42304aSGreg Roach        }
98ce42304aSGreg Roach
99e4d7d4c5SGreg Roach        return response();
100ce42304aSGreg Roach    }
101ce42304aSGreg Roach
102ce42304aSGreg Roach    /**
103ce42304aSGreg Roach     * @param Tree                   $tree
104ce42304aSGreg Roach     * @param ModuleDataFixInterface $module
10536779af1SGreg Roach     * @param Collection<int,object> $rows
106ce42304aSGreg Roach     * @param array<string>          $params
107ce42304aSGreg Roach     *
108ce42304aSGreg Roach     * @return ResponseInterface
109ce42304aSGreg Roach     */
110ce42304aSGreg Roach    private function createUpdateRanges(
111ce42304aSGreg Roach        Tree $tree,
112ce42304aSGreg Roach        ModuleDataFixInterface $module,
113ce42304aSGreg Roach        Collection $rows,
114ce42304aSGreg Roach        array $params
115ce42304aSGreg Roach    ): ResponseInterface {
116ce42304aSGreg Roach        $total = $rows->count();
117ce42304aSGreg Roach
118ce42304aSGreg Roach        $updates = $rows
119ce42304aSGreg Roach            ->chunk(self::CHUNK_SIZE)
120f70bcff5SGreg Roach            ->map(static function (Collection $chunk) use ($module, $params, $tree, $total): object {
121ce42304aSGreg Roach                static $count = 0;
122ce42304aSGreg Roach
123ce42304aSGreg Roach                $count += $chunk->count();
124ce42304aSGreg Roach
125ce42304aSGreg Roach                $start = $chunk->first()->xref;
126ce42304aSGreg Roach                $end   = $chunk->last()->xref;
127ce42304aSGreg Roach                $url   = route(self::class, [
128ce42304aSGreg Roach                        'tree'     => $tree->name(),
129ce42304aSGreg Roach                        'data_fix' => $module->name(),
130ce42304aSGreg Roach                        'start'    => $start,
131ce42304aSGreg Roach                        'end'      => $end,
132ce42304aSGreg Roach                    ] + $params);
133ce42304aSGreg Roach
134ce42304aSGreg Roach                return (object) [
135ce42304aSGreg Roach                    'url'      => $url,
136ce42304aSGreg Roach                    'percent'  => (100.0 * $count / $total) . '%',
137ce42304aSGreg Roach                    'progress' => I18N::percentage($count / $total, 1),
138ce42304aSGreg Roach                ];
139ce42304aSGreg Roach            })
140ce42304aSGreg Roach            ->all();
141ce42304aSGreg Roach
14208b5db2aSGreg Roach        return response(json_encode($updates, JSON_THROW_ON_ERROR));
143ce42304aSGreg Roach    }
144ce42304aSGreg Roach}
145