MediaWiki  REL1_31
SiteStatsInit.php
Go to the documentation of this file.
1 <?php
22 
27  /* @var IDatabase */
28  private $dbr;
30  private $edits;
32  private $articles;
34  private $pages;
36  private $users;
38  private $files;
39 
45  public function __construct( $database = false ) {
46  if ( $database instanceof IDatabase ) {
47  $this->dbr = $database;
48  } elseif ( $database ) {
49  $this->dbr = self::getDB( DB_MASTER );
50  } else {
51  $this->dbr = self::getDB( DB_REPLICA, 'vslow' );
52  }
53  }
54 
59  public function edits() {
60  $this->edits = $this->dbr->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
61  $this->edits += $this->dbr->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
62 
63  return $this->edits;
64  }
65 
70  public function articles() {
71  $config = MediaWikiServices::getInstance()->getMainConfig();
72 
73  $tables = [ 'page' ];
74  $conds = [
75  'page_namespace' => MWNamespace::getContentNamespaces(),
76  'page_is_redirect' => 0,
77  ];
78 
79  if ( $config->get( 'ArticleCountMethod' ) == 'link' ) {
80  $tables[] = 'pagelinks';
81  $conds[] = 'pl_from=page_id';
82  }
83 
84  $this->articles = $this->dbr->selectField(
85  $tables,
86  'COUNT(DISTINCT page_id)',
87  $conds,
88  __METHOD__
89  );
90 
91  return $this->articles;
92  }
93 
98  public function pages() {
99  $this->pages = $this->dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
100 
101  return $this->pages;
102  }
103 
108  public function users() {
109  $this->users = $this->dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
110 
111  return $this->users;
112  }
113 
118  public function files() {
119  $this->files = $this->dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
120 
121  return $this->files;
122  }
123 
134  public static function doAllAndCommit( $database, array $options = [] ) {
135  $options += [ 'update' => false, 'activeUsers' => false ];
136 
137  // Grab the object and count everything
138  $counter = new self( $database );
139 
140  $counter->edits();
141  $counter->articles();
142  $counter->pages();
143  $counter->users();
144  $counter->files();
145 
146  $counter->refresh();
147 
148  // Count active users if need be
149  if ( $options['activeUsers'] ) {
150  SiteStatsUpdate::cacheUpdate( self::getDB( DB_MASTER ) );
151  }
152  }
153 
157  public static function doPlaceholderInit() {
158  $dbw = self::getDB( DB_MASTER );
159  $exists = $dbw->selectField( 'site_stats', '1', [ 'ss_row_id' => 1 ], __METHOD__ );
160  if ( $exists === false ) {
161  $dbw->insert(
162  'site_stats',
163  [ 'ss_row_id' => 1 ] + array_fill_keys( SiteStats::selectFields(), 0 ),
164  __METHOD__,
165  [ 'IGNORE' ]
166  );
167  }
168  }
169 
173  public function refresh() {
174  $values = [
175  'ss_row_id' => 1,
176  'ss_total_edits' => $this->edits === null ? $this->edits() : $this->edits,
177  'ss_good_articles' => $this->articles === null ? $this->articles() : $this->articles,
178  'ss_total_pages' => $this->pages === null ? $this->pages() : $this->pages,
179  'ss_users' => $this->users === null ? $this->users() : $this->users,
180  'ss_images' => $this->files === null ? $this->files() : $this->files,
181  ];
182 
183  self::getDB( DB_MASTER )->upsert(
184  'site_stats',
185  $values,
186  [ 'ss_row_id' ],
187  $values,
188  __METHOD__
189  );
190  }
191 
197  private static function getDB( $index, $groups = [] ) {
198  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
199 
200  return $lb->getConnection( $index, $groups );
201  }
202 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
SiteStatsInit\doAllAndCommit
static doAllAndCommit( $database, array $options=[])
Do all updates and commit them.
Definition: SiteStatsInit.php:134
array
the array() calling protocol came about after MediaWiki 1.4rc1.
SiteStatsInit\$files
int $files
Definition: SiteStatsInit.php:38
SiteStatsInit\articles
articles()
Count pages in article space(s)
Definition: SiteStatsInit.php:70
SiteStatsInit\pages
pages()
Count total pages.
Definition: SiteStatsInit.php:98
SiteStatsInit\getDB
static getDB( $index, $groups=[])
Definition: SiteStatsInit.php:197
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: MWNamespace.php:377
SiteStatsInit
Class designed for counting of stats.
Definition: SiteStatsInit.php:26
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
SiteStatsInit\files
files()
Count total files.
Definition: SiteStatsInit.php:118
SiteStatsInit\$dbr
$dbr
Definition: SiteStatsInit.php:28
SiteStats\selectFields
static selectFields()
Definition: SiteStats.php:224
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:29
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:2001
SiteStatsInit\users
users()
Count total users.
Definition: SiteStatsInit.php:108
SiteStatsInit\$articles
int $articles
Definition: SiteStatsInit.php:32
SiteStatsInit\$pages
int $pages
Definition: SiteStatsInit.php:34
SiteStatsUpdate\cacheUpdate
static cacheUpdate(IDatabase $dbw)
Definition: SiteStatsUpdate.php:150
SiteStatsInit\__construct
__construct( $database=false)
Definition: SiteStatsInit.php:45
$tables
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition: hooks.txt:1015
SiteStatsInit\$users
int $users
Definition: SiteStatsInit.php:36
SiteStatsInit\edits
edits()
Count the total number of edits.
Definition: SiteStatsInit.php:59
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
SiteStatsInit\doPlaceholderInit
static doPlaceholderInit()
Insert a dummy row with all zeroes if no row is present.
Definition: SiteStatsInit.php:157
SiteStatsInit\refresh
refresh()
Refresh site_stats.
Definition: SiteStatsInit.php:173
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
SiteStatsInit\$edits
int $edits
Definition: SiteStatsInit.php:30