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 $text .= $this->getOtherStats();
83 $text .= Xml
::closeElement( 'table' );
86 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
87 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
88 $text .= "\n" . $footer;
91 $wgOut->addHTML( $text );
96 * @param string $text description of the row
97 * @param float $number a number
98 * @param array $trExtraParams
99 * @param string $descMsg
100 * @param string $descMsgParam
101 * @return string table row in HTML format
103 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
106 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
107 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
108 $descriptionText = " ($descriptionText)";
109 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
113 return Xml
::openElement( 'tr', $trExtraParams ) .
114 Xml
::openElement( 'td' ) . $text . Xml
::closeElement( 'td' ) .
115 Xml
::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml
::closeElement( 'td' ) .
116 Xml
::closeElement( 'tr' );
120 * Each of these methods is pretty self-explanatory, get a particular
121 * row for the table of statistics
124 private function getPageStats() {
126 return Xml
::openElement( 'tr' ) .
127 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
128 Xml
::closeElement( 'tr' ) .
129 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
130 $wgLang->formatNum( $this->good
),
131 array( 'class' => 'mw-statistics-articles' ) ) .
132 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
133 $wgLang->formatNum( $this->total
),
134 array( 'class' => 'mw-statistics-pages' ),
135 'statistics-pages-desc' ) .
136 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
137 $wgLang->formatNum( $this->images
),
138 array( 'class' => 'mw-statistics-files' ) );
140 private function getEditStats() {
142 return Xml
::openElement( 'tr' ) .
143 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
144 Xml
::closeElement( 'tr' ) .
145 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
146 $wgLang->formatNum( $this->edits
),
147 array( 'class' => 'mw-statistics-edits' ) ) .
148 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
149 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
150 array( 'class' => 'mw-statistics-edits-average' ) ) .
151 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->numJobs
),
153 array( 'class' => 'mw-statistics-jobqueue' ) );
155 private function getUserStats() {
156 global $wgLang, $wgRCMaxAge;
157 return Xml
::openElement( 'tr' ) .
158 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
159 Xml
::closeElement( 'tr' ) .
160 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
161 $wgLang->formatNum( $this->users
),
162 array( 'class' => 'mw-statistics-users' ) ) .
163 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
164 $wgLang->formatNum( $this->activeUsers
),
165 array( 'class' => 'mw-statistics-users-active' ),
166 'statistics-users-active-desc',
167 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
169 private function getGroupStats() {
170 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
171 $sk = $wgUser->getSkin();
173 foreach( $wgGroupPermissions as $group => $permissions ) {
174 # Skip generic * and implicit groups
175 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
178 $groupname = htmlspecialchars( $group );
179 $msg = wfMsg( 'group-' . $groupname );
180 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
181 $groupnameLocalized = $groupname;
183 $groupnameLocalized = $msg;
185 $msg = wfMsgForContent( 'grouppage-' . $groupname );
186 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
187 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
189 $grouppageLocalized = $msg;
191 $linkTarget = Title
::newFromText( $grouppageLocalized );
192 $grouppage = $sk->link(
194 htmlspecialchars( $groupnameLocalized )
196 $grouplink = $sk->link(
197 SpecialPage
::getTitleFor( 'Listusers' ),
198 wfMsgHtml( 'listgrouprights-members' ),
200 array( 'group' => $group ),
203 # Add a class when a usergroup contains no members to allow hiding these rows
205 $countUsers = SiteStats
::numberingroup( $groupname );
206 if( $countUsers == 0 ) {
207 $classZero = ' statistics-group-zero';
209 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
210 $wgLang->formatNum( $countUsers ),
211 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
215 private function getViewsStats() {
217 return Xml
::openElement( 'tr' ) .
218 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
219 Xml
::closeElement( 'tr' ) .
220 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
221 $wgLang->formatNum( $this->views
),
222 array ( 'class' => 'mw-statistics-views-total' ) ) .
223 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
224 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
225 $this->views
/ $this->edits
: 0 ) ),
226 array ( 'class' => 'mw-statistics-views-peredit' ) );
228 private function getMostViewedPages() {
229 global $wgLang, $wgUser;
231 $dbr = wfGetDB( DB_SLAVE
);
232 $sk = $wgUser->getSkin();
241 'page_is_redirect' => 0,
246 'ORDER BY' => 'page_counter DESC',
250 if( $res->numRows() > 0 ) {
251 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
252 while( $row = $res->fetchObject() ) {
253 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
254 if( $title instanceof Title
) {
255 $text .= $this->formatRow( $sk->link( $title ),
256 $wgLang->formatNum( $row->page_counter
) );
265 private function getOtherStats() {
266 global $wgLang, $wgAllowStatsOther, $wgStatsOther;
268 if( !$wgAllowStatsOther ) return;
270 if ( count( $wgStatsOther ) < 1 ) return;
272 $return = Xml
::openElement( 'tr' ) .
273 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
274 Xml
::closeElement( 'tr' );
276 foreach( $wgStatsOther as $name => $number ) {
277 $name = htmlspecialchars( $name );
278 $number = htmlspecialchars( $number );
280 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
287 * Do the action=raw output for this page. Legacy, but we support
288 * it for backwards compatibility
289 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
291 private function doRawOutput() {
294 header( 'Pragma: nocache' );
295 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
296 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
297 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
298 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";