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