Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2 /**
3 * Implements Special:Statistics
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Special page lists various statistics, including the contents of
28 * `site_stats`, plus page view details if enabled
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialStatistics extends SpecialPage {
33 private $edits, $good, $images, $total, $users,
34 $activeUsers = 0;
35
36 public function __construct() {
37 parent::__construct( 'Statistics' );
38 }
39
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
43
44 $this->edits = SiteStats::edits();
45 $this->good = SiteStats::articles();
46 $this->images = SiteStats::images();
47 $this->total = SiteStats::pages();
48 $this->users = SiteStats::users();
49 $this->activeUsers = SiteStats::activeUsers();
50
51 $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
52
53 # Statistic - pages
54 $text .= $this->getPageStats();
55
56 # Statistic - edits
57 $text .= $this->getEditStats();
58
59 # Statistic - users
60 $text .= $this->getUserStats();
61
62 # Statistic - usergroups
63 $text .= $this->getGroupStats();
64
65 # Statistic - other
66 $extraStats = [];
67 if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
68 $text .= $this->getOtherStats( $extraStats );
69 }
70
71 $text .= Xml::closeElement( 'table' );
72
73 # Customizable footer
74 $footer = $this->msg( 'statistics-footer' );
75 if ( !$footer->isBlank() ) {
76 $text .= "\n" . $footer->parse();
77 }
78
79 $this->getOutput()->addHTML( $text );
80 }
81
82 /**
83 * Format a row
84 * @param string $text Description of the row
85 * @param float $number A statistical number
86 * @param array $trExtraParams Params to table row, see Html::elememt
87 * @param string $descMsg Message key
88 * @param array|string $descMsgParam Message parameters
89 * @return string Table row in HTML format
90 */
91 private function formatRow( $text, $number, $trExtraParams = [],
92 $descMsg = '', $descMsgParam = ''
93 ) {
94 if ( $descMsg ) {
95 $msg = $this->msg( $descMsg, $descMsgParam );
96 if ( !$msg->isDisabled() ) {
97 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
98 ->escaped();
99 $text .= "<br />" . Html::rawElement(
100 'small',
101 [ 'class' => 'mw-statistic-desc' ],
102 " $descriptionHtml"
103 );
104 }
105 }
106
107 return Html::rawElement( 'tr', $trExtraParams,
108 Html::rawElement( 'td', [], $text ) .
109 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
110 );
111 }
112
113 /**
114 * Each of these methods is pretty self-explanatory, get a particular
115 * row for the table of statistics
116 * @return string
117 */
118 private function getPageStats() {
119 $linkRenderer = $this->getLinkRenderer();
120
121 $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
122 $pageStatsHtml = Xml::openElement( 'tr' ) .
123 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
124 ->parse() ) .
125 Xml::closeElement( 'tr' ) .
126 $this->formatRow( $linkRenderer->makeKnownLink(
127 $specialAllPagesTitle,
128 $this->msg( 'statistics-articles' )->text(),
129 [], [ 'hideredirects' => 1 ] ),
130 $this->getLanguage()->formatNum( $this->good ),
131 [ 'class' => 'mw-statistics-articles' ],
132 'statistics-articles-desc' ) .
133 $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
134 $this->msg( 'statistics-pages' )->text() ),
135 $this->getLanguage()->formatNum( $this->total ),
136 [ 'class' => 'mw-statistics-pages' ],
137 'statistics-pages-desc' );
138
139 // Show the image row only, when there are files or upload is possible
140 if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
141 $pageStatsHtml .= $this->formatRow(
142 $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
143 $this->msg( 'statistics-files' )->text() ),
144 $this->getLanguage()->formatNum( $this->images ),
145 [ 'class' => 'mw-statistics-files' ] );
146 }
147
148 return $pageStatsHtml;
149 }
150
151 private function getEditStats() {
152 return Xml::openElement( 'tr' ) .
153 Xml::tags( 'th', [ 'colspan' => '2' ],
154 $this->msg( 'statistics-header-edits' )->parse() ) .
155 Xml::closeElement( 'tr' ) .
156 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
157 $this->getLanguage()->formatNum( $this->edits ),
158 [ 'class' => 'mw-statistics-edits' ]
159 ) .
160 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
161 $this->getLanguage()->formatNum(
162 sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
163 ), [ 'class' => 'mw-statistics-edits-average' ]
164 );
165 }
166
167 private function getUserStats() {
168 return Xml::openElement( 'tr' ) .
169 Xml::tags( 'th', [ 'colspan' => '2' ],
170 $this->msg( 'statistics-header-users' )->parse() ) .
171 Xml::closeElement( 'tr' ) .
172 $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
173 $this->getLinkRenderer()->makeKnownLink(
174 SpecialPage::getTitleFor( 'Listusers' ),
175 $this->msg( 'listgrouprights-members' )->text()
176 ),
177 $this->getLanguage()->formatNum( $this->users ),
178 [ 'class' => 'mw-statistics-users' ]
179 ) .
180 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
181 $this->getLinkRenderer()->makeKnownLink(
182 SpecialPage::getTitleFor( 'Activeusers' ),
183 $this->msg( 'listgrouprights-members' )->text()
184 ),
185 $this->getLanguage()->formatNum( $this->activeUsers ),
186 [ 'class' => 'mw-statistics-users-active' ],
187 'statistics-users-active-desc',
188 $this->getLanguage()->formatNum(
189 $this->getConfig()->get( 'ActiveUserDays' ) )
190 );
191 }
192
193 private function getGroupStats() {
194 $linkRenderer = $this->getLinkRenderer();
195 $text = '';
196 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
197 # Skip generic * and implicit groups
198 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
199 || $group == '*' ) {
200 continue;
201 }
202 $groupname = htmlspecialchars( $group );
203 $msg = $this->msg( 'group-' . $groupname );
204 if ( $msg->isBlank() ) {
205 $groupnameLocalized = $groupname;
206 } else {
207 $groupnameLocalized = $msg->text();
208 }
209 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
210 if ( $msg->isBlank() ) {
211 $grouppageLocalized = MediaWikiServices::getInstance()->getNamespaceInfo()->
212 getCanonicalName( NS_PROJECT ) . ':' . $groupname;
213 } else {
214 $grouppageLocalized = $msg->text();
215 }
216 $linkTarget = Title::newFromText( $grouppageLocalized );
217
218 if ( $linkTarget ) {
219 $grouppage = $linkRenderer->makeLink(
220 $linkTarget,
221 $groupnameLocalized
222 );
223 } else {
224 $grouppage = htmlspecialchars( $groupnameLocalized );
225 }
226
227 $grouplink = $linkRenderer->makeKnownLink(
228 SpecialPage::getTitleFor( 'Listusers' ),
229 $this->msg( 'listgrouprights-members' )->text(),
230 [],
231 [ 'group' => $group ]
232 );
233 # Add a class when a usergroup contains no members to allow hiding these rows
234 $classZero = '';
235 $countUsers = SiteStats::numberingroup( $groupname );
236 if ( $countUsers == 0 ) {
237 $classZero = ' statistics-group-zero';
238 }
239 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
240 $this->getLanguage()->formatNum( $countUsers ),
241 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
242 $classZero ] );
243 }
244
245 return $text;
246 }
247
248 /**
249 * Conversion of external statistics into an internal representation
250 * Following a ([<header-message>][<item-message>] = number) pattern
251 *
252 * @param array $stats
253 * @return string
254 */
255 private function getOtherStats( array $stats ) {
256 $return = '';
257
258 foreach ( $stats as $header => $items ) {
259 // Identify the structure used
260 if ( is_array( $items ) ) {
261 // Ignore headers that are recursively set as legacy header
262 if ( $header !== 'statistics-header-hooks' ) {
263 $return .= $this->formatRowHeader( $header );
264 }
265
266 // Collect all items that belong to the same header
267 foreach ( $items as $key => $value ) {
268 if ( is_array( $value ) ) {
269 $name = $value['name'];
270 $number = $value['number'];
271 } else {
272 $name = $this->msg( $key )->parse();
273 $number = $value;
274 }
275
276 $return .= $this->formatRow(
277 $name,
278 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
279 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
280 );
281 }
282 } else {
283 // Create the legacy header only once
284 if ( $return === '' ) {
285 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
286 }
287
288 // Recursively remap the legacy structure
289 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
290 [ $header => $items ] ] );
291 }
292 }
293
294 return $return;
295 }
296
297 /**
298 * Format row header
299 *
300 * @param string $header
301 * @return string
302 */
303 private function formatRowHeader( $header ) {
304 return Xml::openElement( 'tr' ) .
305 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
306 Xml::closeElement( 'tr' );
307 }
308
309 protected function getGroupName() {
310 return 'wiki';
311 }
312 }