Merge "Set lang in api createaccount regardless of $wgLoginLanguageSelector"
[lhc/web/wiklou.git] / includes / StatCounter.php
1 <?php
2 /**
3 * @defgroup StatCounter StatCounter
4 *
5 * StatCounter is used to increment arbitrary keys for profiling reasons.
6 * The key/values are persisted in several possible ways (see $wgStatsMethod).
7 */
8
9 /**
10 * Aggregator for wfIncrStats() that batches updates per request.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup StatCounter
29 */
30
31 /**
32 * Aggregator for wfIncrStats() that batches updates per request.
33 * This avoids spamming the collector many times for the same key.
34 *
35 * @ingroup StatCounter
36 */
37 class StatCounter {
38 /** @var Array */
39 protected $deltas = array(); // (key => count)
40
41 protected function __construct() {}
42
43 public static function singleton() {
44 static $instance = null;
45 if ( !$instance ) {
46 $instance = new self();
47 }
48 return $instance;
49 }
50
51 /**
52 * Increment a key by delta $count
53 *
54 * @param string $key
55 * @param integer $count
56 * @return void
57 */
58 public function incr( $key, $count = 1 ) {
59 if ( PHP_SAPI === 'cli' ) {
60 $this->sendDelta( $key, $count );
61 } else {
62 if ( !isset( $this->deltas[$key] ) ) {
63 $this->deltas[$key] = 0;
64 }
65 $this->deltas[$key] += $count;
66 }
67 }
68
69 /**
70 * Flush all pending deltas to persistent storage
71 *
72 * @return void
73 */
74 public function flush() {
75 try {
76 foreach ( $this->deltas as $key => $count ) {
77 $this->sendDelta( $key, $count );
78 }
79 } catch ( MWException $e ) {
80 trigger_error( "Caught exception: {$e->getMessage()}");
81 }
82 $this->deltas = array();
83 }
84
85 /**
86 * @param string $key
87 * @param string $count
88 * @return void
89 */
90 protected function sendDelta( $key, $count ) {
91 global $wgStatsMethod;
92
93 $count = intval( $count );
94 if ( $count == 0 ) {
95 return;
96 }
97
98 if ( $wgStatsMethod == 'udp' ) {
99 global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID;
100 static $socket;
101
102 $id = $wgAggregateStatsID !== false ? $wgAggregateStatsID : wfWikiID();
103
104 if ( !$socket ) {
105 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
106 $statline = "stats/{$id} - 1 1 1 1 1 -total\n";
107 socket_sendto(
108 $socket,
109 $statline,
110 strlen( $statline ),
111 0,
112 $wgUDPProfilerHost,
113 $wgUDPProfilerPort
114 );
115 }
116 $statline = "stats/{$id} - {$count} 1 1 1 1 {$key}\n";
117 wfSuppressWarnings();
118 socket_sendto(
119 $socket,
120 $statline,
121 strlen( $statline ),
122 0,
123 $wgUDPProfilerHost,
124 $wgUDPProfilerPort
125 );
126 wfRestoreWarnings();
127 } elseif ( $wgStatsMethod == 'cache' ) {
128 global $wgMemc;
129 $key = wfMemcKey( 'stats', $key );
130 if ( is_null( $wgMemc->incr( $key, $count ) ) ) {
131 $wgMemc->add( $key, $count );
132 }
133 } else {
134 // Disabled
135 }
136 }
137 }