xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogData.php (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * 'Copyright (C) 2023 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\Validator;
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           = Validator::attributes($request)->tree();
72        $params         = $request->getQueryParams();
73        $params['tree'] = $tree->name();
74
75        $query = $this->pending_changes_service->changesQuery($params);
76
77        $callback = function (object $row) use ($tree): array {
78            $old_lines = $row->old_gedcom === '' ? [] : explode("\n", $row->old_gedcom);
79            $new_lines = $row->new_gedcom === '' ? [] : explode("\n", $row->new_gedcom);
80
81            $differences = $this->myers_diff->calculate($old_lines, $new_lines);
82            $diff_lines  = [];
83
84            foreach ($differences as $difference) {
85                switch ($difference[1]) {
86                    case MyersDiff::DELETE:
87                        $diff_lines[] = '<del>' . e($difference[0]) . '</del>';
88                        break;
89                    case MyersDiff::INSERT:
90                        $diff_lines[] = '<ins>' . e($difference[0]) . '</ins>';
91                        break;
92                    default:
93                        $diff_lines[] = e($difference[0]);
94                }
95            }
96
97            // Only convert valid xrefs to links
98            $record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
99
100            return [
101                $row->change_id,
102                Registry::timestampFactory()->fromString($row->change_time)->toDateTimeString(),
103                I18N::translate($row->status),
104                $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref,
105                '<div class="gedcom-data" dir="ltr">' .
106                preg_replace_callback(
107                    '/@(' . Gedcom::REGEX_XREF . ')@/',
108                    static function (array $match) use ($tree): string {
109                        $record = Registry::gedcomRecordFactory()->make($match[1], $tree);
110
111                        return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0];
112                    },
113                    implode("\n", $diff_lines)
114                ) .
115                '</div>',
116                $row->user_name,
117                $row->gedcom_name,
118            ];
119        };
120
121        return $this->datatables_service->handleQuery($request, $query, [], [], $callback);
122    }
123}
124