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