MediaWiki REL1_39
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->dbr->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
63 $this->edits += $this->dbr->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
64
65 return $this->edits;
66 }
67
72 public function articles() {
73 $services = MediaWikiServices::getInstance();
74
75 $tables = [ 'page' ];
76 $conds = [
77 'page_namespace' => $services->getNamespaceInfo()->getContentNamespaces(),
78 'page_is_redirect' => 0,
79 ];
80
81 if ( $services->getMainConfig()->get( MainConfigNames::ArticleCountMethod ) == 'link' ) {
82 $tables[] = 'pagelinks';
83 $conds[] = 'pl_from=page_id';
84 }
85
86 $this->articles = $this->dbr->selectField(
87 $tables,
88 'COUNT(DISTINCT page_id)',
89 $conds,
90 __METHOD__
91 );
92
93 return $this->articles;
94 }
95
100 public function pages() {
101 $this->pages = $this->dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
102
103 return $this->pages;
104 }
105
110 public function users() {
111 $this->users = $this->dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
112
113 return $this->users;
114 }
115
120 public function files() {
121 $this->files = $this->dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
122
123 return $this->files;
124 }
125
136 public static function doAllAndCommit( $database, array $options = [] ) {
137 $options += [ 'update' => false, 'activeUsers' => false ];
138
139 // Grab the object and count everything
140 $counter = new self( $database );
141
142 $counter->edits();
143 $counter->articles();
144 $counter->pages();
145 $counter->users();
146 $counter->files();
147
148 $counter->refresh();
149
150 // Count active users if need be
151 if ( $options['activeUsers'] ) {
152 SiteStatsUpdate::cacheUpdate( self::getDB( DB_PRIMARY ) );
153 }
154 }
155
159 public static function doPlaceholderInit() {
160 $dbw = self::getDB( DB_PRIMARY );
161 $exists = (bool)$dbw->selectField( 'site_stats', '1', [ 'ss_row_id' => 1 ], __METHOD__ );
162 if ( !$exists ) {
163 $dbw->insert(
164 'site_stats',
165 [ 'ss_row_id' => 1 ] + array_fill_keys( SiteStats::selectFields(), 0 ),
166 __METHOD__,
167 [ 'IGNORE' ]
168 );
169 }
170 }
171
172 private function getShardedValue( $value, $noShards, $rowId ) {
173 $remainder = $value % $noShards;
174 $quotient = (int)( ( $value - $remainder ) / $noShards );
175 // Add the reminder to the first row
176 if ( $rowId === 1 ) {
177 return $quotient + $remainder;
178 }
179 return $quotient;
180 }
181
185 public function refresh() {
186 if ( MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MultiShardSiteStats ) ) {
187 $shardCnt = SiteStatsUpdate::SHARDS_ON;
188 for ( $i = 1; $i <= $shardCnt; $i++ ) {
189 $set = [
190 'ss_total_edits' => $this->getShardedValue( $this->edits ?? $this->edits(), $shardCnt, $i ),
191 'ss_good_articles' => $this->getShardedValue( $this->articles ?? $this->articles(), $shardCnt, $i ),
192 'ss_total_pages' => $this->getShardedValue( $this->pages ?? $this->pages(), $shardCnt, $i ),
193 'ss_users' => $this->getShardedValue( $this->users ?? $this->users(), $shardCnt, $i ),
194 'ss_images' => $this->getShardedValue( $this->files ?? $this->files(), $shardCnt, $i ),
195 ];
196 $row = [ 'ss_row_id' => $i ] + $set;
197
198 self::getDB( DB_PRIMARY )->upsert(
199 'site_stats',
200 $row,
201 'ss_row_id',
202 $set,
203 __METHOD__
204 );
205 }
206 } else {
207 $set = [
208 'ss_total_edits' => $this->edits ?? $this->edits(),
209 'ss_good_articles' => $this->articles ?? $this->articles(),
210 'ss_total_pages' => $this->pages ?? $this->pages(),
211 'ss_users' => $this->users ?? $this->users(),
212 'ss_images' => $this->files ?? $this->files(),
213 ];
214 $row = [ 'ss_row_id' => 1 ] + $set;
215
216 self::getDB( DB_PRIMARY )->upsert(
217 'site_stats',
218 $row,
219 'ss_row_id',
220 $set,
221 __METHOD__
222 );
223 }
224 }
225
231 private static function getDB( $index, $groups = [] ) {
232 return MediaWikiServices::getInstance()
233 ->getDBLoadBalancer()
234 ->getConnectionRef( $index, $groups );
235 }
236}
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:39
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28