5 * Description: Generates some statistics for http://pods.jasonrobinson.me/
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 function statistics_json_install() {
14 function statistics_json_uninstall() {
17 function statistics_json_module() {}
19 function statistics_json_init() {
23 "name" => $a->config["sitename"],
24 "version" => FRIENDICA_VERSION,
25 "registrations_open" => ($a->config['register_policy'] != 0),
28 $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
29 FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
31 WHERE `item`.`type` = 'wall'
32 GROUP BY `item`.`uid`) AS `lastitem`
33 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
35 `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
36 AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
37 AND `user`.`verified` AND `contact`.`self`
38 AND NOT `user`.`blocked`
39 AND NOT `user`.`account_removed`
40 AND NOT `user`.`account_expired`");
42 if (!is_array($users)) {
43 $statistics["total_users"] = -1;
44 $statistics["active_users_halfyear"] = -1;
45 $statistics["active_users_monthly"] = -1;
47 $statistics["total_users"] = count($users);
48 $statistics["active_users_halfyear"] = 0;
49 $statistics["active_users_monthly"] = 0;
51 $halfyear = time() - (180 * 24 * 60 * 60);
52 $month = time() - (30 * 24 * 60 * 60);
54 foreach ($users AS $user) {
55 if ((strtotime($user['login_date']) > $halfyear) OR
56 (strtotime($user['lastitem_date']) > $halfyear))
57 ++$statistics["active_users_halfyear"];
59 if ((strtotime($user['login_date']) > $month) OR
60 (strtotime($user['lastitem_date']) > $month))
61 ++$statistics["active_users_monthly"];
66 $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall`");
67 if (!is_array($posts))
68 $statistics["local_posts"] = -1;
70 $statistics["local_posts"] = $posts[0]["local_posts"];
72 header("Content-Type: application/json");
73 echo json_encode($statistics);
75 logger("statistics_init: printed ".print_r($statistics, true));