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