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