Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / specials / SpecialStatistics.php
bloba7a1c581ceeb7863ed7311c82fc32f984cdfbe40
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page lists various statistics, including the contents of
26 * `site_stats`, plus page view details if enabled
28 * @ingroup SpecialPage
30 class SpecialStatistics extends SpecialPage {
31 private $edits, $good, $images, $total, $users,
32 $activeUsers = 0;
34 public function __construct() {
35 parent::__construct( 'Statistics' );
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
42 $this->edits = SiteStats::edits();
43 $this->good = SiteStats::articles();
44 $this->images = SiteStats::images();
45 $this->total = SiteStats::pages();
46 $this->users = SiteStats::users();
47 $this->activeUsers = SiteStats::activeUsers();
48 $this->hook = '';
50 $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
52 # Statistic - pages
53 $text .= $this->getPageStats();
55 # Statistic - edits
56 $text .= $this->getEditStats();
58 # Statistic - users
59 $text .= $this->getUserStats();
61 # Statistic - usergroups
62 $text .= $this->getGroupStats();
64 # Statistic - other
65 $extraStats = [];
66 if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
67 $text .= $this->getOtherStats( $extraStats );
70 $text .= Xml::closeElement( 'table' );
72 # Customizable footer
73 $footer = $this->msg( 'statistics-footer' );
74 if ( !$footer->isBlank() ) {
75 $text .= "\n" . $footer->parse();
78 $this->getOutput()->addHTML( $text );
81 /**
82 * Format a row
83 * @param string $text Description of the row
84 * @param float $number A statistical number
85 * @param array $trExtraParams Params to table row, see Html::elememt
86 * @param string $descMsg Message key
87 * @param array|string $descMsgParam Message parameters
88 * @return string Table row in HTML format
90 private function formatRow( $text, $number, $trExtraParams = [],
91 $descMsg = '', $descMsgParam = ''
92 ) {
93 if ( $descMsg ) {
94 $msg = $this->msg( $descMsg, $descMsgParam );
95 if ( !$msg->isDisabled() ) {
96 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
97 ->escaped();
98 $text .= "<br />" .
99 Html::rawElement( 'small', [ 'class' => 'mw-statistic-desc' ],
100 " $descriptionHtml" );
104 return Html::rawElement( 'tr', $trExtraParams,
105 Html::rawElement( 'td', [], $text ) .
106 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
111 * Each of these methods is pretty self-explanatory, get a particular
112 * row for the table of statistics
113 * @return string
115 private function getPageStats() {
116 $linkRenderer = $this->getLinkRenderer();
118 $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
119 $pageStatsHtml = Xml::openElement( 'tr' ) .
120 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
121 ->parse() ) .
122 Xml::closeElement( 'tr' ) .
123 $this->formatRow( $linkRenderer->makeKnownLink(
124 $specialAllPagesTitle,
125 $this->msg( 'statistics-articles' )->text(),
126 [], [ 'hideredirects' => 1 ] ),
127 $this->getLanguage()->formatNum( $this->good ),
128 [ 'class' => 'mw-statistics-articles' ],
129 'statistics-articles-desc' ) .
130 $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
131 $this->msg( 'statistics-pages' )->text() ),
132 $this->getLanguage()->formatNum( $this->total ),
133 [ 'class' => 'mw-statistics-pages' ],
134 'statistics-pages-desc' );
136 // Show the image row only, when there are files or upload is possible
137 if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
138 $pageStatsHtml .= $this->formatRow(
139 $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
140 $this->msg( 'statistics-files' )->text() ),
141 $this->getLanguage()->formatNum( $this->images ),
142 [ 'class' => 'mw-statistics-files' ] );
145 return $pageStatsHtml;
148 private function getEditStats() {
149 return Xml::openElement( 'tr' ) .
150 Xml::tags( 'th', [ 'colspan' => '2' ],
151 $this->msg( 'statistics-header-edits' )->parse() ) .
152 Xml::closeElement( 'tr' ) .
153 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
154 $this->getLanguage()->formatNum( $this->edits ),
155 [ 'class' => 'mw-statistics-edits' ]
157 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
158 $this->getLanguage()->formatNum(
159 sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
160 ), [ 'class' => 'mw-statistics-edits-average' ]
164 private function getUserStats() {
165 return Xml::openElement( 'tr' ) .
166 Xml::tags( 'th', [ 'colspan' => '2' ],
167 $this->msg( 'statistics-header-users' )->parse() ) .
168 Xml::closeElement( 'tr' ) .
169 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
170 $this->getLanguage()->formatNum( $this->users ),
171 [ 'class' => 'mw-statistics-users' ]
173 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
174 $this->getLinkRenderer()->makeKnownLink(
175 SpecialPage::getTitleFor( 'Activeusers' ),
176 $this->msg( 'listgrouprights-members' )->text()
178 $this->getLanguage()->formatNum( $this->activeUsers ),
179 [ 'class' => 'mw-statistics-users-active' ],
180 'statistics-users-active-desc',
181 $this->getLanguage()->formatNum(
182 $this->getConfig()->get( 'ActiveUserDays' ) )
186 private function getGroupStats() {
187 $linkRenderer = $this->getLinkRenderer();
188 $text = '';
189 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
190 # Skip generic * and implicit groups
191 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
192 || $group == '*' ) {
193 continue;
195 $groupname = htmlspecialchars( $group );
196 $msg = $this->msg( 'group-' . $groupname );
197 if ( $msg->isBlank() ) {
198 $groupnameLocalized = $groupname;
199 } else {
200 $groupnameLocalized = $msg->text();
202 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
203 if ( $msg->isBlank() ) {
204 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) .
205 ':' . $groupname;
206 } else {
207 $grouppageLocalized = $msg->text();
209 $linkTarget = Title::newFromText( $grouppageLocalized );
211 if ( $linkTarget ) {
212 $grouppage = $linkRenderer->makeLink(
213 $linkTarget,
214 $groupnameLocalized
216 } else {
217 $grouppage = htmlspecialchars( $groupnameLocalized );
220 $grouplink = $linkRenderer->makeKnownLink(
221 SpecialPage::getTitleFor( 'Listusers' ),
222 $this->msg( 'listgrouprights-members' )->text(),
224 [ 'group' => $group ]
226 # Add a class when a usergroup contains no members to allow hiding these rows
227 $classZero = '';
228 $countUsers = SiteStats::numberingroup( $groupname );
229 if ( $countUsers == 0 ) {
230 $classZero = ' statistics-group-zero';
232 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
233 $this->getLanguage()->formatNum( $countUsers ),
234 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
235 $classZero ] );
238 return $text;
242 * Conversion of external statistics into an internal representation
243 * Following a ([<header-message>][<item-message>] = number) pattern
245 * @param array $stats
246 * @return string
248 private function getOtherStats( array $stats ) {
249 $return = '';
251 foreach ( $stats as $header => $items ) {
252 // Identify the structure used
253 if ( is_array( $items ) ) {
255 // Ignore headers that are recursively set as legacy header
256 if ( $header !== 'statistics-header-hooks' ) {
257 $return .= $this->formatRowHeader( $header );
260 // Collect all items that belong to the same header
261 foreach ( $items as $key => $value ) {
262 if ( is_array( $value ) ) {
263 $name = $value['name'];
264 $number = $value['number'];
265 } else {
266 $name = $this->msg( $key )->parse();
267 $number = $value;
270 $return .= $this->formatRow(
271 $name,
272 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
273 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
276 } else {
277 // Create the legacy header only once
278 if ( $return === '' ) {
279 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
282 // Recursively remap the legacy structure
283 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
284 [ $header => $items ] ] );
288 return $return;
292 * Format row header
294 * @param string $header
295 * @return string
297 private function formatRowHeader( $header ) {
298 return Xml::openElement( 'tr' ) .
299 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
300 Xml::closeElement( 'tr' );
303 protected function getGroupName() {
304 return 'wiki';