4 * Special page lists various statistics, including the contents of
5 * `site_stats`, plus page view details if enabled
12 * Show the special page
14 * @param mixed $par (not used)
16 class SpecialStatistics
extends SpecialPage
{
18 private $views, $edits, $good, $images, $total, $users,
19 $activeUsers, $admins, $numJobs = 0;
21 public function __construct() {
22 parent
::__construct( 'Statistics' );
25 public function execute( $par ) {
26 global $wgOut, $wgRequest, $wgMessageCache;
27 global $wgDisableCounters, $wgMiserMode;
28 $wgMessageCache->loadAllMessages();
32 $this->views
= SiteStats
::views();
33 $this->edits
= SiteStats
::edits();
34 $this->good
= SiteStats
::articles();
35 $this->images
= SiteStats
::images();
36 $this->total
= SiteStats
::pages();
37 $this->users
= SiteStats
::users();
38 $this->activeUsers
= SiteStats
::activeUsers();
39 $this->admins
= SiteStats
::numberingroup('sysop');
40 $this->numJobs
= SiteStats
::jobs();
44 if( !$wgDisableCounters ) {
45 $viewsStats = $this->getViewsStats();
48 # Set active user count
50 $dbw = wfGetDB( DB_MASTER
);
51 SiteStatsUpdate
::cacheUpdate( $dbw );
55 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
59 $text = Xml
::openElement( 'table', array( 'class' => 'mw-statistics-table' ) );
62 $text .= $this->getPageStats();
65 $text .= $this->getEditStats();
68 $text .= $this->getUserStats();
70 # Statistic - usergroups
71 $text .= $this->getGroupStats();
74 # Statistic - popular pages
75 if( !$wgDisableCounters && !$wgMiserMode ) {
76 $text .= $this->getMostViewedPages();
79 $text .= Xml
::closeElement( 'table' );
82 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
83 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
84 $text .= "\n" . $footer;
87 $wgOut->addHTML( $text );
92 * @param string $text description of the row
93 * @param float $number a number
94 * @return string table row in HTML format
96 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '' ) {
100 $descriptionText = wfMsg( $descMsg );
101 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
102 $descriptionText = " ($descriptionText)";
103 $text = $text . "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'), $descriptionText );
107 return Xml
::openElement( 'tr', $trExtraParams ) .
108 Xml
::openElement( 'td' ) . $text . Xml
::closeElement( 'td' ) .
109 Xml
::openElement( 'td' ) . $number . Xml
::closeElement( 'td' ) .
110 Xml
::closeElement( 'tr' );
114 * Each of these methods is pretty self-explanatory, get a particular
115 * row for the table of statistics
118 private function getPageStats() {
120 return Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-pages' ) ) .
121 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
122 $wgLang->formatNum( $this->good
),
123 array( 'class' => 'mw-statistics-articles' ) ) .
124 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
125 $wgLang->formatNum( $this->total
),
126 array( 'class' => 'mw-statistics-pages' ),
127 'statistics-pages-desc' ) .
128 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
129 $wgLang->formatNum( $this->images
),
130 array( 'class' => 'mw-statistics-files' ) );
132 private function getEditStats() {
134 return Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-edits' ) ) .
135 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
136 $wgLang->formatNum( $this->edits
),
137 array( 'class' => 'mw-statistics-edits' ) ) .
138 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
139 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
140 array( 'class' => 'mw-statistics-edits-average' ) ) .
141 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
142 $wgLang->formatNum( $this->numJobs
),
143 array( 'class' => 'mw-statistics-jobqueue' ) );
145 private function getUserStats() {
147 return Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-users' ) ) .
148 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
149 $wgLang->formatNum( $this->users
),
150 array( 'class' => 'mw-statistics-users' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->activeUsers
),
153 array( 'class' => 'mw-statistics-users-active' ),
154 'statistics-users-active-desc' );
156 private function getGroupStats() {
157 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
158 $sk = $wgUser->getSkin();
160 foreach( $wgGroupPermissions as $group => $permissions ) {
161 # Skip generic * and implicit groups
162 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
165 $groupname = htmlspecialchars( $group );
166 $msg = wfMsg( 'group-' . $groupname );
167 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
168 $groupnameLocalized = $groupname;
170 $groupnameLocalized = $msg;
172 $msg = wfMsgForContent( 'grouppage-' . $groupname );
173 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
174 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
176 $grouppageLocalized = $msg;
178 $grouppage = $sk->makeLink( $grouppageLocalized, $groupnameLocalized );
179 $grouplink = $sk->link( SpecialPage
::getTitleFor( 'Listusers' ),
180 wfMsgHtml( 'listgrouprights-members' ),
182 array( 'group' => $group ),
184 # Add a class when a usergroup contains no members to allow hiding these rows
186 $countUsers = SiteStats
::numberingroup( $groupname );
187 if( $countUsers == 0 ) {
188 $classZero = ' statistics-group-zero';
190 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
191 $wgLang->formatNum( $countUsers ),
192 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
196 private function getViewsStats() {
198 return Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-header-views' ) ) .
199 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
200 $wgLang->formatNum( $this->views
),
201 array ( 'class' => 'mw-statistics-views-total' ) ) .
202 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
203 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
204 $this->views
/ $this->edits
: 0 ) ),
205 array ( 'class' => 'mw-statistics-views-peredit' ) );
207 private function getMostViewedPages() {
208 global $wgLang, $wgUser;
210 $dbr = wfGetDB( DB_SLAVE
);
211 $sk = $wgUser->getSkin();
220 'page_is_redirect' => 0,
225 'ORDER BY' => 'page_counter DESC',
229 if( $res->numRows() > 0 ) {
230 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsg( 'statistics-mostpopular' ) );
231 while( $row = $res->fetchObject() ) {
232 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
233 if( $title instanceof Title
) {
234 $text .= $this->formatRow( $sk->link( $title ),
235 $wgLang->formatNum( $row->page_counter
) );
245 * Do the action=raw output for this page. Legacy, but we support
246 * it for backwards compatibility
247 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
249 private function doRawOutput() {
252 header( 'Pragma: nocache' );
253 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
254 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
255 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
256 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";