]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
fd1e5526aa35135cafa4621a767bd2197960270e
[friendica-addons.git] / statistics_json / statistics_json.php
1 <?php
2
3 /**
4  * Name: Statistics
5  * Description: Generates some statistics for http://http://the-federation.info/
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["services"] = array();
41         $statistics["services"]["appnet"] = statistics_json_plugin_enabled("appnet");
42         $statistics["services"]["blogger"] = statistics_json_plugin_enabled("blogger");
43         $statistics["services"]["buffer"] = statistics_json_plugin_enabled("buffer");
44         $statistics["services"]["dreamwidth"] = statistics_json_plugin_enabled("dwpost");
45         $statistics["services"]["facebook"] = statistics_json_plugin_enabled("fbpost");
46         $statistics["services"]["gnusocial"] = statistics_json_plugin_enabled("statusnet");
47         $statistics["services"]["googleplus"] = statistics_json_plugin_enabled("gpluspost");
48         $statistics["services"]["libertree"] = statistics_json_plugin_enabled("libertree");
49         $statistics["services"]["livejournal"] = statistics_json_plugin_enabled("ljpost");
50         $statistics["services"]["pumpio"] = statistics_json_plugin_enabled("pumpio");
51         $statistics["services"]["twitter"] = statistics_json_plugin_enabled("twitter");
52         $statistics["services"]["tumblr"] = statistics_json_plugin_enabled("tumblr");
53         $statistics["services"]["wordpress"] = statistics_json_plugin_enabled("wppost");
54
55         $statistics["appnet"] = $statistics["services"]["appnet"];
56         $statistics["blogger"] = $statistics["services"]["blogger"];
57         $statistics["buffer"] = $statistics["services"]["buffer"];
58         $statistics["dreamwidth"] = $statistics["services"]["dreamwidth"];
59         $statistics["facebook"] = $statistics["services"]["facebook"];
60         $statistics["gnusocial"] = $statistics["services"]["gnusocial"];
61         $statistics["googleplus"] = $statistics["services"]["googleplus"];
62         $statistics["libertree"] = $statistics["services"]["libertree"];
63         $statistics["livejournal"] = $statistics["services"]["livejournal"];
64         $statistics["pumpio"] = $statistics["services"]["pumpio"];
65         $statistics["twitter"] = $statistics["services"]["twitter"];
66         $statistics["tumblr"] = $statistics["services"]["tumblr"];
67         $statistics["wordpress"] = $statistics["services"]["wordpress"];
68
69         header("Content-Type: application/json");
70         echo json_encode($statistics);
71         logger("statistics_init: printed ".print_r($statistics, true));
72         killme();
73 }
74
75 function statistics_json_cron($a,$b) {
76         $last = get_config('statistics_json','last_calucation');
77
78         if($last) {
79                 // Calculate every 24 hours
80                 $next = $last + (24 * 60 * 60);
81                 if($next > time()) {
82                         logger('statistics_json_cron: calculation intervall not reached');
83                         return;
84                 }
85         }
86         logger('statistics_json_cron: cron_start');
87
88
89         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
90                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
91                                 FROM `item`
92                                         WHERE `item`.`type` = 'wall'
93                                                 GROUP BY `item`.`uid`) AS `lastitem`
94                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
95                                 WHERE
96                                         `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
97                                         AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
98                                         AND `user`.`verified` AND `contact`.`self`
99                                         AND NOT `user`.`blocked`
100                                         AND NOT `user`.`account_removed`
101                                         AND NOT `user`.`account_expired`");
102
103         if (is_array($users)) {
104                         $total_users = count($users);
105                         $active_users_halfyear = 0;
106                         $active_users_monthly = 0;
107
108                         $halfyear = time() - (180 * 24 * 60 * 60);
109                         $month = time() - (30 * 24 * 60 * 60);
110
111                         foreach ($users AS $user) {
112                                 if ((strtotime($user['login_date']) > $halfyear) OR
113                                         (strtotime($user['lastitem_date']) > $halfyear))
114                                         ++$active_users_halfyear;
115
116                                 if ((strtotime($user['login_date']) > $month) OR
117                                         (strtotime($user['lastitem_date']) > $month))
118                                         ++$active_users_monthly;
119
120                         }
121                         set_config('statistics_json','total_users', $total_users);
122                         logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
123
124                         set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
125                         set_config('statistics_json','active_users_monthly', $active_users_monthly);
126         }
127
128         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND left(body, 6) != '[share'");
129
130         if (!is_array($posts))
131                 $local_posts = -1;
132         else
133                 $local_posts = $posts[0]["local_posts"];
134
135         set_config('statistics_json','local_posts', $local_posts);
136
137         logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
138
139         // Now trying to register
140         $url = "http://the-federation.info/register/".$a->get_hostname();
141         logger('statistics_json_cron: registering url: '.$url, LOGGER_DEBUG);
142         $ret = fetch_url($url);
143         logger('statistics_json_cron: registering answer: '.$ret, LOGGER_DEBUG);
144
145         logger('statistics_json_cron: cron_end');
146         set_config('statistics_json','last_calucation', time());
147 }