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