3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 use MediaWiki\MediaWikiServices
;
21 use Wikimedia\Assert\Assert
;
22 use Wikimedia\Rdbms\IDatabase
;
25 * Class for handling updates to the site_stats table
27 class SiteStatsUpdate
implements DeferrableUpdate
, MergeableUpdate
{
33 protected $articles = 0;
37 protected $images = 0;
39 /** @var string[] Map of (table column => counter type) */
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'
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;
56 public function merge( MergeableUpdate
$update ) {
57 /** @var SiteStatsUpdate $update */
58 Assert
::parameterType( __CLASS__
, $update, '$update' );
59 '@phan-var SiteStatsUpdate $update';
61 foreach ( self
::$counters as $field ) {
62 $this->$field +
= $update->$field;
67 * @param int[] $deltas Map of (counter type => integer delta)
68 * @return SiteStatsUpdate
69 * @throws UnexpectedValueException
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 ) ) { // T187585
76 throw new UnexpectedValueException( __METHOD__
. ": no field called '$name'" );
80 foreach ( self
::$counters as $field ) {
81 $update->$field = $deltas[$field] ??
0;
87 public function doUpdate() {
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;
100 ( new AutoCommitUpdate(
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 );
120 // Invalidate cache used by parser functions
125 * @param IDatabase $dbw
128 public static function cacheUpdate( IDatabase
$dbw ) {
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.
135 $rcQuery = RecentChange
::getQueryInfo();
136 $activeUsers = $dbr->selectField(
138 'COUNT( DISTINCT ' . $rcQuery['fields']['rc_user_text'] . ' )',
140 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL
), // Exclude external (Wikidata)
141 ActorMigration
::newMigration()->isNotAnon( $rcQuery['fields']['rc_user'] ),
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 ],
158 // Invalid cache used by parser functions