MediaWiki  1.33.0
SiteStatsUpdate.php
Go to the documentation of this file.
1 <?php
21 use Wikimedia\Assert\Assert;
23 
29  protected $stash;
31  protected $edits = 0;
33  protected $pages = 0;
35  protected $articles = 0;
37  protected $users = 0;
39  protected $images = 0;
40 
41  private static $counters = [ 'edits', 'pages', 'articles', 'users', 'images' ];
42 
43  // @todo deprecate this constructor
44  function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
45  $this->edits = $edits;
46  $this->articles = $good;
47  $this->pages = $pages;
48  $this->users = $users;
49 
50  $this->stash = MediaWikiServices::getInstance()->getMainObjectStash();
51  }
52 
53  public function merge( MergeableUpdate $update ) {
55  Assert::parameterType( __CLASS__, $update, '$update' );
56 
57  foreach ( self::$counters as $field ) {
58  $this->$field += $update->$field;
59  }
60  }
61 
66  public static function factory( array $deltas ) {
67  $update = new self( 0, 0, 0 );
68 
69  foreach ( $deltas as $name => $unused ) {
70  if ( !in_array( $name, self::$counters ) ) { // T187585
71  throw new UnexpectedValueException( __METHOD__ . ": no field called '$name'" );
72  }
73  }
74 
75  foreach ( self::$counters as $field ) {
76  if ( isset( $deltas[$field] ) && $deltas[$field] ) {
77  $update->$field = $deltas[$field];
78  }
79  }
80 
81  return $update;
82  }
83 
84  public function doUpdate() {
85  $this->doUpdateContextStats();
86 
87  $rate = MediaWikiServices::getInstance()->getMainConfig()->get( 'SiteStatsAsyncFactor' );
88  // If set to do so, only do actual DB updates 1 every $rate times.
89  // The other times, just update "pending delta" values in memcached.
90  if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
91  $this->doUpdatePendingDeltas();
92  } else {
93  // Need a separate transaction because this a global lock
94  DeferredUpdates::addCallableUpdate( [ $this, 'tryDBUpdateInternal' ] );
95  }
96  }
97 
101  public function tryDBUpdateInternal() {
102  $services = MediaWikiServices::getInstance();
103  $config = $services->getMainConfig();
104 
105  $dbw = $services->getDBLoadBalancer()->getConnection( DB_MASTER );
106  $lockKey = $dbw->getDomainID() . ':site_stats'; // prepend wiki ID
107  $pd = [];
108  if ( $config->get( 'SiteStatsAsyncFactor' ) ) {
109  // Lock the table so we don't have double DB/memcached updates
110  if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
111  $this->doUpdatePendingDeltas();
112 
113  return;
114  }
115  $pd = $this->getPendingDeltas();
116  // Piggy-back the async deltas onto those of this stats update....
117  $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
118  $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
119  $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
120  $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
121  $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
122  }
123 
124  // Build up an SQL query of deltas and apply them...
125  $updates = '';
126  $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
127  $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
128  $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
129  $this->appendUpdate( $updates, 'ss_users', $this->users );
130  $this->appendUpdate( $updates, 'ss_images', $this->images );
131  if ( $updates != '' ) {
132  $dbw->update( 'site_stats', [ $updates ], [], __METHOD__ );
133  }
134 
135  if ( $config->get( 'SiteStatsAsyncFactor' ) ) {
136  // Decrement the async deltas now that we applied them
137  $this->removePendingDeltas( $pd );
138  // Commit the updates and unlock the table
139  $dbw->unlock( $lockKey, __METHOD__ );
140  }
141 
142  // Invalid cache used by parser functions
144  }
145 
150  public static function cacheUpdate( IDatabase $dbw ) {
151  $services = MediaWikiServices::getInstance();
152  $config = $services->getMainConfig();
153 
154  $dbr = $services->getDBLoadBalancer()->getConnection( DB_REPLICA, 'vslow' );
155  # Get non-bot users than did some recent action other than making accounts.
156  # If account creation is included, the number gets inflated ~20+ fold on enwiki.
157  $rcQuery = RecentChange::getQueryInfo();
158  $activeUsers = $dbr->selectField(
159  $rcQuery['tables'],
160  'COUNT( DISTINCT ' . $rcQuery['fields']['rc_user_text'] . ' )',
161  [
162  'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Exclude external (Wikidata)
163  ActorMigration::newMigration()->isNotAnon( $rcQuery['fields']['rc_user'] ),
164  'rc_bot' => 0,
165  'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
166  'rc_timestamp >= ' . $dbr->addQuotes(
167  $dbr->timestamp( time() - $config->get( 'ActiveUserDays' ) * 24 * 3600 ) ),
168  ],
169  __METHOD__,
170  [],
171  $rcQuery['joins']
172  );
173  $dbw->update(
174  'site_stats',
175  [ 'ss_active_users' => intval( $activeUsers ) ],
176  [ 'ss_row_id' => 1 ],
177  __METHOD__
178  );
179 
180  // Invalid cache used by parser functions
182 
183  return $activeUsers;
184  }
185 
186  protected function doUpdateContextStats() {
187  $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
188  foreach ( [ 'edits', 'articles', 'pages', 'users', 'images' ] as $type ) {
189  $delta = $this->$type;
190  if ( $delta !== 0 ) {
191  $stats->updateCount( "site.$type", $delta );
192  }
193  }
194  }
195 
196  protected function doUpdatePendingDeltas() {
197  $this->adjustPending( 'ss_total_edits', $this->edits );
198  $this->adjustPending( 'ss_good_articles', $this->articles );
199  $this->adjustPending( 'ss_total_pages', $this->pages );
200  $this->adjustPending( 'ss_users', $this->users );
201  $this->adjustPending( 'ss_images', $this->images );
202  }
203 
209  protected function appendUpdate( &$sql, $field, $delta ) {
210  if ( $delta ) {
211  if ( $sql ) {
212  $sql .= ',';
213  }
214  if ( $delta < 0 ) {
215  $sql .= "$field=$field-" . abs( $delta );
216  } else {
217  $sql .= "$field=$field+" . abs( $delta );
218  }
219  }
220  }
221 
228  private function getTypeCacheKey( BagOStuff $stash, $type, $sign ) {
229  return $stash->makeKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
230  }
231 
238  protected function adjustPending( $type, $delta ) {
239  if ( $delta < 0 ) { // decrement
240  $key = $this->getTypeCacheKey( $this->stash, $type, '-' );
241  } else { // increment
242  $key = $this->getTypeCacheKey( $this->stash, $type, '+' );
243  }
244 
245  $magnitude = abs( $delta );
246  $this->stash->incrWithInit( $key, 0, $magnitude, $magnitude );
247  }
248 
253  protected function getPendingDeltas() {
254  $pending = [];
255  foreach ( [ 'ss_total_edits',
256  'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ] as $type
257  ) {
258  // Get pending increments and pending decrements
259  $flg = BagOStuff::READ_LATEST;
260  $pending[$type]['+'] = (int)$this->stash->get(
261  $this->getTypeCacheKey( $this->stash, $type, '+' ),
262  $flg
263  );
264  $pending[$type]['-'] = (int)$this->stash->get(
265  $this->getTypeCacheKey( $this->stash, $type, '-' ),
266  $flg
267  );
268  }
269 
270  return $pending;
271  }
272 
277  protected function removePendingDeltas( array $pd ) {
278  foreach ( $pd as $type => $deltas ) {
279  foreach ( $deltas as $sign => $magnitude ) {
280  // Lower the pending counter now that we applied these changes
281  $key = $this->getTypeCacheKey( $this->stash, $type, $sign );
282  $this->stash->decr( $key, $magnitude );
283  }
284  }
285  }
286 }
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:280
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:145
SiteStatsUpdate\getTypeCacheKey
getTypeCacheKey(BagOStuff $stash, $type, $sign)
Definition: SiteStatsUpdate.php:228
SiteStatsUpdate\$counters
static $counters
Definition: SiteStatsUpdate.php:41
MergeableUpdate
Interface that deferrable updates can implement to signal that updates can be combined.
Definition: MergeableUpdate.php:18
SiteStatsUpdate\merge
merge(MergeableUpdate $update)
Merge this update with $update.
Definition: SiteStatsUpdate.php:53
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:58
BagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: BagOStuff.php:774
SiteStatsUpdate\doUpdateContextStats
doUpdateContextStats()
Definition: SiteStatsUpdate.php:186
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:111
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:38
SiteStatsUpdate\__construct
__construct( $views, $edits, $good, $pages=0, $users=0)
Definition: SiteStatsUpdate.php:44
$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
SiteStatsUpdate\$articles
int $articles
Definition: SiteStatsUpdate.php:35
BagOStuff\READ_LATEST
const READ_LATEST
Bitfield constants for get()/getMulti()
Definition: BagOStuff.php:91
SiteStatsUpdate\$images
int $images
Definition: SiteStatsUpdate.php:39
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\$edits
int $edits
Definition: SiteStatsUpdate.php:31
SiteStatsUpdate\removePendingDeltas
removePendingDeltas(array $pd)
Reduce pending delta counters after updates have been applied.
Definition: SiteStatsUpdate.php:277
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:66
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:33
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
SiteStatsUpdate\$stash
BagOStuff $stash
Definition: SiteStatsUpdate.php:29
SiteStats\unload
static unload()
Trigger a reload next time a field is accessed.
Definition: SiteStats.php:38
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
SiteStatsUpdate\$users
int $users
Definition: SiteStatsUpdate.php:37
Wikimedia\Rdbms\IDatabase\update
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
SiteStatsUpdate\doUpdatePendingDeltas
doUpdatePendingDeltas()
Definition: SiteStatsUpdate.php:196
SiteStatsUpdate\appendUpdate
appendUpdate(&$sql, $field, $delta)
Definition: SiteStatsUpdate.php:209
SiteStatsUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: SiteStatsUpdate.php:84
SiteStatsUpdate\cacheUpdate
static cacheUpdate(IDatabase $dbw)
Definition: SiteStatsUpdate.php:150
SiteStatsUpdate\adjustPending
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
Definition: SiteStatsUpdate.php:238
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
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2220
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
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118
SiteStatsUpdate\getPendingDeltas
getPendingDeltas()
Get pending delta counters for each stat type.
Definition: SiteStatsUpdate.php:253
SiteStatsUpdate\tryDBUpdateInternal
tryDBUpdateInternal()
Do not call this outside of SiteStatsUpdate.
Definition: SiteStatsUpdate.php:101
$type
$type
Definition: testCompression.php:48