60b98d3acbc8e50348efdd927e4f7aaad6aeedee
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' ) ) .
157 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
158 $wgLang->formatNum( $this->numJobs
),
159 array( 'class' => 'mw-statistics-jobqueue' ) );
161 private function getUserStats() {
162 global $wgLang, $wgUser, $wgRCMaxAge;
163 $sk = $wgUser->getSkin();
164 return Xml
::openElement( 'tr' ) .
165 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
166 Xml
::closeElement( 'tr' ) .
167 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
168 $wgLang->formatNum( $this->users
),
169 array( 'class' => 'mw-statistics-users' ) ) .
170 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
172 SpecialPage
::getTitleFor( 'Activeusers' ),
173 wfMsgHtml( 'listgrouprights-members' ),
178 $wgLang->formatNum( $this->activeUsers
),
179 array( 'class' => 'mw-statistics-users-active' ),
180 'statistics-users-active-desc',
181 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
183 private function getGroupStats() {
184 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
185 $sk = $wgUser->getSkin();
187 foreach( $wgGroupPermissions as $group => $permissions ) {
188 # Skip generic * and implicit groups
189 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
192 $groupname = htmlspecialchars( $group );
193 $msg = wfMsg( 'group-' . $groupname );
194 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
195 $groupnameLocalized = $groupname;
197 $groupnameLocalized = $msg;
199 $msg = wfMsgForContent( 'grouppage-' . $groupname );
200 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
201 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
203 $grouppageLocalized = $msg;
205 $linkTarget = Title
::newFromText( $grouppageLocalized );
206 $grouppage = $sk->link(
208 htmlspecialchars( $groupnameLocalized )
210 $grouplink = $sk->link(
211 SpecialPage
::getTitleFor( 'Listusers' ),
212 wfMsgHtml( 'listgrouprights-members' ),
214 array( 'group' => $group ),
217 # Add a class when a usergroup contains no members to allow hiding these rows
219 $countUsers = SiteStats
::numberingroup( $groupname );
220 if( $countUsers == 0 ) {
221 $classZero = ' statistics-group-zero';
223 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
224 $wgLang->formatNum( $countUsers ),
225 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
229 private function getViewsStats() {
231 return Xml
::openElement( 'tr' ) .
232 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
233 Xml
::closeElement( 'tr' ) .
234 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
235 $wgLang->formatNum( $this->views
),
236 array ( 'class' => 'mw-statistics-views-total' ) ) .
237 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
238 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
239 $this->views
/ $this->edits
: 0 ) ),
240 array ( 'class' => 'mw-statistics-views-peredit' ) );
242 private function getMostViewedPages() {
243 global $wgLang, $wgUser;
245 $dbr = wfGetDB( DB_SLAVE
);
246 $sk = $wgUser->getSkin();
255 'page_is_redirect' => 0,
260 'ORDER BY' => 'page_counter DESC',
264 if( $res->numRows() > 0 ) {
265 $text .= Xml
::openElement( 'tr' );
266 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
267 $text .= Xml
::closeElement( 'tr' );
268 while( $row = $res->fetchObject() ) {
269 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
270 if( $title instanceof Title
) {
271 $text .= $this->formatRow( $sk->link( $title ),
272 $wgLang->formatNum( $row->page_counter
) );
281 private function getOtherStats( $stats ) {
284 if ( !count( $stats ) )
287 $return = Xml
::openElement( 'tr' ) .
288 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
289 Xml
::closeElement( 'tr' );
291 foreach( $stats as $name => $number ) {
292 $name = htmlspecialchars( $name );
293 $number = htmlspecialchars( $number );
295 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
302 * Do the action=raw output for this page. Legacy, but we support
303 * it for backwards compatibility
304 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
306 private function doRawOutput() {
309 header( 'Pragma: nocache' );
310 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
311 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
312 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
313 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";