Salvage site_stats row with negative values in miser mode
[lhc/web/wiklou.git] / includes / SiteStatsInit.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20 use Wikimedia\Rdbms\IDatabase;
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Class designed for counting of stats.
25 */
26 class SiteStatsInit {
27 /* @var IDatabase */
28 private $dbr;
29 /** @var int */
30 private $edits;
31 /** @var int */
32 private $articles;
33 /** @var int */
34 private $pages;
35 /** @var int */
36 private $users;
37 /** @var int */
38 private $files;
39
40 /**
41 * @param bool|IDatabase $database
42 * - bool: Whether to use the master DB
43 * - IDatabase: Database connection to use
44 */
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
55 /**
56 * Count the total number of edits
57 * @return int
58 */
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
66 /**
67 * Count pages in article space(s)
68 * @return int
69 */
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 } elseif ( $config->get( 'ArticleCountMethod' ) == 'comma' ) {
83 // To make a correct check for this, we would need, for each page,
84 // to load the text, maybe uncompress it, maybe decode it and then
85 // check if there's one comma.
86 // But one thing we are sure is that if the page is empty, it can't
87 // contain a comma :)
88 $conds[] = 'page_len > 0';
89 }
90
91 $this->articles = $this->dbr->selectField(
92 $tables,
93 'COUNT(DISTINCT page_id)',
94 $conds,
95 __METHOD__
96 );
97
98 return $this->articles;
99 }
100
101 /**
102 * Count total pages
103 * @return int
104 */
105 public function pages() {
106 $this->pages = $this->dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
107
108 return $this->pages;
109 }
110
111 /**
112 * Count total users
113 * @return int
114 */
115 public function users() {
116 $this->users = $this->dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
117
118 return $this->users;
119 }
120
121 /**
122 * Count total files
123 * @return int
124 */
125 public function files() {
126 $this->files = $this->dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
127
128 return $this->files;
129 }
130
131 /**
132 * Do all updates and commit them. More or less a replacement
133 * for the original initStats, but without output.
134 *
135 * @param IDatabase|bool $database
136 * - bool: Whether to use the master DB
137 * - IDatabase: Database connection to use
138 * @param array $options Array of options, may contain the following values
139 * - activeUsers bool: Whether to update the number of active users (default: false)
140 */
141 public static function doAllAndCommit( $database, array $options = [] ) {
142 $options += [ 'update' => false, 'activeUsers' => false ];
143
144 // Grab the object and count everything
145 $counter = new self( $database );
146
147 $counter->edits();
148 $counter->articles();
149 $counter->pages();
150 $counter->users();
151 $counter->files();
152
153 $counter->refresh();
154
155 // Count active users if need be
156 if ( $options['activeUsers'] ) {
157 SiteStatsUpdate::cacheUpdate( self::getDB( DB_MASTER ) );
158 }
159 }
160
161 /**
162 * Insert a dummy row with all zeroes if no row is present
163 */
164 public static function doPlaceholderInit() {
165 $dbw = self::getDB( DB_MASTER );
166 $exists = $dbw->selectField( 'site_stats', '1', [ 'ss_row_id' => 1 ], __METHOD__ );
167 if ( $exists === false ) {
168 $dbw->insert(
169 'site_stats',
170 [ 'ss_row_id' => 1 ] + array_fill_keys( SiteStats::selectFields(), 0 ),
171 __METHOD__,
172 [ 'IGNORE' ]
173 );
174 }
175 }
176
177 /**
178 * Refresh site_stats
179 */
180 public function refresh() {
181 $values = [
182 'ss_row_id' => 1,
183 'ss_total_edits' => $this->edits === null ? $this->edits() : $this->edits,
184 'ss_good_articles' => $this->articles === null ? $this->articles() : $this->articles,
185 'ss_total_pages' => $this->pages === null ? $this->pages() : $this->pages,
186 'ss_users' => $this->users === null ? $this->users() : $this->users,
187 'ss_images' => $this->files === null ? $this->files() : $this->files,
188 ];
189
190 self::getDB( DB_MASTER )->upsert(
191 'site_stats',
192 $values,
193 [ 'ss_row_id' ],
194 $values,
195 __METHOD__
196 );
197 }
198
199 /**
200 * @param int $index
201 * @param string[] $groups
202 * @return IDatabase
203 */
204 private static function getDB( $index, $groups = [] ) {
205 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
206
207 return $lb->getConnection( $index, $groups );
208 }
209 }