3 * Implements Special:Statistics
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
27 * Special page lists various statistics, including the contents of
28 * `site_stats`, plus page view details if enabled
30 * @ingroup SpecialPage
32 class SpecialStatistics
extends SpecialPage
{
33 private $edits, $good, $images, $total, $users,
36 public function __construct() {
37 parent
::__construct( 'Statistics' );
40 public function execute( $par ) {
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
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();
51 $text = Xml
::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
54 $text .= $this->getPageStats();
57 $text .= $this->getEditStats();
60 $text .= $this->getUserStats();
62 # Statistic - usergroups
63 $text .= $this->getGroupStats();
67 if ( Hooks
::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
68 $text .= $this->getOtherStats( $extraStats );
71 $text .= Xml
::closeElement( 'table' );
74 $footer = $this->msg( 'statistics-footer' );
75 if ( !$footer->isBlank() ) {
76 $text .= "\n" . $footer->parse();
79 $this->getOutput()->addHTML( $text );
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
91 private function formatRow( $text, $number, $trExtraParams = [],
92 $descMsg = '', $descMsgParam = ''
95 $msg = $this->msg( $descMsg, $descMsgParam );
96 if ( !$msg->isDisabled() ) {
97 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
99 $text .= "<br />" . Html
::rawElement(
101 [ 'class' => 'mw-statistic-desc' ],
107 return Html
::rawElement( 'tr', $trExtraParams,
108 Html
::rawElement( 'td', [], $text ) .
109 Html
::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
114 * Each of these methods is pretty self-explanatory, get a particular
115 * row for the table of statistics
118 private function getPageStats() {
119 $linkRenderer = $this->getLinkRenderer();
121 $specialAllPagesTitle = SpecialPage
::getTitleFor( 'Allpages' );
122 $pageStatsHtml = Xml
::openElement( 'tr' ) .
123 Xml
::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
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' );
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' ] );
148 return $pageStatsHtml;
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' ]
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' ]
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()
177 $this->getLanguage()->formatNum( $this->users
),
178 [ 'class' => 'mw-statistics-users' ]
180 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
181 $this->getLinkRenderer()->makeKnownLink(
182 SpecialPage
::getTitleFor( 'Activeusers' ),
183 $this->msg( 'listgrouprights-members' )->text()
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' ) )
193 private function getGroupStats() {
194 $linkRenderer = $this->getLinkRenderer();
196 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
197 # Skip generic * and implicit groups
198 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
202 $groupname = htmlspecialchars( $group );
203 $msg = $this->msg( 'group-' . $groupname );
204 if ( $msg->isBlank() ) {
205 $groupnameLocalized = $groupname;
207 $groupnameLocalized = $msg->text();
209 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
210 if ( $msg->isBlank() ) {
211 $grouppageLocalized = MediaWikiServices
::getInstance()->getNamespaceInfo()->
212 getCanonicalName( NS_PROJECT
) . ':' . $groupname;
214 $grouppageLocalized = $msg->text();
216 $linkTarget = Title
::newFromText( $grouppageLocalized );
219 $grouppage = $linkRenderer->makeLink(
224 $grouppage = htmlspecialchars( $groupnameLocalized );
227 $grouplink = $linkRenderer->makeKnownLink(
228 SpecialPage
::getTitleFor( 'Listusers' ),
229 $this->msg( 'listgrouprights-members' )->text(),
231 [ 'group' => $group ]
233 # Add a class when a usergroup contains no members to allow hiding these rows
235 $countUsers = SiteStats
::numberingroup( $groupname );
236 if ( $countUsers == 0 ) {
237 $classZero = ' statistics-group-zero';
239 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
240 $this->getLanguage()->formatNum( $countUsers ),
241 [ 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) .
249 * Conversion of external statistics into an internal representation
250 * Following a ([<header-message>][<item-message>] = number) pattern
252 * @param array $stats
255 private function getOtherStats( array $stats ) {
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 );
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'];
272 $name = $this->msg( $key )->parse();
276 $return .= $this->formatRow(
278 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
279 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
283 // Create the legacy header only once
284 if ( $return === '' ) {
285 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
288 // Recursively remap the legacy structure
289 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
290 [ $header => $items ] ] );
300 * @param string $header
303 private function formatRowHeader( $header ) {
304 return Xml
::openElement( 'tr' ) .
305 Xml
::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
306 Xml
::closeElement( 'tr' );
309 protected function getGroupName() {