52 $this->dbr = $database;
53 } elseif ( $database ) {
65 $this->
edits = $this->countTableRows(
'revision' );
66 $this->
edits += $this->countTableRows(
'archive' );
71 private function countTableRows(
string $tableName ) {
72 return (
int)$this->dbr->newSelectQueryBuilder()
73 ->select(
'COUNT(*)' )
75 ->caller( __METHOD__ )->fetchField();
84 $queryBuilder = $this->dbr->newSelectQueryBuilder()
85 ->select(
'COUNT(DISTINCT page_id)' )
88 'page_namespace' => $services->getNamespaceInfo()->getContentNamespaces(),
89 'page_is_redirect' => 0,
93 $queryBuilder->join(
'pagelinks',
null,
'pl_from=page_id' );
96 $this->
articles = $queryBuilder->caller( __METHOD__ )->fetchField();
98 return $this->articles;
106 $this->
pages = $this->countTableRows(
'page' );
116 $this->
users = $this->countTableRows(
'user' );
126 $this->
files = $this->countTableRows(
'image' );
142 $options += [
'update' =>
false,
'activeUsers' => false ];
145 $counter =
new self( $database );
148 $counter->articles();
156 if ( $options[
'activeUsers'] ) {
166 $exists = (bool)$dbw->newSelectQueryBuilder()
168 ->from(
'site_stats' )
169 ->where( [
'ss_row_id' => 1 ] )
170 ->caller( __METHOD__ )->fetchField();
172 $dbw->newInsertQueryBuilder()
173 ->insertInto(
'site_stats' )
176 ->caller( __METHOD__ )->execute();
180 private function getShardedValue( $value, $noShards, $rowId ) {
181 $remainder = $value % $noShards;
182 $quotient = (int)( ( $value - $remainder ) / $noShards );
184 if ( $rowId === 1 ) {
185 return $quotient + $remainder;
196 for ( $i = 1; $i <= $shardCnt; $i++ ) {
198 'ss_total_edits' => $this->getShardedValue( $this->
edits ?? $this->
edits(), $shardCnt, $i ),
199 'ss_good_articles' => $this->getShardedValue( $this->
articles ?? $this->
articles(), $shardCnt, $i ),
200 'ss_total_pages' => $this->getShardedValue( $this->
pages ?? $this->
pages(), $shardCnt, $i ),
201 'ss_users' => $this->getShardedValue( $this->
users ?? $this->
users(), $shardCnt, $i ),
202 'ss_images' => $this->getShardedValue( $this->
files ?? $this->
files(), $shardCnt, $i ),
204 $row = [
'ss_row_id' => $i ] + $set;
206 ->insertInto(
'site_stats' )
208 ->onDuplicateKeyUpdate()
209 ->uniqueIndexFields( [
'ss_row_id' ] )
211 ->caller( __METHOD__ )->execute();
215 'ss_total_edits' => $this->
edits ?? $this->
edits(),
217 'ss_total_pages' => $this->
pages ?? $this->
pages(),
219 'ss_images' => $this->
files ?? $this->
files(),
221 $row = [
'ss_row_id' => 1 ] + $set;
224 ->insertInto(
'site_stats' )
226 ->onDuplicateKeyUpdate()
227 ->uniqueIndexFields( [
'ss_row_id' ] )
229 ->caller( __METHOD__ )->execute();
238 private static function getDB( $index, $groups = [] ) {
240 ->getDBLoadBalancer()
241 ->getConnectionRef( $index, $groups );
248class_alias( SiteStatsInit::class,
'SiteStatsInit' );
A class containing constants representing the names of configuration variables.
const ArticleCountMethod
Name constant for the ArticleCountMethod setting, for use with Config::get()
const MultiShardSiteStats
Name constant for the MultiShardSiteStats setting, for use with Config::get()
Class for handling updates to the site_stats table.
static cacheUpdate(IDatabase $dbw)