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