]> git.mxchange.org Git - friendica-addons.git/blob - statistics_json/statistics_json.php
2bd2f41f60ff320017353e88329fa70485e4b75c
[friendica-addons.git] / statistics_json / statistics_json.php
1 <?php
2
3 /**
4  * Name: Statistics
5  * Description: Generates some statistics for http://the-federation.info/
6  * Version: 0.2
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  * Status: Unsupported
9  */
10
11 function statistics_json_install() {
12         register_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
13 }
14
15
16 function statistics_json_uninstall() {
17         unregister_hook('cron', 'addon/statistics_json/statistics_json.php', 'statistics_json_cron');
18 }
19
20 function statistics_json_module() {}
21
22 function statistics_json_plugin_enabled($plugin) {
23         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
24         return((bool)(count($r) > 0));
25 }
26
27 function statistics_json_init() {
28         global $a;
29
30         $statistics = array(
31                         "name" => $a->config["sitename"],
32                         "network" => FRIENDICA_PLATFORM,
33                         "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION,
34                         "registrations_open" => ($a->config['register_policy'] != 0),
35                         "total_users" => get_config('statistics_json','total_users'),
36                         "active_users_halfyear" => get_config('statistics_json','active_users_halfyear'),
37                         "active_users_monthly" => get_config('statistics_json','active_users_monthly'),
38                         "local_posts" => get_config('statistics_json','local_posts')
39                         );
40
41         $statistics["services"] = array();
42         $statistics["services"]["appnet"] = statistics_json_plugin_enabled("appnet");
43         $statistics["services"]["blogger"] = statistics_json_plugin_enabled("blogger");
44         $statistics["services"]["buffer"] = statistics_json_plugin_enabled("buffer");
45         $statistics["services"]["dreamwidth"] = statistics_json_plugin_enabled("dwpost");
46         $statistics["services"]["facebook"] = statistics_json_plugin_enabled("fbpost");
47         $statistics["services"]["gnusocial"] = statistics_json_plugin_enabled("statusnet");
48         $statistics["services"]["googleplus"] = statistics_json_plugin_enabled("gpluspost");
49         $statistics["services"]["libertree"] = statistics_json_plugin_enabled("libertree");
50         $statistics["services"]["livejournal"] = statistics_json_plugin_enabled("ljpost");
51         $statistics["services"]["pumpio"] = statistics_json_plugin_enabled("pumpio");
52         $statistics["services"]["twitter"] = statistics_json_plugin_enabled("twitter");
53         $statistics["services"]["tumblr"] = statistics_json_plugin_enabled("tumblr");
54         $statistics["services"]["wordpress"] = statistics_json_plugin_enabled("wppost");
55
56         $statistics["appnet"] = $statistics["services"]["appnet"];
57         $statistics["blogger"] = $statistics["services"]["blogger"];
58         $statistics["buffer"] = $statistics["services"]["buffer"];
59         $statistics["dreamwidth"] = $statistics["services"]["dreamwidth"];
60         $statistics["facebook"] = $statistics["services"]["facebook"];
61         $statistics["gnusocial"] = $statistics["services"]["gnusocial"];
62         $statistics["googleplus"] = $statistics["services"]["googleplus"];
63         $statistics["libertree"] = $statistics["services"]["libertree"];
64         $statistics["livejournal"] = $statistics["services"]["livejournal"];
65         $statistics["pumpio"] = $statistics["services"]["pumpio"];
66         $statistics["twitter"] = $statistics["services"]["twitter"];
67         $statistics["tumblr"] = $statistics["services"]["tumblr"];
68         $statistics["wordpress"] = $statistics["services"]["wordpress"];
69
70         header("Content-Type: application/json");
71         echo json_encode($statistics);
72         logger("statistics_init: printed ".print_r($statistics, true));
73         killme();
74 }
75
76 function statistics_json_cron($a,$b) {
77         $last = get_config('statistics_json','last_calucation');
78
79         if($last) {
80                 // Calculate every 24 hours
81                 $next = $last + (24 * 60 * 60);
82                 if($next > time()) {
83                         logger('statistics_json_cron: calculation intervall not reached');
84                         return;
85                 }
86         }
87         logger('statistics_json_cron: cron_start');
88
89
90         $users = q("SELECT profile.*, `user`.`login_date`, `lastitem`.`lastitem_date`
91                         FROM (SELECT MAX(`item`.`changed`) as `lastitem_date`, `item`.`uid`
92                                 FROM `item`
93                                         WHERE `item`.`type` = 'wall'
94                                                 GROUP BY `item`.`uid`) AS `lastitem`
95                                                 RIGHT OUTER JOIN `user` ON `user`.`uid` = `lastitem`.`uid`, `contact`, `profile`
96                                 WHERE
97                                         `user`.`uid` = `contact`.`uid` AND `profile`.`uid` = `user`.`uid`
98                                         AND `profile`.`is-default` AND (`profile`.`publish` OR `profile`.`net-publish`)
99                                         AND `user`.`verified` AND `contact`.`self`
100                                         AND NOT `user`.`blocked`
101                                         AND NOT `user`.`account_removed`
102                                         AND NOT `user`.`account_expired`");
103
104         if (is_array($users)) {
105                         $total_users = count($users);
106                         $active_users_halfyear = 0;
107                         $active_users_monthly = 0;
108
109                         $halfyear = time() - (180 * 24 * 60 * 60);
110                         $month = time() - (30 * 24 * 60 * 60);
111
112                         foreach ($users AS $user) {
113                                 if ((strtotime($user['login_date']) > $halfyear) ||
114                                         (strtotime($user['lastitem_date']) > $halfyear))
115                                         ++$active_users_halfyear;
116
117                                 if ((strtotime($user['login_date']) > $month) ||
118                                         (strtotime($user['lastitem_date']) > $month))
119                                         ++$active_users_monthly;
120
121                         }
122                         set_config('statistics_json','total_users', $total_users);
123                         logger('statistics_json_cron: total_users: '.$total_users, LOGGER_DEBUG);
124
125                         set_config('statistics_json','active_users_halfyear', $active_users_halfyear);
126                         set_config('statistics_json','active_users_monthly', $active_users_monthly);
127         }
128
129         $posts = q("SELECT COUNT(*) AS local_posts FROM `item` WHERE `wall` AND `uid` != 0 AND `id` = `parent` AND left(body, 6) != '[share'");
130
131         if (!is_array($posts))
132                 $local_posts = -1;
133         else
134                 $local_posts = $posts[0]["local_posts"];
135
136         set_config('statistics_json','local_posts', $local_posts);
137
138         logger('statistics_json_cron: local_posts: '.$local_posts, LOGGER_DEBUG);
139
140         // Now trying to register
141         $url = "http://the-federation.info/register/".$a->get_hostname();
142         logger('statistics_json_cron: registering url: '.$url, LOGGER_DEBUG);
143         $ret = fetch_url($url);
144         logger('statistics_json_cron: registering answer: '.$ret, LOGGER_DEBUG);
145
146         logger('statistics_json_cron: cron_end');
147         set_config('statistics_json','last_calucation', time());
148 }