187 lines
3.1 KiB
C++
Executable File
187 lines
3.1 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2010, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
|
|
#include "core/log.h"
|
|
#include "core/request.h"
|
|
#include "core/config.h"
|
|
#include "core/db.h"
|
|
|
|
#include "bot.h"
|
|
#include "stats.h"
|
|
#include "templates.h"
|
|
#include "statssession.h"
|
|
|
|
|
|
extern "C" void Init(PluginInfo &);
|
|
const char plugin_name[] = "stats";
|
|
|
|
|
|
namespace Stats
|
|
{
|
|
|
|
Stats stats;
|
|
Bot bot;
|
|
long current_item_id;
|
|
|
|
|
|
|
|
|
|
|
|
void CalcItemId(Request * request)
|
|
{
|
|
if( request->is_item )
|
|
current_item_id = request->item.id;
|
|
else
|
|
if( !request->dir_table.empty() )
|
|
current_item_id = request->dir_table.back()->id;
|
|
else
|
|
current_item_id = -1;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UpdateStats(PluginInfo & info, Stats::ItemStats & item_stats)
|
|
{
|
|
bot.SetBrowserName(info.request->env_http_user_agent);
|
|
|
|
stats.global_all += 1;
|
|
item_stats.all += 1;
|
|
|
|
if( bot.IsGoogle() )
|
|
{
|
|
stats.global_google += 1;
|
|
item_stats.google += 1;
|
|
}
|
|
|
|
if( bot.IsYahoo() )
|
|
{
|
|
stats.global_yahoo += 1;
|
|
item_stats.yahoo += 1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ContentMake(PluginInfo & info)
|
|
{
|
|
StatsSession * stats_session = 0;
|
|
|
|
CalcItemId(info.request);
|
|
|
|
if( current_item_id == - 1 || !info.plugin_data_base )
|
|
return;
|
|
|
|
stats_session = reinterpret_cast<StatsSession*>(info.plugin_data_base);
|
|
|
|
// this simply prevents F5 (refresh) from a webbrowser
|
|
if( stats_session->last_visited != -1 &&
|
|
stats_session->last_visited == current_item_id )
|
|
return;
|
|
|
|
stats_session->last_visited = current_item_id;
|
|
|
|
UpdateStats(info, stats.stats_tab[current_item_id]);
|
|
|
|
if( !stats_session->calculated )
|
|
{
|
|
stats.global_unique += 1;
|
|
stats_session->calculated = true;
|
|
}
|
|
|
|
stats.PeriodicSave();
|
|
}
|
|
|
|
|
|
|
|
void SessionCreated(PluginInfo & info)
|
|
{
|
|
StatsSession * d = new StatsSession();
|
|
info.request->session->plugin_data.Assign(d);
|
|
|
|
log << log3 << "created stats plugin data"
|
|
<< ", plugin id: " << info.plugin_id
|
|
<< ", pointer: " << d << logend;
|
|
}
|
|
|
|
|
|
|
|
void RemoveSession(PluginInfo & info)
|
|
{
|
|
log << log1 << "deleting stats plugin data"
|
|
<< ", plugin id: " << info.plugin_id
|
|
<< ", pointer: " << info.plugin_data_base << logend;
|
|
|
|
delete info.plugin_data_base;
|
|
}
|
|
|
|
|
|
|
|
|
|
void Close(PluginInfo & info)
|
|
{
|
|
stats.SaveStats();
|
|
}
|
|
|
|
|
|
|
|
|
|
void RemoveFile(PluginInfo & info)
|
|
{
|
|
stats.RemoveItem(info.l1);
|
|
}
|
|
|
|
|
|
void RemoveDir(PluginInfo & info)
|
|
{
|
|
Db::ItemQuery query;
|
|
std::vector<long> items;
|
|
size_t i;
|
|
|
|
// !! may only files can be retrieved here?
|
|
query.SetAll(false, false);
|
|
query.WhereParentId(info.l1);
|
|
info.db->GetItems(items, query);
|
|
|
|
// removing childs
|
|
for(i=0 ; i<items.size() ; ++i)
|
|
stats.RemoveItem(items[i]);
|
|
|
|
// removing the directory
|
|
stats.RemoveItem(info.l1);
|
|
}
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void Init(PluginInfo & info)
|
|
{
|
|
using namespace Stats;
|
|
|
|
plugin.Assign(WINIX_TEMPLATES_CREATEFUNCTIONS, CreateFunctions);
|
|
plugin.Assign(WINIX_CONTENT_MAKE, ContentMake);
|
|
plugin.Assign(WINIX_SESSION_CREATED, SessionCreated);
|
|
plugin.Assign(WINIX_SESSION_REMOVE, RemoveSession);
|
|
plugin.Assign(WINIX_CLOSE, Close);
|
|
plugin.Assign(WINIX_FILE_REMOVED, RemoveFile);
|
|
plugin.Assign(WINIX_DIR_PREPARE_TO_REMOVE, RemoveDir);
|
|
|
|
stats.ReadConfig(info.config);
|
|
stats.ReadStats();
|
|
|
|
info.p1 = (void*)plugin_name;
|
|
}
|
|
|
|
|