MediaWiki master
SpecialStatistics.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
32
40 private int $edits;
41 private int $good;
42 private int $images;
43 private int $total;
44 private int $users;
45 private int $activeUsers;
46
47 private UserGroupManager $userGroupManager;
48
52 public function __construct( UserGroupManager $userGroupManager ) {
53 parent::__construct( 'Statistics' );
54 $this->userGroupManager = $userGroupManager;
55 }
56
57 public function execute( $par ) {
58 $this->setHeaders();
59 $this->outputHeader();
60 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
61
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();
68
69 $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
70
71 # Statistic - pages
72 $text .= $this->getPageStats();
73
74 # Statistic - edits
75 $text .= $this->getEditStats();
76
77 # Statistic - users
78 $text .= $this->getUserStats();
79
80 # Statistic - usergroups
81 $text .= $this->getGroupStats();
82
83 # Statistic - other
84 $extraStats = [];
85 if ( $this->getHookRunner()->onSpecialStatsAddExtra(
86 $extraStats, $this->getContext() )
87 ) {
88 $text .= $this->getOtherStats( $extraStats );
89 }
90
91 $text .= Xml::closeElement( 'table' );
92
93 # Customizable footer
94 $footer = $this->msg( 'statistics-footer' );
95 if ( !$footer->isBlank() ) {
96 $text .= "\n" . $footer->parse();
97 }
98
99 $this->getOutput()->addHTML( $text );
100 }
101
111 private function formatRow( $text, $number, $trExtraParams = [],
112 $descMsg = '', $descMsgParam = ''
113 ) {
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"
123 );
124 }
125 }
126
127 return Html::rawElement( 'tr', $trExtraParams,
128 Html::rawElement( 'td', [], $text ) .
129 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
130 );
131 }
132
138 private function getPageStats() {
139 $linkRenderer = $this->getLinkRenderer();
140
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(
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' );
161
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' );
169 }
170
171 return $pageStatsHtml;
172 }
173
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' ]
182 ) .
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' ]
187 );
188 }
189
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()
199 ),
200 $this->getLanguage()->formatNum( $this->users ),
201 [ 'class' => 'mw-statistics-users' ]
202 ) .
203 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
204 $this->getLinkRenderer()->makeKnownLink(
205 SpecialPage::getTitleFor( 'Activeusers' ),
206 $this->msg( 'listgrouprights-members' )->text()
207 ),
208 $this->getLanguage()->formatNum( $this->activeUsers ),
209 [ 'class' => 'mw-statistics-users-active' ],
210 'statistics-users-active-desc',
211 $this->getLanguage()->formatNum(
213 );
214 }
215
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 );
224
225 if ( $linkTarget ) {
226 $grouppage = $linkRenderer->makeLink(
227 $linkTarget,
228 $groupnameLocalized
229 );
230 } else {
231 $grouppage = htmlspecialchars( $groupnameLocalized );
232 }
233
234 $grouplink = $linkRenderer->makeKnownLink(
235 SpecialPage::getTitleFor( 'Listusers' ),
236 $this->msg( 'listgrouprights-members' )->text(),
237 [],
238 [ 'group' => $group ]
239 );
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';
245 }
246 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
247 $this->getLanguage()->formatNum( $countUsers ),
248 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
249 $classZero ] );
250 }
251
252 return $text;
253 }
254
262 private function getOtherStats( array $stats ) {
263 $return = '';
264
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 );
271 }
272
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;
281 }
282
283 $return .= $this->formatRow(
284 $name,
285 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
286 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
287 );
288 }
289 } else {
290 // Create the legacy header only once
291 if ( $return === '' ) {
292 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
293 }
294
295 // Recursively remap the legacy structure
296 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
297 [ $header => $items ] ] );
298 }
299 }
300
301 return $return;
302 }
303
310 private function formatRowHeader( $header ) {
311 return Html::rawElement( 'tr', [],
312 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() )
313 );
314 }
315
316 protected function getGroupName() {
317 return 'wiki';
318 }
319}
320
325class_alias( SpecialStatistics::class, 'SpecialStatistics' );
const NS_PROJECT
Definition Defines.php:69
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition Html.php:240
static closeElement( $element)
Returns "</$element>".
Definition Html.php:304
A class containing constants representing the names of configuration variables.
const EnableUploads
Name constant for the EnableUploads setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Static accessor class for site_stats and related things.
Definition SiteStats.php:36
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
Special page lists various statistics, including the contents of site_stats, plus page view details i...
execute( $par)
Default execute method Checks user permissions.
__construct(UserGroupManager $userGroupManager)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Represents a title within MediaWiki.
Definition Title.php:79
Represents a "user group membership" – a specific instance of a user belonging to a group.
Module of static functions for generating XML.
Definition Xml.php:37
$header
$footer