Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialStatistics.php
blob44e9802104b547de7cd4b8093562dd3227af2c66
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.
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
18 * @file
21 namespace MediaWiki\Specials;
23 use MediaWiki\Html\Html;
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\Parser\Sanitizer;
26 use MediaWiki\SiteStats\SiteStats;
27 use MediaWiki\SpecialPage\SpecialPage;
28 use MediaWiki\Title\Title;
29 use MediaWiki\User\UserGroupManager;
30 use MediaWiki\User\UserGroupMembership;
31 use MediaWiki\Xml\Xml;
33 /**
34 * Special page lists various statistics, including the contents of
35 * `site_stats`, plus page view details if enabled
37 * @ingroup SpecialPage
39 class SpecialStatistics extends SpecialPage {
40 private int $edits;
41 private int $good;
42 private int $images;
43 private int $total;
44 private int $users;
45 private int $activeUsers;
47 private UserGroupManager $userGroupManager;
49 /**
50 * @param UserGroupManager $userGroupManager
52 public function __construct( UserGroupManager $userGroupManager ) {
53 parent::__construct( 'Statistics' );
54 $this->userGroupManager = $userGroupManager;
57 public function execute( $par ) {
58 $this->setHeaders();
59 $this->outputHeader();
60 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
62 $this->edits = SiteStats::edits();
63 $this->good = SiteStats::articles();
64 $this->images = SiteStats::images();
65 $this->total = SiteStats::pages();
66 $this->users = SiteStats::users();
67 $this->activeUsers = SiteStats::activeUsers();
69 $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
71 # Statistic - pages
72 $text .= $this->getPageStats();
74 # Statistic - edits
75 $text .= $this->getEditStats();
77 # Statistic - users
78 $text .= $this->getUserStats();
80 # Statistic - usergroups
81 $text .= $this->getGroupStats();
83 # Statistic - other
84 $extraStats = [];
85 if ( $this->getHookRunner()->onSpecialStatsAddExtra(
86 $extraStats, $this->getContext() )
87 ) {
88 $text .= $this->getOtherStats( $extraStats );
91 $text .= Xml::closeElement( 'table' );
93 # Customizable footer
94 $footer = $this->msg( 'statistics-footer' );
95 if ( !$footer->isBlank() ) {
96 $text .= "\n" . $footer->parse();
99 $this->getOutput()->addHTML( $text );
103 * Format a row
104 * @param string $text Description of the row
105 * @param float|string $number A statistical number
106 * @param array $trExtraParams Params to table row, see Html::element
107 * @param string $descMsg Message key
108 * @param array|string $descMsgParam Message parameters
109 * @return string Table row in HTML format
111 private function formatRow( $text, $number, $trExtraParams = [],
112 $descMsg = '', $descMsgParam = ''
114 if ( $descMsg ) {
115 $msg = $this->msg( $descMsg, $descMsgParam );
116 if ( !$msg->isDisabled() ) {
117 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
118 ->escaped();
119 $text .= "<br />" . Html::rawElement(
120 'small',
121 [ 'class' => 'mw-statistic-desc' ],
122 " $descriptionHtml"
127 return Html::rawElement( 'tr', $trExtraParams,
128 Html::rawElement( 'td', [], $text ) .
129 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
134 * Each of these methods is pretty self-explanatory, get a particular
135 * row for the table of statistics
136 * @return string
138 private function getPageStats() {
139 $linkRenderer = $this->getLinkRenderer();
141 $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
142 $pageStatsHtml = Html::rawElement( 'tr', [],
143 Xml::tags( 'th', [ 'colspan' => '2' ],
144 $this->msg( 'statistics-header-pages' )->parse()
145 ) ) .
146 $this->formatRow(
147 $this->getConfig()->get( MainConfigNames::MiserMode )
148 ? $this->msg( 'statistics-articles' )->escaped()
149 : $linkRenderer->makeKnownLink(
150 $specialAllPagesTitle,
151 $this->msg( 'statistics-articles' )->text(),
152 [], [ 'hideredirects' => 1 ] ),
153 $this->getLanguage()->formatNum( $this->good ),
154 [ 'class' => 'mw-statistics-articles' ],
155 'statistics-articles-desc' ) .
156 $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
157 $this->msg( 'statistics-pages' )->text() ),
158 $this->getLanguage()->formatNum( $this->total ),
159 [ 'class' => 'mw-statistics-pages' ],
160 'statistics-pages-desc' );
162 // Show the image row only, when there are files or upload is possible
163 if ( $this->images !== 0 || $this->getConfig()->get( MainConfigNames::EnableUploads ) ) {
164 $pageStatsHtml .= $this->formatRow(
165 $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
166 $this->msg( 'statistics-files' )->text() ),
167 $this->getLanguage()->formatNum( $this->images ),
168 [ 'class' => 'mw-statistics-files' ], 'statistics-files-desc' );
171 return $pageStatsHtml;
174 private function getEditStats() {
175 return Html::rawElement( 'tr', [],
176 Xml::tags( 'th', [ 'colspan' => '2' ],
177 $this->msg( 'statistics-header-edits' )->parse()
178 ) ) .
179 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
180 $this->getLanguage()->formatNum( $this->edits ),
181 [ 'class' => 'mw-statistics-edits' ]
183 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
184 $this->getLanguage()->formatNum(
185 sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
186 ), [ 'class' => 'mw-statistics-edits-average' ]
190 private function getUserStats() {
191 return Html::rawElement( 'tr', [],
192 Xml::tags( 'th', [ 'colspan' => '2' ],
193 $this->msg( 'statistics-header-users' )->parse()
194 ) ) .
195 $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
196 $this->getLinkRenderer()->makeKnownLink(
197 SpecialPage::getTitleFor( 'Listusers' ),
198 $this->msg( 'listgrouprights-members' )->text()
200 $this->getLanguage()->formatNum( $this->users ),
201 [ 'class' => 'mw-statistics-users' ]
203 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
204 $this->getLinkRenderer()->makeKnownLink(
205 SpecialPage::getTitleFor( 'Activeusers' ),
206 $this->msg( 'listgrouprights-members' )->text()
208 $this->getLanguage()->formatNum( $this->activeUsers ),
209 [ 'class' => 'mw-statistics-users-active' ],
210 'statistics-users-active-desc',
211 $this->getLanguage()->formatNum(
212 $this->getConfig()->get( MainConfigNames::ActiveUserDays ) )
216 private function getGroupStats() {
217 $linkRenderer = $this->getLinkRenderer();
218 $lang = $this->getLanguage();
219 $text = '';
220 foreach ( $this->userGroupManager->listAllGroups() as $group ) {
221 $groupnameLocalized = $lang->getGroupName( $group );
222 $linkTarget = UserGroupMembership::getGroupPage( $group )
223 ?: Title::makeTitleSafe( NS_PROJECT, $group );
225 if ( $linkTarget ) {
226 $grouppage = $linkRenderer->makeLink(
227 $linkTarget,
228 $groupnameLocalized
230 } else {
231 $grouppage = htmlspecialchars( $groupnameLocalized );
234 $grouplink = $linkRenderer->makeKnownLink(
235 SpecialPage::getTitleFor( 'Listusers' ),
236 $this->msg( 'listgrouprights-members' )->text(),
238 [ 'group' => $group ]
240 # Add a class when a usergroup contains no members to allow hiding these rows
241 $classZero = '';
242 $countUsers = SiteStats::numberingroup( $group );
243 if ( $countUsers == 0 ) {
244 $classZero = ' statistics-group-zero';
246 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
247 $this->getLanguage()->formatNum( $countUsers ),
248 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
249 $classZero ] );
252 return $text;
256 * Conversion of external statistics into an internal representation
257 * Following a ([<header-message>][<item-message>] = number) pattern
259 * @param array $stats
260 * @return string
262 private function getOtherStats( array $stats ) {
263 $return = '';
265 foreach ( $stats as $header => $items ) {
266 // Identify the structure used
267 if ( is_array( $items ) ) {
268 // Ignore headers that are recursively set as legacy header
269 if ( $header !== 'statistics-header-hooks' ) {
270 $return .= $this->formatRowHeader( $header );
273 // Collect all items that belong to the same header
274 foreach ( $items as $key => $value ) {
275 if ( is_array( $value ) ) {
276 $name = $value['name'];
277 $number = $value['number'];
278 } else {
279 $name = $this->msg( $key )->parse();
280 $number = $value;
283 $return .= $this->formatRow(
284 $name,
285 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
286 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
289 } else {
290 // Create the legacy header only once
291 if ( $return === '' ) {
292 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
295 // Recursively remap the legacy structure
296 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
297 [ $header => $items ] ] );
301 return $return;
305 * Format row header
307 * @param string $header
308 * @return string
310 private function formatRowHeader( $header ) {
311 return Html::rawElement( 'tr', [],
312 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() )
316 protected function getGroupName() {
317 return 'wiki';
322 * Retain the old class name for backwards compatibility.
323 * @deprecated since 1.41
325 class_alias( SpecialStatistics::class, 'SpecialStatistics' );