MediaWiki REL1_30
SiteStatsUpdate.php
Go to the documentation of this file.
1<?php
21use Wikimedia\Assert\Assert;
23
29 protected $edits = 0;
31 protected $pages = 0;
33 protected $articles = 0;
35 protected $users = 0;
37 protected $images = 0;
38
39 private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
40
41 // @todo deprecate this constructor
42 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
43 $this->edits = $edits;
44 $this->articles = $good;
45 $this->pages = $pages;
46 $this->users = $users;
47 }
48
49 public function merge( MergeableUpdate $update ) {
51 Assert::parameterType( __CLASS__, $update, '$update' );
52
53 foreach ( self::$counters as $field ) {
54 $this->$field += $update->$field;
55 }
56 }
57
62 public static function factory( array $deltas ) {
63 $update = new self( 0, 0, 0 );
64
65 foreach ( self::$counters as $field ) {
66 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
67 $update->$field = $deltas[$field];
68 }
69 }
70
71 return $update;
72 }
73
74 public function doUpdate() {
76
77 $this->doUpdateContextStats();
78
79 $rate = $wgSiteStatsAsyncFactor; // convenience
80 // If set to do so, only do actual DB updates 1 every $rate times.
81 // The other times, just update "pending delta" values in memcached.
82 if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
83 $this->doUpdatePendingDeltas();
84 } else {
85 // Need a separate transaction because this a global lock
86 DeferredUpdates::addCallableUpdate( [ $this, 'tryDBUpdateInternal' ] );
87 }
88 }
89
93 public function tryDBUpdateInternal() {
95
96 $dbw = wfGetDB( DB_MASTER );
97 $lockKey = wfWikiID() . ':site_stats'; // prepend wiki ID
98 $pd = [];
100 // Lock the table so we don't have double DB/memcached updates
101 if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
102 || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
103 ) {
104 $this->doUpdatePendingDeltas();
105
106 return;
107 }
108 $pd = $this->getPendingDeltas();
109 // Piggy-back the async deltas onto those of this stats update....
110 $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
111 $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
112 $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
113 $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
114 $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
115 }
116
117 // Build up an SQL query of deltas and apply them...
118 $updates = '';
119 $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
120 $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
121 $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
122 $this->appendUpdate( $updates, 'ss_users', $this->users );
123 $this->appendUpdate( $updates, 'ss_images', $this->images );
124 if ( $updates != '' ) {
125 $dbw->update( 'site_stats', [ $updates ], [], __METHOD__ );
126 }
127
129 // Decrement the async deltas now that we applied them
130 $this->removePendingDeltas( $pd );
131 // Commit the updates and unlock the table
132 $dbw->unlock( $lockKey, __METHOD__ );
133 }
134
135 // Invalid cache used by parser functions
137 }
138
143 public static function cacheUpdate( $dbw ) {
145 $dbr = wfGetDB( DB_REPLICA, 'vslow' );
146 # Get non-bot users than did some recent action other than making accounts.
147 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
148 $activeUsers = $dbr->selectField(
149 'recentchanges',
150 'COUNT( DISTINCT rc_user_text )',
151 [
152 'rc_user != 0',
153 'rc_bot' => 0,
154 'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
155 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX )
156 - $wgActiveUserDays * 24 * 3600 ) ),
157 ],
158 __METHOD__
159 );
160 $dbw->update(
161 'site_stats',
162 [ 'ss_active_users' => intval( $activeUsers ) ],
163 [ 'ss_row_id' => 1 ],
164 __METHOD__
165 );
166
167 // Invalid cache used by parser functions
169
170 return $activeUsers;
171 }
172
173 protected function doUpdateContextStats() {
174 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
175 foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
176 $delta = $this->$type;
177 if ( $delta !== 0 ) {
178 $stats->updateCount( "site.$type", $delta );
179 }
180 }
181 }
182
183 protected function doUpdatePendingDeltas() {
184 $this->adjustPending( 'ss_total_edits', $this->edits );
185 $this->adjustPending( 'ss_good_articles', $this->articles );
186 $this->adjustPending( 'ss_total_pages', $this->pages );
187 $this->adjustPending( 'ss_users', $this->users );
188 $this->adjustPending( 'ss_images', $this->images );
189 }
190
196 protected function appendUpdate( &$sql, $field, $delta ) {
197 if ( $delta ) {
198 if ( $sql ) {
199 $sql .= ',';
200 }
201 if ( $delta < 0 ) {
202 $sql .= "$field=$field-" . abs( $delta );
203 } else {
204 $sql .= "$field=$field+" . abs( $delta );
205 }
206 }
207 }
208
215 private function getTypeCacheKey( BagOStuff $cache, $type, $sign ) {
216 return $cache->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
217 }
218
225 protected function adjustPending( $type, $delta ) {
226 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
227 if ( $delta < 0 ) { // decrement
228 $key = $this->getTypeCacheKey( $cache, $type, '-' );
229 } else { // increment
230 $key = $this->getTypeCacheKey( $cache, $type, '+' );
231 }
232
233 $magnitude = abs( $delta );
234 $cache->incrWithInit( $key, 0, $magnitude, $magnitude );
235 }
236
241 protected function getPendingDeltas() {
242 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
243
244 $pending = [];
245 foreach ( [ 'ss_total_edits',
246 'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
247 ) {
248 // Get pending increments and pending decrements
249 $flg = BagOStuff::READ_LATEST;
250 $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '+' ), $flg );
251 $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $cache, $type, '-' ), $flg );
252 }
253
254 return $pending;
255 }
256
261 protected function removePendingDeltas( array $pd ) {
262 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
263
264 foreach ( $pd as $type => $deltas ) {
265 foreach ( $deltas as $sign => $magnitude ) {
266 // Lower the pending counter now that we applied these changes
267 $cache->decr( $this->getTypeCacheKey( $cache, $type, $sign ), $magnitude );
268 }
269 }
270 }
271}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgActiveUserDays
How many days user must be idle before he is considered inactive.
$wgSiteStatsAsyncFactor
Set this to an integer to only do synchronous site_stats updates one every this many updates.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
MediaWikiServices is the service locator for the application scope of MediaWiki.
Class for handling updates to the site_stats table.
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
static factory(array $deltas)
doUpdate()
Perform the actual work.
getPendingDeltas()
Get pending delta counters for each stat type.
tryDBUpdateInternal()
Do not call this outside of SiteStatsUpdate.
__construct( $views, $edits, $good, $pages=0, $users=0)
appendUpdate(&$sql, $field, $delta)
removePendingDeltas(array $pd)
Reduce pending delta counters after updates have been applied.
merge(MergeableUpdate $update)
Merge this update with $update.
static cacheUpdate( $dbw)
getTypeCacheKey(BagOStuff $cache, $type, $sign)
static unload()
Definition SiteStats.php:40
The ContentHandler facility adds support for arbitrary content types on wiki pages
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs which is used to update link tables of transcluding pages after edits
Definition deferred.txt:17
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface that deferrable updates should implement.
Interface that deferrable updates can implement.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
$cache
Definition mcc.php:33
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26