/user/plugins/admin/classes/popularity.php

https://gitlab.com/3dplex/3d-plex-main-site · PHP · 346 lines · 244 code · 56 blank · 46 comment · 32 complexity · 7b341fceae0f7dbacb3c8132a9652158 MD5 · raw file

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