changed: the way how plugins work

all your plugin functions can have signature either:
void my_function(PluginInfo & info); or
void my_function();
only the main Init should have:
extern "C" void Init(PluginFunction & info);

added: directory 'plugins' for plugins
added: 'stats' plugin
		  


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@624 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2010-07-27 20:41:56 +00:00
parent e4683b9a05
commit 031ace3fe5
33 changed files with 1268 additions and 399 deletions

27
plugins/stats/Makefile Executable file
View File

@@ -0,0 +1,27 @@
include Makefile.o.dep
name = stats.so
all: $(o)
$(CXX) -shared -Wl,-soname,$(name).so -o $(name) $(CXXFLAGS) *.o
.SUFFIXES: .cpp .o
.cpp.o:
$(CXX) -c $(CXXFLAGS) $<
depend:
makedepend -Y. -I../.. -I../../../ezc/src -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
clean:
rm -f *.o
rm -f $(name)
include Makefile.dep

18
plugins/stats/Makefile.dep Executable file
View File

@@ -0,0 +1,18 @@
# DO NOT DELETE
init.o: ../../core/log.h ../../core/request.h ../../core/requesttypes.h
init.o: ../../core/session.h ../../core/item.h ../../core/error.h
init.o: ../../core/log.h ../../core/user.h ../../core/rebus.h
init.o: ../../core/plugindata.h ../../core/function.h ../../core/thread.h
init.o: ../../core/compress.h ../../core/acceptencodingparser.h
init.o: ../../core/acceptbaseparser.h ../../core/htmlfilter.h
init.o: ../../core/postmultiparser.h ../../core/ticket.h ../../core/config.h
init.o: ../../confparser/confparser.h ../../core/db.h ../../core/group.h
init.o: ../../core/dircontainer.h ../../core/ugcontainer.h stats.h
init.o: templates.h ../../core/plugin.h ../../core/request.h data.h
init.o: ../../core/plugindata.h ../../core/pluginmsg.h
stats.o: stats.h data.h ../../core/plugindata.h ../../core/log.h
templates.o: templates.h ../../core/plugin.h ../../core/request.h data.h
templates.o: stats.h ../../core/plugindata.h ../../core/pluginmsg.h
templates.o: ../../core/log.h ../../core/plugindata.h ../../../ezc/src/ezc.h
templates.o: ../../core/misc.h ../../core/item.h

1
plugins/stats/Makefile.o.dep Executable file
View File

@@ -0,0 +1 @@
o = init.o stats.o templates.o

52
plugins/stats/data.h Executable file
View File

@@ -0,0 +1,52 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfilecmsluplugin_stats_data
#define headerfilecmsluplugin_stats_data
#include "stats.h"
#include <map>
#include "core/plugindata.h"
#include <ctime>
namespace Stats
{
// session data for the plugin
struct StatsData : public PluginDataBase
{
// whether this session has been calculated
bool calculated;
long last_visited;
StatsData()
{
calculated = false;
last_visited = -1;
}
};
extern time_t stats_start;
// statistics for all pages
extern StatsGlobal stat_global;
// statistics for one item
// <item_id, Stats>
extern std::map<long, Stats> stats_tab;
long ItemId();
} // namespace
#endif

246
plugins/stats/init.cpp Executable file
View File

