xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogData.php (revision c8d78f19d0bdf4c0ec4728253bd37250d2e6cec4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\Algorithm\MyersDiff;
23use Fisharebest\Webtrees\Carbon;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\DatatablesService;
28use Fisharebest\Webtrees\Services\PendingChangesService;
29use Fisharebest\Webtrees\Tree;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function e;
35use function explode;
36use function implode;
37use function preg_replace_callback;
38
39/**
40 * Find pending changes.
41 */
42class PendingChangesLogData implements RequestHandlerInterface
43{
44    private DatatablesService $datatables_service;
45
46    private MyersDiff $myers_diff;
47
48    private PendingChangesService $pending_changes_service;
49
50    /**
51     * @param DatatablesService     $datatables_service
52     * @param MyersDiff             $myers_diff
53     * @param PendingChangesService $pending_changes_service
54     */
55    public function __construct(
56        DatatablesService $datatables_service,
57        MyersDiff $myers_diff,
58        PendingChangesService $pending_changes_service
59    ) {
60        $this->datatables_service      = $datatables_service;
61        $this->myers_diff              = $myers_diff;
62        $this->pending_changes_service = $pending_changes_service;
63    }
64
65    /**
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree = $request->getAttribute('tree');
73        assert($tree instanceof Tree);
74
75        $params         = $request->getQueryParams();
76        $params['tree'] = $tree->name();
77
78        $query = $this->pending_changes_service->changesQuery($params);
79
80        $callback = function (object $row) use ($tree): array {
81            $old_lines = $row->old_gedcom === '' ? [] : explode("\n", $row->old_gedcom);
82            $new_lines = $row->new_gedcom === '' ? [] : explode("\n", $row->new_gedcom);
83
84            $differences = $this->myers_diff->calculate($old_lines, $new_lines);
85            $diff_lines  = [];
86
87            foreach ($differences as $difference) {
88                switch ($difference[1]) {
89                    case MyersDiff::DELETE:
90                        $diff_lines[] = '<del>' . e($difference[0]) . '</del>';
91                        break;
92                    case MyersDiff::INSERT:
93                        $diff_lines[] = '<ins>' . e($difference[0]) . '</ins>';
94                        break;
95                    default:
96                        $diff_lines[] = e($difference[0]);
97                }
98            }
99
100            // Only convert valid xrefs to links
101            $record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
102
103            return [
104                $row->change_id,
105                Carbon::make($row->change_time)->local()->format('Y-m-d H:i:s'),
106                I18N::translate($row->status),
107                $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref,
108                '<div class="gedcom-data" dir="ltr">' .
109                preg_replace_callback(
110                    '/@(' . Gedcom::REGEX_XREF . ')@/',
111                    static function (array $match) use ($tree): string {
112                        $record = Registry::gedcomRecordFactory()->make($match[1], $tree);
113
114                        return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0];
115                    },
116                    implode("\n", $diff_lines)
117                ) .
118                '</div>',
119                $row->user_name,
120                $row->gedcom_name,
121            ];
122        };
123
124        return $this->datatables_service->handleQuery($request, $query, [], [], $callback);
125    }
126}
127