MediaWiki  1.27.1
SiteStats.php
Go to the documentation of this file.
1 <?php
26 class SiteStats {
28  private static $row;
29 
31  private static $loaded = false;
32 
34  private static $jobs;
35 
37  private static $pageCount = [];
38 
39  static function recache() {
40  self::load( true );
41  }
42 
46  static function load( $recache = false ) {
47  if ( self::$loaded && !$recache ) {
48  return;
49  }
50 
51  self::$row = self::loadAndLazyInit();
52 
53  # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS
54  if ( !isset( self::$row->ss_total_pages ) && self::$row->ss_total_pages == -1 ) {
55  # Update schema
56  $u = new SiteStatsUpdate( 0, 0, 0 );
57  $u->doUpdate();
58  self::$row = self::doLoad( wfGetDB( DB_SLAVE ) );
59  }
60 
61  self::$loaded = true;
62  }
63 
67  static function loadAndLazyInit() {
69 
70  wfDebug( __METHOD__ . ": reading site_stats from slave\n" );
71  $row = self::doLoad( wfGetDB( DB_SLAVE ) );
72 
73  if ( !self::isSane( $row ) ) {
74  // Might have just been initialized during this request? Underflow?
75  wfDebug( __METHOD__ . ": site_stats damaged or missing on slave\n" );
76  $row = self::doLoad( wfGetDB( DB_MASTER ) );
77  }
78 
79  if ( !$wgMiserMode && !self::isSane( $row ) ) {
80  // Normally the site_stats table is initialized at install time.
81  // Some manual construction scenarios may leave the table empty or
82  // broken, however, for instance when importing from a dump into a
83  // clean schema with mwdumper.
84  wfDebug( __METHOD__ . ": initializing damaged or missing site_stats\n" );
85 
87 
88  $row = self::doLoad( wfGetDB( DB_MASTER ) );
89  }
90 
91  if ( !self::isSane( $row ) ) {
92  wfDebug( __METHOD__ . ": site_stats persistently nonsensical o_O\n" );
93  }
94  return $row;
95  }
96 
101  static function doLoad( $db ) {
102  return $db->selectRow( 'site_stats', [
103  'ss_row_id',
104  'ss_total_edits',
105  'ss_good_articles',
106  'ss_total_pages',
107  'ss_users',
108  'ss_active_users',
109  'ss_images',
110  ], false, __METHOD__ );
111  }
112 
121  static function views() {
122  wfDeprecated( __METHOD__, '1.25' );
123  return 0;
124  }
125 
129  static function edits() {
130  self::load();
131  return self::$row->ss_total_edits;
132  }
133 
137  static function articles() {
138  self::load();
139  return self::$row->ss_good_articles;
140  }
141 
145  static function pages() {
146  self::load();
147  return self::$row->ss_total_pages;
148  }
149 
153  static function users() {
154  self::load();
155  return self::$row->ss_users;
156  }
157 
161  static function activeUsers() {
162  self::load();
163  return self::$row->ss_active_users;
164  }
165 
169  static function images() {
170  self::load();
171  return self::$row->ss_images;
172  }
173 
179  static function numberingroup( $group ) {
181  return $cache->getWithSetCallback(
182  wfMemcKey( 'SiteStats', 'groupcounts', $group ),
183  $cache::TTL_HOUR,
184  function ( $oldValue, &$ttl, array &$setOpts ) use ( $group ) {
185  $dbr = wfGetDB( DB_SLAVE );
186 
187  $setOpts += Database::getCacheSetOptions( $dbr );
188 
189  return $dbr->selectField(
190  'user_groups',
191  'COUNT(*)',
192  [ 'ug_group' => $group ],
193  __METHOD__
194  );
195  },
196  [ 'pcTTL' => 10 ]
197  );
198  }
199 
203  static function jobs() {
204  if ( !isset( self::$jobs ) ) {
205  try{
206  self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
207  } catch ( JobQueueError $e ) {
208  self::$jobs = 0;
209  }
214  if ( self::$jobs == 1 ) {
215  self::$jobs = 0;
216  }
217  }
218  return self::$jobs;
219  }
220 
226  static function pagesInNs( $ns ) {
227  if ( !isset( self::$pageCount[$ns] ) ) {
228  $dbr = wfGetDB( DB_SLAVE );
229  self::$pageCount[$ns] = (int)$dbr->selectField(
230  'page',
231  'COUNT(*)',
232  [ 'page_namespace' => $ns ],
233  __METHOD__
234  );
235  }
236  return self::$pageCount[$ns];
237  }
238 
248  private static function isSane( $row ) {
249  if ( $row === false
250  || $row->ss_total_pages < $row->ss_good_articles
251  || $row->ss_total_edits < $row->ss_total_pages
252  ) {
253  return false;
254  }
255  // Now check for underflow/overflow
256  foreach ( [
257  'ss_total_edits',
258  'ss_good_articles',
259  'ss_total_pages',
260  'ss_users',
261  'ss_images',
262  ] as $member ) {
263  if ( $row->$member > 2000000000 || $row->$member < 0 ) {
264  return false;
265  }
266  }
267  return true;
268  }
269 }
270 
275 
276  // Database connection
277  private $db;
278 
279  // Various stats
280  private $mEdits = null, $mArticles = null, $mPages = null;
281  private $mUsers = null, $mFiles = null;
282 
289  public function __construct( $database = false ) {
290  if ( $database instanceof IDatabase ) {
291  $this->db = $database;
292  } elseif ( $database ) {
293  $this->db = wfGetDB( DB_MASTER );
294  } else {
295  $this->db = wfGetDB( DB_SLAVE, 'vslow' );
296  }
297  }
298 
303  public function edits() {
304  $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
305  $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
306  return $this->mEdits;
307  }
308 
313  public function articles() {
314  global $wgArticleCountMethod;
315 
316  $tables = [ 'page' ];
317  $conds = [
318  'page_namespace' => MWNamespace::getContentNamespaces(),
319  'page_is_redirect' => 0,
320  ];
321 
322  if ( $wgArticleCountMethod == 'link' ) {
323  $tables[] = 'pagelinks';
324  $conds[] = 'pl_from=page_id';
325  } elseif ( $wgArticleCountMethod == 'comma' ) {
326  // To make a correct check for this, we would need, for each page,
327  // to load the text, maybe uncompress it, maybe decode it and then
328  // check if there's one comma.
329  // But one thing we are sure is that if the page is empty, it can't
330  // contain a comma :)
331  $conds[] = 'page_len > 0';
332  }
333 
334  $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
335  $conds, __METHOD__ );
336  return $this->mArticles;
337  }
338 
343  public function pages() {
344  $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
345  return $this->mPages;
346  }
347 
352  public function users() {
353  $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
354  return $this->mUsers;
355  }
356 
361  public function files() {
362  $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
363  return $this->mFiles;
364  }
365 
376  public static function doAllAndCommit( $database, array $options = [] ) {
377  $options += [ 'update' => false, 'activeUsers' => false ];
378 
379  // Grab the object and count everything
380  $counter = new SiteStatsInit( $database );
381 
382  $counter->edits();
383  $counter->articles();
384  $counter->pages();
385  $counter->users();
386  $counter->files();
387 
388  $counter->refresh();
389 
390  // Count active users if need be
391  if ( $options['activeUsers'] ) {
393  }
394  }
395 
399  public function refresh() {
400  $values = [
401  'ss_row_id' => 1,
402  'ss_total_edits' => ( $this->mEdits === null ? $this->edits() : $this->mEdits ),
403  'ss_good_articles' => ( $this->mArticles === null ? $this->articles() : $this->mArticles ),
404  'ss_total_pages' => ( $this->mPages === null ? $this->pages() : $this->mPages ),
405  'ss_users' => ( $this->mUsers === null ? $this->users() : $this->mUsers ),
406  'ss_images' => ( $this->mFiles === null ? $this->files() : $this->mFiles ),
407  ];
408 
409  $dbw = wfGetDB( DB_MASTER );
410  $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ );
411  }
412 }
static getMainWANInstance()
Get the main WAN cache object.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
files()
Count total files.
Definition: SiteStats.php:361
the array() calling protocol came about after MediaWiki 1.4rc1.
static jobs()
Definition: SiteStats.php:203
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
MediaWiki has optional support for a high distributed memory object caching system For general information on but for a larger site with heavy load
Definition: memcached.txt:1
static int $jobs
Definition: SiteStats.php:34
static activeUsers()
Definition: SiteStats.php:161
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static loadAndLazyInit()
Definition: SiteStats.php:67
static doAllAndCommit($database, array $options=[])
Do all updates and commit them.
Definition: SiteStats.php:376
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist & $tables
Definition: hooks.txt:965
pages()
Count total pages.
Definition: SiteStats.php:343
static cacheUpdate($dbw)
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static edits()
Definition: SiteStats.php:129
static bool $loaded
Definition: SiteStats.php:31
static views()
Return the total number of page views.
Definition: SiteStats.php:121
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1004
$wgMiserMode
Disable database-intensive features.
Class for handling updates to the site_stats table.
$cache
Definition: mcc.php:33
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Class designed for counting of stats.
Definition: SiteStats.php:274
const DB_SLAVE
Definition: Defines.php:46
Static accessor class for site_stats and related things.
Definition: SiteStats.php:26
static getCacheSetOptions(IDatabase $db1)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Definition: Database.php:2904
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
static singleton($wiki=false)
static images()
Definition: SiteStats.php:169
static int[] $pageCount
Definition: SiteStats.php:37
refresh()
Refresh site_stats.
Definition: SiteStats.php:399
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
static load($recache=false)
Definition: SiteStats.php:46
static recache()
Definition: SiteStats.php:39
static doLoad($db)
Definition: SiteStats.php:101
edits()
Count the total number of edits.
Definition: SiteStats.php:303
static articles()
Definition: SiteStats.php:137
__construct($database=false)
Constructor.
Definition: SiteStats.php:289
static pages()
Definition: SiteStats.php:145
static pagesInNs($ns)
Definition: SiteStats.php:226
wfMemcKey()
Make a cache key for the local wiki.
const DB_MASTER
Definition: Defines.php:47
users()
Count total users.
Definition: SiteStats.php:352
static numberingroup($group)
Find the number of users in a given user group.
Definition: SiteStats.php:179
articles()
Count pages in article space(s)
Definition: SiteStats.php:313
static users()
Definition: SiteStats.php:153
static isSane($row)
Is the provided row of site stats sane, or should it be regenerated?
Definition: SiteStats.php:248
static bool ResultWrapper $row
Definition: SiteStats.php:28
Basic database interface for live and lazy-loaded DB handles.
Definition: IDatabase.php:35