21use Wikimedia\Assert\Assert;
41 private static $counters = [
'edits',
'pages',
'articles',
'users',
'images' ];
44 function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
46 $this->articles = $good;
50 $this->stash = MediaWikiServices::getInstance()->getMainObjectStash();
55 Assert::parameterType( __CLASS__, $update,
'$update' );
57 foreach ( self::$counters as $field ) {
58 $this->$field += $update->$field;
67 $update =
new self( 0, 0, 0 );
69 foreach ( $deltas as $name => $unused ) {
70 if ( !in_array( $name, self::$counters ) ) {
71 throw new UnexpectedValueException( __METHOD__ .
": no field called '$name'" );
75 foreach ( self::$counters as $field ) {
76 if ( isset( $deltas[$field] ) && $deltas[$field] ) {
77 $update->$field = $deltas[$field];
87 $rate = MediaWikiServices::getInstance()->getMainConfig()->get(
'SiteStatsAsyncFactor' );
90 if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
94 DeferredUpdates::addCallableUpdate( [ $this,
'tryDBUpdateInternal' ] );
102 $services = MediaWikiServices::getInstance();
106 $lockKey = $dbw->getDomainID() .
':site_stats';
108 if ( $config->get(
'SiteStatsAsyncFactor' ) ) {
110 if ( !$dbw->lock( $lockKey, __METHOD__, 0 ) ) {
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'][
'-'] );
127 $this->
appendUpdate( $updates,
'ss_good_articles', $this->articles );
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__ );
135 if ( $config->get(
'SiteStatsAsyncFactor' ) ) {
139 $dbw->unlock( $lockKey, __METHOD__ );
151 $services = MediaWikiServices::getInstance();
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(
160 'COUNT( DISTINCT ' . $rcQuery[
'fields'][
'rc_user_text'] .
' )',
163 ActorMigration::newMigration()->isNotAnon( $rcQuery[
'fields'][
'rc_user'] ),
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 ) ),
175 [
'ss_active_users' => intval( $activeUsers ) ],
176 [
'ss_row_id' => 1 ],
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 );
215 $sql .=
"$field=$field-" . abs( $delta );
217 $sql .=
"$field=$field+" . abs( $delta );
229 return $stash->
makeKey(
'sitestatsupdate',
'pendingdelta',
$type, $sign );
245 $magnitude = abs( $delta );
246 $this->stash->incrWithInit( $key, 0, $magnitude, $magnitude );
255 foreach ( [
'ss_total_edits',
256 'ss_good_articles',
'ss_total_pages',
'ss_users',
'ss_images' ] as
$type
259 $flg = BagOStuff::READ_LATEST;
260 $pending[
$type][
'+'] = (int)$this->stash->get(
261 $this->getTypeCacheKey( $this->stash,
$type,
'+' ),
264 $pending[
$type][
'-'] = (int)$this->stash->get(
265 $this->getTypeCacheKey( $this->stash,
$type,
'-' ),
278 foreach ( $pd as
$type => $deltas ) {
279 foreach ( $deltas as $sign => $magnitude ) {
282 $this->stash->decr( $key, $magnitude );
Class representing a cache/ephemeral data store.
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Class for handling updates to the site_stats table.
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
getTypeCacheKey(BagOStuff $stash, $type, $sign)
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.
static cacheUpdate(IDatabase $dbw)
merge(MergeableUpdate $update)
Merge this update with $update.
static unload()
Trigger a reload next time a field is accessed.
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
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
Interface that deferrable updates should implement.
Interface that deferrable updates can implement.
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))