]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
statistics.json: it now contains the connectors as well.
[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_plugin_enabled($plugin) {
22         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
23         return((bool)(count($r) > 0));
24 }
25
26 function statistics_json_init() {
27         global $a;
28
29         $statistics = array(
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')
38                         );
39
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");
51
52         header("Content-Type: application/json");
53         echo json_encode($statistics);
54         logger("statistics_init: printed ".print_r($statistics, true));
55         killme();
56 }
57
58 function statistics_json_cron($a,$b) {
59         $last = get_config('statistics_json','last_calucation');
60
61         if($last) {
62                 // Calculate every 24 hours
63                 $next = $last + (24 * 60 * 60);
64                 if($next > time()) {
65                         logger('statistics_json_cron: calculation intervall not reached');
66                         return;
67                 }
68         }
69         logger('statistics_json_cron: cron_start');
70
71
72         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
73                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
74                                 FROM `item`
75                                         WHERE `item`.`type` = 'wall'
76                                                 GROUP BY `item`.`uid`) AS `lastitem`
77                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
78                                 WHERE
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`");
85
86         if (is_array($users)) {
87                         $total_users = count($users);
88                         $active_users_halfyear = 0;
89                         $active_users_monthly = 0;
90
91                         $halfyear = time() - (180 * 24 * 60 * 60);
92                         $month = time() - (30 * 24 * 60 * 60);
93
94                         foreach ($users AS $user) {
95                                 if ((strtotime($user['login_date']) > $halfyear) OR
96                                         (strtotime($user['lastitem_date']) > $halfyear))
97                                         ++$active_users_halfyear;
98
99                                 if ((strtotime($user['login_date']) > $month) OR
100                                         (strtotime($user['lastitem_date']) > $month))
101                                         ++$active_users_monthly;
102
103                         }
104                         set_config('statistics_json','total_users', $total_users);
105                         logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
106
107                         set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
108                         set_config('statistics_json','active_users_monthly', $active_users_monthly);
109         }
110
111         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND left(body, 6) != '[share'");
112
113         if (!is_array($posts))
114                 $local_posts = -1;
115         else
116                 $local_posts = $posts[0]["local_posts"];
117
118         set_config('statistics_json','local_posts', $local_posts);
119
120         logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
121
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);
127
128         logger('statistics_json_cron: cron_end');
129         set_config('statistics_json','last_calucation', time());
130 }