2014-09-06 16:59:58 -06:00
|
|
|
<?php
|
|
|
|
|
namespace Grav\Plugin;
|
|
|
|
|
|
|
|
|
|
use Grav\Common\User\User;
|
|
|
|
|
use Grav\Common\User\Authentication;
|
|
|
|
|
use Grav\Common\Filesystem\File;
|
|
|
|
|
use Grav\Common\Grav;
|
|
|
|
|
use Grav\Common\Plugins;
|
|
|
|
|
use Grav\Common\Session;
|
|
|
|
|
use Grav\Common\Themes;
|
|
|
|
|
use Grav\Common\Uri;
|
|
|
|
|
use Grav\Common\Page\Pages;
|
|
|
|
|
use Grav\Common\Page\Page;
|
|
|
|
|
use Grav\Common\Data;
|
|
|
|
|
use Grav\Common\GravTrait;
|
|
|
|
|
|
|
|
|
|
class Popularity
|
|
|
|
|
{
|
|
|
|
|
use GravTrait;
|
|
|
|
|
|
|
|
|
|
protected $data_path;
|
|
|
|
|
protected $data_file;
|
|
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
const MONTHLY_FILE = 'monthly.json';
|
|
|
|
|
const TOTALS_FILE = 'totals.json';
|
|
|
|
|
|
2014-09-06 16:59:58 -06:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->data_path = LOG_DIR . 'popularity';
|
|
|
|
|
$this->data_file = date('W-Y') . '.json';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function trackHit()
|
|
|
|
|
{
|
2014-09-06 18:13:04 -06:00
|
|
|
$page = self::$grav['page'];
|
2014-09-07 09:02:39 -06:00
|
|
|
$config = self::$grav['config'];
|
|
|
|
|
$relative_url = str_replace($config->get('system.base_url_relative'), '', $page->url());
|
2014-09-06 16:59:58 -06:00
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
// 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;
|
|
|
|
|
}
|
2014-09-06 16:59:58 -06:00
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
// Make sure no 'widcard-style' ignore matches this url
|
|
|
|
|
foreach ((array) self::$grav['config']->get('plugins.admin.popularity.ignore') as $ignore) {
|
|
|
|
|
if (fnmatch($ignore, $relative_url)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Used more than once, so make a variable!
|
|
|
|
|
$monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
|
|
|
|
|
$totals_file = $this->data_path.'/'.self::TOTALS_FILE;
|
2014-09-06 16:59:58 -06:00
|
|
|
|
|
|
|
|
// initial creation if it doesn't exist
|
|
|
|
|
if (!file_exists($this->data_path)) {
|
|
|
|
|
mkdir($this->data_path);
|
2014-09-07 09:02:39 -06:00
|
|
|
file_put_contents($monthly_file, array());
|
|
|
|
|
file_put_contents($totals_file, array());
|
2014-09-06 16:59:58 -06:00
|
|
|
}
|
|
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
// Update the data we want to track
|
|
|
|
|
$this->updateMonthly($monthly_file);
|
|
|
|
|
$this->updateTotals($totals_file, $relative_url);
|
2014-09-06 16:59:58 -06:00
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function flushData($weeks = 52)
|
|
|
|
|
{
|
|
|
|
|
// flush data older than 1 year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function updateMonthly($path)
|
|
|
|
|
{
|
|
|
|
|
$data = (array) @json_decode(file_get_contents($path), true);
|
|
|
|
|
$month_year = date('m-Y');
|
|
|
|
|
|
|
|
|
|
if (array_key_exists($month_year, $data)) {
|
|
|
|
|
$data[$month_year] = intval($data[$month_year]) + 1;
|
2014-09-06 16:59:58 -06:00
|
|
|
} else {
|
2014-09-07 09:02:39 -06:00
|
|
|
$data[$month_year] = 1;
|
2014-09-06 16:59:58 -06:00
|
|
|
}
|
|
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
file_put_contents($path, json_encode($data));
|
2014-09-06 16:59:58 -06:00
|
|
|
}
|
|
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
protected function updateTotals($path, $url)
|
2014-09-06 16:59:58 -06:00
|
|
|
{
|
2014-09-07 09:02:39 -06:00
|
|
|
$data = (array) @json_decode(file_get_contents($path), true);
|
2014-09-06 16:59:58 -06:00
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
if (array_key_exists($url, $data)) {
|
|
|
|
|
$data[$url] = intval($data[$url]) + 1;
|
|
|
|
|
} else {
|
|
|
|
|
$data[$url] = 1;
|
|
|
|
|
}
|
2014-09-06 16:59:58 -06:00
|
|
|
|
2014-09-07 09:02:39 -06:00
|
|
|
file_put_contents($path, json_encode($data));
|
2014-09-06 16:59:58 -06:00
|
|
|
}
|
|
|
|
|
}
|