Removal of unused globals
[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, $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 if( $descMsg ) {
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'),
131 $descriptionText );
132 }
133 }
134 return
135 Html::rawElement( 'tr', $trExtraParams,
136 Html::rawElement( 'td', array(), $text ) .
137 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
138 );
139 }
140
141 /**
142 * Each of these methods is pretty self-explanatory, get a particular
143 * row for the table of statistics
144 * @return string
145 */
146 private function getPageStats() {
147 global $wgLang;
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' ) );
161 }
162 private function getEditStats() {
163 global $wgLang;
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' ) );
173 }
174
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' ) ) . ' ' .
185 $sk->link(
186 SpecialPage::getTitleFor( 'Activeusers' ),
187 wfMsgHtml( 'listgrouprights-members' ),
188 array(),
189 array(),
190 'known'
191 ),
192 $wgLang->formatNum( $this->activeUsers ),
193 array( 'class' => 'mw-statistics-users-active' ),
194 'statistics-users-active-desc',
195 $wgLang->formatNum( $wgActiveUserDays ) );
196 }
197 private function getGroupStats() {
198 global $wgGroupPermissions, $wgImplicitGroups, $wgLang, $wgUser;
199 $sk = $wgUser->getSkin();
200 $text = '';
201 foreach( $wgGroupPermissions as $group => $permissions ) {
202 # Skip generic * and implicit groups
203 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
204 continue;
205 }
206 $groupname = htmlspecialchars( $group );
207 $msg = wfMsg( 'group-' . $groupname );
208 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
209 $groupnameLocalized = $groupname;
210 } else {
211 $groupnameLocalized = $msg;
212 }
213 $msg = wfMsgForContent( 'grouppage-' . $groupname );
214 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
215 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
216 } else {
217 $grouppageLocalized = $msg;
218 }
219 $linkTarget = Title::newFromText( $grouppageLocalized );
220 $grouppage = $sk->link(
221 $linkTarget,
222 htmlspecialchars( $groupnameLocalized )
223 );
224 $grouplink = $sk->link(
225 SpecialPage::getTitleFor( 'Listusers' ),
226 wfMsgHtml( 'listgrouprights-members' ),
227 array(),
228 array( 'group' => $group ),
229 'known'
230 );
231 # Add a class when a usergroup contains no members to allow hiding these rows
232 $classZero = '';
233 $countUsers = SiteStats::numberingroup( $groupname );
234 if( $countUsers == 0 ) {
235 $classZero = ' statistics-group-zero';
236 }
237 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
238 $wgLang->formatNum( $countUsers ),
239 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
240 }
241 return $text;
242 }
243 private function getViewsStats() {
244 global $wgLang;
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' ) );
255 }
256 private function getMostViewedPages() {
257 global $wgLang, $wgUser;
258 $text = '';
259 $dbr = wfGetDB( DB_SLAVE );
260 $sk = $wgUser->getSkin();
261 $res = $dbr->select(
262 'page',
263 array(
264 'page_namespace',
265 'page_title',
266 'page_counter',
267 ),
268 array(
269 'page_is_redirect' => 0,
270 'page_counter > 0',
271 ),
272 __METHOD__,
273 array(
274 'ORDER BY' => 'page_counter DESC',
275 'LIMIT' => 10,
276 )
277 );
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 ) );
287
288 }
289 }
290 $res->free();
291 }
292 return $text;
293 }
294
295 private function getOtherStats( $stats ) {
296 global $wgLang;
297
298 if ( !count( $stats ) )
299 return '';
300
301 $return = Xml::openElement( 'tr' ) .
302 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
303 Xml::closeElement( 'tr' );
304
305 foreach( $stats as $name => $number ) {
306 $name = htmlspecialchars( $name );
307 $number = htmlspecialchars( $number );
308
309 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
310 }
311
312 return $return;
313 }
314
315 /**
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
319 */
320 private function doRawOutput() {
321 global $wgOut;
322 $wgOut->disable();
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";
328 return;
329 }
330 }