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();
52 $text = Xml
::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
55 $text .= $this->getPageStats();
58 $text .= $this->getEditStats();
61 $text .= $this->getUserStats();
63 # Statistic - usergroups
64 $text .= $this->getGroupStats();
68 if ( Hooks
::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
69 $text .= $this->getOtherStats( $extraStats );
72 $text .= Xml
::closeElement( 'table' );
75 $footer = $this->msg( 'statistics-footer' );
76 if ( !$footer->isBlank() ) {
77 $text .= "\n" . $footer->parse();
80 $this->getOutput()->addHTML( $text );
85 * @param string $text Description of the row
86 * @param float $number A statistical number
87 * @param array $trExtraParams Params to table row, see Html::elememt
88 * @param string $descMsg Message key
89 * @param array|string $descMsgParam Message parameters
90 * @return string Table row in HTML format
92 private function formatRow( $text, $number, $trExtraParams = [],
93 $descMsg = '', $descMsgParam = ''
96 $msg = $this->msg( $descMsg, $descMsgParam );
97 if ( !$msg->isDisabled() ) {
98 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
100 $text .= "<br />" . Html
::rawElement(
102 [ 'class' => 'mw-statistic-desc' ],
108 return Html
::rawElement( 'tr', $trExtraParams,
109 Html
::rawElement( 'td', [], $text ) .
110 Html
::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
115 * Each of these methods is pretty self-explanatory, get a particular
116 * row for the table of statistics
119 private function getPageStats() {
120 $linkRenderer = $this->getLinkRenderer();
122 $specialAllPagesTitle = SpecialPage
::getTitleFor( 'Allpages' );
123 $pageStatsHtml = Xml
::openElement( 'tr' ) .
124 Xml
::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
126 Xml
::closeElement( 'tr' ) .
127 $this->formatRow( $linkRenderer->makeKnownLink(
128 $specialAllPagesTitle,
129 $this->msg( 'statistics-articles' )->text(),
130 [], [ 'hideredirects' => 1 ] ),
131 $this->getLanguage()->formatNum( $this->good
),
132 [ 'class' => 'mw-statistics-articles' ],
133 'statistics-articles-desc' ) .
134 $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
135 $this->msg( 'statistics-pages' )->text() ),
136 $this->getLanguage()->formatNum( $this->total
),
137 [ 'class' => 'mw-statistics-pages' ],
138 'statistics-pages-desc' );
140 // Show the image row only, when there are files or upload is possible
141 if ( $this->images
!== 0 ||
$this->getConfig()->get( 'EnableUploads' ) ) {
142 $pageStatsHtml .= $this->formatRow(
143 $linkRenderer->makeKnownLink( SpecialPage
::getTitleFor( 'MediaStatistics' ),
144 $this->msg( 'statistics-files' )->text() ),
145 $this->getLanguage()->formatNum( $this->images
),
146 [ 'class' => 'mw-statistics-files' ] );
149 return $pageStatsHtml;
152 private function getEditStats() {
153 return Xml
::openElement( 'tr' ) .
154 Xml
::tags( 'th', [ 'colspan' => '2' ],
155 $this->msg( 'statistics-header-edits' )->parse() ) .
156 Xml
::closeElement( 'tr' ) .
157 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
158 $this->getLanguage()->formatNum( $this->edits
),
159 [ 'class' => 'mw-statistics-edits' ]
161 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
162 $this->getLanguage()->formatNum(
163 sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 )
164 ), [ 'class' => 'mw-statistics-edits-average' ]
168 private function getUserStats() {
169 return Xml
::openElement( 'tr' ) .
170 Xml
::tags( 'th', [ 'colspan' => '2' ],
171 $this->msg( 'statistics-header-users' )->parse() ) .
172 Xml
::closeElement( 'tr' ) .
173 $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
174 $this->getLinkRenderer()->makeKnownLink(
175 SpecialPage
::getTitleFor( 'Listusers' ),
176 $this->msg( 'listgrouprights-members' )->text()
178 $this->getLanguage()->formatNum( $this->users
),
179 [ 'class' => 'mw-statistics-users' ]
181 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
182 $this->getLinkRenderer()->makeKnownLink(
183 SpecialPage
::getTitleFor( 'Activeusers' ),
184 $this->msg( 'listgrouprights-members' )->text()
186 $this->getLanguage()->formatNum( $this->activeUsers
),
187 [ 'class' => 'mw-statistics-users-active' ],
188 'statistics-users-active-desc',
189 $this->getLanguage()->formatNum(
190 $this->getConfig()->get( 'ActiveUserDays' ) )
194 private function getGroupStats() {
195 $linkRenderer = $this->getLinkRenderer();
197 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
198 # Skip generic * and implicit groups
199 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
203 $groupname = htmlspecialchars( $group );
204 $msg = $this->msg( 'group-' . $groupname );
205 if ( $msg->isBlank() ) {
206 $groupnameLocalized = $groupname;
208 $groupnameLocalized = $msg->text();
210 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
211 if ( $msg->isBlank() ) {
212 $grouppageLocalized = MediaWikiServices
::getInstance()->getNamespaceInfo()->
213 getCanonicalName( NS_PROJECT
) . ':' . $groupname;
215 $grouppageLocalized = $msg->text();
217 $linkTarget = Title
::newFromText( $grouppageLocalized );
220 $grouppage = $linkRenderer->makeLink(
225 $grouppage = htmlspecialchars( $groupnameLocalized );
228 $grouplink = $linkRenderer->makeKnownLink(
229 SpecialPage
::getTitleFor( 'Listusers' ),
230 $this->msg( 'listgrouprights-members' )->text(),
232 [ 'group' => $group ]
234 # Add a class when a usergroup contains no members to allow hiding these rows
236 $countUsers = SiteStats
::numberingroup( $groupname );
237 if ( $countUsers == 0 ) {
238 $classZero = ' statistics-group-zero';
240 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
241 $this->getLanguage()->formatNum( $countUsers ),
242 [ 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) .
250 * Conversion of external statistics into an internal representation
251 * Following a ([<header-message>][<item-message>] = number) pattern
253 * @param array $stats
256 private function getOtherStats( array $stats ) {
259 foreach ( $stats as $header => $items ) {
260 // Identify the structure used
261 if ( is_array( $items ) ) {
262 // Ignore headers that are recursively set as legacy header
263 if ( $header !== 'statistics-header-hooks' ) {
264 $return .= $this->formatRowHeader( $header );
267 // Collect all items that belong to the same header
268 foreach ( $items as $key => $value ) {
269 if ( is_array( $value ) ) {
270 $name = $value['name'];
271 $number = $value['number'];
273 $name = $this->msg( $key )->parse();
277 $return .= $this->formatRow(
279 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
280 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
284 // Create the legacy header only once
285 if ( $return === '' ) {
286 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
289 // Recursively remap the legacy structure
290 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
291 [ $header => $items ] ] );
301 * @param string $header
304 private function formatRowHeader( $header ) {
305 return Xml
::openElement( 'tr' ) .
306 Xml
::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
307 Xml
::closeElement( 'tr' );
310 protected function getGroupName() {