4 * Special page lists various statistics, including the contents of
5 * `site_stats`, plus page view details if enabled
12 * Some statistics about the wiki
14 class SpecialStatistics
extends SpecialPage
{
16 private $views, $edits, $good, $images, $total, $users,
17 $activeUsers, $admins, $numJobs = 0;
19 public function __construct() {
20 parent
::__construct( 'Statistics' );
23 public function execute( $par ) {
24 global $wgOut, $wgRequest, $wgMessageCache, $wgMemc;
25 global $wgDisableCounters, $wgMiserMode;
29 $this->views
= SiteStats
::views();
30 $this->edits
= SiteStats
::edits();
31 $this->good
= SiteStats
::articles();
32 $this->images
= SiteStats
::images();
33 $this->total
= SiteStats
::pages();
34 $this->users
= SiteStats
::users();
35 $this->activeUsers
= SiteStats
::activeUsers();
36 $this->admins
= SiteStats
::numberingroup('sysop');
37 $this->numJobs
= SiteStats
::jobs();
42 if( !$wgDisableCounters ) {
43 $viewsStats = $this->getViewsStats();
46 # Set active user count
48 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
49 // Re-calculate the count if the last tally is old...
50 if( !$wgMemc->get($key) ) {
51 $dbw = wfGetDB( DB_MASTER
);
52 SiteStatsUpdate
::cacheUpdate( $dbw );
53 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
58 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
62 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
65 $text .= $this->getPageStats();
68 $text .= $this->getEditStats();
71 $text .= $this->getUserStats();
73 # Statistic - usergroups
74 $text .= $this->getGroupStats();
77 # Statistic - popular pages
78 if( !$wgDisableCounters && !$wgMiserMode ) {
79 $text .= $this->getMostViewedPages();
83 $extraStats = array();
84 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
85 $text .= $this->getOtherStats( $extraStats );
88 $text .= Xml
::closeElement( 'table' );
91 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
92 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
93 $text .= "\n" . $footer;
96 $wgOut->addHTML( $text );
101 * @param $text String: description of the row
102 * @param $number Float: a statistical number
103 * @param $trExtraParams Array: params to table row, see Html::elememt
104 * @param $descMsg String: message key
105 * @param $descMsgParam Array: message params
106 * @return string table row in HTML format
108 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
111 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
112 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
113 $descriptionText = " ($descriptionText)";
114 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
119 Html
::rawElement( 'tr', $trExtraParams,
120 Html
::rawElement( 'td', array(), $text ) .
121 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
126 * Each of these methods is pretty self-explanatory, get a particular
127 * row for the table of statistics
130 private function getPageStats() {
132 return Xml
::openElement( 'tr' ) .
133 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
134 Xml
::closeElement( 'tr' ) .
135 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
136 $wgLang->formatNum( $this->good
),
137 array( 'class' => 'mw-statistics-articles' ) ) .
138 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
139 $wgLang->formatNum( $this->total
),
140 array( 'class' => 'mw-statistics-pages' ),
141 'statistics-pages-desc' ) .
142 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
143 $wgLang->formatNum( $this->images
),
144 array( 'class' => 'mw-statistics-files' ) );
146 private function getEditStats() {
148 return Xml
::openElement( 'tr' ) .
149 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
150 Xml
::closeElement( 'tr' ) .
151 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->edits
),
153 array( 'class' => 'mw-statistics-edits' ) ) .
154 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
155 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
156 array( 'class' => 'mw-statistics-edits-average' ) );
159 private function getUserStats() {
160 global $wgLang, $wgUser, $wgRCMaxAge;
161 $sk = $wgUser->getSkin();
162 return Xml
::openElement( 'tr' ) .
163 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
164 Xml
::closeElement( 'tr' ) .
165 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
166 $wgLang->formatNum( $this->users
),
167 array( 'class' => 'mw-statistics-users' ) ) .
168 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
170 SpecialPage
::getTitleFor( 'Activeusers' ),
171 wfMsgHtml( 'listgrouprights-members' ),
176 $wgLang->formatNum( $this->activeUsers
),
177 array( 'class' => 'mw-statistics-users-active' ),
178 'statistics-users-active-desc',
179 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
181 private function getGroupStats() {
182 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
183 $sk = $wgUser->getSkin();
185 foreach( $wgGroupPermissions as $group => $permissions ) {
186 # Skip generic * and implicit groups
187 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
190 $groupname = htmlspecialchars( $group );
191 $msg = wfMsg( 'group-' . $groupname );
192 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
193 $groupnameLocalized = $groupname;
195 $groupnameLocalized = $msg;
197 $msg = wfMsgForContent( 'grouppage-' . $groupname );
198 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
199 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
201 $grouppageLocalized = $msg;
203 $linkTarget = Title
::newFromText( $grouppageLocalized );
204 $grouppage = $sk->link(
206 htmlspecialchars( $groupnameLocalized )
208 $grouplink = $sk->link(
209 SpecialPage
::getTitleFor( 'Listusers' ),
210 wfMsgHtml( 'listgrouprights-members' ),
212 array( 'group' => $group ),
215 # Add a class when a usergroup contains no members to allow hiding these rows
217 $countUsers = SiteStats
::numberingroup( $groupname );
218 if( $countUsers == 0 ) {
219 $classZero = ' statistics-group-zero';
221 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
222 $wgLang->formatNum( $countUsers ),
223 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
227 private function getViewsStats() {
229 return Xml
::openElement( 'tr' ) .
230 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
231 Xml
::closeElement( 'tr' ) .
232 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
233 $wgLang->formatNum( $this->views
),
234 array ( 'class' => 'mw-statistics-views-total' ) ) .
235 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
236 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
237 $this->views
/ $this->edits
: 0 ) ),
238 array ( 'class' => 'mw-statistics-views-peredit' ) );
240 private function getMostViewedPages() {
241 global $wgLang, $wgUser;
243 $dbr = wfGetDB( DB_SLAVE
);
244 $sk = $wgUser->getSkin();
253 'page_is_redirect' => 0,
258 'ORDER BY' => 'page_counter DESC',
262 if( $res->numRows() > 0 ) {
263 $text .= Xml
::openElement( 'tr' );
264 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
265 $text .= Xml
::closeElement( 'tr' );
266 while( $row = $res->fetchObject() ) {
267 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
268 if( $title instanceof Title
) {
269 $text .= $this->formatRow( $sk->link( $title ),
270 $wgLang->formatNum( $row->page_counter
) );
279 private function getOtherStats( $stats ) {
282 if ( !count( $stats ) )
285 $return = Xml
::openElement( 'tr' ) .
286 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
287 Xml
::closeElement( 'tr' );
289 foreach( $stats as $name => $number ) {
290 $name = htmlspecialchars( $name );
291 $number = htmlspecialchars( $number );
293 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
300 * Do the action=raw output for this page. Legacy, but we support
301 * it for backwards compatibility
302 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
304 private function doRawOutput() {
307 header( 'Pragma: nocache' );
308 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
309 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
310 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
311 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";