xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogData.php (revision b5961194694c0b5b2dc4269689207eb972e3b20c)
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        $this->datatables_service      = $datatables_service;
65        $this->myers_diff              = $myers_diff;
66        $this->pending_changes_service = $pending_changes_service;
67    }
68
69    /**
70     * @param ServerRequestInterface $request
71     *
72     * @return ResponseInterface
73     */
74    public function handle(ServerRequestInterface $request): ResponseInterface
75    {
76        $tree = $request->getAttribute('tree');
77        assert($tree instanceof Tree);
78
79        $params         = $request->getQueryParams();
80        $params['tree'] = $tree->name();
81
82        $query = $this->pending_changes_service->changesQuery($params);
83
84        $callback = function (stdClass $row) use ($tree): array {
85            $old_lines = explode("\n", $row->old_gedcom);
86            $new_lines = explode("\n", $row->new_gedcom);
87
88            $differences = $this->myers_diff->calculate($old_lines, $new_lines);
89            $diff_lines  = [];
90
91            foreach ($differences as $difference) {
92                switch ($difference[1]) {
93                    case MyersDiff::DELETE:
94                        $diff_lines[] = '<del>' . e($difference[0]) . '</del>';
95                        break;
96                    case MyersDiff::INSERT:
97                        $diff_lines[] = '<ins>' . e($difference[0]) . '</ins>';
98                        break;
99                    default:
100                        $diff_lines[] = e($difference[0]);
101                }
102            }
103
104            // Only convert valid xrefs to links
105            $record = GedcomRecord::getInstance($row->xref, $tree);
106
107            return [
108                $row->change_id,
109                Carbon::make($row->change_time)->local()->format('Y-m-d H:i:s'),
110                I18N::translate($row->status),
111                $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref,
112                '<div class="gedcom-data" dir="ltr">' .
113                preg_replace_callback(
114                    '/@(' . Gedcom::REGEX_XREF . ')@/',
115                    static function (array $match) use ($tree): string {
116                        $record = GedcomRecord::getInstance($match[1], $tree);
117
118                        return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0];
119                    },
120                    implode("\n", $diff_lines)
121                ) .
122                '</div>',
123                $row->user_name,
124                $row->gedcom_name,
125            ];
126        };
127
128        return $this->datatables_service->handleQuery($request, $query, [], [], $callback);
129    }
130}
131