popularity.php 8.2 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] = intval($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 = intval($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 = intval($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. } else {
  119. return 0;
  120. }
  121. }
  122. /**
  123. * @return int
  124. */
  125. public function getWeeklyTotal()
  126. {
  127. if (!$this->daily_data) {
  128. $this->daily_data = $this->getData($this->daily_file);
  129. }
  130. $day = 0;
  131. $total = 0;
  132. foreach (array_reverse($this->daily_data) as $daily) {
  133. $total += $daily;
  134. $day++;
  135. if ($day == 7) {
  136. break;
  137. }
  138. }
  139. return $total;
  140. }
  141. /**
  142. * @return int
  143. */
  144. public function getMonthlyTotal()
  145. {
  146. if (!$this->monthly_data) {
  147. $this->monthly_data = $this->getData($this->monthly_file);
  148. }
  149. if (isset($this->monthly_data[date(self::MONTHLY_FORMAT)])) {
  150. return $this->monthly_data[date(self::MONTHLY_FORMAT)];
  151. } else {
  152. return 0;
  153. }
  154. }
  155. protected function updateMonthly()
  156. {
  157. if (!$this->monthly_data) {
  158. $this->monthly_data = $this->getData($this->monthly_file);
  159. }
  160. $month_year = date(self::MONTHLY_FORMAT);
  161. // get the monthly access count
  162. if (array_key_exists($month_year, $this->monthly_data)) {
  163. $this->monthly_data[$month_year] = intval($this->monthly_data[$month_year]) + 1;
  164. } else {
  165. $this->monthly_data[$month_year] = 1;
  166. }
  167. // keep correct number as set by history
  168. $count = intval($this->config->get('plugins.admin.popularity.history.monthly', 12));
  169. $total = count($this->monthly_data);
  170. $this->monthly_data = array_slice($this->monthly_data, $total - $count, $count);
  171. file_put_contents($this->monthly_file, json_encode($this->monthly_data));
  172. }
  173. /**
  174. * @return array
  175. */
  176. protected function getMonthyChartData()
  177. {
  178. if (!$this->monthly_data) {
  179. $this->monthly_data = $this->getData($this->monthly_file);
  180. }
  181. $labels = [];
  182. $data = [];
  183. foreach ($this->monthly_data as $date => $count) {
  184. $labels[] = date('M', strtotime($date));
  185. $data[] = $count;
  186. }
  187. return ['labels' => $labels, 'data' => $data];
  188. }
  189. /**
  190. * @param string $url
  191. */
  192. protected function updateTotals($url)
  193. {
  194. if (!$this->totals_data) {
  195. $this->totals_data = $this->getData($this->totals_file);
  196. }
  197. // get the totals for this url
  198. if (array_key_exists($url, $this->totals_data)) {
  199. $this->totals_data[$url] = intval($this->totals_data[$url]) + 1;
  200. } else {
  201. $this->totals_data[$url] = 1;
  202. }
  203. file_put_contents($this->totals_file, json_encode($this->totals_data));
  204. }
  205. /**
  206. * @param string $ip
  207. */
  208. protected function updateVisitors($ip)
  209. {
  210. if (!$this->visitors_data) {
  211. $this->visitors_data = $this->getData($this->visitors_file);
  212. }
  213. // update with current timestamp
  214. $this->visitors_data[$ip] = time();
  215. $visitors = $this->visitors_data;
  216. arsort($visitors);
  217. $count = intval($this->config->get('plugins.admin.popularity.history.visitors', 20));
  218. $this->visitors_data = array_slice($visitors, 0, $count, true);
  219. file_put_contents($this->visitors_file, json_encode($this->visitors_data));
  220. }
  221. /**
  222. * @param string $path
  223. *
  224. * @return array
  225. */
  226. protected function getData($path)
  227. {
  228. if (file_exists($path)) {
  229. return (array)json_decode(file_get_contents($path), true);
  230. } else {
  231. return [];
  232. }
  233. }
  234. public function flushPopularity()
  235. {
  236. file_put_contents($this->daily_file, []);
  237. file_put_contents($this->monthly_file, []);
  238. file_put_contents($this->totals_file, []);
  239. file_put_contents($this->visitors_file, []);
  240. }
  241. }