5 * Description: Generates some statistics for http://pods.jasonrobinson.me/
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,
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["facebook"] = statistics_json_plugin_enabled("fbpost");
41 $statistics["twitter"] = statistics_json_plugin_enabled("twitter");
42 $statistics["tumblr"] = statistics_json_plugin_enabled("tumblr");
43 $statistics["wordpress"] = statistics_json_plugin_enabled("wppost");
44 $statistics["appnet"] = statistics_json_plugin_enabled("appnet");
45 $statistics["blogger"] = statistics_json_plugin_enabled("blogger");
46 $statistics["buffer"] = statistics_json_plugin_enabled("buffer");
47 $statistics["googleplus"] = statistics_json_plugin_enabled("gpluspost");
48 $statistics["libertree"] = statistics_json_plugin_enabled("libertree");
49 $statistics["pumpio"] = statistics_json_plugin_enabled("pumpio");
50 $statistics["gnusocial"] = statistics_json_plugin_enabled("statusnet");
52 header("Content-Type: application/json");
53 echo json_encode($statistics);
54 logger("statistics_init: printed ".print_r($statistics, true));
58 function statistics_json_cron($a,$b) {
59 $last = get_config('statistics_json','last_calucation');
62 // Calculate every 24 hours
63 $next = $last + (24 * 60 * 60);
65 logger('statistics_json_cron: calculation intervall not reached');
69 logger('statistics_json_cron: cron_start');
72 $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
73 FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
75 WHERE `item`.`type` = 'wall'
76 GROUP BY `item`.`uid`) AS `lastitem`
77 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
79 `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
80 AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
81 AND `user`.`verified` AND `contact`.`self`
82 AND NOT `user`.`blocked`
83 AND NOT `user`.`account_removed`
84 AND NOT `user`.`account_expired`");
86 if (is_array($users)) {
87 $total_users = count($users);
88 $active_users_halfyear = 0;
89 $active_users_monthly = 0;
91 $halfyear = time() - (180 * 24 * 60 * 60);
92 $month = time() - (30 * 24 * 60 * 60);
94 foreach ($users AS $user) {
95 if ((strtotime($user['login_date']) > $halfyear) OR
96 (strtotime($user['lastitem_date']) > $halfyear))
97 ++$active_users_halfyear;
99 if ((strtotime($user['login_date']) > $month) OR
100 (strtotime($user['lastitem_date']) > $month))
101 ++$active_users_monthly;
104 set_config('statistics_json','total_users', $total_users);
105 logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
107 set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
108 set_config('statistics_json','active_users_monthly', $active_users_monthly);
111 $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND left(body, 6) != '[share'");
113 if (!is_array($posts))
116 $local_posts = $posts[0]["local_posts"];
118 set_config('statistics_json','local_posts', $local_posts);
120 logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
122 // Now trying to register
123 $url = "http://pods.jasonrobinson.me/register/".$a->get_hostname();
124 logger('statistics_json_cron: registering url: '.$url, LOGGER_DEBUG);
125 $ret = fetch_url($url);
126 logger('statistics_json_cron: registering answer: '.$ret, LOGGER_DEBUG);
128 logger('statistics_json_cron: cron_end');
129 set_config('statistics_json','last_calucation', time());