0e704586882b03d441b4d10f6f7f325cc1cbc0d9
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Special page lists various statistics, including the contents of
22 * `site_stats`, plus page view details if enabled
25 * @ingroup SpecialPage
29 * Some statistics about the wiki
31 class SpecialStatistics
extends SpecialPage
{
33 private $views, $edits, $good, $images, $total, $users,
34 $activeUsers, $admins, $numJobs = 0;
36 public function __construct() {
37 parent
::__construct( 'Statistics' );
40 public function execute( $par ) {
41 global $wgOut, $wgRequest, $wgMessageCache, $wgMemc;
42 global $wgDisableCounters, $wgMiserMode;
46 $this->views
= SiteStats
::views();
47 $this->edits
= SiteStats
::edits();
48 $this->good
= SiteStats
::articles();
49 $this->images
= SiteStats
::images();
50 $this->total
= SiteStats
::pages();
51 $this->users
= SiteStats
::users();
52 $this->activeUsers
= SiteStats
::activeUsers();
53 $this->admins
= SiteStats
::numberingroup('sysop');
54 $this->numJobs
= SiteStats
::jobs();
59 if( !$wgDisableCounters ) {
60 $viewsStats = $this->getViewsStats();
63 # Set active user count
65 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
66 // Re-calculate the count if the last tally is old...
67 if( !$wgMemc->get($key) ) {
68 $dbw = wfGetDB( DB_MASTER
);
69 SiteStatsUpdate
::cacheUpdate( $dbw );
70 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
75 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
79 $text = Xml
::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
82 $text .= $this->getPageStats();
85 $text .= $this->getEditStats();
88 $text .= $this->getUserStats();
90 # Statistic - usergroups
91 $text .= $this->getGroupStats();
94 # Statistic - popular pages
95 if( !$wgDisableCounters && !$wgMiserMode ) {
96 $text .= $this->getMostViewedPages();
100 $extraStats = array();
101 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
102 $text .= $this->getOtherStats( $extraStats );
105 $text .= Xml
::closeElement( 'table' );
107 # Customizable footer
108 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
109 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
110 $text .= "\n" . $footer;
113 $wgOut->addHTML( $text );
118 * @param $text String: description of the row
119 * @param $number Float: a statistical number
120 * @param $trExtraParams Array: params to table row, see Html::elememt
121 * @param $descMsg String: message key
122 * @param $descMsgParam Array: message params
123 * @return string table row in HTML format
125 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
128 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
129 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
130 $descriptionText = " ($descriptionText)";
131 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
136 Html
::rawElement( 'tr', $trExtraParams,
137 Html
::rawElement( 'td', array(), $text ) .
138 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
143 * Each of these methods is pretty self-explanatory, get a particular
144 * row for the table of statistics
147 private function getPageStats() {
149 return Xml
::openElement( 'tr' ) .
150 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
151 Xml
::closeElement( 'tr' ) .
152 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
153 $wgLang->formatNum( $this->good
),
154 array( 'class' => 'mw-statistics-articles' ) ) .
155 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
156 $wgLang->formatNum( $this->total
),
157 array( 'class' => 'mw-statistics-pages' ),
158 'statistics-pages-desc' ) .
159 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
160 $wgLang->formatNum( $this->images
),
161 array( 'class' => 'mw-statistics-files' ) );
163 private function getEditStats() {
165 return Xml
::openElement( 'tr' ) .
166 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
167 Xml
::closeElement( 'tr' ) .
168 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
169 $wgLang->formatNum( $this->edits
),
170 array( 'class' => 'mw-statistics-edits' ) ) .
171 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
172 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
173 array( 'class' => 'mw-statistics-edits-average' ) );
176 private function getUserStats() {
177 global $wgLang, $wgUser, $wgRCMaxAge;
178 $sk = $wgUser->getSkin();
179 return Xml
::openElement( 'tr' ) .
180 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
181 Xml
::closeElement( 'tr' ) .
182 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
183 $wgLang->formatNum( $this->users
),
184 array( 'class' => 'mw-statistics-users' ) ) .
185 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
187 SpecialPage
::getTitleFor( 'Activeusers' ),
188 wfMsgHtml( 'listgrouprights-members' ),
193 $wgLang->formatNum( $this->activeUsers
),
194 array( 'class' => 'mw-statistics-users-active' ),
195 'statistics-users-active-desc',
196 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
198 private function getGroupStats() {
199 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
200 $sk = $wgUser->getSkin();
202 foreach( $wgGroupPermissions as $group => $permissions ) {
203 # Skip generic * and implicit groups
204 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
207 $groupname = htmlspecialchars( $group );
208 $msg = wfMsg( 'group-' . $groupname );
209 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
210 $groupnameLocalized = $groupname;
212 $groupnameLocalized = $msg;
214 $msg = wfMsgForContent( 'grouppage-' . $groupname );
215 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
216 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
218 $grouppageLocalized = $msg;
220 $linkTarget = Title
::newFromText( $grouppageLocalized );
221 $grouppage = $sk->link(
223 htmlspecialchars( $groupnameLocalized )
225 $grouplink = $sk->link(
226 SpecialPage
::getTitleFor( 'Listusers' ),
227 wfMsgHtml( 'listgrouprights-members' ),
229 array( 'group' => $group ),
232 # Add a class when a usergroup contains no members to allow hiding these rows
234 $countUsers = SiteStats
::numberingroup( $groupname );
235 if( $countUsers == 0 ) {
236 $classZero = ' statistics-group-zero';
238 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
239 $wgLang->formatNum( $countUsers ),
240 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
244 private function getViewsStats() {
246 return Xml
::openElement( 'tr' ) .
247 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
248 Xml
::closeElement( 'tr' ) .
249 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
250 $wgLang->formatNum( $this->views
),
251 array ( 'class' => 'mw-statistics-views-total' ) ) .
252 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
253 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
254 $this->views
/ $this->edits
: 0 ) ),
255 array ( 'class' => 'mw-statistics-views-peredit' ) );
257 private function getMostViewedPages() {
258 global $wgLang, $wgUser;
260 $dbr = wfGetDB( DB_SLAVE
);
261 $sk = $wgUser->getSkin();
270 'page_is_redirect' => 0,
275 'ORDER BY' => 'page_counter DESC',
279 if( $res->numRows() > 0 ) {
280 $text .= Xml
::openElement( 'tr' );
281 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
282 $text .= Xml
::closeElement( 'tr' );
283 while( $row = $res->fetchObject() ) {
284 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
285 if( $title instanceof Title
) {
286 $text .= $this->formatRow( $sk->link( $title ),
287 $wgLang->formatNum( $row->page_counter
) );
296 private function getOtherStats( $stats ) {
299 if ( !count( $stats ) )
302 $return = Xml
::openElement( 'tr' ) .
303 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
304 Xml
::closeElement( 'tr' );
306 foreach( $stats as $name => $number ) {
307 $name = htmlspecialchars( $name );
308 $number = htmlspecialchars( $number );
310 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
317 * Do the action=raw output for this page. Legacy, but we support
318 * it for backwards compatibility
319 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
321 private function doRawOutput() {
324 header( 'Pragma: nocache' );
325 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
326 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
327 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
328 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";