For wikis with $wgMiserMode off, update activeuser count daily rather than per page...
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2
3 /**
4 * Special page lists various statistics, including the contents of
5 * `site_stats`, plus page view details if enabled
6 *
7 * @file
8 * @ingroup SpecialPage
9 */
10
11 /**
12 * Show the special page
13 *
14 * @param mixed $par (not used)
15 */
16 class SpecialStatistics extends SpecialPage {
17
18 private $views, $edits, $good, $images, $total, $users,
19 $activeUsers, $admins, $numJobs = 0;
20
21 public function __construct() {
22 parent::__construct( 'Statistics' );
23 }
24
25 public function execute( $par ) {
26 global $wgOut, $wgRequest, $wgMessageCache, $wgMemc;
27 global $wgDisableCounters, $wgMiserMode;
28 $wgMessageCache->loadAllMessages();
29
30 $this->setHeaders();
31
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();
41 $this->hook = '';
42
43 # Staticic - views
44 $viewsStats = '';
45 if( !$wgDisableCounters ) {
46 $viewsStats = $this->getViewsStats();
47 }
48
49 # Set active user count
50 if( !$wgMiserMode ) {
51 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
52 // Re-calculate the count if the last tally is old...
53 if( !$wgMemc->get($key) ) {
54 $dbw = wfGetDB( DB_MASTER );
55 SiteStatsUpdate::cacheUpdate( $dbw );
56 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
57 }
58 }
59
60 # Do raw output
61 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
62 $this->doRawOutput();
63 }
64
65 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
66
67 # Statistic - pages
68 $text .= $this->getPageStats();
69
70 # Statistic - edits
71 $text .= $this->getEditStats();
72
73 # Statistic - users
74 $text .= $this->getUserStats();
75
76 # Statistic - usergroups
77 $text .= $this->getGroupStats();
78 $text .= $viewsStats;
79
80 # Statistic - popular pages
81 if( !$wgDisableCounters && !$wgMiserMode ) {
82 $text .= $this->getMostViewedPages();
83 }
84
85 # Statistic - other
86 $extraStats = array();
87 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
88 $text .= $this->getOtherStats( $extraStats );
89 }
90
91 $text .= Xml::closeElement( 'table' );
92
93 # Customizable footer
94 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
95 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
96 $text .= "\n" . $footer;
97 }
98
99 $wgOut->addHTML( $text );
100 }
101
102 /**
103 * Format a row
104 * @param string $text description of the row
105 * @param float $number a number
106 * @param array $trExtraParams
107 * @param string $descMsg
108 * @param string $descMsgParam
109 * @return string table row in HTML format
110 */
111 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
112 global $wgStylePath;
113 if( $descMsg ) {
114 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
115 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
116 $descriptionText = " ($descriptionText)";
117 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
118 $descriptionText );
119 }
120 }
121 return Xml::openElement( 'tr', $trExtraParams ) .
122 Xml::openElement( 'td' ) . $text . Xml::closeElement( 'td' ) .
123 Xml::openElement( 'td', array( 'class' => 'mw-statistics-numbers' ) ) . $number . Xml::closeElement( 'td' ) .
124 Xml::closeElement( 'tr' );
125 }
126
127 /**
128 * Each of these methods is pretty self-explanatory, get a particular
129 * row for the table of statistics
130 * @return string
131 */
132 private function getPageStats() {
133 global $wgLang;
134 return Xml::openElement( 'tr' ) .
135 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
136 Xml::closeElement( 'tr' ) .
137 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
138 $wgLang->formatNum( $this->good ),
139 array( 'class' => 'mw-statistics-articles' ) ) .
140 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
141 $wgLang->formatNum( $this->total ),
142 array( 'class' => 'mw-statistics-pages' ),
143 'statistics-pages-desc' ) .
144 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
145 $wgLang->formatNum( $this->images ),
146 array( 'class' => 'mw-statistics-files' ) );
147 }
148 private function getEditStats() {
149 global $wgLang;
150 return Xml::openElement( 'tr' ) .
151 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
152 Xml::closeElement( 'tr' ) .
153 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
154 $wgLang->formatNum( $this->edits ),
155 array( 'class' => 'mw-statistics-edits' ) ) .
156 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
157 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
158 array( 'class' => 'mw-statistics-edits-average' ) ) .
159 $this->formatRow( wfMsgExt( 'statistics-jobqueue', array( 'parseinline' ) ),
160 $wgLang->formatNum( $this->numJobs ),
161 array( 'class' => 'mw-statistics-jobqueue' ) );
162 }
163 private function getUserStats() {
164 global $wgLang, $wgRCMaxAge;
165 return Xml::openElement( 'tr' ) .
166 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
167 Xml::closeElement( 'tr' ) .
168 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
169 $wgLang->formatNum( $this->users ),
170 array( 'class' => 'mw-statistics-users' ) ) .
171 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ),
172 $wgLang->formatNum( $this->activeUsers ),
173 array( 'class' => 'mw-statistics-users-active' ),
174 'statistics-users-active-desc',
175 $wgLang->formatNum( ceil( $wgRCMaxAge / ( 3600 * 24 ) ) ) );
176 }
177 private function getGroupStats() {
178 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
179 $sk = $wgUser->getSkin();
180 $text = '';
181 foreach( $wgGroupPermissions as $group => $permissions ) {
182 # Skip generic * and implicit groups
183 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
184 continue;
185 }
186 $groupname = htmlspecialchars( $group );
187 $msg = wfMsg( 'group-' . $groupname );
188 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
189 $groupnameLocalized = $groupname;
190 } else {
191 $groupnameLocalized = $msg;
192 }
193 $msg = wfMsgForContent( 'grouppage-' . $groupname );
194 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
195 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
196 } else {
197 $grouppageLocalized = $msg;
198 }
199 $linkTarget = Title::newFromText( $grouppageLocalized );
200 $grouppage = $sk->link(
201 $linkTarget,
202 htmlspecialchars( $groupnameLocalized )
203 );
204 $grouplink = $sk->link(
205 SpecialPage::getTitleFor( 'Listusers' ),
206 wfMsgHtml( 'listgrouprights-members' ),
207 array(),
208 array( 'group' => $group ),
209 'known'
210 );
211 # Add a class when a usergroup contains no members to allow hiding these rows
212 $classZero = '';
213 $countUsers = SiteStats::numberingroup( $groupname );
214 if( $countUsers == 0 ) {
215 $classZero = ' statistics-group-zero';
216 }
217 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
218 $wgLang->formatNum( $countUsers ),
219 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
220 }
221 return $text;
222 }
223 private function getViewsStats() {
224 global $wgLang;
225 return Xml::openElement( 'tr' ) .
226 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
227 Xml::closeElement( 'tr' ) .
228 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
229 $wgLang->formatNum( $this->views ),
230 array ( 'class' => 'mw-statistics-views-total' ) ) .
231 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
232 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
233 $this->views / $this->edits : 0 ) ),
234 array ( 'class' => 'mw-statistics-views-peredit' ) );
235 }
236 private function getMostViewedPages() {
237 global $wgLang, $wgUser;
238 $text = '';
239 $dbr = wfGetDB( DB_SLAVE );
240 $sk = $wgUser->getSkin();
241 $res = $dbr->select(
242 'page',
243 array(
244 'page_namespace',
245 'page_title',
246 'page_counter',
247 ),
248 array(
249 'page_is_redirect' => 0,
250 'page_counter > 0',
251 ),
252 __METHOD__,
253 array(
254 'ORDER BY' => 'page_counter DESC',
255 'LIMIT' => 10,
256 )
257 );
258 if( $res->numRows() > 0 ) {
259 $text .= Xml::openElement( 'tr' );
260 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
261 $text .= Xml::closeElement( 'tr' );
262 while( $row = $res->fetchObject() ) {
263 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
264 if( $title instanceof Title ) {
265 $text .= $this->formatRow( $sk->link( $title ),
266 $wgLang->formatNum( $row->page_counter ) );
267
268 }
269 }
270 $res->free();
271 }
272 return $text;
273 }
274
275 private function getOtherStats( $stats ) {
276 global $wgLang;
277
278 $return = Xml::openElement( 'tr' ) .
279 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
280 Xml::closeElement( 'tr' );
281
282 foreach( $stats as $name => $number ) {
283 $name = htmlspecialchars( $name );
284 $number = htmlspecialchars( $number );
285
286 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
287 }
288
289 return $return;
290 }
291
292 /**
293 * Do the action=raw output for this page. Legacy, but we support
294 * it for backwards compatibility
295 * http://lists.wikimedia.org/pipermail/wikitech-l/2008-August/039202.html
296 */
297 private function doRawOutput() {
298 global $wgOut;
299 $wgOut->disable();
300 header( 'Pragma: nocache' );
301 echo "total=" . $this->total . ";good=" . $this->good . ";views=" .
302 $this->views . ";edits=" . $this->edits . ";users=" . $this->users . ";";
303 echo "activeusers=" . $this->activeUsers . ";admins=" . $this->admins .
304 ";images=" . $this->images . ";jobs=" . $this->numJobs . "\n";
305 return;
306 }
307 }