MediaWiki  1.29.1
SiteStatsUpdate.php
Go to the documentation of this file.
1 <?php
21 use 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() {
75  global $wgSiteStatsAsyncFactor;
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() {
94  global $wgSiteStatsAsyncFactor;
95 
96  $dbw = wfGetDB( DB_MASTER );
97  $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
98  $pd = [];
99  if ( $wgSiteStatsAsyncFactor ) {
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 
128  if ( $wgSiteStatsAsyncFactor ) {
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 ) {
144  global $wgActiveUserDays;
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 
214  private function getTypeCacheKey( $type, $sign ) {
215  return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
216  }
217 
224  protected function adjustPending( $type, $delta ) {
226  if ( $delta < 0 ) { // decrement
227  $key = $this->getTypeCacheKey( $type, '-' );
228  } else { // increment
229  $key = $this->getTypeCacheKey( $type, '+' );
230  }
231 
232  $magnitude = abs( $delta );
233  $cache->incrWithInit( $key, 0, $magnitude, $magnitude );
234  }
235 
240  protected function getPendingDeltas() {
242 
243  $pending = [];
244  foreach ( [ 'ss_total_edits',
245  'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
246  ) {
247  // Get pending increments and pending decrements
248  $flg = BagOStuff::READ_LATEST;
249  $pending[$type]['+'] = (int)$cache->get( $this->getTypeCacheKey( $type, '+' ), $flg );
250  $pending[$type]['-'] = (int)$cache->get( $this->getTypeCacheKey( $type, '-' ), $flg );
251  }
252 
253  return $pending;
254  }
255 
260  protected function removePendingDeltas( array $pd ) {
262 
263  foreach ( $pd as $type => $deltas ) {
264  foreach ( $deltas as $sign => $magnitude ) {
265  // Lower the pending counter now that we applied these changes
266  $cache->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
267  }
268  }
269  }
270 }
SiteStatsUpdate\$counters
static $counters
Definition: SiteStatsUpdate.php:39
SiteStatsUpdate\cacheUpdate
static cacheUpdate( $dbw)
Definition: SiteStatsUpdate.php:143
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:1994
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SiteStatsUpdate\getTypeCacheKey
getTypeCacheKey( $type, $sign)
Definition: SiteStatsUpdate.php:214
SiteStatsUpdate\merge
merge(MergeableUpdate $update)
Merge this update with $update.
Definition: SiteStatsUpdate.php:49
$type
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 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:2536
SiteStatsUpdate\doUpdateContextStats
doUpdateContextStats()
Definition: SiteStatsUpdate.php:173
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
SiteStatsUpdate\__construct
__construct( $views, $edits, $good, $pages=0, $users=0)
Definition: SiteStatsUpdate.php:42
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:111
wfMemcKey
wfMemcKey()
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2961
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:3060
SiteStatsUpdate\$articles
int $articles
Definition: SiteStatsUpdate.php:33
BagOStuff\READ_LATEST
const READ_LATEST
Bitfield constants for get()/getMulti()
Definition: BagOStuff.php:83
SiteStatsUpdate\$images
int $images
Definition: SiteStatsUpdate.php:37
SiteStatsUpdate\$edits
int $edits
Definition: SiteStatsUpdate.php:29
SiteStatsUpdate\removePendingDeltas
removePendingDeltas(array $pd)
Reduce pending delta counters after updates have been applied.
Definition: SiteStatsUpdate.php:260
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:62
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:25
SiteStatsUpdate
Class for handling updates to the site_stats table.
Definition: SiteStatsUpdate.php:27
SiteStatsUpdate\$pages
int $pages
Definition: SiteStatsUpdate.php:31
DB_MASTER
const DB_MASTER
Definition: defines.php:26
SiteStats\unload
static unload()
Definition: SiteStats.php:42
SiteStatsUpdate\$users
int $users
Definition: SiteStatsUpdate.php:35
SiteStatsUpdate\doUpdatePendingDeltas
doUpdatePendingDeltas()
Definition: SiteStatsUpdate.php:183
SiteStatsUpdate\appendUpdate
appendUpdate(&$sql, $field, $delta)
Definition: SiteStatsUpdate.php:196
SiteStatsUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: SiteStatsUpdate.php:74
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$cache
$cache
Definition: mcc.php:33
SiteStatsUpdate\adjustPending
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
Definition: SiteStatsUpdate.php:224
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
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
SiteStatsUpdate\getPendingDeltas
getPendingDeltas()
Get pending delta counters for each stat type.
Definition: SiteStatsUpdate.php:240
array
the array() calling protocol came about after MediaWiki 1.4rc1.
SiteStatsUpdate\tryDBUpdateInternal
tryDBUpdateInternal()
Do not call this outside of SiteStatsUpdate.
Definition: SiteStatsUpdate.php:93