5 * Description: Generates some statistics for http://the-federation.info/
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 function statistics_json_install() {
11 register_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
15 function statistics_json_uninstall() {
16 unregister_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
19 function statistics_json_module() {}
21 function statistics_json_plugin_enabled($plugin) {
22 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
23 return((bool)(count($r) > 0));
26 function statistics_json_init() {
30 "name" => $a->config["sitename"],
31 "network" => FRIENDICA_PLATFORM,
32 "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION,
33 "registrations_open" => ($a->config['register_policy'] != 0),
34 "total_users" => get_config('statistics_json','total_users'),
35 "active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
36 "active_users_monthly" => get_config('statistics_json','active_users_monthly'),
37 "local_posts" => get_config('statistics_json','local_posts')
40 $statistics["services"] = array();
41 $statistics["services"]["appnet"] = statistics_json_plugin_enabled("appnet");
42 $statistics["services"]["blogger"] = statistics_json_plugin_enabled("blogger");
43 $statistics["services"]["buffer"] = statistics_json_plugin_enabled("buffer");
44 $statistics["services"]["dreamwidth"] = statistics_json_plugin_enabled("dwpost");
45 $statistics["services"]["facebook"] = statistics_json_plugin_enabled("fbpost");
46 $statistics["services"]["gnusocial"] = statistics_json_plugin_enabled("statusnet");
47 $statistics["services"]["googleplus"] = statistics_json_plugin_enabled("gpluspost");
48 $statistics["services"]["libertree"] = statistics_json_plugin_enabled("libertree");
49 $statistics["services"]["livejournal"] = statistics_json_plugin_enabled("ljpost");
50 $statistics["services"]["pumpio"] = statistics_json_plugin_enabled("pumpio");
51 $statistics["services"]["twitter"] = statistics_json_plugin_enabled("twitter");
52 $statistics["services"]["tumblr"] = statistics_json_plugin_enabled("tumblr");
53 $statistics["services"]["wordpress"] = statistics_json_plugin_enabled("wppost");
55 $statistics["appnet"] = $statistics["services"]["appnet"];
56 $statistics["blogger"] = $statistics["services"]["blogger"];
57 $statistics["buffer"] = $statistics["services"]["buffer"];
58 $statistics["dreamwidth"] = $statistics["services"]["dreamwidth"];
59 $statistics["facebook"] = $statistics["services"]["facebook"];
60 $statistics["gnusocial"] = $statistics["services"]["gnusocial"];
61 $statistics["googleplus"] = $statistics["services"]["googleplus"];
62 $statistics["libertree"] = $statistics["services"]["libertree"];
63 $statistics["livejournal"] = $statistics["services"]["livejournal"];
64 $statistics["pumpio"] = $statistics["services"]["pumpio"];
65 $statistics["twitter"] = $statistics["services"]["twitter"];
66 $statistics["tumblr"] = $statistics["services"]["tumblr"];
67 $statistics["wordpress"] = $statistics["services"]["wordpress"];
69 header("Content-Type: application/json");
70 echo json_encode($statistics);
71 logger("statistics_init: printed ".print_r($statistics, true));
75 function statistics_json_cron($a,$b) {
76 $last = get_config('statistics_json','last_calucation');
79 // Calculate every 24 hours
80 $next = $last + (24 * 60 * 60);
82 logger('statistics_json_cron: calculation intervall not reached');
86 logger('statistics_json_cron: cron_start');
89 $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
90 FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
92 WHERE `item`.`type` = 'wall'
93 GROUP BY `item`.`uid`) AS `lastitem`
94 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
96 `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
97 AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
98 AND `user`.`verified` AND `contact`.`self`
99 AND NOT `user`.`blocked`
100 AND NOT `user`.`account_removed`
101 AND NOT `user`.`account_expired`");
103 if (is_array($users)) {
104 $total_users = count($users);
105 $active_users_halfyear = 0;
106 $active_users_monthly = 0;
108 $halfyear = time() - (180 * 24 * 60 * 60);
109 $month = time() - (30 * 24 * 60 * 60);
111 foreach ($users AS $user) {
112 if ((strtotime($user['login_date']) > $halfyear) OR
113 (strtotime($user['lastitem_date']) > $halfyear))
114 ++$active_users_halfyear;
116 if ((strtotime($user['login_date']) > $month) OR
117 (strtotime($user['lastitem_date']) > $month))
118 ++$active_users_monthly;
121 set_config('statistics_json','total_users', $total_users);
122 logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
124 set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
125 set_config('statistics_json','active_users_monthly', $active_users_monthly);
128 $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND `uid` != 0 AND `id` = `parent` AND left(body, 6) != '[share'");
130 if (!is_array($posts))
133 $local_posts = $posts[0]["local_posts"];
135 set_config('statistics_json','local_posts', $local_posts);
137 logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
139 // Now trying to register
140 $url = "http://the-federation.info/register/".$a->get_hostname();
141 logger('statistics_json_cron: registering url: '.$url, LOGGER_DEBUG);
142 $ret = fetch_url($url);
143 logger('statistics_json_cron: registering answer: '.$ret, LOGGER_DEBUG);
145 logger('statistics_json_cron: cron_end');
146 set_config('statistics_json','last_calucation', time());