21 use Wikimedia\Assert\Assert;
41 'ss_total_edits' =>
'edits',
42 'ss_total_pages' =>
'pages',
43 'ss_good_articles' =>
'articles',
44 'ss_users' =>
'users',
45 'ss_images' =>
'images'
51 $this->articles = $good;
58 Assert::parameterType( __CLASS__, $update,
'$update' );
59 '@phan-var SiteStatsUpdate $update';
61 foreach ( self::$counters as $field ) {
62 $this->$field += $update->$field;
71 public static function factory( array $deltas ) {
72 $update =
new self( 0, 0, 0 );
74 foreach ( $deltas as $name => $unused ) {
75 if ( !in_array( $name, self::$counters ) ) {
76 throw new UnexpectedValueException( __METHOD__ .
": no field called '$name'" );
80 foreach ( self::$counters as $field ) {
81 $update->$field = $deltas[$field] ?? 0;
88 $services = MediaWikiServices::getInstance();
89 $stats = $services->getStatsdDataFactory();
92 foreach ( self::$counters as
$type ) {
93 $delta = $this->$type;
95 $stats->updateCount(
"site.$type", $delta );
97 $deltaByType[
$type] = $delta;
101 $services->getDBLoadBalancer()->getConnectionRef(
DB_MASTER ),
103 function (
IDatabase $dbw, $fname ) use ( $deltaByType ) {
105 foreach ( self::$counters as $column =>
$type ) {
106 $delta = (int)$deltaByType[
$type];
108 $set[] =
"$column=$column+" . abs( $delta );
109 } elseif ( $delta < 0 ) {
110 $set[] =
"$column=$column-" . abs( $delta );
115 $dbw->
update(
'site_stats', $set, [
'ss_row_id' => 1 ], $fname );
129 $services = MediaWikiServices::getInstance();
130 $config = $services->getMainConfig();
132 $dbr = $services->getDBLoadBalancer()->getConnectionRef(
DB_REPLICA,
'vslow' );
133 # Get non-bot users than did some recent action other than making accounts.
134 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
136 $activeUsers =
$dbr->selectField(
138 'COUNT( DISTINCT ' . $rcQuery[
'fields'][
'rc_user_text'] .
' )',
143 'rc_log_type != ' .
$dbr->addQuotes(
'newusers' ) .
' OR rc_log_type IS NULL',
144 'rc_timestamp >= ' .
$dbr->addQuotes(
145 $dbr->timestamp( time() - $config->get(
'ActiveUserDays' ) * 24 * 3600 ) ),
153 [
'ss_active_users' => intval( $activeUsers ) ],
154 [
'ss_row_id' => 1 ],