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