Coding style
[lhc/web/wiklou.git] / includes / specials / SpecialStatistics.php
1 <?php
2 /**
3 * Implements Special:Statistics
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page lists various statistics, including the contents of
26 * `site_stats`, plus page view details if enabled
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialStatistics extends SpecialPage {
31
32 private $views, $edits, $good, $images, $total, $users,
33 $activeUsers, $admins, $numJobs = 0;
34
35 public function __construct() {
36 parent::__construct( 'Statistics' );
37 }
38
39 public function execute( $par ) {
40 global $wgOut, $wgRequest, $wgMemc;
41 global $wgDisableCounters, $wgMiserMode;
42
43 $this->setHeaders();
44
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 = '';
55
56 # Staticic - views
57 $viewsStats = '';
58 if( !$wgDisableCounters ) {
59 $viewsStats = $this->getViewsStats();
60 }
61
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
70 }
71 }
72
73 # Do raw output
74 if( $wgRequest->getVal( 'action' ) == 'raw' ) {
75 $this->doRawOutput();
76 }
77
78 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
79
80 # Statistic - pages
81 $text .= $this->getPageStats();
82
83 # Statistic - edits
84 $text .= $this->getEditStats();
85
86 # Statistic - users
87 $text .= $this->getUserStats();
88
89 # Statistic - usergroups
90 $text .= $this->getGroupStats();
91 $text .= $viewsStats;
92
93 # Statistic - popular pages
94 if( !$wgDisableCounters && !$wgMiserMode ) {
95 $text .= $this->getMostViewedPages();
96 }
97
98 # Statistic - other
99 $extraStats = array();
100 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
101 $text .= $this->getOtherStats( $extraStats );
102 }
103
104 $text .= Xml::closeElement( 'table' );
105
106 # Customizable footer
107 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
108 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
109 $text .= "\n" . $footer;
110 }
111
112 $wgOut->addHTML( $text );
113 }
114
115 /**
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
123 */
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 );
131 }
132 }
133 return
134 Html::rawElement( 'tr', $trExtraParams,
135 Html::rawElement( 'td', array(), $text ) .
136 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
137 );
138 }
139
140 /**
141 * Each of these methods is pretty self-explanatory, get a particular
142 * row for the table of statistics
143 * @return string
144 */
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' ) );
160 }
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' ) );
172 }
173
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'
190 ),
191 $wgLang->formatNum( $this->activeUsers ),
192 array( 'class' => 'mw-statistics-users-active' ),
193 'statistics-users-active-desc',
194 $wgLang->formatNum( $wgActiveUserDays ) );
195 }
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;
204 }
205 $groupname = htmlspecialchars( $group );
206 $msg = wfMsg( 'group-' . $groupname );
207 if ( wfEmptyMsg( 'group-' . $groupname, $msg ) || $msg == '' ) {
208 $groupnameLocalized = $groupname;
209 } else {
210 $groupnameLocalized = $msg;
211 }
212 $msg = wfMsgForContent( 'grouppage-' . $groupname );
213 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $msg == '' ) {
214 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
215 } else {
216 $grouppageLocalized = $msg;
217 }
218 $linkTarget = Title::newFromText( $grouppageLocalized );
219 $grouppage = $sk->link(
220 $linkTarget,
221 htmlspecialchars( $groupnameLocalized )
222 );
223 $grouplink = $sk->link(
224 SpecialPage::getTitleFor( 'Listusers' ),
225 wfMsgHtml( 'listgrouprights-members' ),
226 array(),
227 array( 'group' => $group ),
228 'known'
229 );
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';
235 }
236 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
237 $wgLang->formatNum( $countUsers ),
238 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
239 }
240 return $text;
241 }
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' ) );
254 }
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',
266 ),
267 array(
268 'page_is_redirect' => 0,
269 'page_counter > 0',
270 ),
271 __METHOD__,
272 array(
273 'ORDER BY' => 'page_counter DESC',
274 'LIMIT' => 10,
275 )
276 );
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 while( $row = $res->fetchObject() ) {
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 ) );
286
287 }
288 }
289 $res->free();
290 }
291 return $text;
292 }
293
294 private function getOtherStats( $stats ) {
295 global $wgLang;
296
297 if ( !count( $stats ) )
298 return '';
299
300 $return = Xml::openElement( 'tr' ) .
301 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
302 Xml::closeElement( 'tr' );
303
304 foreach( $stats as $name => $number ) {
305 $name = htmlspecialchars( $name );
306 $number = htmlspecialchars( $number );
307
308 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
309 }
310
311 return $return;
312 }
313
314 /**
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
318 */
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;
328 }
329 }