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