SpecialLinkSearch: clean up munged query variable handling
[mediawiki.git] / includes / deferred / SiteStatsUpdate.php
blob97a17c39110e9a193357a02a73c19f3c81cda1f2
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.
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
18 * @file
21 /**
22 * Class for handling updates to the site_stats table
24 class SiteStatsUpdate implements DeferrableUpdate {
25 /** @var int */
26 protected $edits = 0;
28 /** @var int */
29 protected $pages = 0;
31 /** @var int */
32 protected $articles = 0;
34 /** @var int */
35 protected $users = 0;
37 /** @var int */
38 protected $images = 0;
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;
48 /**
49 * @param array $deltas
50 * @return SiteStatsUpdate
52 public static function factory( array $deltas ) {
53 $update = new self( 0, 0, 0 );
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];
62 return $update;
65 public function doUpdate() {
66 global $wgSiteStatsAsyncFactor;
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' ) );
79 /**
80 * Do not call this outside of SiteStatsUpdate
82 public function tryDBUpdateInternal() {
83 global $wgSiteStatsAsyncFactor;
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();
95 return;
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']['-'] );
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__ );
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__ );
126 * @param DatabaseBase $dbw
127 * @return bool|mixed
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 ) ),
144 __METHOD__
146 $dbw->update(
147 'site_stats',
148 array( 'ss_active_users' => intval( $activeUsers ) ),
149 array( 'ss_row_id' => 1 ),
150 __METHOD__
153 return $activeUsers;
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 );
165 * @param string $sql
166 * @param string $field
167 * @param int $delta
169 protected function appendUpdate( &$sql, $field, $delta ) {
170 if ( $delta ) {
171 if ( $sql ) {
172 $sql .= ',';
174 if ( $delta < 0 ) {
175 $sql .= "$field=$field-" . abs( $delta );
176 } else {
177 $sql .= "$field=$field+" . abs( $delta );
183 * @param string $type
184 * @param string $sign ('+' or '-')
185 * @return string
187 private function getTypeCacheKey( $type, $sign ) {
188 return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
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)
197 protected function adjustPending( $type, $delta ) {
198 global $wgMemc;
200 if ( $delta < 0 ) { // decrement
201 $key = $this->getTypeCacheKey( $type, '-' );
202 } else { // increment
203 $key = $this->getTypeCacheKey( $type, '+' );
206 $magnitude = abs( $delta );
207 if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
208 if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
209 $wgMemc->incr( $key, $magnitude );
215 * Get pending delta counters for each stat type
216 * @return array Positive and negative deltas for each type
218 protected function getPendingDeltas() {
219 global $wgMemc;
221 $pending = array();
222 foreach ( array( 'ss_total_edits',
223 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type
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, '-' ) );
230 return $pending;
234 * Reduce pending delta counters after updates have been applied
235 * @param array $pd Result of getPendingDeltas(), used for DB update
237 protected function removePendingDeltas( array $pd ) {
238 global $wgMemc;
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 );