popularity.php 8.1 KB

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