@@ -0,0 +1,246 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include <map>
#include "core/log.h"
#include "core/request.h"
#include "core/config.h"
#include "core/db.h"
#include "stats.h"
#include "templates.h"
#include "data.h"
extern "C" void Init(PluginInfo &);
const char plugin_name[] = "stats";
namespace Stats
{
// when the statistics start
time_t stats_start;
// statistics for all pages
StatsGlobal stat_global;
// statistics for one item
// <item_id, Stats>
std::map<long, Stats> stats_tab;
// when to save the config (how many requests should have gone)
// (for safety: power failure etc)
// default: 1000
// you can set: stats_req_save_freq in the config file to overwrite it
// 0 - turn it off
int req_save_freq = 1000;
int req_current = 0; // helper
long ItemId()
{
long item_id;
if( request.is_item )
item_id = request.item.id;
else
item_id = request.dir_table.back()->id;
return item_id;
}
void CheckSaving()
{
req_current += 1;
if( req_save_freq != 0 && req_current >= req_save_freq )
{
SaveStats();
req_current = 0;
}
}
bool BrowserHas(const char * name)
{
const char * is = strstr(request.env_http_user_agent, name);
return is != 0;
}
bool IsGoogle()
{
return BrowserHas("Googlebot") && BrowserHas("www.google.com");
}
bool IsYahoo()
{
return BrowserHas("Yahoo!") && BrowserHas("yahoo.com");
}
void UpdateStats(Stats & item_stats)
{
stat_global.all += 1;
item_stats.all += 1;
if( IsGoogle() )
{
stat_global.google += 1;
item_stats.google += 1;
}
if( IsYahoo() )
{
stat_global.yahoo += 1;
item_stats.yahoo += 1;
}
}
void ContentMake(PluginInfo & info)
{
StatsData * d = 0;
long id = ItemId();
if( info.plugin_data_base )
d = reinterpret_cast<StatsData*>(info.plugin_data_base);
// this simply prevents F5 (refresh) from a webbrowser
if( d && d->last_visited != -1 && d->last_visited == id )
return;
if( d )
d->last_visited = id;
UpdateStats(stats_tab[id]);
if( d && !d->calculated )
{
stat_global.unique += 1;
d->calculated = true;
}
CheckSaving();
}
void SessionCreated(PluginInfo & info)
{
StatsData * d = new StatsData();
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 ReadConfig()
{
stats_file = config.Text("stats_file");
req_save_freq = config.Int("stats_req_save_freq", req_save_freq);
if( stats_file.empty() )
log << log1 << "you should set stats_file in your config to keep statistics between restarting winix" << logend;
else
log << log2 << "stats_file: " << stats_file << logend;
stats_start = time(0); // it will be overwritten if the config file exists
ReadStats();
}
void Close()
{
SaveStats();
}
void RemoveItem(long id)
{
std::map<long, Stats>::iterator i = stats_tab.find(id);
if( i == stats_tab.end() )
return;
stats_tab.erase(i);
log << log3 << "Stats: removed stats for item.id: " << id << logend;
}
void RemoveFile(PluginInfo & info)
{
RemoveItem(info.l1);
}
void RemoveDir(PluginInfo & info)
{
Db::ItemQuery query;
std::vector<long> items;
size_t i;
query.SetAll(false, false);
query.WhereParentId(info.l1);
db.GetItems(items, query);
// removing childs
for(i=0 ; i<items.size() ; ++i)
RemoveItem(items[i]);
// removing the directory
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_PLUGIN_INIT, ReadConfig);
plugin.Assign(WINIX_CLOSE, Close);
plugin.Assign(WINIX_FILE_REMOVED, RemoveFile);
plugin.Assign(WINIX_DIR_PREPARE_TO_REMOVE, RemoveDir);
info.p1 = (void*)plugin_name;
}

120
plugins/stats/stats.cpp Executable file
View File

@@ -0,0 +1,120 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "stats.h"
#include <fstream>
#include "data.h"
#include "core/log.h"
namespace Stats
{
std::string stats_file;
void ReadStats(std::ifstream & file)
{
file >> stats_start;
file >> stat_global.all;
file >> stat_global.unique;
file >> stat_global.google;
file >> stat_global.yahoo;
size_t len;
file >> len;
Stats s;
long item_id;
for(size_t i = 0 ; i<len && !file.eof() ; ++i)
{
file >> item_id;
file >> s.all;
file >> s.google;
file >> s.yahoo;
stats_tab.insert( std::make_pair(item_id, s) );
}
}
void ReadStats()
{
if( stats_file.empty() )
return;
std::ifstream file(stats_file.c_str());
if( !file )
{
log << log1 << "Stats: I cannot open the stats file: " << stats_file << logend;
return;
}
ReadStats(file);
file.close();
log << log3 << "Stats: statistics loaded from: " << stats_file
<< " (" << stats_tab.size() << " items)" << logend;
}
void SaveStats(std::ofstream & file)
{
file << stats_start << std::endl;
file << stat_global.all << ' ';
file << stat_global.unique << ' ';
file << stat_global.google << ' ';
file << stat_global.yahoo << ' ';
file << std::endl;
file << stats_tab.size() << std::endl;
std::map<long, Stats>::iterator i = stats_tab.begin();
for( ; i != stats_tab.end() ; ++i)
{
file << i->first << ' ';
file << i->second.all << ' ';
file << i->second.google << ' ';
file << i->second.yahoo << ' ';
file << std::endl;
}
}
void SaveStats()
{
if( stats_file.empty() )
return;
std::ofstream file(stats_file.c_str());
if( !file )
{
log << log1 << "Stats: I cannot open the stats file: " << stats_file << logend;
return;
}
SaveStats(file);
file.close();
log << log3 << "Stats: statistics saved to: " << stats_file << logend;
}
} // namespace

60
plugins/stats/stats.h Executable file
View File

@@ -0,0 +1,60 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfilecmsluplugin_stats_stats
#define headerfilecmsluplugin_stats_stats
#include <string>
namespace Stats
{
struct StatsGlobal
{
int all;
int unique;
int google;
int yahoo;
StatsGlobal()
{
all = 0;
unique = 0;
google = 0;
yahoo = 0;
}
};
struct Stats
{
int all;
int google;
int yahoo;
Stats()
{
all = 0;
google = 0;
yahoo = 0;
}
};
extern std::string stats_file;
void ReadStats();
void SaveStats();
} // namespace
#endif

91
plugins/stats/templates.cpp Executable file
View File

@@ -0,0 +1,91 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#include "templates.h"
#include "ezc.h"
#include "data.h"
#include "core/misc.h"
namespace Stats
{
void stats_from(Ezc::Info & i)
{
i.out << DateToStrWithoutHours(stats_start);
}
void stats_all(Ezc::Info & i)
{
i.out << stat_global.all;
}
void stats_unique(Ezc::Info & i)
{
i.out << stat_global.unique;
}
void stats_google(Ezc::Info & i)
{
i.out << stat_global.google;
}
void stats_yahoo(Ezc::Info & i)
{
i.out << stat_global.yahoo;
}
void stats_item_all(Ezc::Info & i)
{
i.out << stats_tab[ItemId()].all;
}
void stats_item_google(Ezc::Info & i)
{
i.out << stats_tab[ItemId()].google;
}
void stats_item_yahoo(Ezc::Info & i)
{
i.out << stats_tab[ItemId()].yahoo;
}
void CreateFunctions(PluginInfo & info)
{
if( !info.p1 )
{
log << log1 << "S: functions object doesn't set" << logend;
return;
}
Ezc::Functions * fun = reinterpret_cast<Ezc::Functions*>(info.p1);
fun->Insert("stats_from", stats_from);
fun->Insert("stats_all", stats_all);
fun->Insert("stats_unique", stats_unique);
fun->Insert("stats_google", stats_google);
fun->Insert("stats_yahoo", stats_yahoo);
fun->Insert("stats_item_all", stats_item_all);
fun->Insert("stats_item_google", stats_item_google);
fun->Insert("stats_item_yahoo", stats_item_yahoo);
}
} // namespace

27
plugins/stats/templates.h Executable file
View File

@@ -0,0 +1,27 @@
/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfilecmsluplugin_stats_templates
#define headerfilecmsluplugin_stats_templates
#include "core/plugin.h"
namespace Stats
{
void CreateFunctions(PluginInfo & info);
} // namespace
#endif