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