]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
statistics: removed debug statements
[friendica-addons.git] / statistics_json / statistics_json.php
1 <?php
2
3 /**
4  * Name: Statistics
5  * Description: Generates some statistics for http://pods.jasonrobinson.me/
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
10 function statistics_json_install() {
11 }
12
13
14 function statistics_json_uninstall() {
15 }
16
17 function statistics_json_module() {}
18
19 function statistics_json_init() {
20         global $a;
21
22         $statistics = array(
23                         "name" => $a->config["sitename"],
24                         "version" => FRIENDICA_VERSION,
25                         "registrations_open" => ($a->config['register_policy'] != 0),
26                         );
27
28         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
29                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
30                                 FROM `item`
31                                         WHERE `item`.`type` = 'wall'
32                                                 GROUP BY `item`.`uid`) AS `lastitem`
33                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
34                                 WHERE
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`");
41
42         if (!is_array($users)) {
43                         $statistics["total_users"] = -1;
44                         $statistics["active_users_halfyear"] = -1;
45                         $statistics["active_users_monthly"] = -1;
46         } else {
47                         $statistics["total_users"] = count($users);
48                         $statistics["active_users_halfyear"] = 0;
49                         $statistics["active_users_monthly"] = 0;
50
51                         $halfyear = time() - (180 * 24 * 60 * 60);
52                         $month = time() - (30 * 24 * 60 * 60);
53
54                         foreach ($users AS $user) {
55                                 if ((strtotime($user['login_date']) > $halfyear) OR
56                                         (strtotime($user['lastitem_date']) > $halfyear))
57                                         ++$statistics["active_users_halfyear"];
58
59                                 if ((strtotime($user['login_date']) > $month) OR
60                                         (strtotime($user['lastitem_date']) > $month))
61                                         ++$statistics["active_users_monthly"];
62
63                         }
64         }
65
66         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall`");
67         if (!is_array($posts))
68                 $statistics["local_posts"] = -1;
69         else
70                 $statistics["local_posts"] = $posts[0]["local_posts"];
71
72         header("Content-Type: application/json");
73         echo json_encode($statistics);
74         logger("statistics_init: printed ".print_r($statistics, true));
75         killme();
76 }