MediaWiki  REL1_28
SiteStatsUpdate.php
Go to the documentation of this file.
1 <?php
20 use Wikimedia\Assert\Assert;
21 
27  protected $edits = 0;
29  protected $pages = 0;
31  protected $articles = 0;
33  protected $users = 0;
35  protected $images = 0;
36 
37  private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
38 
39  // @todo deprecate this constructor
40  function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
41  $this->edits = $edits;
42  $this->articles = $good;
43  $this->pages = $pages;
44  $this->users = $users;
45  }
46 
47  public function merge( MergeableUpdate $update ) {
49  Assert::parameterType( __CLASS__, $update, '$update' );
50 
51  foreach ( self::$counters as $field ) {
52  $this->$field += $update->$field;
53  }
54  }
55 
60  public static function factory( array $deltas ) {
61  $update = new self( 0, 0, 0 );
62 
63  foreach ( self::$counters as $field ) {
64  if ( isset( $deltas[$field] ) && $deltas[$field] ) {
65  $update->$field = $deltas[$field];
66  }
67  }
68 
69  return $update;
70  }
71 
72  public function doUpdate() {
73  global $wgSiteStatsAsyncFactor;
74 
75  $this->doUpdateContextStats();
76 
77  $rate = $wgSiteStatsAsyncFactor; // convenience
78  // If set to do so, only do actual DB updates 1 every $rate times.
79  // The other times, just update "pending delta" values in memcached.
80  if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
81  $this->doUpdatePendingDeltas();
82  } else {
83  // Need a separate transaction because this a global lock
84  DeferredUpdates::addCallableUpdate( [ $this, 'tryDBUpdateInternal' ] );
85  }
86  }
87 
91  public function tryDBUpdateInternal() {
92  global $wgSiteStatsAsyncFactor;
93 
94  $dbw = wfGetDB( DB_MASTER );
95  $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
96  $pd = [];
97  if ( $wgSiteStatsAsyncFactor ) {
98  // Lock the table so we don't have double DB/memcached updates
99  if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
100  || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
101  ) {
102  $this->doUpdatePendingDeltas();
103 
104  return;
105  }
106  $pd = $this->getPendingDeltas();
107  // Piggy-back the async deltas onto those of this stats update....
108  $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
109  $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
110  $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
111  $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
112  $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
113  }
114 
115  // Build up an SQL query of deltas and apply them...
116  $updates = '';
117  $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
118  $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
119  $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
120  $this->appendUpdate( $updates, 'ss_users', $this->users );
121  $this->appendUpdate( $updates, 'ss_images', $this->images );
122  if ( $updates != '' ) {
123  $dbw->update( 'site_stats', [ $updates ], [], __METHOD__ );
124  }
125 
126  if ( $wgSiteStatsAsyncFactor ) {
127  // Decrement the async deltas now that we applied them
128  $this->removePendingDeltas( $pd );
129  // Commit the updates and unlock the table
130  $dbw->unlock( $lockKey, __METHOD__ );
131  }
132 
133  // Invalid cache used by parser functions
135  }
136 
141  public static function cacheUpdate( $dbw ) {
142  global $wgActiveUserDays;
143  $dbr = wfGetDB( DB_REPLICA, 'vslow' );
144  # Get non-bot users than did some recent action other than making accounts.
145  # If account creation is included, the number gets inflated ~20+ fold on enwiki.
146  $activeUsers = $dbr->selectField(
147  'recentchanges',
148  'COUNT( DISTINCT rc_user_text )',
149  [
150  'rc_user != 0',
151  'rc_bot' => 0,
152  'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
153  'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX )
154  - $wgActiveUserDays * 24 * 3600 ) ),
155  ],
156  __METHOD__
157  );
158  $dbw->update(
159  'site_stats',
160  [ 'ss_active_users' => intval( $activeUsers ) ],
161  [ 'ss_row_id' => 1 ],
162  __METHOD__
163  );
164 
165  // Invalid cache used by parser functions
167 
168  return $activeUsers;
169  }
170 
171  protected function doUpdateContextStats() {
172  $stats = RequestContext::getMain()->getStats();
173  foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
174  $delta = $this->$type;
175  if ( $delta !== 0 ) {
176  $stats->updateCount( "site.$type", $delta );
177  }
178  }
179  }
180 
181  protected function doUpdatePendingDeltas() {
182  $this->adjustPending( 'ss_total_edits', $this->edits );
183  $this->adjustPending( 'ss_good_articles', $this->articles );
184  $this->adjustPending( 'ss_total_pages', $this->pages );
185  $this->adjustPending( 'ss_users', $this->users );
186  $this->adjustPending( 'ss_images', $this->images );
187  }
188 
194  protected function appendUpdate( &$sql, $field, $delta ) {
195  if ( $delta ) {
196  if ( $sql ) {
197  $sql .= ',';
198  }
199  if ( $delta < 0 ) {
200  $sql .= "$field=$field-" . abs( $delta );
201  } else {
202  $sql .= "$field=$field+" . abs( $delta );
203  }
204  }
205  }
206 
212  private function getTypeCacheKey( $type, $sign ) {
213  return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
214  }
215 
222  protected function adjustPending( $type, $delta ) {
224  if ( $delta < 0 ) { // decrement
225  $key = $this->getTypeCacheKey( $type, '-' );
226  } else { // increment
227  $key = $this->getTypeCacheKey( $type, '+' );
228  }
229 
230  $magnitude = abs( $delta );
231  $cache->incrWithInit( $key, 0, $magnitude, $magnitude );
232  }
233 
238  protected function getPendingDeltas() {
240 
241  $pending = [];
242  foreach ( [ 'ss_total_edits',
243  'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
244  ) {
245  // Get pending increments and pending decrements
246  $flg = BagOStuff::READ_LATEST;
247  $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $type, '+' ), $flg );
248  $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $type, '-' ), $flg );
249  }
250 
251  return $pending;
252  }
253 
258  protected function removePendingDeltas( array $pd ) {
260 
261  foreach ( $pd as $type => $deltas ) {
262  foreach ( $deltas as $sign => $magnitude ) {
263  // Lower the pending counter now that we applied these changes
264  $cache->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
265  }
266  }
267  }
268 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:3
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
SiteStatsUpdate\$counters
static $counters
Definition: SiteStatsUpdate.php:37
SiteStatsUpdate\cacheUpdate
static cacheUpdate( $dbw)
Definition: SiteStatsUpdate.php:141
MergeableUpdate
Interface that deferrable updates can implement.
Definition: MergeableUpdate.php:9
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2007
SiteStatsUpdate\getTypeCacheKey
getTypeCacheKey( $type, $sign)
Definition: SiteStatsUpdate.php:212
SiteStatsUpdate\merge
merge(MergeableUpdate $update)
Merge this update with $update.
Definition: SiteStatsUpdate.php:47
SiteStatsUpdate\doUpdateContextStats
doUpdateContextStats()
Definition: SiteStatsUpdate.php:171
php
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:35
pages
The ContentHandler facility adds support for arbitrary content types on wiki pages
Definition: contenthandler.txt:1
SiteStatsUpdate\__construct
__construct( $views, $edits, $good, $pages=0, $users=0)
Definition: SiteStatsUpdate.php:40
$dbr
$dbr
Definition: testCompression.php:50
edits
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:11
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:105
wfMemcKey
wfMemcKey()
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2974
ObjectCache\getMainStashInstance
static getMainStashInstance()
Get the cache object for the main stash.
Definition: ObjectCache.php:393
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3073
SiteStatsUpdate\$articles
int $articles
Definition: SiteStatsUpdate.php:31
BagOStuff\READ_LATEST
const READ_LATEST
Bitfield constants for get()/getMulti()
Definition: BagOStuff.php:83
SiteStatsUpdate\$images
int $images
Definition: SiteStatsUpdate.php:35
SiteStatsUpdate\$edits
int $edits
Definition: SiteStatsUpdate.php:27
SiteStatsUpdate\removePendingDeltas
removePendingDeltas(array $pd)
Reduce pending delta counters after updates have been applied.
Definition: SiteStatsUpdate.php:258
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:60
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:22
SiteStatsUpdate
Class for handling updates to the site_stats table.
Definition: SiteStatsUpdate.php:25
SiteStatsUpdate\$pages
int $pages
Definition: SiteStatsUpdate.php:29
DB_MASTER
const DB_MASTER
Definition: defines.php:23
SiteStats\unload
static unload()
Definition: SiteStats.php:39
$type
namespace are movable Hooks may change this value to override the return value of MWNamespace::isMovable(). 'NewDifferenceEngine' do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2259
SiteStatsUpdate\$users
int $users
Definition: SiteStatsUpdate.php:33
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
SiteStatsUpdate\doUpdatePendingDeltas
doUpdatePendingDeltas()
Definition: SiteStatsUpdate.php:181
SiteStatsUpdate\appendUpdate
appendUpdate(&$sql, $field, $delta)
Definition: SiteStatsUpdate.php:194
SiteStatsUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: SiteStatsUpdate.php:72
$cache
$cache
Definition: mcc.php:33
SiteStatsUpdate\adjustPending
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
Definition: SiteStatsUpdate.php:222
as
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
Definition: distributors.txt:9
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:9
SiteStatsUpdate\getPendingDeltas
getPendingDeltas()
Get pending delta counters for each stat type.
Definition: SiteStatsUpdate.php:238
SiteStatsUpdate\tryDBUpdateInternal
tryDBUpdateInternal()
Do not call this outside of SiteStatsUpdate.
Definition: SiteStatsUpdate.php:91