775963f642d7664f394926eae7c5449a4bc96cc8
[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
22 /**
23 * Class designed for counting of stats.
24 */
25 class SiteStatsInit {
26
27 // Database connection
28 private $db;
29
30 // Various stats
31 private $mEdits = null, $mArticles = null, $mPages = null;
32 private $mUsers = null, $mFiles = null;
33
34 /**
35 * @param bool|IDatabase $database
36 * - bool: Whether to use the master DB
37 * - IDatabase: Database connection to use
38 */
39 public function __construct( $database = false ) {
40 if ( $database instanceof IDatabase ) {
41 $this->db = $database;
42 } elseif ( $database ) {
43 $this->db = wfGetDB( DB_MASTER );
44 } else {
45 $this->db = wfGetDB( DB_REPLICA, 'vslow' );
46 }
47 }
48
49 /**
50 * Count the total number of edits
51 * @return int
52 */
53 public function edits() {
54 $this->mEdits = $this->db->selectField( 'revision', 'COUNT(*)', '', __METHOD__ );
55 $this->mEdits += $this->db->selectField( 'archive', 'COUNT(*)', '', __METHOD__ );
56 return $this->mEdits;
57 }
58
59 /**
60 * Count pages in article space(s)
61 * @return int
62 */
63 public function articles() {
64 global $wgArticleCountMethod;
65
66 $tables = [ 'page' ];
67 $conds = [
68 'page_namespace' => MWNamespace::getContentNamespaces(),
69 'page_is_redirect' => 0,
70 ];
71
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
80 // contain a comma :)
81 $conds[] = 'page_len > 0';
82 }
83
84 $this->mArticles = $this->db->selectField( $tables, 'COUNT(DISTINCT page_id)',
85 $conds, __METHOD__ );
86 return $this->mArticles;
87 }
88
89 /**
90 * Count total pages
91 * @return int
92 */
93 public function pages() {
94 $this->mPages = $this->db->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
95 return $this->mPages;
96 }
97
98 /**
99 * Count total users
100 * @return int
101 */
102 public function users() {
103 $this->mUsers = $this->db->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
104 return $this->mUsers;
105 }
106
107 /**
108 * Count total files
109 * @return int
110 */
111 public function files() {
112 $this->mFiles = $this->db->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
113 return $this->mFiles;
114 }
115
116 /**
117 * Do all updates and commit them. More or less a replacement
118 * for the original initStats, but without output.
119 *
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)
125 */
126 public static function doAllAndCommit( $database, array $options = [] ) {
127 $options += [ 'update' => false, 'activeUsers' => false ];
128
129 // Grab the object and count everything
130 $counter = new SiteStatsInit( $database );
131
132 $counter->edits();
133 $counter->articles();
134 $counter->pages();
135 $counter->users();
136 $counter->files();
137
138 $counter->refresh();
139
140 // Count active users if need be
141 if ( $options['activeUsers'] ) {
142 SiteStatsUpdate::cacheUpdate( wfGetDB( DB_MASTER ) );
143 }
144 }
145
146 /**
147 * Insert a dummy row with all zeroes if no row is present
148 */
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 ) {
153 $dbw->insert(
154 'site_stats',
155 [ 'ss_row_id' => 1 ] + array_fill_keys( SiteStats::selectFields(), 0 ),
156 __METHOD__,
157 [ 'IGNORE' ]
158 );
159 }
160 }
161
162 /**
163 * Refresh site_stats
164 */
165 public function refresh() {
166 $values = [
167 'ss_row_id' => 1,
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 ),
173 ];
174
175 $dbw = wfGetDB( DB_MASTER );
176 $dbw->upsert( 'site_stats', $values, [ 'ss_row_id' ], $values, __METHOD__ );
177 }
178 }