MediaWiki  master
SiteStatsUpdate.php
Go to the documentation of this file.
1 <?php
23 
29  protected $edits = 0;
31  protected $pages = 0;
33  protected $articles = 0;
35  protected $users = 0;
37  protected $images = 0;
38 
40  private static $counters = [
41  'ss_total_edits' => 'edits',
42  'ss_total_pages' => 'pages',
43  'ss_good_articles' => 'articles',
44  'ss_users' => 'users',
45  'ss_images' => 'images'
46  ];
47 
48  // @todo deprecate this constructor
49  function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
50  $this->edits = $edits;
51  $this->articles = $good;
52  $this->pages = $pages;
53  $this->users = $users;
54  }
55 
56  public function merge( MergeableUpdate $update ) {
58  Assert::parameterType( __CLASS__, $update, '$update' );
59 
60  foreach ( self::$counters as $field ) {
61  $this->$field += $update->$field;
62  }
63  }
64 
70  public static function factory( array $deltas ) {
71  $update = new self( 0, 0, 0 );
72 
73  foreach ( $deltas as $name => $unused ) {
74  if ( !in_array( $name, self::$counters ) ) { // T187585
75  throw new UnexpectedValueException( __METHOD__ . ": no field called '$name'" );
76  }
77  }
78 
79  foreach ( self::$counters as $field ) {
80  $update->$field = $deltas[$field] ?? 0;
81  }
82 
83  return $update;
84  }
85 
86  public function doUpdate() {
87  $services = MediaWikiServices::getInstance();
88  $stats = $services->getStatsdDataFactory();
89 
90  $deltaByType = [];
91  foreach ( self::$counters as $type ) {
92  $delta = $this->$type;
93  if ( $delta !== 0 ) {
94  $stats->updateCount( "site.$type", $delta );
95  }
96  $deltaByType[$type] = $delta;
97  }
98 
99  ( new AutoCommitUpdate(
100  $services->getDBLoadBalancer()->getConnectionRef( DB_MASTER ),
101  __METHOD__,
102  function ( IDatabase $dbw, $fname ) use ( $deltaByType ) {
103  $set = [];
104  foreach ( self::$counters as $column => $type ) {
105  $delta = (int)$deltaByType[$type];
106  if ( $delta > 0 ) {
107  $set[] = "$column=$column+" . abs( $delta );
108  } elseif ( $delta < 0 ) {
109  $set[] = "$column=$column-" . abs( $delta );
110  }
111  }
112 
113  if ( $set ) {
114  $dbw->update( 'site_stats', $set, [ 'ss_row_id' => 1 ], $fname );
115  }
116  }
117  ) )->doUpdate();
118 
119  // Invalidate cache used by parser functions
121  }
122 
127  public static function cacheUpdate( IDatabase $dbw ) {
128  $services = MediaWikiServices::getInstance();
129  $config = $services->getMainConfig();
130 
131  $dbr = $services->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, 'vslow' );
132  # Get non-bot users than did some recent action other than making accounts.
133  # If account creation is included, the number gets inflated ~20+ fold on enwiki.
134  $rcQuery = RecentChange::getQueryInfo();
135  $activeUsers = $dbr->selectField(
136  $rcQuery['tables'],
137  'COUNT( DISTINCT ' . $rcQuery['fields']['rc_user_text'] . ' )',
138  [
139  'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Exclude external (Wikidata)
140  ActorMigration::newMigration()->isNotAnon( $rcQuery['fields']['rc_user'] ),
141  'rc_bot' => 0,
142  'rc_log_type != ' . $dbr->addQuotes( 'newusers' ) . ' OR rc_log_type IS NULL',
143  'rc_timestamp >= ' . $dbr->addQuotes(
144  $dbr->timestamp( time() - $config->get( 'ActiveUserDays' ) * 24 * 3600 ) ),
145  ],
146  __METHOD__,
147  [],
148  $rcQuery['joins']
149  );
150  $dbw->update(
151  'site_stats',
152  [ 'ss_active_users' => intval( $activeUsers ) ],
153  [ 'ss_row_id' => 1 ],
154  __METHOD__
155  );
156 
157  // Invalid cache used by parser functions
159 
160  return $activeUsers;
161  }
162 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
merge(MergeableUpdate $update)
Merge this update with $update.
Interface that deferrable updates can implement to signal that updates can be combined.
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
const DB_MASTER
Definition: defines.php:26
static unload()
Trigger a reload next time a field is accessed.
Definition: SiteStats.php:38
static newMigration()
Static constructor.
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:2211
static factory(array $deltas)
static string [] $counters
Map of (table column => counter type)
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
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
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:131
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
The ContentHandler facility adds support for arbitrary content types on wiki pages
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
static cacheUpdate(IDatabase $dbw)
doUpdate()
Perform the actual work.
static string [] __construct( $views, $edits, $good, $pages=0, $users=0)
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object...
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:277
const RC_EXTERNAL
Definition: Defines.php:125
const DB_REPLICA
Definition: defines.php:25
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.