]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
Merge pull request #181 from annando/master
[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         register_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
12 }
13
14
15 function statistics_json_uninstall() {
16         unregister_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
17 }
18
19 function statistics_json_module() {}
20
21 function statistics_json_init() {
22         global $a;
23
24         $statistics = array(
25                         "name" => $a->config["sitename"],
26                         "network" => FRIENDICA_PLATFORM,
27                         "version" => FRIENDICA_VERSION,
28                         "registrations_open" => ($a->config['register_policy'] != 0),
29                         "total_users" => get_config('statistics_json','total_users'),
30                         "active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
31                         "active_users_monthly" => get_config('statistics_json','active_users_monthly'),
32                         "local_posts" => get_config('statistics_json','local_posts')
33                         );
34
35         header("Content-Type: application/json");
36         echo json_encode($statistics);
37         logger("statistics_init: printed ".print_r($statistics, true));
38         killme();
39 }
40
41 function statistics_json_cron($a,$b) {
42         $last = get_config('statistics_json','last_calucation');
43
44         if($last) {
45                 // Calculate all 3 hours
46                 $next = $last + (180 * 60);
47                 if($next > time()) {
48                         logger('statistics_json_cron: calculation intervall not reached');
49                         return;
50                 }
51         }
52         logger('statistics_json_cron: cron_start');
53
54
55         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
56                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
57                                 FROM `item`
58                                         WHERE `item`.`type` = 'wall'
59                                                 GROUP BY `item`.`uid`) AS `lastitem`
60                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
61                                 WHERE
62                                         `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
63                                         AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
64                                         AND `user`.`verified` AND `contact`.`self`
65                                         AND NOT `user`.`blocked`
66                                         AND NOT `user`.`account_removed`
67                                         AND NOT `user`.`account_expired`");
68
69         if (is_array($users)) {
70                         $total_users = count($users);
71                         $active_users_halfyear = 0;
72                         $active_users_monthly = 0;
73
74                         $halfyear = time() - (180 * 24 * 60 * 60);
75                         $month = time() - (30 * 24 * 60 * 60);
76
77                         foreach ($users AS $user) {
78                                 if ((strtotime($user['login_date']) > $halfyear) OR
79                                         (strtotime($user['lastitem_date']) > $halfyear))
80                                         ++$active_users_halfyear;
81
82                                 if ((strtotime($user['login_date']) > $month) OR
83                                         (strtotime($user['lastitem_date']) > $month))
84                                         ++$active_users_monthly;
85
86                         }
87                         set_config('statistics_json','total_users', $total_users);
88                         logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
89
90                         set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
91                         set_config('statistics_json','active_users_monthly', $active_users_monthly);
92         }
93
94         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND left(body, 6) != '[share'");
95
96         if (!is_array($posts))
97                 $local_posts = -1;
98         else
99                 $local_posts = $posts[0]["local_posts"];
100
101         set_config('statistics_json','local_posts', $local_posts);
102
103         logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
104
105         logger('statistics_json_cron: cron_end');
106         set_config('statistics_json','last_calucation', time());
107 }