MediaWiki  1.23.12
SiteStatsUpdate.php
Go to the documentation of this file.
1 <?php
26  protected $views = 0;
27 
29  protected $edits = 0;
30 
32  protected $pages = 0;
33 
35  protected $articles = 0;
36 
38  protected $users = 0;
39 
41  protected $images = 0;
42 
43  // @todo deprecate this constructor
44  function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
45  $this->views = $views;
46  $this->edits = $edits;
47  $this->articles = $good;
48  $this->pages = $pages;
49  $this->users = $users;
50  }
51 
56  public static function factory( array $deltas ) {
57  $update = new self( 0, 0, 0 );
58 
59  $fields = array( 'views', 'edits', 'pages', 'articles', 'users', 'images' );
60  foreach ( $fields as $field ) {
61  if ( isset( $deltas[$field] ) && $deltas[$field] ) {
62  $update->$field = $deltas[$field];
63  }
64  }
65 
66  return $update;
67  }
68 
69  public function doUpdate() {
70  global $wgSiteStatsAsyncFactor;
71 
72  $rate = $wgSiteStatsAsyncFactor; // convenience
73  // If set to do so, only do actual DB updates 1 every $rate times.
74  // The other times, just update "pending delta" values in memcached.
75  if ( $rate && ( $rate < 0 || mt_rand( 0, $rate - 1 ) != 0 ) ) {
76  $this->doUpdatePendingDeltas();
77  } else {
78  // Need a separate transaction because this a global lock
79  wfGetDB( DB_MASTER )->onTransactionIdle( array( $this, 'tryDBUpdateInternal' ) );
80  }
81  }
82 
86  public function tryDBUpdateInternal() {
87  global $wgSiteStatsAsyncFactor;
88 
89  $dbw = wfGetDB( DB_MASTER );
90  $lockKey = wfMemcKey( 'site_stats' ); // prepend wiki ID
91  $pd = array();
92  if ( $wgSiteStatsAsyncFactor ) {
93  // Lock the table so we don't have double DB/memcached updates
94  if ( !$dbw->lockIsFree( $lockKey, __METHOD__ )
95  || !$dbw->lock( $lockKey, __METHOD__, 1 ) // 1 sec timeout
96  ) {
97  $this->doUpdatePendingDeltas();
98 
99  return;
100  }
101  $pd = $this->getPendingDeltas();
102  // Piggy-back the async deltas onto those of this stats update....
103  $this->views += ( $pd['ss_total_views']['+'] - $pd['ss_total_views']['-'] );
104  $this->edits += ( $pd['ss_total_edits']['+'] - $pd['ss_total_edits']['-'] );
105  $this->articles += ( $pd['ss_good_articles']['+'] - $pd['ss_good_articles']['-'] );
106  $this->pages += ( $pd['ss_total_pages']['+'] - $pd['ss_total_pages']['-'] );
107  $this->users += ( $pd['ss_users']['+'] - $pd['ss_users']['-'] );
108  $this->images += ( $pd['ss_images']['+'] - $pd['ss_images']['-'] );
109  }
110 
111  // Build up an SQL query of deltas and apply them...
112  $updates = '';
113  $this->appendUpdate( $updates, 'ss_total_views', $this->views );
114  $this->appendUpdate( $updates, 'ss_total_edits', $this->edits );
115  $this->appendUpdate( $updates, 'ss_good_articles', $this->articles );
116  $this->appendUpdate( $updates, 'ss_total_pages', $this->pages );
117  $this->appendUpdate( $updates, 'ss_users', $this->users );
118  $this->appendUpdate( $updates, 'ss_images', $this->images );
119  if ( $updates != '' ) {
120  $dbw->update( 'site_stats', array( $updates ), array(), __METHOD__ );
121  }
122 
123  if ( $wgSiteStatsAsyncFactor ) {
124  // Decrement the async deltas now that we applied them
125  $this->removePendingDeltas( $pd );
126  // Commit the updates and unlock the table
127  $dbw->unlock( $lockKey, __METHOD__ );
128  }
129  }
130 
135  public static function cacheUpdate( $dbw ) {
136  global $wgActiveUserDays;
137  $dbr = wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow' ) );
138  # Get non-bot users than did some recent action other than making accounts.
139  # If account creation is included, the number gets inflated ~20+ fold on enwiki.
140  $activeUsers = $dbr->selectField(
141  'recentchanges',
142  'COUNT( DISTINCT rc_user_text )',
143  array(
144  'rc_user != 0',
145  'rc_bot' => 0,
146  'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
147  'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( wfTimestamp( TS_UNIX )
148  - $wgActiveUserDays * 24 * 3600 ) ),
149  ),
150  __METHOD__
151  );
152  $dbw->update(
153  'site_stats',
154  array( 'ss_active_users' => intval( $activeUsers ) ),
155  array( 'ss_row_id' => 1 ),
156  __METHOD__
157  );
158 
159  return $activeUsers;
160  }
161 
162  protected function doUpdatePendingDeltas() {
163  $this->adjustPending( 'ss_total_views', $this->views );
164  $this->adjustPending( 'ss_total_edits', $this->edits );
165  $this->adjustPending( 'ss_good_articles', $this->articles );
166  $this->adjustPending( 'ss_total_pages', $this->pages );
167  $this->adjustPending( 'ss_users', $this->users );
168  $this->adjustPending( 'ss_images', $this->images );
169  }
170 
176  protected function appendUpdate( &$sql, $field, $delta ) {
177  if ( $delta ) {
178  if ( $sql ) {
179  $sql .= ',';
180  }
181  if ( $delta < 0 ) {
182  $sql .= "$field=$field-" . abs( $delta );
183  } else {
184  $sql .= "$field=$field+" . abs( $delta );
185  }
186  }
187  }
188 
194  private function getTypeCacheKey( $type, $sign ) {
195  return wfMemcKey( 'sitestatsupdate', 'pendingdelta', $type, $sign );
196  }
197 
204  protected function adjustPending( $type, $delta ) {
205  global $wgMemc;
206 
207  if ( $delta < 0 ) { // decrement
208  $key = $this->getTypeCacheKey( $type, '-' );
209  } else { // increment
210  $key = $this->getTypeCacheKey( $type, '+' );
211  }
212 
213  $magnitude = abs( $delta );
214  if ( !$wgMemc->incr( $key, $magnitude ) ) { // not there?
215  if ( !$wgMemc->add( $key, $magnitude ) ) { // race?
216  $wgMemc->incr( $key, $magnitude );
217  }
218  }
219  }
220 
225  protected function getPendingDeltas() {
226  global $wgMemc;
227 
228  $pending = array();
229  foreach ( array( 'ss_total_views', 'ss_total_edits',
230  'ss_good_articles', 'ss_total_pages', 'ss_users', 'ss_images' ) as $type
231  ) {
232  // Get pending increments and pending decrements
233  $pending[$type]['+'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '+' ) );
234  $pending[$type]['-'] = (int)$wgMemc->get( $this->getTypeCacheKey( $type, '-' ) );
235  }
236 
237  return $pending;
238  }
239 
244  protected function removePendingDeltas( array $pd ) {
245  global $wgMemc;
246 
247  foreach ( $pd as $type => $deltas ) {
248  foreach ( $deltas as $sign => $magnitude ) {
249  // Lower the pending counter now that we applied these changes
250  $wgMemc->decr( $this->getTypeCacheKey( $type, $sign ), $magnitude );
251  }
252  }
253  }
254 }
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SiteStatsUpdate\cacheUpdate
static cacheUpdate( $dbw)
Definition: SiteStatsUpdate.php:129
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
SiteStatsUpdate\getTypeCacheKey
getTypeCacheKey( $type, $sign)
Definition: SiteStatsUpdate.php:188
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:38
$dbr
$dbr
Definition: testCompression.php:48
SiteStatsUpdate\$views
int $views
Definition: SiteStatsUpdate.php:25
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
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3627
SiteStatsUpdate\$articles
int $articles
Definition: SiteStatsUpdate.php:31
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:238
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:50
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
SiteStatsUpdate
Class for handling updates to the site_stats table.
Definition: SiteStatsUpdate.php:24
SiteStatsUpdate\$pages
int $pages
Definition: SiteStatsUpdate.php:29
SiteStatsUpdate\$users
int $users
Definition: SiteStatsUpdate.php:33
SiteStatsUpdate\doUpdatePendingDeltas
doUpdatePendingDeltas()
Definition: SiteStatsUpdate.php:156
SiteStatsUpdate\appendUpdate
appendUpdate(&$sql, $field, $delta)
Definition: SiteStatsUpdate.php:170
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
SiteStatsUpdate\doUpdate
doUpdate()
Perform the actual work.
Definition: SiteStatsUpdate.php:63
SiteStatsUpdate\adjustPending
adjustPending( $type, $delta)
Adjust the pending deltas for a stat type.
Definition: SiteStatsUpdate.php:198
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2473
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: DeferredUpdates.php:29
SiteStatsUpdate\getPendingDeltas
getPendingDeltas()
Get pending delta counters for each stat type.
Definition: SiteStatsUpdate.php:219
SiteStatsUpdate\tryDBUpdateInternal
tryDBUpdateInternal()
Do not call this outside of SiteStatsUpdate.
Definition: SiteStatsUpdate.php:80
$type
$type
Definition: testCompression.php:46