Made active users concept have some meaning by decoupling it from recent changes...
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2 /**
3 *
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.
8 *
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.
13 *
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
18 */
19
20 /**
21 * Special page lists various statistics, including the contents of
22 * `site_stats`, plus page view details if enabled
23 *
24 * @file
25 * @ingroup SpecialPage
26 */
27
28 /**
29 * Some statistics about the wiki
30 */
31 class SpecialStatistics extends SpecialPage {
32
33 private $views, $edits, $good, $images, $total, $users,
34 $activeUsers, $admins, $numJobs = 0;
35
36 public function __construct() {
37 parent::__construct( 'Statistics' );
38 }
39
40 public function execute( $par ) {
41 global $wgOut, $wgRequest, $wgMessageCache, $wgMemc;
42 global $wgDisableCounters, $wgMiserMode;
43
44 $this->setHeaders();
45
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();
55 $this->hook = '';
56
57 # Staticic - views
58 $viewsStats = '';
59 if( !$wgDisableCounters ) {
60 $viewsStats = $this->getViewsStats();
61 }
62
63 # Set active user count
64 if( !$wgMiserMode ) {
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
71 }
72 }
73
74 # Do raw output
75 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
76 $this->doRawOutput();
77 }
78
79 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
80
81 # Statistic - pages
82 $text .= $this->getPageStats();
83
84 # Statistic - edits
85 $text .= $this->getEditStats();
86
87 # Statistic - users
88 $text .= $this->getUserStats();
89
90 # Statistic - usergroups
91 $text .= $this->getGroupStats();
92 $text .= $viewsStats;
93
94 # Statistic - popular pages
95 if( !$wgDisableCounters && !$wgMiserMode ) {
96 $text .= $this->getMostViewedPages();
97 }
98
99 # Statistic - other
100 $extraStats = array();
101 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
102 $text .= $this->getOtherStats( $extraStats );
103 }
104
105 $text .= Xml::closeElement( 'table' );
106
107 # Customizable footer
108 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
109 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
110 $text .= "\n" . $footer;
111 }
112
113 $wgOut->addHTML( $text );
114 }
115
116 /**
117 * Format a row
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
124 */
125 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
126 global $wgStylePath;
127 if( $descMsg ) {
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'),
132 $descriptionText );
133 }
134 }
135 return
136 Html::rawElement( 'tr', $trExtraParams,
137 Html::rawElement( 'td', array(), $text ) .
138 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
139 );
140 }
141
142 /**
143 * Each of these methods is pretty self-explanatory, get a particular
144 * row for the table of statistics
145 * @return string
146 */
147 private function getPageStats() {
148 global $wgLang;
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' ) );
162 }
163 private function getEditStats() {
164 global $wgLang;
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' ) );
174 }
175
176 private function getUserStats() {
177 global $wgLang, $wgUser, $wgActiveUserDays;
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' ) ) . ' ' .
186 $sk->link(
187 SpecialPage::getTitleFor( 'Activeusers' ),
188 wfMsgHtml( 'listgrouprights-members' ),
189 array(),
190 array(),
191 'known'
192 ),
193 $wgLang->formatNum( $this->activeUsers ),
194 array( 'class' => 'mw-statistics-users-active' ),
195 'statistics-users-active-desc',
196 $wgLang->formatNum( $wgActiveUserDays ) );
197 }
198 private function getGroupStats() {
199 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
200 $sk = $wgUser->getSkin();
201 $text = '';
202 foreach( $wgGroupPermissions as $group => $permissions ) {
203 # Skip generic * and implicit groups
204 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
205 continue;
206 }
207 $groupname = htmlspecialchars( $group );
208 $msg = wfMsg( 'group-' . $groupname );
209 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
210 $groupnameLocalized = $groupname;
211 } else {
212 $groupnameLocalized = $msg;
213 }
214 $msg = wfMsgForContent( 'grouppage-' . $groupname );
215 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
216 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
217 } else {
218 $grouppageLocalized = $msg;
219 }
220 $linkTarget = Title::newFromText( $grouppageLocalized );
221 $grouppage = $sk->link(
222 $linkTarget,
223 htmlspecialchars( $groupnameLocalized )
224 );
225 $grouplink = $sk->link(
226 SpecialPage::getTitleFor( 'Listusers' ),
227 wfMsgHtml( 'listgrouprights-members' ),
228 array(),
229 array( 'group' => $group ),
230 'known'
231 );
232 # Add a class when a usergroup contains no members to allow hiding these rows
233 $classZero = '';
234 $countUsers = SiteStats::numberingroup( $groupname );
235 if( $countUsers == 0 ) {
236 $classZero = ' statistics-group-zero';
237 }
238 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
239 $wgLang->formatNum( $countUsers ),
240 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
241 }
242 return $text;
243 }
244 private function getViewsStats() {
245 global $wgLang;
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' ) );
256 }
257 private function getMostViewedPages() {
258 global $wgLang, $wgUser;
259 $text = '';
260 $dbr = wfGetDB( DB_SLAVE );
261 $sk = $wgUser->getSkin();
262 $res = $dbr->select(
263 'page',
264 array(
265 'page_namespace',
266 'page_title',
267 'page_counter',
268 ),
269 array(
270 'page_is_redirect' => 0,
271 'page_counter > 0',
272 ),
273 __METHOD__,
274 array(
275 'ORDER BY' => 'page_counter DESC',
276 'LIMIT' => 10,
277 )
278 );
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 ) );
288
289 }
290 }
291 $res->free();
292 }
293 return $text;
294 }
295
296 private function getOtherStats( $stats ) {
297 global $wgLang;
298
299 if ( !count( $stats ) )
300 return '';
301
302 $return = Xml::openElement( 'tr' ) .
303 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
304 Xml::closeElement( 'tr' );
305
306 foreach( $stats as $name => $number ) {
307 $name = htmlspecialchars( $name );
308 $number = htmlspecialchars( $number );
309
310 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
311 }
312
313 return $return;
314 }
315
316 /**
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
320 */
321 private function doRawOutput() {
322 global $wgOut;
323 $wgOut->disable();
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";
329 return;
330 }
331 }