HTML escape parameter 'text' of hook 'SkinEditSectionLinks'
[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 }
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
94 /**
95 * Count total pages
96 * @return int
97 */
98 public function pages() {
99 $this->pages = $this->dbr->selectField( 'page', 'COUNT(*)', '', __METHOD__ );
100
101 return $this->pages;
102 }
103
104 /**
105 * Count total users
106 * @return int
107 */
108 public function users() {
109 $this->users = $this->dbr->selectField( 'user', 'COUNT(*)', '', __METHOD__ );
110
111 return $this->users;
112 }
113
114 /**
115 * Count total files
116 * @return int
117 */
118 public function files() {
119 $this->files = $this->dbr->selectField( 'image', 'COUNT(*)', '', __METHOD__ );
120
121 return $this->files;
122 }
123
124 /**
125 * Do all updates and commit them. More or less a replacement
126 * for the original initStats, but without output.
127 *
128 * @param IDatabase|bool $database
129 * - bool: Whether to use the master DB
130 * - IDatabase: Database connection to use
131 * @param array $options Array of options, may contain the following values
132 * - activeUsers bool: Whether to update the number of active users (default: false)
133 */
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
154 /**
155 * Insert a dummy row with all zeroes if no row is present
156 */
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
170 /**
171 * Refresh site_stats
172 */
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
192 /**
193 * @param int $index
194 * @param string[] $groups
195 * @return IDatabase
196 */
197 private static function getDB( $index, $groups = [] ) {
198 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
199
200 return $lb->getConnection( $index, $groups );
201 }
202 }