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