Merge "Update formatting on includes/deferred/"
[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 protected $views = 0;
26 protected $edits = 0;
27 protected $pages = 0;
28 protected $articles = 0;
29 protected $users = 0;
30 protected $images = 0;
31
32 // @todo deprecate this constructor
33 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
34 $this->views = $views;
35 $this->edits = $edits;
36 $this->articles = $good;
37 $this->pages = $pages;
38 $this->users = $users;
39 }
40
41 /**
42 * @param $deltas Array
43 * @return SiteStatsUpdate
44 */
45 public static function factory( array $deltas ) {
46 $update = new self( 0, 0, 0 );
47
48 $fields = array( 'views', 'edits', 'pages', 'articles', 'users', 'images' );
49 foreach ( $fields as $field ) {
50 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
51 $update->$field = $deltas[$field];
52 }
53 }
54
55 return $update;
56 }
57
58 public function doUpdate() {
59 global $wgSiteStatsAsyncFactor;
60
61 $rate = $wgSiteStatsAsyncFactor; // convenience
62 // If set to do so, only do actual DB updates 1 every $rate times.
63 // The other times, just update "pending delta" values in memcached.
64 if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
65 $this->doUpdatePendingDeltas();
66 } else {
67 // Need a separate transaction because this a global lock
68 wfGetDB( DB_MASTER )->onTransactionIdle( array( $this, 'tryDBUpdateInternal' ) );
69 }
70 }
71
72 /**
73 * Do not call this outside of SiteStatsUpdate
74 *
75 * @return void
76 */
77 public function tryDBUpdateInternal() {
78 global $wgSiteStatsAsyncFactor;
79
80 $dbw = wfGetDB( DB_MASTER );
81 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
82 if ( $wgSiteStatsAsyncFactor ) {
83 // Lock the table so we don't have double DB/memcached updates
84 if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
85 || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
86 ) {
87 $this->doUpdatePendingDeltas();
88
89 return;
90 }
91 $pd = $this->getPendingDeltas();
92 // Piggy-back the async deltas onto those of this stats update....
93 $this->views += ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] );
94 $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
95 $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
96 $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
97 $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
98 $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
99 }
100
101 // Build up an SQL query of deltas and apply them...
102 $updates = '';
103 $this->appendUpdate( $updates, 'ss_total_views', $this->views );
104 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
105 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
106 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
107 $this->appendUpdate( $updates, 'ss_users', $this->users );
108 $this->appendUpdate( $updates, 'ss_images', $this->images );
109 if ( $updates != '' ) {
110 $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ );
111 }
112
113 if ( $wgSiteStatsAsyncFactor ) {
114 // Decrement the async deltas now that we applied them
115 $this->removePendingDeltas( $pd );
116 // Commit the updates and unlock the table
117 $dbw->unlock( $lockKey, __METHOD__ );
118 }
119 }
120
121 /**
122 * @param $dbw DatabaseBase
123 * @return bool|mixed
124 */
125 public static function cacheUpdate( $dbw ) {
126 global $wgActiveUserDays;
127 $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow' ) );
128 # Get non-bot users than did some recent action other than making accounts.
129 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
130 $activeUsers = $dbr->selectField(
131 'recentchanges',
132 'COUNT( DISTINCT rc_user_text )',
133 array(
134 'rc_user != 0',
135 'rc_bot' => 0,
136 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
137 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays * 24 * 3600 ) ),
138 ),
139 __METHOD__
140 );
141 $dbw->update(
142 'site_stats',
143 array( 'ss_active_users' => intval( $activeUsers ) ),
144 array( 'ss_row_id' => 1 ),
145 __METHOD__
146 );
147
148 return $activeUsers;
149 }
150
151 protected function doUpdatePendingDeltas() {
152 $this->adjustPending( 'ss_total_views', $this->views );
153 $this->adjustPending( 'ss_total_edits', $this->edits );
154 $this->adjustPending( 'ss_good_articles', $this->articles );
155 $this->adjustPending( 'ss_total_pages', $this->pages );
156 $this->adjustPending( 'ss_users', $this->users );
157 $this->adjustPending( 'ss_images', $this->images );
158 }
159
160 /**
161 * @param $sql string
162 * @param $field string
163 * @param $delta integer
164 */
165 protected function appendUpdate( &$sql, $field, $delta ) {
166 if ( $delta ) {
167 if ( $sql ) {
168 $sql .= ',';
169 }
170 if ( $delta < 0 ) {
171 $sql .= "$field=$field-" . abs( $delta );
172 } else {
173 $sql .= "$field=$field+" . abs( $delta );
174 }
175 }
176 }
177
178 /**
179 * @param $type string
180 * @param string $sign ('+' or '-')
181 * @return string
182 */
183 private function getTypeCacheKey( $type, $sign ) {
184 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
185 }
186
187 /**
188 * Adjust the pending deltas for a stat type.
189 * Each stat type has two pending counters, one for increments and decrements
190 * @param $type string
191 * @param $delta integer Delta (positive or negative)
192 * @return void
193 */
194 protected function adjustPending( $type, $delta ) {
195 global $wgMemc;
196
197 if ( $delta < 0 ) { // decrement
198 $key = $this->getTypeCacheKey( $type, '-' );
199 } else { // increment
200 $key = $this->getTypeCacheKey( $type, '+' );
201 }
202
203 $magnitude = abs( $delta );
204 if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
205 if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
206 $wgMemc->incr( $key, $magnitude );
207 }
208 }
209 }
210
211 /**
212 * Get pending delta counters for each stat type
213 * @return Array Positive and negative deltas for each type
214 * @return void
215 */
216 protected function getPendingDeltas() {
217 global $wgMemc;
218
219 $pending = array();
220 foreach ( array( 'ss_total_views', 'ss_total_edits',
221 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type
222 ) {
223 // Get pending increments and pending decrements
224 $pending[$type]['+'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '+' ) );
225 $pending[$type]['-'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '-' ) );
226 }
227
228 return $pending;
229 }
230
231 /**
232 * Reduce pending delta counters after updates have been applied
233 * @param array $pd Result of getPendingDeltas(), used for DB update
234 * @return void
235 */
236 protected function removePendingDeltas( array $pd ) {
237 global $wgMemc;
238
239 foreach ( $pd as $type => $deltas ) {
240 foreach ( $deltas as $sign => $magnitude ) {
241 // Lower the pending counter now that we applied these changes
242 $wgMemc->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
243 }
244 }
245 }
246 }