]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/snapshot.php
338c8d559de81af3eec25d05022673d5ada419a8
[quix0rs-gnu-social.git] / lib / snapshot.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * A snapshot of site stats that can report itself to headquarters
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Stats
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * A snapshot of site stats that can report itself to headquarters
36  *
37  * This class will collect statistics on the site and report them to
38  * a statistics server of the admin's choice. (Default is the big one
39  * at laconi.ca.)
40  *
41  * It can either be called from a cron job, or run occasionally by the
42  * Web site.
43  *
44  * @category Stats
45  * @package  Laconica
46  * @author   Evan Prodromou <evan@controlyourself.ca>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://laconi.ca/
49  *
50  */
51
52 class Snapshot {
53
54     var $stats = null;
55
56     function __construct()
57     {
58     }
59
60     static function check()
61     {
62         switch (common_config('snapshot', 'run')) {
63          case 'web':
64             // skip if we're not running on the Web.
65             if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {
66                 break;
67             }
68             // Run once every frequency hits
69             // XXX: do frequency by time (once a week, etc.) rather than
70             // hits
71             if (rand() % common_config('snapshot', 'frequency') == 0) {
72                 $snapshot = new Snapshot();
73                 if ($snapshot->take()) {
74                     $snapshot->report();
75                 }
76             }
77             break;
78          case 'cron':
79             // skip if we're running on the Web
80             if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
81                 break;
82             }
83             // We're running from the command line; assume
84             $snapshot = new Snapshot();
85             if ($snapshot->take()) {
86                 $snapshot->report();
87             }
88             break;
89          case 'never':
90             break;
91          default:
92             common_log(LOG_WARNING, "Unrecognized value for snapshot run config.");
93         }
94     }
95
96     function take()
97     {
98         $this->stats = array();
99
100         // Some basic identification stuff
101
102         $this->stats['version'] = LACONICA_VERSION;
103         $this->stats['phpversion'] = phpversion();
104         $this->stats['name'] = common_config('site', 'name');
105         $this->stats['root'] = common_root_url();
106
107         // non-identifying stats on various tables. Primary
108         // interest is size and rate of activity of service.
109
110         $tables = array('user',
111                         'notice',
112                         'subscription',
113                         'remote_profile',
114                         'user_group');
115
116         foreach ($tables as $table) {
117             $this->tableStats($table);
118         }
119
120         // stats on some important config options
121
122         $this->stats['theme'] = common_config('site', 'theme');
123         $this->stats['dbtype'] = common_config('db', 'type');
124         $this->stats['xmpp'] = common_config('xmpp', 'enabled');
125         $this->stats['inboxes'] = common_config('inboxes', 'enabled');
126         $this->stats['queue'] = common_config('queue', 'enabled');
127         $this->stats['license'] = common_config('license', 'url');
128         $this->stats['fancy'] = common_config('site', 'fancy');
129         $this->stats['private'] = common_config('site', 'private');
130         $this->stats['closed'] = common_config('site', 'closed');
131         $this->stats['memcached'] = common_config('memcached', 'enabled');
132         $this->stats['language'] = common_config('site', 'language');
133         $this->stats['timezone'] = common_config('site', 'timezone');
134     }
135
136     function report()
137     {
138         // XXX: Use OICU2 and OAuth to make authorized requests
139
140         $postdata = http_build_query($this->stats);
141
142         $opts = array('http' =>
143                       array(
144                             'method'  => 'POST',
145                             'header'  => 'Content-type: application/x-www-form-urlencoded',
146                             'content' => $postdata,
147                             'user_agent' => 'Laconica/'.LACONICA_VERSION
148                             )
149                       );
150
151         $context = stream_context_create($opts);
152
153         $reporturl = common_config('snapshot', 'reporturl');
154
155         $result = file_get_contents($reporturl, false, $context);
156     }
157
158     function tableStats($table)
159     {
160         $inst = DB_DataObject::Factory($table);
161         $res = $inst->query('SELECT count(*) as cnt, '.
162                             'min(created) as first, '.
163                             'max(created) as last '.
164                             'from ' . $table);
165         if ($res) {
166             $this->stats[$table.'count'] = $inst->cnt;
167             $this->stats[$table.'first'] = $inst->first;
168             $this->stats[$table.'last'] = $inst->last;
169         }
170         $inst->free();
171         unset($inst);
172     }
173 }