Popularity.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Grav\Plugin\Admin;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Page\Interfaces\PageInterface;
  6. /**
  7. * Class Popularity
  8. * @package Grav\Plugin
  9. */
  10. class Popularity
  11. {
  12. /** @var Config */
  13. protected $config;
  14. protected $data_path;
  15. protected $daily_file;
  16. protected $monthly_file;
  17. protected $totals_file;
  18. protected $visitors_file;
  19. protected $daily_data;
  20. protected $monthly_data;
  21. protected $totals_data;
  22. protected $visitors_data;
  23. const DAILY_FORMAT = 'd-m-Y';
  24. const MONTHLY_FORMAT = 'm-Y';
  25. const DAILY_FILE = 'daily.json';
  26. const MONTHLY_FILE = 'monthly.json';
  27. const TOTALS_FILE = 'totals.json';
  28. const VISITORS_FILE = 'visitors.json';
  29. public function __construct()
  30. {
  31. $this->config = Grav::instance()['config'];
  32. $this->data_path = Grav::instance()['locator']->findResource('log://popularity', true, true);
  33. $this->daily_file = $this->data_path . '/' . self::DAILY_FILE;
  34. $this->monthly_file = $this->data_path . '/' . self::MONTHLY_FILE;
  35. $this->totals_file = $this->data_path . '/' . self::TOTALS_FILE;
  36. $this->visitors_file = $this->data_path . '/' . self::VISITORS_FILE;
  37. }
  38. public function trackHit()
  39. {
  40. // Don't track bot or crawler requests
  41. if (!Grav::instance()['browser']->isHuman()) {
  42. return;
  43. }
  44. // Respect visitors "do not track" setting
  45. if (!Grav::instance()['browser']->isTrackable()) {
  46. return;
  47. }
  48. /** @var PageInterface $page */
  49. $page = Grav::instance()['page'];
  50. $relative_url = str_replace(Grav::instance()['base_url_relative'], '', $page->url());
  51. // Don't track error pages or pages that have no route
  52. if ($page->template() === 'error' || !$page->route()) {
  53. return;
  54. }
  55. // Make sure no 'widcard-style' ignore matches this url
  56. foreach ((array)$this->config->get('plugins.admin.popularity.ignore') as $ignore) {
  57. if (fnmatch($ignore, $relative_url)) {
  58. return;
  59. }
  60. }
  61. // initial creation if it doesn't exist
  62. if (!file_exists($this->data_path)) {
  63. mkdir($this->data_path);
  64. $this->flushPopularity();
  65. }
  66. // Update the data we want to track
  67. $this->updateDaily();
  68. $this->updateMonthly();
  69. $this->updateTotals($page->route());
  70. $this->updateVisitors(Grav::instance()['uri']->ip());
  71. }
  72. protected function updateDaily()
  73. {
  74. if (!$this->daily_data) {
  75. $this->daily_data = $this->getData($this->daily_file);
  76. }
  77. $day_month_year = date(self::DAILY_FORMAT);
  78. // get the daily access count
  79. if (array_key_exists($day_month_year, $this->daily_data)) {
  80. $this->daily_data[$day_month_year] = (int)$this->daily_data[$day_month_year] + 1;
  81. } else {
  82. $this->daily_data[$day_month_year] = 1;
  83. }
  84. // keep correct number as set by history
  85. $count = (int)$this->config->get('plugins.admin.popularity.history.daily', 30);
  86. $total = count($this->daily_data);
  87. if ($total > $count) {
  88. $this->daily_data = array_slice($this->daily_data, -$count, $count, true);
  89. }
  90. file_put_contents($this->daily_file, json_encode($this->daily_data));
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function getDailyChartData()
  96. {
  97. if (!$this->daily_data) {
  98. $this->daily_data = $this->getData($this->daily_file);
  99. }
  100. $limit = (int)$this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7);
  101. $chart_data = array_slice($this->daily_data, -$limit, $limit);
  102. $labels = [];
  103. $data = [];
  104. /** @var Admin $admin */
  105. $admin = Grav::instance()['admin'];
  106. foreach ($chart_data as $date => $count) {
  107. $labels[] = $admin::translate([
  108. 'PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]) .
  109. '<br>' . date('M d', strtotime($date));
  110. $data[] = $count;
  111. }
  112. return ['labels' => $labels, 'data' => $data];
  113. }
  114. /**
  115. * @return int
  116. */
  117. public function getDailyTotal()
  118. {
  119. if (!$this->daily_data) {
  120. $this->daily_data = $this->getData($this->daily_file);
  121. }
  122. if (isset($this->daily_data[date(self::DAILY_FORMAT)])) {
  123. return $this->daily_data[date(self::DAILY_FORMAT)];
  124. }
  125. return 0;
  126. }
  127. /**
  128. * @return int
  129. */
  130. public function getWeeklyTotal()
  131. {
  132. if (!$this->daily_data) {
  133. $this->daily_data = $this->getData($this->daily_file);
  134. }
  135. $day = 0;
  136. $total = 0;
  137. foreach (array_reverse($this->daily_data) as $daily) {
  138. $total += $daily;
  139. $day++;
  140. if ($day === 7) {
  141. break;
  142. }
  143. }
  144. return $total;
  145. }
  146. /**
  147. * @return int
  148. */
  149. public function getMonthlyTotal()
  150. {
  151. if (!$this->monthly_data) {
  152. $this->monthly_data = $this->getData($this->monthly_file);
  153. }
  154. if (isset($this->monthly_data[date(self::MONTHLY_FORMAT)])) {
  155. return $this->monthly_data[date(self::MONTHLY_FORMAT)];
  156. }
  157. return 0;
  158. }
  159. protected function updateMonthly()
  160. {
  161. if (!$this->monthly_data) {
  162. $this->monthly_data = $this->getData($this->monthly_file);
  163. }
  164. $month_year = date(self::MONTHLY_FORMAT);
  165. // get the monthly access count
  166. if (array_key_exists($month_year, $this->monthly_data)) {
  167. $this->monthly_data[$month_year] = (int)$this->monthly_data[$month_year] + 1;
  168. } else {
  169. $this->monthly_data[$month_year] = 1;
  170. }
  171. // keep correct number as set by history
  172. $count = (int)$this->config->get('plugins.admin.popularity.history.monthly', 12);
  173. $total = count($this->monthly_data);
  174. $this->monthly_data = array_slice($this->monthly_data, $total - $count, $count);
  175. file_put_contents($this->monthly_file, json_encode($this->monthly_data));
  176. }
  177. /**
  178. * @return array
  179. */
  180. protected function getMonthyChartData()
  181. {
  182. if (!$this->monthly_data) {
  183. $this->monthly_data = $this->getData($this->monthly_file);
  184. }
  185. $labels = [];
  186. $data = [];
  187. foreach ($this->monthly_data as $date => $count) {
  188. $labels[] = date('M', strtotime($date));
  189. $data[] = $count;
  190. }
  191. return ['labels' => $labels, 'data' => $data];
  192. }
  193. /**
  194. * @param string $url
  195. */
  196. protected function updateTotals($url)
  197. {
  198. if (!$this->totals_data) {
  199. $this->totals_data = $this->getData($this->totals_file);
  200. }
  201. // get the totals for this url
  202. if (array_key_exists($url, $this->totals_data)) {
  203. $this->totals_data[$url] = (int)$this->totals_data[$url] + 1;
  204. } else {
  205. $this->totals_data[$url] = 1;
  206. }
  207. file_put_contents($this->totals_file, json_encode($this->totals_data));
  208. }
  209. /**
  210. * @param string $ip
  211. */
  212. protected function updateVisitors($ip)
  213. {
  214. if (!$this->visitors_data) {
  215. $this->visitors_data = $this->getData($this->visitors_file);
  216. }
  217. // update with current timestamp
  218. $this->visitors_data[hash('sha1', $ip)] = time();
  219. $visitors = $this->visitors_data;
  220. arsort($visitors);
  221. $count = (int)$this->config->get('plugins.admin.popularity.history.visitors', 20);
  222. $this->visitors_data = array_slice($visitors, 0, $count, true);
  223. file_put_contents($this->visitors_file, json_encode($this->visitors_data));
  224. }
  225. /**
  226. * @param string $path
  227. *
  228. * @return array
  229. */
  230. protected function getData($path)
  231. {
  232. if (file_exists($path)) {
  233. return (array)json_decode(file_get_contents($path), true);
  234. }
  235. return [];
  236. }
  237. public function flushPopularity()
  238. {
  239. file_put_contents($this->daily_file, []);
  240. file_put_contents($this->monthly_file, []);
  241. file_put_contents($this->totals_file, []);
  242. file_put_contents($this->visitors_file, []);
  243. }
  244. }