Part 1 of 2, moving ResourceLoader*Module classes to their own files - this commit...
[mediawiki.git] / includes / specials / SpecialStatistics.php
blob7eb1a2fb05b24985c81aed899b5de8f25ed0d5dd
1 <?php
2 /**
3 * Implements Special:Statistics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page lists various statistics, including the contents of
26 * `site_stats`, plus page view details if enabled
28 * @ingroup SpecialPage
30 class SpecialStatistics extends SpecialPage {
32 private $views, $edits, $good, $images, $total, $users,
33 $activeUsers, $admins, $numJobs = 0;
35 public function __construct() {
36 parent::__construct( 'Statistics' );
39 public function execute( $par ) {
40 global $wgOut, $wgRequest, $wgMemc;
41 global $wgDisableCounters, $wgMiserMode;
43 $this->setHeaders();
45 $this->views = SiteStats::views();
46 $this->edits = SiteStats::edits();
47 $this->good = SiteStats::articles();
48 $this->images = SiteStats::images();
49 $this->total = SiteStats::pages();
50 $this->users = SiteStats::users();
51 $this->activeUsers = SiteStats::activeUsers();
52 $this->admins = SiteStats::numberingroup('sysop');
53 $this->numJobs = SiteStats::jobs();
54 $this->hook = '';
56 # Staticic - views
57 $viewsStats = '';
58 if( !$wgDisableCounters ) {
59 $viewsStats = $this->getViewsStats();
62 # Set active user count
63 if( !$wgMiserMode ) {
64 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
65 // Re-calculate the count if the last tally is old...
66 if( !$wgMemc->get($key) ) {
67 $dbw = wfGetDB( DB_MASTER );
68 SiteStatsUpdate::cacheUpdate( $dbw );
69 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
73 # Do raw output
74 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
75 $this->doRawOutput();
78 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
80 # Statistic - pages
81 $text .= $this->getPageStats();
83 # Statistic - edits
84 $text .= $this->getEditStats();
86 # Statistic - users
87 $text .= $this->getUserStats();
89 # Statistic - usergroups
90 $text .= $this->getGroupStats();
91 $text .= $viewsStats;
93 # Statistic - popular pages
94 if( !$wgDisableCounters && !$wgMiserMode ) {
95 $text .= $this->getMostViewedPages();
98 # Statistic - other
99 $extraStats = array();
100 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
101 $text .= $this->getOtherStats( $extraStats );
104 $text .= Xml::closeElement( 'table' );
106 # Customizable footer
107 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
108 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
109 $text .= "\n" . $footer;
112 $wgOut->addHTML( $text );
116 * Format a row
117 * @param $text String: description of the row
118 * @param $number Float: a statistical number
119 * @param $trExtraParams Array: params to table row, see Html::elememt
120 * @param $descMsg String: message key
121 * @param $descMsgParam Array: message params
122 * @return string table row in HTML format
124 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
125 if( $descMsg ) {
126 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
127 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
128 $descriptionText = " ($descriptionText)";
129 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
130 $descriptionText );
133 return
134 Html::rawElement( 'tr', $trExtraParams,
135 Html::rawElement( 'td', array(), $text ) .
136 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
141 * Each of these methods is pretty self-explanatory, get a particular
142 * row for the table of statistics
143 * @return string
145 private function getPageStats() {
146 global $wgLang;
147 return Xml::openElement( 'tr' ) .
148 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
149 Xml::closeElement( 'tr' ) .
150 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
151 $wgLang->formatNum( $this->good ),
152 array( 'class' => 'mw-statistics-articles' ) ) .
153 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
154 $wgLang->formatNum( $this->total ),
155 array( 'class' => 'mw-statistics-pages' ),
156 'statistics-pages-desc' ) .
157 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
158 $wgLang->formatNum( $this->images ),
159 array( 'class' => 'mw-statistics-files' ) );
161 private function getEditStats() {
162 global $wgLang;
163 return Xml::openElement( 'tr' ) .
164 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
165 Xml::closeElement( 'tr' ) .
166 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
167 $wgLang->formatNum( $this->edits ),
168 array( 'class' => 'mw-statistics-edits' ) ) .
169 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
170 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
171 array( 'class' => 'mw-statistics-edits-average' ) );
174 private function getUserStats() {
175 global $wgLang, $wgUser, $wgActiveUserDays;
176 $sk = $wgUser->getSkin();
177 return Xml::openElement( 'tr' ) .
178 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
179 Xml::closeElement( 'tr' ) .
180 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
181 $wgLang->formatNum( $this->users ),
182 array( 'class' => 'mw-statistics-users' ) ) .
183 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
184 $sk->link(
185 SpecialPage::getTitleFor( 'Activeusers' ),
186 wfMsgHtml( 'listgrouprights-members' ),
187 array(),
188 array(),
189 'known'
191 $wgLang->formatNum( $this->activeUsers ),
192 array( 'class' => 'mw-statistics-users-active' ),
193 'statistics-users-active-desc',
194 $wgLang->formatNum( $wgActiveUserDays ) );
196 private function getGroupStats() {
197 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
198 $sk = $wgUser->getSkin();
199 $text = '';
200 foreach( $wgGroupPermissions as $group => $permissions ) {
201 # Skip generic * and implicit groups
202 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
203 continue;
205 $groupname = htmlspecialchars( $group );
206 $msg = wfMsg( 'group-' . $groupname );
207 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
208 $groupnameLocalized = $groupname;
209 } else {
210 $groupnameLocalized = $msg;
212 $msg = wfMsgForContent( 'grouppage-' . $groupname );
213 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
214 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
215 } else {
216 $grouppageLocalized = $msg;
218 $linkTarget = Title::newFromText( $grouppageLocalized );
219 $grouppage = $sk->link(
220 $linkTarget,
221 htmlspecialchars( $groupnameLocalized )
223 $grouplink = $sk->link(
224 SpecialPage::getTitleFor( 'Listusers' ),
225 wfMsgHtml( 'listgrouprights-members' ),
226 array(),
227 array( 'group' => $group ),
228 'known'
230 # Add a class when a usergroup contains no members to allow hiding these rows
231 $classZero = '';
232 $countUsers = SiteStats::numberingroup( $groupname );
233 if( $countUsers == 0 ) {
234 $classZero = ' statistics-group-zero';
236 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
237 $wgLang->formatNum( $countUsers ),
238 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
240 return $text;
242 private function getViewsStats() {
243 global $wgLang;
244 return Xml::openElement( 'tr' ) .
245 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
246 Xml::closeElement( 'tr' ) .
247 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
248 $wgLang->formatNum( $this->views ),
249 array ( 'class' => 'mw-statistics-views-total' ) ) .
250 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
251 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
252 $this->views / $this->edits : 0 ) ),
253 array ( 'class' => 'mw-statistics-views-peredit' ) );
255 private function getMostViewedPages() {
256 global $wgLang, $wgUser;
257 $text = '';
258 $dbr = wfGetDB( DB_SLAVE );
259 $sk = $wgUser->getSkin();
260 $res = $dbr->select(
261 'page',
262 array(
263 'page_namespace',
264 'page_title',
265 'page_counter',
267 array(
268 'page_is_redirect' => 0,
269 'page_counter > 0',
271 __METHOD__,
272 array(
273 'ORDER BY' => 'page_counter DESC',
274 'LIMIT' => 10,
277 if( $res->numRows() > 0 ) {
278 $text .= Xml::openElement( 'tr' );
279 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
280 $text .= Xml::closeElement( 'tr' );
281 foreach ( $res as $row ) {
282 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
283 if( $title instanceof Title ) {
284 $text .= $this->formatRow( $sk->link( $title ),
285 $wgLang->formatNum( $row->page_counter ) );
289 $res->free();
291 return $text;
294 private function getOtherStats( $stats ) {
295 global $wgLang;
297 if ( !count( $stats ) )
298 return '';
300 $return = Xml::openElement( 'tr' ) .
301 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
302 Xml::closeElement( 'tr' );
304 foreach( $stats as $name => $number ) {
305 $name = htmlspecialchars( $name );
306 $number = htmlspecialchars( $number );
308 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
311 return $return;
315 * Do the action=raw output for this page. Legacy, but we support
316 * it for backwards compatibility
317 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
319 private function doRawOutput() {
320 global $wgOut;
321 $wgOut->disable();
322 header( 'Pragma: nocache' );
323 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
324 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
325 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
326 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
327 return;