Files
Grav-Admin-Plugin/classes/popularity.php

263 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace Grav\Plugin;
2014-10-01 22:28:16 +03:00
use Grav\Common\Config\Config;
use Grav\Common\Grav;
use Grav\Common\Plugins;
use Grav\Common\Themes;
use Grav\Common\Page\Page;
use Grav\Common\Data;
use Grav\Common\GravTrait;
class Popularity
{
use GravTrait;
2014-10-01 22:28:16 +03:00
/** @var Config */
2014-09-07 19:58:04 -06:00
protected $config;
protected $data_path;
2014-09-07 19:58:04 -06:00
2014-09-08 12:07:35 -06:00
protected $daily_file;
2014-09-07 19:58:04 -06:00
protected $monthly_file;
protected $totals_file;
protected $visitors_file;
2014-09-08 12:07:35 -06:00
protected $daily_data;
2014-09-07 19:58:04 -06:00
protected $monthly_data;
protected $totals_data;
protected $visitors_data;
2014-09-08 12:07:35 -06:00
const DAILY_FORMAT = 'd-m-Y';
const MONTHLY_FORMAT = 'm-Y';
const DAILY_FILE = 'daily.json';
const MONTHLY_FILE = 'monthly.json';
const TOTALS_FILE = 'totals.json';
2014-09-07 19:58:04 -06:00
const VISITORS_FILE = 'visitors.json';
public function __construct()
{
2015-02-05 15:10:59 -07:00
$this->config = self::getGrav()['config'];
2014-09-07 19:58:04 -06:00
2015-02-27 16:24:33 -07:00
$this->data_path = self::$grav['locator']->findResource('log://popularity', true, true);
2014-09-08 12:07:35 -06:00
$this->daily_file = $this->data_path.'/'.self::DAILY_FILE;
2014-09-07 19:58:04 -06:00
$this->monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
$this->totals_file = $this->data_path.'/'.self::TOTALS_FILE;
$this->visitors_file = $this->data_path.'/'.self::VISITORS_FILE;
}
public function trackHit()
{
// Don't track bot or crawler requests
if (!self::getGrav()['browser']->isHuman()) {
return;
}
2014-10-01 22:28:16 +03:00
/** @var Page $page */
2015-02-05 15:10:59 -07:00
$page = self::getGrav()['page'];
$relative_url = str_replace(self::getGrav()['base_url_relative'], '', $page->url());
// Don't track error pages or pages that have no route
if ($page->template() == 'error' || !$page->route()) {
2014-09-06 18:13:04 -06:00
return;
}
// Make sure no 'widcard-style' ignore matches this url
2014-09-07 19:58:04 -06:00
foreach ((array) $this->config->get('plugins.admin.popularity.ignore') as $ignore) {
if (fnmatch($ignore, $relative_url)) {
return;
}
}
// initial creation if it doesn't exist
if (!file_exists($this->data_path)) {
mkdir($this->data_path);
2014-09-08 12:07:35 -06:00
$this->flushPopularity();
}
// Update the data we want to track
2014-09-08 12:07:35 -06:00
$this->updateDaily();
2014-09-07 19:58:04 -06:00
$this->updateMonthly();
$this->updateTotals($page->route());
2015-02-05 15:10:59 -07:00
$this->updateVisitors(self::getGrav()['uri']->ip());
}
2014-09-08 12:07:35 -06:00
protected function updateDaily()
{
if (!$this->daily_data) {
$this->daily_data = $this->getData($this->daily_file);
}
$day_month_year = date(self::DAILY_FORMAT);
// get the daily access count
if (array_key_exists($day_month_year, $this->daily_data)) {
$this->daily_data[$day_month_year] = intval($this->daily_data[$day_month_year]) + 1;
} else {
$this->daily_data[$day_month_year] = 1;
}
// keep correct number as set by history
2015-07-24 17:31:29 -06:00
$count = intval($this->config->get('plugins.admin.popularity.history.daily', 30));
2014-09-08 12:07:35 -06:00
$total = count($this->daily_data);
2015-07-24 17:31:29 -06:00
if ($total > $count) {
$this->daily_data = array_slice($this->daily_data, -$count, $count, true);
}
2014-09-08 12:07:35 -06:00
file_put_contents($this->daily_file, json_encode($this->daily_data));
}
public function getDailyChartData()
{
if (!$this->daily_data) {
$this->daily_data = $this->getData($this->daily_file);
}
2015-07-24 17:31:29 -06:00
$limit = intval($this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7));
$chart_data = array_slice($this->daily_data, -$limit, $limit);
$labels = array();
$data = array();
2015-07-24 17:31:29 -06:00
foreach ($chart_data as $date => $count) {
$labels[] = self::getGrav()['grav']['admin']->translate(['PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]);
$data[] = $count;
}
return array('labels' => json_encode($labels), 'data' => json_encode($data));
}
public function getDailyTotal()
{
if (!$this->daily_data) {
$this->daily_data = $this->getData($this->daily_file);
}
if (isset($this->daily_data[date(self::DAILY_FORMAT)])) {
return $this->daily_data[date(self::DAILY_FORMAT)];
} else {
return 0;
}
}
public function getWeeklyTotal()
{
if (!$this->daily_data) {
$this->daily_data = $this->getData($this->daily_file);
}
$day = 0;
$total = 0;
foreach (array_reverse($this->daily_data) as $daily) {
$total += $daily;
$day++;
if ($day == 7) break;
}
return $total;
}
public function getMonthlyTotal()
{
if (!$this->monthly_data) {
$this->monthly_data = $this->getData($this->monthly_file);
}
if (isset($this->monthly_data[date(self::MONTHLY_FORMAT)])) {
return $this->monthly_data[date(self::MONTHLY_FORMAT)];
} else {
return 0;
}
}
2014-09-07 19:58:04 -06:00
protected function updateMonthly()
{
2014-09-07 19:58:04 -06:00
if (!$this->monthly_data) {
$this->monthly_data = $this->getData($this->monthly_file);
}
2014-09-08 12:07:35 -06:00
$month_year = date(self::MONTHLY_FORMAT);
2014-09-07 19:58:04 -06:00
// get the monthly access count
if (array_key_exists($month_year, $this->monthly_data)) {
$this->monthly_data[$month_year] = intval($this->monthly_data[$month_year]) + 1;
} else {
$this->monthly_data[$month_year] = 1;
}
2014-09-08 12:07:35 -06:00
// keep correct number as set by history
$count = intval($this->config->get('plugins.admin.popularity.history.monthly', 12));
$total = count($this->monthly_data);
$this->monthly_data = array_slice($this->monthly_data, $total - $count, $count);
2014-09-07 19:58:04 -06:00
file_put_contents($this->monthly_file, json_encode($this->monthly_data));
}
protected function getMonthyChartData()
{
if (!$this->monthly_data) {
$this->monthly_data = $this->getData($this->monthly_file);
}
$labels = array();
$data = array();
foreach ($this->monthly_data as $date => $count) {
2015-07-24 17:31:29 -06:00
$labels[] = date('M', strtotime($date));
$data[] = $count;
}
return array('labels' => $labels, 'data' => $data);
}
2014-09-07 19:58:04 -06:00
protected function updateTotals($url)
{
2014-09-07 19:58:04 -06:00
if (!$this->totals_data) {
$this->totals_data = $this->getData($this->totals_file);
}
2014-09-07 19:58:04 -06:00
// get the totals for this url
if (array_key_exists($url, $this->totals_data)) {
$this->totals_data[$url] = intval($this->totals_data[$url]) + 1;
} else {
2014-09-07 19:58:04 -06:00
$this->totals_data[$url] = 1;
}
2014-09-07 19:58:04 -06:00
file_put_contents($this->totals_file, json_encode($this->totals_data));
}
2014-09-07 19:58:04 -06:00
protected function updateVisitors($ip)
{
2014-09-07 19:58:04 -06:00
if (!$this->visitors_data) {
$this->visitors_data = $this->getData($this->visitors_file);
}
2014-09-07 19:58:04 -06:00
// update with current timestamp
$this->visitors_data[$ip] = time();
$visitors = $this->visitors_data;
arsort($visitors);
2014-09-08 12:07:35 -06:00
$count = intval($this->config->get('plugins.admin.popularity.history.visitors', 20));
2014-12-12 16:08:07 -07:00
$this->visitors_data = array_slice($visitors, 0, $count, true);
2014-09-07 19:58:04 -06:00
file_put_contents($this->visitors_file, json_encode($this->visitors_data));
}
protected function getData($path)
{
return (array) @json_decode(file_get_contents($path), true);
}
2014-09-08 12:07:35 -06:00
public function flushPopularity()
2014-09-07 19:58:04 -06:00
{
2014-09-08 12:07:35 -06:00
file_put_contents($this->daily_file, array());
file_put_contents($this->monthly_file, array());
file_put_contents($this->totals_file, array());
file_put_contents($this->visitors_file, array());
}
}