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 $views, $edits, $good, $images, $total, $users,
34 public function __construct() {
35 parent
::__construct( 'Statistics' );
38 public function execute( $par ) {
39 global $wgMemc, $wgDisableCounters, $wgMiserMode;
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
44 $this->views
= SiteStats
::views();
45 $this->edits
= SiteStats
::edits();
46 $this->good
= SiteStats
::articles();
47 $this->images
= SiteStats
::images();
48 $this->total
= SiteStats
::pages();
49 $this->users
= SiteStats
::users();
50 $this->activeUsers
= SiteStats
::activeUsers();
55 if ( !$wgDisableCounters ) {
56 $viewsStats = $this->getViewsStats();
59 # Set active user count
60 if ( !$wgMiserMode ) {
61 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
62 // Re-calculate the count if the last tally is old...
63 if ( !$wgMemc->get( $key ) ) {
64 $dbw = wfGetDB( DB_MASTER
);
65 SiteStatsUpdate
::cacheUpdate( $dbw );
66 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
70 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
73 $text .= $this->getPageStats();
76 $text .= $this->getEditStats();
79 $text .= $this->getUserStats();
81 # Statistic - usergroups
82 $text .= $this->getGroupStats();
85 # Statistic - popular pages
86 if ( !$wgDisableCounters && !$wgMiserMode ) {
87 $text .= $this->getMostViewedPages();
91 $extraStats = array();
92 if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
93 $text .= $this->getOtherStats( $extraStats );
96 $text .= Xml
::closeElement( 'table' );
99 $footer = $this->msg( 'statistics-footer' );
100 if ( !$footer->isBlank() ) {
101 $text .= "\n" . $footer->parse();
104 $this->getOutput()->addHTML( $text );
109 * @param string $text Description of the row
110 * @param float $number A statistical number
111 * @param array $trExtraParams Params to table row, see Html::elememt
112 * @param string $descMsg Message key
113 * @param array|string $descMsgParam Message parameters
114 * @return string Table row in HTML format
116 private function formatRow( $text, $number, $trExtraParams = array(),
117 $descMsg = '', $descMsgParam = ''
120 $msg = $this->msg( $descMsg, $descMsgParam );
121 if ( $msg->exists() ) {
122 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
123 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc' ),
124 " $descriptionText" );
128 return Html
::rawElement( 'tr', $trExtraParams,
129 Html
::rawElement( 'td', array(), $text ) .
130 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
135 * Each of these methods is pretty self-explanatory, get a particular
136 * row for the table of statistics
139 private function getPageStats() {
140 return Xml
::openElement( 'tr' ) .
141 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
142 Xml
::closeElement( 'tr' ) .
143 $this->formatRow( Linker
::linkKnown( SpecialPage
::getTitleFor( 'Allpages' ),
144 $this->msg( 'statistics-articles' )->parse() ),
145 $this->getLanguage()->formatNum( $this->good
),
146 array( 'class' => 'mw-statistics-articles' ) ) .
147 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
148 $this->getLanguage()->formatNum( $this->total
),
149 array( 'class' => 'mw-statistics-pages' ),
150 'statistics-pages-desc' ) .
151 $this->formatRow( Linker
::linkKnown( SpecialPage
::getTitleFor( 'Listfiles' ),
152 $this->msg( 'statistics-files' )->parse() ),
153 $this->getLanguage()->formatNum( $this->images
),
154 array( 'class' => 'mw-statistics-files' ) );
157 private function getEditStats() {
158 return Xml
::openElement( 'tr' ) .
159 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
160 Xml
::closeElement( 'tr' ) .
161 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
162 $this->getLanguage()->formatNum( $this->edits
),
163 array( 'class' => 'mw-statistics-edits' )
165 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
167 ->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
168 array( 'class' => 'mw-statistics-edits-average' )
172 private function getUserStats() {
173 global $wgActiveUserDays;
175 return Xml
::openElement( 'tr' ) .
176 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
177 Xml
::closeElement( 'tr' ) .
178 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
179 $this->getLanguage()->formatNum( $this->users
),
180 array( 'class' => 'mw-statistics-users' )
182 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
184 SpecialPage
::getTitleFor( 'Activeusers' ),
185 $this->msg( 'listgrouprights-members' )->escaped()
187 $this->getLanguage()->formatNum( $this->activeUsers
),
188 array( 'class' => 'mw-statistics-users-active' ),
189 'statistics-users-active-desc',
190 $this->getLanguage()->formatNum( $wgActiveUserDays )
194 private function getGroupStats() {
195 global $wgGroupPermissions, $wgImplicitGroups;
197 foreach ( $wgGroupPermissions as $group => $permissions ) {
198 # Skip generic * and implicit groups
199 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
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 = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
213 $grouppageLocalized = $msg->text();
215 $linkTarget = Title
::newFromText( $grouppageLocalized );
216 $grouppage = Linker
::link(
218 htmlspecialchars( $groupnameLocalized )
220 $grouplink = Linker
::linkKnown(
221 SpecialPage
::getTitleFor( 'Listusers' ),
222 $this->msg( 'listgrouprights-members' )->escaped(),
224 array( 'group' => $group )
226 # Add a class when a usergroup contains no members to allow hiding these rows
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 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
240 private function getViewsStats() {
241 return Xml
::openElement( 'tr' ) .
242 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
243 Xml
::closeElement( 'tr' ) .
244 $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
245 $this->getLanguage()->formatNum( $this->views
),
246 array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
247 $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
248 $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
249 $this->views
/ $this->edits
: 0 ) ),
250 array( 'class' => 'mw-statistics-views-peredit' ) );
253 private function getMostViewedPages() {
255 $dbr = wfGetDB( DB_SLAVE
);
264 'page_is_redirect' => 0,
269 'ORDER BY' => 'page_counter DESC',
274 if ( $res->numRows() > 0 ) {
275 $text .= Xml
::openElement( 'tr' );
278 array( 'colspan' => '2' ),
279 $this->msg( 'statistics-mostpopular' )->parse()
281 $text .= Xml
::closeElement( 'tr' );
283 foreach ( $res as $row ) {
284 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
286 if ( $title instanceof Title
) {
287 $text .= $this->formatRow( Linker
::link( $title ),
288 $this->getLanguage()->formatNum( $row->page_counter
) );
298 * Conversion of external statistics into an internal representation
299 * Following a ([<header-message>][<item-message>] = number) pattern
301 * @param array $stats
304 private function getOtherStats( array $stats ) {
307 foreach ( $stats as $header => $items ) {
308 // Identify the structure used
309 if ( is_array( $items ) ) {
311 // Ignore headers that are recursively set as legacy header
312 if ( $header !== 'statistics-header-hooks' ) {
313 $return .= $this->formatRowHeader( $header );
316 // Collect all items that belong to the same header
317 foreach ( $items as $key => $value ) {
318 $name = $this->msg( $key )->parse();
319 $number = htmlspecialchars( $value );
321 $return .= $this->formatRow(
323 $this->getLanguage()->formatNum( $number ),
324 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
328 // Create the legacy header only once
329 if ( $return === '' ) {
330 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
333 // Recursively remap the legacy structure
334 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
335 array( $header => $items ) ) );
345 * @param string $header
348 private function formatRowHeader( $header ) {
349 return Xml
::openElement( 'tr' ) .
350 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
351 Xml
::closeElement( 'tr' );
354 protected function getGroupName() {