MediaWiki  master
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'] ) {
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 
175  public function refresh() {
176  $set = [
177  'ss_total_edits' => $this->edits ?? $this->edits(),
178  'ss_good_articles' => $this->articles ?? $this->articles(),
179  'ss_total_pages' => $this->pages ?? $this->pages(),
180  'ss_users' => $this->users ?? $this->users(),
181  'ss_images' => $this->files ?? $this->files(),
182  ];
183  $row = [ 'ss_row_id' => 1 ] + $set;
184 
185  self::getDB( DB_PRIMARY )->upsert(
186  'site_stats',
187  $row,
188  'ss_row_id',
189  $set,
190  __METHOD__
191  );
192  }
193 
199  private static function getDB( $index, $groups = [] ) {
200  return MediaWikiServices::getInstance()
201  ->getDBLoadBalancer()
202  ->getConnectionRef( $index, $groups );
203  }
204 }
getDB()
A class containing constants representing the names of configuration variables.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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.
IDatabase $dbr
static doPlaceholderInit()
Insert a dummy row with all zeroes if no row is present.
files()
Count total files.
static getDB( $index, $groups=[])
refresh()
Refresh site_stats.
users()
Count total users.
__construct( $database=false)
static cacheUpdate(IDatabase $dbw)
static selectFields()
Definition: SiteStats.php:227
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39
const DB_REPLICA
Definition: defines.php:25
const DB_PRIMARY
Definition: defines.php:27