Remove action=raw from Special:Statistics. We wanted to keep it for back-compat ...
[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 = 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->hook = '';
54
55 # Staticic - views
56 $viewsStats = '';
57 if( !$wgDisableCounters ) {
58 $viewsStats = $this->getViewsStats();
59 }
60
61 # Set active user count
62 if( !$wgMiserMode ) {
63 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
64 // Re-calculate the count if the last tally is old...
65 if( !$wgMemc->get($key) ) {
66 $dbw = wfGetDB( DB_MASTER );
67 SiteStatsUpdate::cacheUpdate( $dbw );
68 $wgMemc->set( $key, '1', 24*3600 ); // don't update for 1 day
69 }
70 }
71
72 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
73
74 # Statistic - pages
75 $text .= $this->getPageStats();
76
77 # Statistic - edits
78 $text .= $this->getEditStats();
79
80 # Statistic - users
81 $text .= $this->getUserStats();
82
83 # Statistic - usergroups
84 $text .= $this->getGroupStats();
85 $text .= $viewsStats;
86
87 # Statistic - popular pages
88 if( !$wgDisableCounters && !$wgMiserMode ) {
89 $text .= $this->getMostViewedPages();
90 }
91
92 # Statistic - other
93 $extraStats = array();
94 if( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
95 $text .= $this->getOtherStats( $extraStats );
96 }
97
98 $text .= Xml::closeElement( 'table' );
99
100 # Customizable footer
101 $footer = wfMsgExt( 'statistics-footer', array('parseinline') );
102 if( !wfEmptyMsg( 'statistics-footer', $footer ) && $footer != '' ) {
103 $text .= "\n" . $footer;
104 }
105
106 $wgOut->addHTML( $text );
107 }
108
109 /**
110 * Format a row
111 * @param $text String: description of the row
112 * @param $number Float: a statistical number
113 * @param $trExtraParams Array: params to table row, see Html::elememt
114 * @param $descMsg String: message key
115 * @param $descMsgParam Array: message params
116 * @return string table row in HTML format
117 */
118 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
119 if( $descMsg ) {
120 $descriptionText = wfMsgExt( $descMsg, array( 'parseinline' ), $descMsgParam );
121 if ( !wfEmptyMsg( $descMsg, $descriptionText ) ) {
122 $descriptionText = " ($descriptionText)";
123 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc'),
124 $descriptionText );
125 }
126 }
127 return
128 Html::rawElement( 'tr', $trExtraParams,
129 Html::rawElement( 'td', array(), $text ) .
130 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
131 );
132 }
133
134 /**
135 * Each of these methods is pretty self-explanatory, get a particular
136 * row for the table of statistics
137 * @return string
138 */
139 private function getPageStats() {
140 global $wgLang;
141 return Xml::openElement( 'tr' ) .
142 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-pages', array( 'parseinline' ) ) ) .
143 Xml::closeElement( 'tr' ) .
144 $this->formatRow( wfMsgExt( 'statistics-articles', array( 'parseinline' ) ),
145 $wgLang->formatNum( $this->good ),
146 array( 'class' => 'mw-statistics-articles' ) ) .
147 $this->formatRow( wfMsgExt( 'statistics-pages', array( 'parseinline' ) ),
148 $wgLang->formatNum( $this->total ),
149 array( 'class' => 'mw-statistics-pages' ),
150 'statistics-pages-desc' ) .
151 $this->formatRow( wfMsgExt( 'statistics-files', array( 'parseinline' ) ),
152 $wgLang->formatNum( $this->images ),
153 array( 'class' => 'mw-statistics-files' ) );
154 }
155 private function getEditStats() {
156 global $wgLang;
157 return Xml::openElement( 'tr' ) .
158 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-edits', array( 'parseinline' ) ) ) .
159 Xml::closeElement( 'tr' ) .
160 $this->formatRow( wfMsgExt( 'statistics-edits', array( 'parseinline' ) ),
161 $wgLang->formatNum( $this->edits ),
162 array( 'class' => 'mw-statistics-edits' ) ) .
163 $this->formatRow( wfMsgExt( 'statistics-edits-average', array( 'parseinline' ) ),
164 $wgLang->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
165 array( 'class' => 'mw-statistics-edits-average' ) );
166 }
167
168 private function getUserStats() {
169 global $wgLang, $wgUser, $wgActiveUserDays;
170 $sk = $wgUser->getSkin();
171 return Xml::openElement( 'tr' ) .
172 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-users', array( 'parseinline' ) ) ) .
173 Xml::closeElement( 'tr' ) .
174 $this->formatRow( wfMsgExt( 'statistics-users', array( 'parseinline' ) ),
175 $wgLang->formatNum( $this->users ),
176 array( 'class' => 'mw-statistics-users' ) ) .
177 $this->formatRow( wfMsgExt( 'statistics-users-active', array( 'parseinline' ) ) . ' ' .
178 $sk->link(
179 SpecialPage::getTitleFor( 'Activeusers' ),
180 wfMsgHtml( 'listgrouprights-members' ),
181 array(),
182 array(),
183 'known'
184 ),
185 $wgLang->formatNum( $this->activeUsers ),
186 array( 'class' => 'mw-statistics-users-active' ),
187 'statistics-users-active-desc',
188 $wgLang->formatNum( $wgActiveUserDays ) );
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 ) || $msg == '' ) {
202 $groupnameLocalized = $groupname;
203 } else {
204 $groupnameLocalized = $msg;
205 }
206 $msg = wfMsgForContent( 'grouppage-' . $groupname );
207 if ( wfEmptyMsg( 'grouppage-' . $groupname, $msg ) || $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 private function getViewsStats() {
237 global $wgLang;
238 return Xml::openElement( 'tr' ) .
239 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-views', array( 'parseinline' ) ) ) .
240 Xml::closeElement( 'tr' ) .
241 $this->formatRow( wfMsgExt( 'statistics-views-total', array( 'parseinline' ) ),
242 $wgLang->formatNum( $this->views ),
243 array ( 'class' => 'mw-statistics-views-total' ) ) .
244 $this->formatRow( wfMsgExt( 'statistics-views-peredit', array( 'parseinline' ) ),
245 $wgLang->formatNum( sprintf( '%.2f', $this->edits ?
246 $this->views / $this->edits : 0 ) ),
247 array ( 'class' => 'mw-statistics-views-peredit' ) );
248 }
249 private function getMostViewedPages() {
250 global $wgLang, $wgUser;
251 $text = '';
252 $dbr = wfGetDB( DB_SLAVE );
253 $sk = $wgUser->getSkin();
254 $res = $dbr->select(
255 'page',
256 array(
257 'page_namespace',
258 'page_title',
259 'page_counter',
260 ),
261 array(
262 'page_is_redirect' => 0,
263 'page_counter > 0',
264 ),
265 __METHOD__,
266 array(
267 'ORDER BY' => 'page_counter DESC',
268 'LIMIT' => 10,
269 )
270 );
271 if( $res->numRows() > 0 ) {
272 $text .= Xml::openElement( 'tr' );
273 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-mostpopular', array( 'parseinline' ) ) );
274 $text .= Xml::closeElement( 'tr' );
275 foreach ( $res as $row ) {
276 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
277 if( $title instanceof Title ) {
278 $text .= $this->formatRow( $sk->link( $title ),
279 $wgLang->formatNum( $row->page_counter ) );
280
281 }
282 }
283 $res->free();
284 }
285 return $text;
286 }
287
288 private function getOtherStats( $stats ) {
289 global $wgLang;
290
291 if ( !count( $stats ) )
292 return '';
293
294 $return = Xml::openElement( 'tr' ) .
295 Xml::tags( 'th', array( 'colspan' => '2' ), wfMsgExt( 'statistics-header-hooks', array( 'parseinline' ) ) ) .
296 Xml::closeElement( 'tr' );
297
298 foreach( $stats as $name => $number ) {
299 $name = htmlspecialchars( $name );
300 $number = htmlspecialchars( $number );
301
302 $return .= $this->formatRow( $name, $wgLang->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
303 }
304
305 return $return;
306 }
307 }