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 ) {
41 $disableCounters = $this->getConfig()->get( 'DisableCounters' );
42 $miserMode = $this->getConfig()->get( 'MiserMode' );
45 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
47 $this->views
= SiteStats
::views();
48 $this->edits
= SiteStats
::edits();
49 $this->good
= SiteStats
::articles();
50 $this->images
= SiteStats
::images();
51 $this->total
= SiteStats
::pages();
52 $this->users
= SiteStats
::users();
53 $this->activeUsers
= SiteStats
::activeUsers();
58 if ( !$disableCounters ) {
59 $viewsStats = $this->getViewsStats();
62 # Set active user count
64 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
65 // Re-calculate the count if the last tally is old...
66 if ( !$wgMemc->get( $key ) ) {
67 $dbw = wfGetDB( DB_MASTER
);
68 SiteStatsUpdate
::cacheUpdate( $dbw );
69 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
73 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
76 $text .= $this->getPageStats();
79 $text .= $this->getEditStats();
82 $text .= $this->getUserStats();
84 # Statistic - usergroups
85 $text .= $this->getGroupStats();
88 # Statistic - popular pages
89 if ( !$disableCounters && !$miserMode ) {
90 $text .= $this->getMostViewedPages();
94 $extraStats = array();
95 if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
96 $text .= $this->getOtherStats( $extraStats );
99 $text .= Xml
::closeElement( 'table' );
101 # Customizable footer
102 $footer = $this->msg( 'statistics-footer' );
103 if ( !$footer->isBlank() ) {
104 $text .= "\n" . $footer->parse();
107 $this->getOutput()->addHTML( $text );
112 * @param string $text Description of the row
113 * @param float $number A statistical number
114 * @param array $trExtraParams Params to table row, see Html::elememt
115 * @param string $descMsg Message key
116 * @param array|string $descMsgParam Message parameters
117 * @return string Table row in HTML format
119 private function formatRow( $text, $number, $trExtraParams = array(),
120 $descMsg = '', $descMsgParam = ''
123 $msg = $this->msg( $descMsg, $descMsgParam );
124 if ( $msg->exists() ) {
125 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
126 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc' ),
127 " $descriptionText" );
131 return Html
::rawElement( 'tr', $trExtraParams,
132 Html
::rawElement( 'td', array(), $text ) .
133 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
138 * Each of these methods is pretty self-explanatory, get a particular
139 * row for the table of statistics
142 private function getPageStats() {
143 return Xml
::openElement( 'tr' ) .
144 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
145 Xml
::closeElement( 'tr' ) .
146 $this->formatRow( Linker
::linkKnown( SpecialPage
::getTitleFor( 'Allpages' ),
147 $this->msg( 'statistics-articles' )->parse() ),
148 $this->getLanguage()->formatNum( $this->good
),
149 array( 'class' => 'mw-statistics-articles' ) ) .
150 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
151 $this->getLanguage()->formatNum( $this->total
),
152 array( 'class' => 'mw-statistics-pages' ),
153 'statistics-pages-desc' ) .
154 $this->formatRow( Linker
::linkKnown( SpecialPage
::getTitleFor( 'Listfiles' ),
155 $this->msg( 'statistics-files' )->parse() ),
156 $this->getLanguage()->formatNum( $this->images
),
157 array( 'class' => 'mw-statistics-files' ) );
160 private function getEditStats() {
161 return Xml
::openElement( 'tr' ) .
162 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
163 Xml
::closeElement( 'tr' ) .
164 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
165 $this->getLanguage()->formatNum( $this->edits
),
166 array( 'class' => 'mw-statistics-edits' )
168 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
170 ->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
171 array( 'class' => 'mw-statistics-edits-average' )
175 private function getUserStats() {
176 return Xml
::openElement( 'tr' ) .
177 Xml
::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
178 Xml
::closeElement( 'tr' ) .
179 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
180 $this->getLanguage()->formatNum( $this->users
),
181 array( 'class' => 'mw-statistics-users' )
183 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
185 SpecialPage
::getTitleFor( 'Activeusers' ),
186 $this->msg( 'listgrouprights-members' )->escaped()
188 $this->getLanguage()->formatNum( $this->activeUsers
),
189 array( 'class' => 'mw-statistics-users-active' ),
190 'statistics-users-active-desc',
191 $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
195 private function getGroupStats() {
197 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
198 # Skip generic * and implicit groups
199 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) ||
$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() {