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