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.
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.
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
22 * Class for handling updates to the site_stats table
24 class SiteStatsUpdate
implements DeferrableUpdate
{
35 protected $articles = 0;
41 protected $images = 0;
43 // @todo deprecate this constructor
44 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
45 $this->views
= $views;
46 $this->edits
= $edits;
47 $this->articles
= $good;
48 $this->pages
= $pages;
49 $this->users
= $users;
53 * @param array $deltas
54 * @return SiteStatsUpdate
56 public static function factory( array $deltas ) {
57 $update = new self( 0, 0, 0 );
59 $fields = array( 'views', 'edits', 'pages', 'articles', 'users', 'images' );
60 foreach ( $fields as $field ) {
61 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
62 $update->$field = $deltas[$field];
69 public function doUpdate() {
70 global $wgSiteStatsAsyncFactor;
72 $rate = $wgSiteStatsAsyncFactor; // convenience
73 // If set to do so, only do actual DB updates 1 every $rate times.
74 // The other times, just update "pending delta" values in memcached.
75 if ( $rate && ( $rate < 0 ||
mt_rand( 0, $rate - 1 ) != 0 ) ) {
76 $this->doUpdatePendingDeltas();
78 // Need a separate transaction because this a global lock
79 wfGetDB( DB_MASTER
)->onTransactionIdle( array( $this, 'tryDBUpdateInternal' ) );
84 * Do not call this outside of SiteStatsUpdate
86 public function tryDBUpdateInternal() {
87 global $wgSiteStatsAsyncFactor;
89 $dbw = wfGetDB( DB_MASTER
);
90 $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
92 if ( $wgSiteStatsAsyncFactor ) {
93 // Lock the table so we don't have double DB/memcached updates
94 if ( !$dbw->lockIsFree( $lockKey, __METHOD__
)
95 ||
!$dbw->lock( $lockKey, __METHOD__
, 1 ) // 1 sec timeout
97 $this->doUpdatePendingDeltas();
101 $pd = $this->getPendingDeltas();
102 // Piggy-back the async deltas onto those of this stats update....
103 $this->views +
= ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] );
104 $this->edits +
= ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
105 $this->articles +
= ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
106 $this->pages +
= ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
107 $this->users +
= ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
108 $this->images +
= ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
111 // Build up an SQL query of deltas and apply them...
113 $this->appendUpdate( $updates, 'ss_total_views', $this->views
);
114 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits
);
115 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles
);
116 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages
);
117 $this->appendUpdate( $updates, 'ss_users', $this->users
);
118 $this->appendUpdate( $updates, 'ss_images', $this->images
);
119 if ( $updates != '' ) {
120 $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__
);
123 if ( $wgSiteStatsAsyncFactor ) {
124 // Decrement the async deltas now that we applied them
125 $this->removePendingDeltas( $pd );
126 // Commit the updates and unlock the table
127 $dbw->unlock( $lockKey, __METHOD__
);
132 * @param DatabaseBase $dbw
135 public static function cacheUpdate( $dbw ) {
136 global $wgActiveUserDays;
137 $dbr = wfGetDB( DB_SLAVE
, array( 'SpecialStatistics', 'vslow' ) );
138 # Get non-bot users than did some recent action other than making accounts.
139 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
140 $activeUsers = $dbr->selectField(
142 'COUNT( DISTINCT rc_user_text )',
146 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
147 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX
)
148 - $wgActiveUserDays * 24 * 3600 ) ),
154 array( 'ss_active_users' => intval( $activeUsers ) ),
155 array( 'ss_row_id' => 1 ),
162 protected function doUpdatePendingDeltas() {
163 $this->adjustPending( 'ss_total_views', $this->views
);
164 $this->adjustPending( 'ss_total_edits', $this->edits
);
165 $this->adjustPending( 'ss_good_articles', $this->articles
);
166 $this->adjustPending( 'ss_total_pages', $this->pages
);
167 $this->adjustPending( 'ss_users', $this->users
);
168 $this->adjustPending( 'ss_images', $this->images
);
173 * @param string $field
176 protected function appendUpdate( &$sql, $field, $delta ) {
182 $sql .= "$field=$field-" . abs( $delta );
184 $sql .= "$field=$field+" . abs( $delta );
190 * @param string $type
191 * @param string $sign ('+' or '-')
194 private function getTypeCacheKey( $type, $sign ) {
195 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
199 * Adjust the pending deltas for a stat type.
200 * Each stat type has two pending counters, one for increments and decrements
201 * @param string $type
202 * @param int $delta Delta (positive or negative)
204 protected function adjustPending( $type, $delta ) {
207 if ( $delta < 0 ) { // decrement
208 $key = $this->getTypeCacheKey( $type, '-' );
209 } else { // increment
210 $key = $this->getTypeCacheKey( $type, '+' );
213 $magnitude = abs( $delta );
214 if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
215 if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
216 $wgMemc->incr( $key, $magnitude );
222 * Get pending delta counters for each stat type
223 * @return array Positive and negative deltas for each type
225 protected function getPendingDeltas() {
229 foreach ( array( 'ss_total_views', 'ss_total_edits',
230 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type
232 // Get pending increments and pending decrements
233 $pending[$type]['+'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '+' ) );
234 $pending[$type]['-'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '-' ) );
241 * Reduce pending delta counters after updates have been applied
242 * @param array $pd Result of getPendingDeltas(), used for DB update
244 protected function removePendingDeltas( array $pd ) {
247 foreach ( $pd as $type => $deltas ) {
248 foreach ( $deltas as $sign => $magnitude ) {
249 // Lower the pending counter now that we applied these changes
250 $wgMemc->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );