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