Debian Codegraph @

includes/deferred/SiteStatsUpdate.php

raw symbols SCIP
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Deferred;
22
23 use MediaWiki\MainConfigNames;
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\SiteStats\SiteStats;
26 use UnexpectedValueException;
27 use Wikimedia\Assert\Assert;
28 use Wikimedia\Rdbms\IDatabase;
29 use Wikimedia\Rdbms\RawSQLValue;
30
31 /**
32 * Class for handling updates to the site_stats table
33 */
34 class SiteStatsUpdate implements DeferrableUpdate, MergeableUpdate {
35 /** @var int */
36 protected $edits = 0;
37 /** @var int */
38 protected $pages = 0;
39 /** @var int */
40 protected $articles = 0;
41 /** @var int */
42 protected $users = 0;
43 /** @var int */
44 protected $images = 0;
45
46 private const SHARDS_OFF = 1;
47 public const SHARDS_ON = 10;
48
49 /** @var string[] Map of (table column => counter type) */
50 private const COUNTERS = [
51 'ss_total_edits' => 'edits',
52 'ss_total_pages' => 'pages',
53 'ss_good_articles' => 'articles',
54 'ss_users' => 'users',
55 'ss_images' => 'images'
56 ];
57
58 /**
59 * @deprecated since 1.39 Use SiteStatsUpdate::factory() instead.
60 */
61 public function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
62 $this->edits = $edits;
63 $this->articles = $good;
64 $this->pages = $pages;
65 $this->users = $users;
66 }
67
68 public function merge( MergeableUpdate $update ) {
69 /** @var SiteStatsUpdate $update */
70 Assert::parameterType( __CLASS__, $update, '$update' );
71 '@phan-var SiteStatsUpdate $update';
72
73 foreach ( self::COUNTERS as $field ) {
74 $this->$field += $update->$field;
75 }
76 }
77
78 /**
79 * @param int[] $deltas Map of (counter type => integer delta) e.g.
80 * ```
81 * SiteStatsUpdate::factory( [
82 * 'edits' => 10,
83 * 'articles' => 2,
84 * 'pages' => 7,
85 * 'users' => 5,
86 * ] );
87 * ```
88 * @return SiteStatsUpdate
89 * @throws UnexpectedValueException
90 */
91 public static function factory( array $deltas ) {
92 $update = new self( 0, 0, 0 );
93
94 foreach ( $deltas as $name => $unused ) {
95 if ( !in_array( $name, self::COUNTERS ) ) { // T187585
96 throw new UnexpectedValueException( __METHOD__ . ": no field called '$name'" );
97 }
98 }
99
100 foreach ( self::COUNTERS as $field ) {
101 $update->$field = $deltas[$field] ?? 0;
102 }
103
104 return $update;
105 }
106
107 public function doUpdate() {
108 $services = MediaWikiServices::getInstance();
109 $metric = $services->getStatsFactory()->getCounter( 'site_stats_total' );
110 $shards = $services->getMainConfig()->get( MainConfigNames::MultiShardSiteStats ) ?
111 self::SHARDS_ON : self::SHARDS_OFF;
112
113 $deltaByType = [];
114 foreach ( self::COUNTERS as $type ) {
115 $delta = $this->$type;
116 if ( $delta !== 0 ) {
117 $metric->setLabel( 'engagement', $type )
118 ->copyToStatsdAt( "site.$type" )
119 ->incrementBy( $delta );
120 }
121 $deltaByType[$type] = $delta;
122 }
123
124 ( new AutoCommitUpdate(
125 $services->getConnectionProvider()->getPrimaryDatabase(),
126 __METHOD__,
127 static function ( IDatabase $dbw, $fname ) use ( $deltaByType, $shards ) {
128 $set = [];
129 $initValues = [];
130 if ( $shards > 1 ) {
131 $shard = mt_rand( 1, $shards );
132 } else {
133 $shard = 1;
134 }
135
136 $hasNegativeDelta = false;
137 foreach ( self::COUNTERS as $field => $type ) {
138 $delta = (int)$deltaByType[$type];
139 $initValues[$field] = $delta;
140 if ( $delta > 0 ) {
141 $set[$field] = new RawSQLValue( $dbw->addIdentifierQuotes( $field ) . '+' . abs( $delta ) );
142 } elseif ( $delta < 0 ) {
143 $hasNegativeDelta = true;
144 $set[$field] = new RawSQLValue( $dbw->buildGreatest(
145 [ 'new' => $dbw->addIdentifierQuotes( $field ) ],
146 abs( $delta )
147 ) . '-' . abs( $delta ) );
148 }
149 }
150
151 if ( $set ) {
152 if ( $hasNegativeDelta ) {
153 $dbw->newUpdateQueryBuilder()
154 ->update( 'site_stats' )
155 ->set( $set )
156 ->where( [ 'ss_row_id' => $shard ] )
157 ->caller( $fname )->execute();
158 } else {
159 $dbw->newInsertQueryBuilder()
160 ->insertInto( 'site_stats' )
161 ->row( array_merge( [ 'ss_row_id' => $shard ], $initValues ) )
162 ->onDuplicateKeyUpdate()
163 ->uniqueIndexFields( [ 'ss_row_id' ] )
164 ->set( $set )
165 ->caller( $fname )->execute();
166 }
167 }
168 }
169 ) )->doUpdate();
170
171 // Invalidate cache used by parser functions
172 SiteStats::unload();
173 }
174
175 /**
176 * @param IDatabase $dbw
177 * @return bool|mixed
178 */
179 public static function cacheUpdate( IDatabase $dbw ) {
180 $services = MediaWikiServices::getInstance();
181 $config = $services->getMainConfig();
182
183 $dbr = $services->getConnectionProvider()->getReplicaDatabase( false, 'vslow' );
184 # Get non-bot users than did some recent action other than making accounts.
185 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
186 $activeUsers = $dbr->newSelectQueryBuilder()
187 ->select( 'COUNT(DISTINCT rc_actor)' )
188 ->from( 'recentchanges' )
189 ->join( 'actor', 'actor', 'actor_id=rc_actor' )
190 ->where( [
191 $dbr->expr( 'rc_type', '!=', RC_EXTERNAL ), // Exclude external (Wikidata)
192 $dbr->expr( 'actor_user', '!=', null ),
193 $dbr->expr( 'rc_bot', '=', 0 ),
194 $dbr->expr( 'rc_log_type', '!=', 'newusers' )->or( 'rc_log_type', '=', null ),
195 $dbr->expr( 'rc_timestamp', '>=',
196 $dbr->timestamp( time() - $config->get( MainConfigNames::ActiveUserDays ) * 24 * 3600 ) )
197 ] )
198 ->caller( __METHOD__ )
199 ->fetchField();
200 $dbw->newUpdateQueryBuilder()
201 ->update( 'site_stats' )
202 ->set( [ 'ss_active_users' => intval( $activeUsers ) ] )
203 ->where( [ 'ss_row_id' => 1 ] )
204 ->caller( __METHOD__ )->execute();
205
206 // Invalid cache used by parser functions
207 SiteStats::unload();
208
209 return $activeUsers;
210 }
211 }
212
213 /** @deprecated class alias since 1.42 */
214 class_alias( SiteStatsUpdate::class, 'SiteStatsUpdate' );