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();
45 if( !$wgDisableCounters ) {
46 $viewsStats = $this->getViewsStats();
49 # Set active user count
51 $dbw = wfGetDB( DB_MASTER
);
52 SiteStatsUpdate
::cacheUpdate( $dbw );
56 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
60 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
63 $text .= $this->getPageStats();
66 $text .= $this->getEditStats();
69 $text .= $this->getUserStats();
71 # Statistic - usergroups
72 $text .= $this->getGroupStats();
75 # Statistic - popular pages
76 if( !$wgDisableCounters && !$wgMiserMode ) {
77 $text .= $this->getMostViewedPages();
81 $extraStats = array();
82 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
83 $text .= $this->getOtherStats( $extraStats );
86 $text .= Xml
::closeElement( 'table' );
89 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
90 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
91 $text .= "\n" . $footer;
94 $wgOut->addHTML( $text );
99 * @param string $text description of the row
100 * @param float $number a number
101 * @param array $trExtraParams
102 * @param string $descMsg
103 * @param string $descMsgParam
104 * @return string table row in HTML format
106 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
109 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
110 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
111 $descriptionText = " ($descriptionText)";
112 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
116 return Xml
::openElement( 'tr', $trExtraParams ) .
117 Xml
::openElement( 'td' ) . $text . Xml
::closeElement( 'td' ) .
118 Xml
::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml
::closeElement( 'td' ) .
119 Xml
::closeElement( 'tr' );
123 * Each of these methods is pretty self-explanatory, get a particular
124 * row for the table of statistics
127 private function getPageStats() {
129 return Xml
::openElement( 'tr' ) .
130 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
131 Xml
::closeElement( 'tr' ) .
132 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
133 $wgLang->formatNum( $this->good
),
134 array( 'class' => 'mw-statistics-articles' ) ) .
135 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
136 $wgLang->formatNum( $this->total
),
137 array( 'class' => 'mw-statistics-pages' ),
138 'statistics-pages-desc' ) .
139 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
140 $wgLang->formatNum( $this->images
),
141 array( 'class' => 'mw-statistics-files' ) );
143 private function getEditStats() {
145 return Xml
::openElement( 'tr' ) .
146 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
147 Xml
::closeElement( 'tr' ) .
148 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
149 $wgLang->formatNum( $this->edits
),
150 array( 'class' => 'mw-statistics-edits' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
152 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
153 array( 'class' => 'mw-statistics-edits-average' ) ) .
154 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
155 $wgLang->formatNum( $this->numJobs
),
156 array( 'class' => 'mw-statistics-jobqueue' ) );
158 private function getUserStats() {
159 global $wgLang, $wgRCMaxAge;
160 return Xml
::openElement( 'tr' ) .
161 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
162 Xml
::closeElement( 'tr' ) .
163 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
164 $wgLang->formatNum( $this->users
),
165 array( 'class' => 'mw-statistics-users' ) ) .
166 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
167 $wgLang->formatNum( $this->activeUsers
),
168 array( 'class' => 'mw-statistics-users-active' ),
169 'statistics-users-active-desc',
170 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
172 private function getGroupStats() {
173 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
174 $sk = $wgUser->getSkin();
176 foreach( $wgGroupPermissions as $group => $permissions ) {
177 # Skip generic * and implicit groups
178 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
181 $groupname = htmlspecialchars( $group );
182 $msg = wfMsg( 'group-' . $groupname );
183 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
184 $groupnameLocalized = $groupname;
186 $groupnameLocalized = $msg;
188 $msg = wfMsgForContent( 'grouppage-' . $groupname );
189 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
190 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
192 $grouppageLocalized = $msg;
194 $linkTarget = Title
::newFromText( $grouppageLocalized );
195 $grouppage = $sk->link(
197 htmlspecialchars( $groupnameLocalized )
199 $grouplink = $sk->link(
200 SpecialPage
::getTitleFor( 'Listusers' ),
201 wfMsgHtml( 'listgrouprights-members' ),
203 array( 'group' => $group ),
206 # Add a class when a usergroup contains no members to allow hiding these rows
208 $countUsers = SiteStats
::numberingroup( $groupname );
209 if( $countUsers == 0 ) {
210 $classZero = ' statistics-group-zero';
212 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
213 $wgLang->formatNum( $countUsers ),
214 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
218 private function getViewsStats() {
220 return Xml
::openElement( 'tr' ) .
221 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
222 Xml
::closeElement( 'tr' ) .
223 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
224 $wgLang->formatNum( $this->views
),
225 array ( 'class' => 'mw-statistics-views-total' ) ) .
226 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
227 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
228 $this->views
/ $this->edits
: 0 ) ),
229 array ( 'class' => 'mw-statistics-views-peredit' ) );
231 private function getMostViewedPages() {
232 global $wgLang, $wgUser;
234 $dbr = wfGetDB( DB_SLAVE
);
235 $sk = $wgUser->getSkin();
244 'page_is_redirect' => 0,
249 'ORDER BY' => 'page_counter DESC',
253 if( $res->numRows() > 0 ) {
254 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
255 while( $row = $res->fetchObject() ) {
256 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
257 if( $title instanceof Title
) {
258 $text .= $this->formatRow( $sk->link( $title ),
259 $wgLang->formatNum( $row->page_counter
) );
268 private function getOtherStats( $stats ) {
269 global $wgLang, $wgAllowStatsOther, $wgStatsOther;
271 $return = Xml
::openElement( 'tr' ) .
272 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
273 Xml
::closeElement( 'tr' );
275 foreach( $stats as $name => $number ) {
276 $name = htmlspecialchars( $name );
277 $number = htmlspecialchars( $number );
279 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
286 * Do the action=raw output for this page. Legacy, but we support
287 * it for backwards compatibility
288 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
290 private function doRawOutput() {
293 header( 'Pragma: nocache' );
294 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
295 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
296 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
297 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";