3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 use Wikimedia\Rdbms\IDatabase
;
23 * Class designed for counting of stats.
27 // Database connection
31 private $mEdits = null, $mArticles = null, $mPages = null;
32 private $mUsers = null, $mFiles = null;
35 * @param bool|IDatabase $database
36 * - bool: Whether to use the master DB
37 * - IDatabase: Database connection to use
39 public function __construct( $database = false ) {
40 if ( $database instanceof IDatabase
) {
41 $this->db
= $database;
42 } elseif ( $database ) {
43 $this->db
= wfGetDB( DB_MASTER
);
45 $this->db
= wfGetDB( DB_REPLICA
, 'vslow' );
50 * Count the total number of edits
53 public function edits() {
54 $this->mEdits
= $this->db
->selectField( 'revision', 'COUNT(*)', '', __METHOD__
);
55 $this->mEdits +
= $this->db
->selectField( 'archive', 'COUNT(*)', '', __METHOD__
);
60 * Count pages in article space(s)
63 public function articles() {
64 global $wgArticleCountMethod;
68 'page_namespace' => MWNamespace
::getContentNamespaces(),
69 'page_is_redirect' => 0,
72 if ( $wgArticleCountMethod == 'link' ) {
73 $tables[] = 'pagelinks';
74 $conds[] = 'pl_from=page_id';
75 } elseif ( $wgArticleCountMethod == 'comma' ) {
76 // To make a correct check for this, we would need, for each page,
77 // to load the text, maybe uncompress it, maybe decode it and then
78 // check if there's one comma.
79 // But one thing we are sure is that if the page is empty, it can't
81 $conds[] = 'page_len > 0';
84 $this->mArticles
= $this->db
->selectField( $tables, 'COUNT(DISTINCT page_id)',
86 return $this->mArticles
;
93 public function pages() {
94 $this->mPages
= $this->db
->selectField( 'page', 'COUNT(*)', '', __METHOD__
);
102 public function users() {
103 $this->mUsers
= $this->db
->selectField( 'user', 'COUNT(*)', '', __METHOD__
);
104 return $this->mUsers
;
111 public function files() {
112 $this->mFiles
= $this->db
->selectField( 'image', 'COUNT(*)', '', __METHOD__
);
113 return $this->mFiles
;
117 * Do all updates and commit them. More or less a replacement
118 * for the original initStats, but without output.
120 * @param IDatabase|bool $database
121 * - bool: Whether to use the master DB
122 * - IDatabase: Database connection to use
123 * @param array $options Array of options, may contain the following values
124 * - activeUsers bool: Whether to update the number of active users (default: false)
126 public static function doAllAndCommit( $database, array $options = [] ) {
127 $options +
= [ 'update' => false, 'activeUsers' => false ];
129 // Grab the object and count everything
130 $counter = new SiteStatsInit( $database );
133 $counter->articles();
140 // Count active users if need be
141 if ( $options['activeUsers'] ) {
142 SiteStatsUpdate
::cacheUpdate( wfGetDB( DB_MASTER
) );
147 * Insert a dummy row with all zeroes if no row is present
149 public static function doPlaceholderInit() {
150 $dbw = wfGetDB( DB_MASTER
);
151 $exists = $dbw->selectField( 'site_stats', '1', [ 'ss_row_id' => 1 ], __METHOD__
);
152 if ( $exists === false ) {
155 [ 'ss_row_id' => 1 ] +
array_fill_keys( SiteStats
::selectFields(), 0 ),
165 public function refresh() {
168 'ss_total_edits' => ( $this->mEdits
=== null ?
$this->edits() : $this->mEdits
),
169 'ss_good_articles' => ( $this->mArticles
=== null ?
$this->articles() : $this->mArticles
),
170 'ss_total_pages' => ( $this->mPages
=== null ?
$this->pages() : $this->mPages
),
171 'ss_users' => ( $this->mUsers
=== null ?
$this->users() : $this->mUsers
),
172 'ss_images' => ( $this->mFiles
=== null ?
$this->files() : $this->mFiles
),
175 $dbw = wfGetDB( DB_MASTER
);
176 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__
);