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 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 Illuminate\Database\Capsule\Manager as DB; 36use Psr\Http\Message\ResponseInterface; 37use Psr\Http\Message\ServerRequestInterface; 38use Psr\Http\Server\MiddlewareInterface; 39use Psr\Http\Server\RequestHandlerInterface; 40 41use function assert; 42 43/** 44 * Class HitCountFooterModule - show the number of page hits in the footer. 45 */ 46class HitCountFooterModule extends AbstractModule implements ModuleFooterInterface, MiddlewareInterface 47{ 48 use ModuleFooterTrait; 49 50 // Which routes do we count? 51 // For historical reasons, we record the names of the original webtrees script and parameter. 52 protected const PAGE_NAMES = [ 53 FamilyPage::class => 'family.php', 54 IndividualPage::class => 'individual.php', 55 MediaPage::class => 'mediaviewer.php', 56 NotePage::class => 'note.php', 57 RepositoryPage::class => 'repo.php', 58 SourcePage::class => 'source.php', 59 SubmitterPage::class => 'submitter', 60 TreePage::class => 'index.php', 61 UserPage::class => 'index.php', 62 ]; 63 64 /** @var int Count of visits to the current page */ 65 protected $page_hits = 0; 66 67 /** 68 * How should this module be labelled on tabs, footers, etc.? 69 * 70 * @return string 71 */ 72 public function title(): string 73 { 74 /* I18N: Name of a module */ 75 return I18N::translate('Hit counters'); 76 } 77 78 /** 79 * A sentence describing what this module does. 80 * 81 * @return string 82 */ 83 public function description(): string 84 { 85 /* I18N: Description of the “Hit counters” module */ 86 return I18N::translate('Count the visits to each page'); 87 } 88 89 /** 90 * The default position for this footer. It can be changed in the control panel. 91 * 92 * @return int 93 */ 94 public function defaultFooterOrder(): int 95 { 96 return 3; 97 } 98 99 /** 100 * A footer, to be added at the bottom of every page. 101 * 102 * @param ServerRequestInterface $request 103 * 104 * @return string 105 */ 106 public function getFooter(ServerRequestInterface $request): string 107 { 108 if ($this->page_hits === 0) { 109 return ''; 110 } 111 112 $digits = '<span class="odometer">' . I18N::digits($this->page_hits) . '</span>'; 113 114 return view('modules/hit-counter/footer', [ 115 'hit_counter' => I18N::plural('This page has been viewed %s time.', 'This page has been viewed %s times.', $this->page_hits, $digits), 116 ]); 117 } 118 119 /** 120 * @param ServerRequestInterface $request 121 * @param RequestHandlerInterface $handler 122 * 123 * @return ResponseInterface 124 */ 125 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 126 { 127 $route = $request->getAttribute('route'); 128 assert($route instanceof Route); 129 130 $tree = $request->getAttribute('tree'); 131 $user = $request->getAttribute('user'); 132 133 if ($tree instanceof Tree && $tree->getPreference('SHOW_COUNTER')) { 134 $page_name = self::PAGE_NAMES[$route->name] ?? ''; 135 136 switch ($route->name) { 137 case FamilyPage::class: 138 case IndividualPage::class: 139 case MediaPage::class: 140 case NotePage::class: 141 case RepositoryPage::class: 142 case SourcePage::class: 143 case SubmitterPage::class: 144 $this->page_hits = $this->countHit($tree, $page_name, $request->getAttribute('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