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 * @param array $trExtraParams
95 * @param string $descMsg
96 * @param string $descMsgParam
97 * @return string table row in HTML format
99 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
102 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
103 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
104 $descriptionText = " ($descriptionText)";
105 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
109 return Xml
::openElement( 'tr', $trExtraParams ) .
110 Xml
::openElement( 'td' ) . $text . Xml
::closeElement( 'td' ) .
111 Xml
::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml
::closeElement( 'td' ) .
112 Xml
::closeElement( 'tr' );
116 * Each of these methods is pretty self-explanatory, get a particular
117 * row for the table of statistics
120 private function getPageStats() {
122 return Xml
::openElement( 'tr' ) .
123 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
124 Xml
::closeElement( 'tr' ) .
125 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
126 $wgLang->formatNum( $this->good
),
127 array( 'class' => 'mw-statistics-articles' ) ) .
128 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
129 $wgLang->formatNum( $this->total
),
130 array( 'class' => 'mw-statistics-pages' ),
131 'statistics-pages-desc' ) .
132 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
133 $wgLang->formatNum( $this->images
),
134 array( 'class' => 'mw-statistics-files' ) );
136 private function getEditStats() {
138 return Xml
::openElement( 'tr' ) .
139 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
140 Xml
::closeElement( 'tr' ) .
141 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
142 $wgLang->formatNum( $this->edits
),
143 array( 'class' => 'mw-statistics-edits' ) ) .
144 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
145 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
146 array( 'class' => 'mw-statistics-edits-average' ) ) .
147 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
148 $wgLang->formatNum( $this->numJobs
),
149 array( 'class' => 'mw-statistics-jobqueue' ) );
151 private function getUserStats() {
152 global $wgLang, $wgRCMaxAge;
153 return Xml
::openElement( 'tr' ) .
154 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
155 Xml
::closeElement( 'tr' ) .
156 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
157 $wgLang->formatNum( $this->users
),
158 array( 'class' => 'mw-statistics-users' ) ) .
159 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
160 $wgLang->formatNum( $this->activeUsers
),
161 array( 'class' => 'mw-statistics-users-active' ),
162 'statistics-users-active-desc',
163 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
165 private function getGroupStats() {
166 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
167 $sk = $wgUser->getSkin();
169 foreach( $wgGroupPermissions as $group => $permissions ) {
170 # Skip generic * and implicit groups
171 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
174 $groupname = htmlspecialchars( $group );
175 $msg = wfMsg( 'group-' . $groupname );
176 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
177 $groupnameLocalized = $groupname;
179 $groupnameLocalized = $msg;
181 $msg = wfMsgForContent( 'grouppage-' . $groupname );
182 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
183 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
185 $grouppageLocalized = $msg;
187 $grouppage = $sk->makeLink( $grouppageLocalized, htmlspecialchars( $groupnameLocalized ) );
188 $grouplink = $sk->link( SpecialPage
::getTitleFor( 'Listusers' ),
189 wfMsgHtml( 'listgrouprights-members' ),
191 array( 'group' => $group ),
193 # Add a class when a usergroup contains no members to allow hiding these rows
195 $countUsers = SiteStats
::numberingroup( $groupname );
196 if( $countUsers == 0 ) {
197 $classZero = ' statistics-group-zero';
199 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
200 $wgLang->formatNum( $countUsers ),
201 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
205 private function getViewsStats() {
207 return Xml
::openElement( 'tr' ) .
208 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
209 Xml
::closeElement( 'tr' ) .
210 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
211 $wgLang->formatNum( $this->views
),
212 array ( 'class' => 'mw-statistics-views-total' ) ) .
213 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
214 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
215 $this->views
/ $this->edits
: 0 ) ),
216 array ( 'class' => 'mw-statistics-views-peredit' ) );
218 private function getMostViewedPages() {
219 global $wgLang, $wgUser;
221 $dbr = wfGetDB( DB_SLAVE
);
222 $sk = $wgUser->getSkin();
231 'page_is_redirect' => 0,
236 'ORDER BY' => 'page_counter DESC',
240 if( $res->numRows() > 0 ) {
241 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
242 while( $row = $res->fetchObject() ) {
243 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
244 if( $title instanceof Title
) {
245 $text .= $this->formatRow( $sk->link( $title ),
246 $wgLang->formatNum( $row->page_counter
) );
256 * Do the action=raw output for this page. Legacy, but we support
257 * it for backwards compatibility
258 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
260 private function doRawOutput() {
263 header( 'Pragma: nocache' );
264 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
265 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
266 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
267 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";