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
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,
34 public function __construct() {
35 parent
::__construct( 'Statistics' );
38 public function execute( $par ) {
41 $miserMode = $this->getConfig()->get( 'MiserMode' );
44 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
46 $this->edits
= SiteStats
::edits();
47 $this->good
= SiteStats
::articles();
48 $this->images
= SiteStats
::images();
49 $this->total
= SiteStats
::pages();
50 $this->users
= SiteStats
::users();
51 $this->activeUsers
= SiteStats
::activeUsers();
54 # Set active user count
56 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
57 // Re-calculate the count if the last tally is old...
58 if ( !$wgMemc->get( $key ) ) {
59 $dbw = wfGetDB( DB_MASTER
);
60 SiteStatsUpdate
::cacheUpdate( $dbw );
61 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
65 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
68 $text .= $this->getPageStats();
71 $text .= $this->getEditStats();
74 $text .= $this->getUserStats();
76 # Statistic - usergroups
77 $text .= $this->getGroupStats();
80 $extraStats = array();
81 if ( Hooks
::run( 'SpecialStatsAddExtra', array( &$extraStats, $this->getContext() ) ) ) {
82 $text .= $this->getOtherStats( $extraStats );
85 $text .= Xml
::closeElement( 'table' );
88 $footer = $this->msg( 'statistics-footer' );
89 if ( !$footer->isBlank() ) {
90 $text .= "\n" . $footer->parse();
93 $this->getOutput()->addHTML( $text );
98 * @param string $text Description of the row
99 * @param float $number A statistical number
100 * @param array $trExtraParams Params to table row, see Html::elememt
101 * @param string $descMsg Message key
102 * @param array|string $descMsgParam Message parameters
103 * @return string Table row in HTML format
105 private function formatRow( $text, $number, $trExtraParams = array(),
106 $descMsg = '', $descMsgParam = ''
109 $msg = $this->msg( $descMsg, $descMsgParam );
110 if ( $msg->exists() ) {
111 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
113 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc' ),
114 " $descriptionText" );
118 return Html
::rawElement( 'tr', $trExtraParams,
119 Html
::rawElement( 'td', array(), $text ) .
120 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
125 * Each of these methods is pretty self-explanatory, get a particular
126 * row for the table of statistics
129 private function getPageStats() {
130 $pageStatsHtml = Xml
::openElement( 'tr' ) .
131 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )
133 Xml
::closeElement( 'tr' ) .
134 $this->formatRow( Linker
::linkKnown( SpecialPage
::getTitleFor( 'Allpages' ),
135 $this->msg( 'statistics-articles' )->parse() ),
136 $this->getLanguage()->formatNum( $this->good
),
137 array( 'class' => 'mw-statistics-articles' ) ) .
138 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
139 $this->getLanguage()->formatNum( $this->total
),
140 array( 'class' => 'mw-statistics-pages' ),
141 'statistics-pages-desc' );
143 // Show the image row only, when there are files or upload is possible
144 if ( $this->images
!== 0 ||
$this->getConfig()->get( 'EnableUploads' ) ) {
145 $pageStatsHtml .= $this->formatRow(
146 Linker
::linkKnown( SpecialPage
::getTitleFor( 'MediaStatistics' ),
147 $this->msg( 'statistics-files' )->parse() ),
148 $this->getLanguage()->formatNum( $this->images
),
149 array( 'class' => 'mw-statistics-files' ) );
152 return $pageStatsHtml;
155 private function getEditStats() {
156 return Xml
::openElement( 'tr' ) .
157 Xml
::tags( 'th', array( 'colspan' => '2' ),
158 $this->msg( 'statistics-header-edits' )->parse() ) .
159 Xml
::closeElement( 'tr' ) .
160 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
161 $this->getLanguage()->formatNum( $this->edits
),
162 array( 'class' => 'mw-statistics-edits' )
164 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
166 ->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
167 array( 'class' => 'mw-statistics-edits-average' )
171 private function getUserStats() {
172 return Xml
::openElement( 'tr' ) .
173 Xml
::tags( 'th', array( 'colspan' => '2' ),
174 $this->msg( 'statistics-header-users' )->parse() ) .
175 Xml
::closeElement( 'tr' ) .
176 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
177 $this->getLanguage()->formatNum( $this->users
),
178 array( 'class' => 'mw-statistics-users' )
180 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
182 SpecialPage
::getTitleFor( 'Activeusers' ),
183 $this->msg( 'listgrouprights-members' )->escaped()
185 $this->getLanguage()->formatNum( $this->activeUsers
),
186 array( 'class' => 'mw-statistics-users-active' ),
187 'statistics-users-active-desc',
188 $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
192 private function getGroupStats() {
194 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
195 # Skip generic * and implicit groups
196 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) ||
$group == '*' ) {
199 $groupname = htmlspecialchars( $group );
200 $msg = $this->msg( 'group-' . $groupname );
201 if ( $msg->isBlank() ) {
202 $groupnameLocalized = $groupname;
204 $groupnameLocalized = $msg->text();
206 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
207 if ( $msg->isBlank() ) {
208 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
210 $grouppageLocalized = $msg->text();
212 $linkTarget = Title
::newFromText( $grouppageLocalized );
215 $grouppage = Linker
::link(
217 htmlspecialchars( $groupnameLocalized )
220 $grouppage = htmlspecialchars( $groupnameLocalized );
223 $grouplink = Linker
::linkKnown(
224 SpecialPage
::getTitleFor( 'Listusers' ),
225 $this->msg( 'listgrouprights-members' )->escaped(),
227 array( 'group' => $group )
229 # Add a class when a usergroup contains no members to allow hiding these rows
231 $countUsers = SiteStats
::numberingroup( $groupname );
232 if ( $countUsers == 0 ) {
233 $classZero = ' statistics-group-zero';
235 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
236 $this->getLanguage()->formatNum( $countUsers ),
237 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) .
245 * Conversion of external statistics into an internal representation
246 * Following a ([<header-message>][<item-message>] = number) pattern
248 * @param array $stats
251 private function getOtherStats( array $stats ) {
254 foreach ( $stats as $header => $items ) {
255 // Identify the structure used
256 if ( is_array( $items ) ) {
258 // Ignore headers that are recursively set as legacy header
259 if ( $header !== 'statistics-header-hooks' ) {
260 $return .= $this->formatRowHeader( $header );
263 // Collect all items that belong to the same header
264 foreach ( $items as $key => $value ) {
265 if ( is_array( $value ) ) {
266 $name = $value['name'];
267 $number = $value['number'];
269 $name = $this->msg( $key )->parse();
273 $return .= $this->formatRow(
275 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
276 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
280 // Create the legacy header only once
281 if ( $return === '' ) {
282 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
285 // Recursively remap the legacy structure
286 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
287 array( $header => $items ) ) );
297 * @param string $header
300 private function formatRowHeader( $header ) {
301 return Xml
::openElement( 'tr' ) .
302 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
303 Xml
::closeElement( 'tr' );
306 protected function getGroupName() {