MediaWiki 1.40.4
SiteStatsInit.php
Go to the documentation of this file.
1<?php
24
30 private $dbr;
32 private $edits;
34 private $articles;
36 private $pages;
38 private $users;
40 private $files;
41
47 public function __construct( $database = false ) {
48 if ( $database instanceof IDatabase ) {
49 $this->dbr = $database;
50 } elseif ( $database ) {
51 $this->dbr = self::getDB( DB_PRIMARY );
52 } else {
53 $this->dbr = self::getDB( DB_REPLICA, 'vslow' );
54 }
55 }
56
61 public function edits() {
62 $this->edits = $this->countTableRows( 'revision' );
63 $this->edits += $this->countTableRows( 'archive' );
64
65 return $this->edits;
66 }
67
68 private function countTableRows( string $tableName ) {
69 return (int)$this->dbr->newSelectQueryBuilder()
70 ->select( 'COUNT(*)' )
71 ->from( $tableName )
72 ->caller( __METHOD__ )->fetchField();
73 }
74
79 public function articles() {
80 $services = MediaWikiServices::getInstance();
81 $queryBuilder = $this->dbr->newSelectQueryBuilder()
82 ->select( 'COUNT(DISTINCT page_id)' )
83 ->from( 'page' )
84 ->where( [
85 'page_namespace' => $services->getNamespaceInfo()->getContentNamespaces(),
86 'page_is_redirect' => 0,
87 ] );
88
89 if ( $services->getMainConfig()->get( MainConfigNames::ArticleCountMethod ) == 'link' ) {
90 $queryBuilder->join( 'pagelinks', null, 'pl_from=page_id' );
91 }
92
93 $this->articles = $queryBuilder->caller( __METHOD__ )->fetchField();
94
95 return $this->articles;
96 }
97
102 public function pages() {
103 $this->pages = $this->countTableRows( 'page' );
104
105 return $this->pages;
106 }
107
112 public function users() {
113 $this->users = $this->countTableRows( 'user' );
114
115 return $this->users;
116 }
117
122 public function files() {
123 $this->files = $this->countTableRows( 'image' );
124
125 return $this->files;
126 }
127
138 public static function doAllAndCommit( $database, array $options = [] ) {
139 $options += [ 'update' => false, 'activeUsers' => false ];
140
141 // Grab the object and count everything
142 $counter = new self( $database );
143
144 $counter->edits();
145 $counter->articles();
146 $counter->pages();
147 $counter->users();
148 $counter->files();
149
150 $counter->refresh();
151
152 // Count active users if need be
153 if ( $options['activeUsers'] ) {
154 SiteStatsUpdate::cacheUpdate( self::getDB( DB_PRIMARY ) );
155 }
156 }
157
161 public static function doPlaceholderInit() {
162 $dbw = self::getDB( DB_PRIMARY );
163 $exists = (bool)$dbw->selectField( 'site_stats', '1', [ 'ss_row_id' => 1 ], __METHOD__ );
164 if ( !$exists ) {
165 $dbw->insert(
166 'site_stats',
167 [ 'ss_row_id' => 1 ] + array_fill_keys( SiteStats::selectFields(), 0 ),
168 __METHOD__,
169 [ 'IGNORE' ]
170 );
171 }
172 }
173
174 private function getShardedValue( $value, $noShards, $rowId ) {
175 $remainder = $value % $noShards;
176 $quotient = (int)( ( $value - $remainder ) / $noShards );
177 // Add the reminder to the first row
178 if ( $rowId === 1 ) {
179 return $quotient + $remainder;
180 }
181 return $quotient;
182 }
183
187 public function refresh() {
188 if ( MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MultiShardSiteStats ) ) {
189 $shardCnt = SiteStatsUpdate::SHARDS_ON;
190 for ( $i = 1; $i <= $shardCnt; $i++ ) {
191 $set = [
192 'ss_total_edits' => $this->getShardedValue( $this->edits ?? $this->edits(), $shardCnt, $i ),
193 'ss_good_articles' => $this->getShardedValue( $this->articles ?? $this->articles(), $shardCnt, $i ),
194 'ss_total_pages' => $this->getShardedValue( $this->pages ?? $this->pages(), $shardCnt, $i ),
195 'ss_users' => $this->getShardedValue( $this->users ?? $this->users(), $shardCnt, $i ),
196 'ss_images' => $this->getShardedValue( $this->files ?? $this->files(), $shardCnt, $i ),
197 ];
198 $row = [ 'ss_row_id' => $i ] + $set;
199
200 self::getDB( DB_PRIMARY )->upsert(
201 'site_stats',
202 $row,
203 'ss_row_id',
204 $set,
205 __METHOD__
206 );
207 }
208 } else {
209 $set = [
210 'ss_total_edits' => $this->edits ?? $this->edits(),
211 'ss_good_articles' => $this->articles ?? $this->articles(),
212 'ss_total_pages' => $this->pages ?? $this->pages(),
213 'ss_users' => $this->users ?? $this->users(),
214 'ss_images' => $this->files ?? $this->files(),
215 ];
216 $row = [ 'ss_row_id' => 1 ] + $set;
217
218 self::getDB( DB_PRIMARY )->upsert(
219 'site_stats',
220 $row,
221 'ss_row_id',
222 $set,
223 __METHOD__
224 );
225 }
226 }
227
233 private static function getDB( $index, $groups = [] ) {
234 return MediaWikiServices::getInstance()
235 ->getDBLoadBalancer()
236 ->getConnectionRef( $index, $groups );
237 }
238}
getDB()
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Class designed for counting of stats.
edits()
Count the total number of edits.
pages()
Count total pages.
articles()
Count pages in article space(s)
static doAllAndCommit( $database, array $options=[])
Do all updates and commit them.
static doPlaceholderInit()
Insert a dummy row with all zeroes if no row is present.
files()
Count total files.
refresh()
Refresh site_stats.
users()
Count total users.
__construct( $database=false)
static selectFields()
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28