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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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, $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 = '' ) {
127 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
128 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
129 $descriptionText = " ($descriptionText)";
130 $text .= "<br />" . Xml
::element( 'small', array( 'class' => 'mw-statistic-desc'),
135 Html
::rawElement( 'tr', $trExtraParams,
136 Html
::rawElement( 'td', array(), $text ) .
137 Html
::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
142 * Each of these methods is pretty self-explanatory, get a particular
143 * row for the table of statistics
146 private function getPageStats() {
148 return Xml
::openElement( 'tr' ) .
149 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
150 Xml
::closeElement( 'tr' ) .
151 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->good
),
153 array( 'class' => 'mw-statistics-articles' ) ) .
154 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
155 $wgLang->formatNum( $this->total
),
156 array( 'class' => 'mw-statistics-pages' ),
157 'statistics-pages-desc' ) .
158 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
159 $wgLang->formatNum( $this->images
),
160 array( 'class' => 'mw-statistics-files' ) );
162 private function getEditStats() {
164 return Xml
::openElement( 'tr' ) .
165 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
166 Xml
::closeElement( 'tr' ) .
167 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
168 $wgLang->formatNum( $this->edits
),
169 array( 'class' => 'mw-statistics-edits' ) ) .
170 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
171 $wgLang->formatNum( sprintf( '%.2f', $this->total ?
$this->edits
/ $this->total
: 0 ) ),
172 array( 'class' => 'mw-statistics-edits-average' ) );
175 private function getUserStats() {
176 global $wgLang, $wgUser, $wgActiveUserDays;
177 $sk = $wgUser->getSkin();
178 return Xml
::openElement( 'tr' ) .
179 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
180 Xml
::closeElement( 'tr' ) .
181 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
182 $wgLang->formatNum( $this->users
),
183 array( 'class' => 'mw-statistics-users' ) ) .
184 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
186 SpecialPage
::getTitleFor( 'Activeusers' ),
187 wfMsgHtml( 'listgrouprights-members' ),
192 $wgLang->formatNum( $this->activeUsers
),
193 array( 'class' => 'mw-statistics-users-active' ),
194 'statistics-users-active-desc',
195 $wgLang->formatNum( $wgActiveUserDays ) );
197 private function getGroupStats() {
198 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
199 $sk = $wgUser->getSkin();
201 foreach( $wgGroupPermissions as $group => $permissions ) {
202 # Skip generic * and implicit groups
203 if ( in_array( $group, $wgImplicitGroups ) ||
$group == '*' ) {
206 $groupname = htmlspecialchars( $group );
207 $msg = wfMsg( 'group-' . $groupname );
208 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) ||
$msg == '' ) {
209 $groupnameLocalized = $groupname;
211 $groupnameLocalized = $msg;
213 $msg = wfMsgForContent( 'grouppage-' . $groupname );
214 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) ||
$msg == '' ) {
215 $grouppageLocalized = MWNamespace
::getCanonicalName( NS_PROJECT
) . ':' . $groupname;
217 $grouppageLocalized = $msg;
219 $linkTarget = Title
::newFromText( $grouppageLocalized );
220 $grouppage = $sk->link(
222 htmlspecialchars( $groupnameLocalized )
224 $grouplink = $sk->link(
225 SpecialPage
::getTitleFor( 'Listusers' ),
226 wfMsgHtml( 'listgrouprights-members' ),
228 array( 'group' => $group ),
231 # Add a class when a usergroup contains no members to allow hiding these rows
233 $countUsers = SiteStats
::numberingroup( $groupname );
234 if( $countUsers == 0 ) {
235 $classZero = ' statistics-group-zero';
237 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
238 $wgLang->formatNum( $countUsers ),
239 array( 'class' => 'statistics-group-' . Sanitizer
::escapeClass( $group ) . $classZero ) );
243 private function getViewsStats() {
245 return Xml
::openElement( 'tr' ) .
246 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
247 Xml
::closeElement( 'tr' ) .
248 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
249 $wgLang->formatNum( $this->views
),
250 array ( 'class' => 'mw-statistics-views-total' ) ) .
251 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
252 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
253 $this->views
/ $this->edits
: 0 ) ),
254 array ( 'class' => 'mw-statistics-views-peredit' ) );
256 private function getMostViewedPages() {
257 global $wgLang, $wgUser;
259 $dbr = wfGetDB( DB_SLAVE
);
260 $sk = $wgUser->getSkin();
269 'page_is_redirect' => 0,
274 'ORDER BY' => 'page_counter DESC',
278 if( $res->numRows() > 0 ) {
279 $text .= Xml
::openElement( 'tr' );
280 $text .= Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
281 $text .= Xml
::closeElement( 'tr' );
282 while( $row = $res->fetchObject() ) {
283 $title = Title
::makeTitleSafe( $row->page_namespace
, $row->page_title
);
284 if( $title instanceof Title
) {
285 $text .= $this->formatRow( $sk->link( $title ),
286 $wgLang->formatNum( $row->page_counter
) );
295 private function getOtherStats( $stats ) {
298 if ( !count( $stats ) )
301 $return = Xml
::openElement( 'tr' ) .
302 Xml
::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
303 Xml
::closeElement( 'tr' );
305 foreach( $stats as $name => $number ) {
306 $name = htmlspecialchars( $name );
307 $number = htmlspecialchars( $number );
309 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
316 * Do the action=raw output for this page. Legacy, but we support
317 * it for backwards compatibility
318 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
320 private function doRawOutput() {
323 header( 'Pragma: nocache' );
324 echo "total=" . $this->total
. ";good=" . $this->good
. ";views=" .
325 $this->views
. ";edits=" . $this->edits
. ";users=" . $this->users
. ";";
326 echo "activeusers=" . $this->activeUsers
. ";admins=" . $this->admins
.
327 ";images=" . $this->images
. ";jobs=" . $this->numJobs
. "\n";