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
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
;
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
{
45 private int $activeUsers;
47 private UserGroupManager
$userGroupManager;
50 * @param UserGroupManager $userGroupManager
52 public function __construct( UserGroupManager
$userGroupManager ) {
53 parent
::__construct( 'Statistics' );
54 $this->userGroupManager
= $userGroupManager;
57 public function execute( $par ) {
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' ] );
72 $text .= $this->getPageStats();
75 $text .= $this->getEditStats();
78 $text .= $this->getUserStats();
80 # Statistic - usergroups
81 $text .= $this->getGroupStats();
85 if ( $this->getHookRunner()->onSpecialStatsAddExtra(
86 $extraStats, $this->getContext() )
88 $text .= $this->getOtherStats( $extraStats );
91 $text .= Xml
::closeElement( 'table' );
94 $footer = $this->msg( 'statistics-footer' );
95 if ( !$footer->isBlank() ) {
96 $text .= "\n" . $footer->parse();
99 $this->getOutput()->addHTML( $text );
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 = ''
115 $msg = $this->msg( $descMsg, $descMsgParam );
116 if ( !$msg->isDisabled() ) {
117 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
119 $text .= "<br />" . Html
::rawElement(
121 [ 'class' => 'mw-statistic-desc' ],
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
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()
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()
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()
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();
220 foreach ( $this->userGroupManager
->listAllGroups() as $group ) {
221 $groupnameLocalized = $lang->getGroupName( $group );
222 $linkTarget = UserGroupMembership
::getGroupPage( $group )
223 ?
: Title
::makeTitleSafe( NS_PROJECT
, $group );
226 $grouppage = $linkRenderer->makeLink(
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
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 ) .
256 * Conversion of external statistics into an internal representation
257 * Following a ([<header-message>][<item-message>] = number) pattern
259 * @param array $stats
262 private function getOtherStats( array $stats ) {
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'];
279 $name = $this->msg( $key )->parse();
283 $return .= $this->formatRow(
285 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
286 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
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 ] ] );
307 * @param string $header
310 private function formatRowHeader( $header ) {
311 return Html
::rawElement( 'tr', [],
312 Xml
::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() )
316 protected function getGroupName() {
322 * Retain the old class name for backwards compatibility.
323 * @deprecated since 1.41
325 class_alias( SpecialStatistics
::class, 'SpecialStatistics' );