Merge "Add language name for aeb"
[lhc/web/wiklou.git] / includes / deferred / SiteStatsUpdate.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 /**
22 * Class for handling updates to the site_stats table
23 */
24 class SiteStatsUpdate implements DeferrableUpdate {
25 /** @var int */
26 protected $edits = 0;
27
28 /** @var int */
29 protected $pages = 0;
30
31 /** @var int */
32 protected $articles = 0;
33
34 /** @var int */
35 protected $users = 0;
36
37 /** @var int */
38 protected $images = 0;
39
40 // @todo deprecate this constructor
41 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
42 $this->edits = $edits;
43 $this->articles = $good;
44 $this->pages = $pages;
45 $this->users = $users;
46 }
47
48 /**
49 * @param array $deltas
50 * @return SiteStatsUpdate
51 */
52 public static function factory( array $deltas ) {
53 $update = new self( 0, 0, 0 );
54
55 $fields = array( 'views', 'edits', 'pages', 'articles', 'users', 'images' );
56 foreach ( $fields as $field ) {
57 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
58 $update->$field = $deltas[$field];
59 }
60 }
61
62 return $update;
63 }
64
65 public function doUpdate() {
66 global $wgSiteStatsAsyncFactor;
67
68 $rate = $wgSiteStatsAsyncFactor; // convenience
69 // If set to do so, only do actual DB updates 1 every $rate times.
70 // The other times, just update "pending delta" values in memcached.
71 if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
72 $this->doUpdatePendingDeltas();
73 } else {
74 // Need a separate transaction because this a global lock
75 wfGetDB( DB_MASTER )->onTransactionIdle( array( $this, 'tryDBUpdateInternal' ) );
76 }
77 }
78
79 /**
80 * Do not call this outside of SiteStatsUpdate
81 */
82 public function tryDBUpdateInternal() {
83 global $wgSiteStatsAsyncFactor;
84
85 $dbw = wfGetDB( DB_MASTER );
86 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
87 $pd = array();
88 if ( $wgSiteStatsAsyncFactor ) {
89 // Lock the table so we don't have double DB/memcached updates
90 if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
91 || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
92 ) {
93 $this->doUpdatePendingDeltas();
94
95 return;
96 }
97 $pd = $this->getPendingDeltas();
98 // Piggy-back the async deltas onto those of this stats update....
99 $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
100 $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
101 $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
102 $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
103 $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
104 }
105
106 // Build up an SQL query of deltas and apply them...
107 $updates = '';
108 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
109 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
110 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
111 $this->appendUpdate( $updates, 'ss_users', $this->users );
112 $this->appendUpdate( $updates, 'ss_images', $this->images );
113 if ( $updates != '' ) {
114 $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ );
115 }
116
117 if ( $wgSiteStatsAsyncFactor ) {
118 // Decrement the async deltas now that we applied them
119 $this->removePendingDeltas( $pd );
120 // Commit the updates and unlock the table
121 $dbw->unlock( $lockKey, __METHOD__ );
122 }
123 }
124
125 /**
126 * @param DatabaseBase $dbw
127 * @return bool|mixed
128 */
129 public static function cacheUpdate( $dbw ) {
130 global $wgActiveUserDays;
131 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow' ) );
132 # Get non-bot users than did some recent action other than making accounts.
133 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
134 $activeUsers = $dbr->selectField(
135 'recentchanges',
136 'COUNT( DISTINCT rc_user_text )',
137 array(
138 'rc_user != 0',
139 'rc_bot' => 0,
140 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
141 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX )
142 - $wgActiveUserDays * 24 * 3600 ) ),
143 ),
144 __METHOD__
145 );
146 $dbw->update(
147 'site_stats',
148 array( 'ss_active_users' => intval( $activeUsers ) ),
149 array( 'ss_row_id' => 1 ),
150 __METHOD__
151 );
152
153 return $activeUsers;
154 }
155
156 protected function doUpdatePendingDeltas() {
157 $this->adjustPending( 'ss_total_edits', $this->edits );
158 $this->adjustPending( 'ss_good_articles', $this->articles );
159 $this->adjustPending( 'ss_total_pages', $this->pages );
160 $this->adjustPending( 'ss_users', $this->users );
161 $this->adjustPending( 'ss_images', $this->images );
162 }
163
164 /**
165 * @param string $sql
166 * @param string $field
167 * @param int $delta
168 */
169 protected function appendUpdate( &$sql, $field, $delta ) {
170 if ( $delta ) {
171 if ( $sql ) {
172 $sql .= ',';
173 }
174 if ( $delta < 0 ) {
175 $sql .= "$field=$field-" . abs( $delta );
176 } else {
177 $sql .= "$field=$field+" . abs( $delta );
178 }
179 }
180 }
181
182 /**
183 * @param string $type
184 * @param string $sign ('+' or '-')
185 * @return string
186 */
187 private function getTypeCacheKey( $type, $sign ) {
188 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
189 }
190
191 /**
192 * Adjust the pending deltas for a stat type.
193 * Each stat type has two pending counters, one for increments and decrements
194 * @param string $type
195 * @param int $delta Delta (positive or negative)
196 */
197 protected function adjustPending( $type, $delta ) {
198 global $wgMemc;
199
200 if ( $delta < 0 ) { // decrement
201 $key = $this->getTypeCacheKey( $type, '-' );
202 } else { // increment
203 $key = $this->getTypeCacheKey( $type, '+' );
204 }
205
206 $magnitude = abs( $delta );
207 if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
208 if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
209 $wgMemc->incr( $key, $magnitude );
210 }
211 }
212 }
213
214 /**
215 * Get pending delta counters for each stat type
216 * @return array Positive and negative deltas for each type
217 */
218 protected function getPendingDeltas() {
219 global $wgMemc;
220
221 $pending = array();
222 foreach ( array( 'ss_total_edits',
223 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type
224 ) {
225 // Get pending increments and pending decrements
226 $pending[$type]['+'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '+' ) );
227 $pending[$type]['-'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '-' ) );
228 }
229
230 return $pending;
231 }
232
233 /**
234 * Reduce pending delta counters after updates have been applied
235 * @param array $pd Result of getPendingDeltas(), used for DB update
236 */
237 protected function removePendingDeltas( array $pd ) {
238 global $wgMemc;
239
240 foreach ( $pd as $type => $deltas ) {
241 foreach ( $deltas as $sign => $magnitude ) {
242 // Lower the pending counter now that we applied these changes
243 $wgMemc->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
244 }
245 }
246 }
247 }