1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Module; 21 22use Aura\Router\Route; 23use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage; 24use Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 25use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 26use Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 27use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage; 28use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage; 29use Fisharebest\Webtrees\Http\RequestHandlers\SubmitterPage; 30use Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 31use Fisharebest\Webtrees\Http\RequestHandlers\UserPage; 32use Fisharebest\Webtrees\I18N; 33use Fisharebest\Webtrees\Session; 34use Fisharebest\Webtrees\Tree; 35use Fisharebest\Webtrees\Validator; 36use Illuminate\Database\Capsule\Manager as DB; 37use Psr\Http\Message\ResponseInterface; 38use Psr\Http\Message\ServerRequestInterface; 39use Psr\Http\Server\MiddlewareInterface; 40use Psr\Http\Server\RequestHandlerInterface; 41 42use function assert; 43 44/** 45 * Class HitCountFooterModule - show the number of page hits in the footer. 46 */ 47class HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface 48{ 49 use ModuleFooterTrait; 50 51 // Which routes do we count? 52 // For historical reasons, we record the names of the original webtrees script and parameter. 53 protected const PAGE_NAMES = [ 54 FamilyPage::class => 'family.php', 55 IndividualPage::class => 'individual.php', 56 MediaPage::class => 'mediaviewer.php', 57 NotePage::class => 'note.php', 58 RepositoryPage::class => 'repo.php', 59 SourcePage::class => 'source.php', 60 SubmitterPage::class => 'submitter', 61 TreePage::class => 'index.php', 62 UserPage::class => 'index.php', 63 ]; 64 65 // Count of visits to the current page 66 protected int $page_hits = 0; 67 68 /** 69 * How should this module be labelled on tabs, footers, etc.? 70 * 71 * @return string 72 */ 73 public function title(): string 74 { 75 /* I18N: Name of a module */ 76 return I18N::translate('Hit counters'); 77 } 78 79 /** 80 * A sentence describing what this module does. 81 * 82 * @return string 83 */ 84 public function description(): string 85 { 86 /* I18N: Description of the “Hit counters” module */ 87 return I18N::translate('Count the visits to each page'); 88 } 89 90 /** 91 * The default position for this footer. It can be changed in the control panel. 92 * 93 * @return int 94 */ 95 public function defaultFooterOrder(): int 96 { 97 return 3; 98 } 99 100 /** 101 * A footer, to be added at the bottom of every page. 102 * 103 * @param ServerRequestInterface $request 104 * 105 * @return string 106 */ 107 public function getFooter(ServerRequestInterface $request): string 108 { 109 if ($this->page_hits === 0) { 110 return ''; 111 } 112 113 $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>'; 114 115 return view('modules/hit-counter/footer', [ 116 'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits), 117 ]); 118 } 119 120 /** 121 * @param ServerRequestInterface $request 122 * @param RequestHandlerInterface $handler 123 * 124 * @return ResponseInterface 125 */ 126 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 127 { 128 $route = Validator::attributes($request)->route(); 129 $tree = Validator::attributes($request)->treeOptional(); 130 $user = Validator::attributes($request)->user(); 131 132 if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) { 133 $page_name = self::PAGE_NAMES[$route->name] ?? ''; 134 135 switch ($route->name) { 136 case FamilyPage::class: 137 case IndividualPage::class: 138 case MediaPage::class: 139 case NotePage::class: 140 case RepositoryPage::class: 141 case SourcePage::class: 142 case SubmitterPage::class: 143 $xref = Validator::attributes($request)->isXref()->string('xref'); 144 $this->page_hits = $this->countHit($tree, $page_name, $xref); 145 break; 146 147 case TreePage::class: 148 $this->page_hits = $this->countHit($tree, $page_name, 'gedcom:' . $tree->id()); 149 break; 150 151 case UserPage::class: 152 $this->page_hits = $this->countHit($tree, $page_name, 'user:' . $user->id()); 153 break; 154 } 155 } 156 157 return $handler->handle($request); 158 } 159 160 /** 161 * Increment the page count. 162 * 163 * @param Tree $tree 164 * @param string $page 165 * @param string $parameter 166 * 167 * @return int 168 */ 169 protected function countHit(Tree $tree, string $page, string $parameter): int 170 { 171 // Don't increment the counter while we stay on the same page. 172 if ( 173 Session::get('last_page_name') === $page && 174 Session::get('last_page_parameter') === $parameter && 175 Session::get('last_gedcom_id') === $tree->id() 176 ) { 177 return (int) Session::get('last_count'); 178 } 179 180 $count = (int) DB::table('hit_counter') 181 ->where('gedcom_id', '=', $tree->id()) 182 ->where('page_name', '=', $page) 183 ->where('page_parameter', '=', $parameter) 184 ->value('page_count'); 185 186 $count++; 187 188 DB::table('hit_counter')->updateOrInsert([ 189 'gedcom_id' => $tree->id(), 190 'page_name' => $page, 191 'page_parameter' => $parameter, 192 ], [ 193 'page_count' => $count, 194 ]); 195 196 Session::put('last_gedcom_id', $tree->id()); 197 Session::put('last_page_name', $page); 198 Session::put('last_page_parameter', $parameter); 199 Session::put('last_count', $count); 200 201 return $count; 202 } 203} 204