Use Xml::element instead of Html::element for empty elements
[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 private $views, $edits, $good, $images, $total, $users,
32 $activeUsers = 0;
33
34 public function __construct() {
35 parent::__construct( 'Statistics' );
36 }
37
38 public function execute( $par ) {
39 global $wgMemc, $wgDisableCounters, $wgMiserMode;
40
41 $this->setHeaders();
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
43
44 $this->views = SiteStats::views();
45 $this->edits = SiteStats::edits();
46 $this->good = SiteStats::articles();
47 $this->images = SiteStats::images();
48 $this->total = SiteStats::pages();
49 $this->users = SiteStats::users();
50 $this->activeUsers = SiteStats::activeUsers();
51 $this->hook = '';
52
53 # Staticic - views
54 $viewsStats = '';
55 if ( !$wgDisableCounters ) {
56 $viewsStats = $this->getViewsStats();
57 }
58
59 # Set active user count
60 if ( !$wgMiserMode ) {
61 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
62 // Re-calculate the count if the last tally is old...
63 if ( !$wgMemc->get( $key ) ) {
64 $dbw = wfGetDB( DB_MASTER );
65 SiteStatsUpdate::cacheUpdate( $dbw );
66 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
67 }
68 }
69
70 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
71
72 # Statistic - pages
73 $text .= $this->getPageStats();
74
75 # Statistic - edits
76 $text .= $this->getEditStats();
77
78 # Statistic - users
79 $text .= $this->getUserStats();
80
81 # Statistic - usergroups
82 $text .= $this->getGroupStats();
83 $text .= $viewsStats;
84
85 # Statistic - popular pages
86 if ( !$wgDisableCounters && !$wgMiserMode ) {
87 $text .= $this->getMostViewedPages();
88 }
89
90 # Statistic - other
91 $extraStats = array();
92 if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
93 $text .= $this->getOtherStats( $extraStats );
94 }
95
96 $text .= Xml::closeElement( 'table' );
97
98 # Customizable footer
99 $footer = $this->msg( 'statistics-footer' );
100 if ( !$footer->isBlank() ) {
101 $text .= "\n" . $footer->parse();
102 }
103
104 $this->getOutput()->addHTML( $text );
105 }
106
107 /**
108 * Format a row
109 * @param string $text Description of the row
110 * @param float $number A statistical number
111 * @param array $trExtraParams Params to table row, see Html::elememt
112 * @param string $descMsg Message key
113 * @param array|string $descMsgParam Message parameters
114 * @return string Table row in HTML format
115 */
116 private function formatRow( $text, $number, $trExtraParams = array(),
117 $descMsg = '', $descMsgParam = ''
118 ) {
119 if ( $descMsg ) {
120 $msg = $this->msg( $descMsg, $descMsgParam );
121 if ( $msg->exists() ) {
122 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
123 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc' ),
124 " $descriptionText" );
125 }
126 }
127
128 return 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 return Xml::openElement( 'tr' ) .
141 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
142 Xml::closeElement( 'tr' ) .
143 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
144 $this->msg( 'statistics-articles' )->parse() ),
145 $this->getLanguage()->formatNum( $this->good ),
146 array( 'class' => 'mw-statistics-articles' ) ) .
147 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
148 $this->getLanguage()->formatNum( $this->total ),
149 array( 'class' => 'mw-statistics-pages' ),
150 'statistics-pages-desc' ) .
151 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Listfiles' ),
152 $this->msg( 'statistics-files' )->parse() ),
153 $this->getLanguage()->formatNum( $this->images ),
154 array( 'class' => 'mw-statistics-files' ) );
155 }
156
157 private function getEditStats() {
158 return Xml::openElement( 'tr' ) .
159 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
160 Xml::closeElement( 'tr' ) .
161 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
162 $this->getLanguage()->formatNum( $this->edits ),
163 array( 'class' => 'mw-statistics-edits' )
164 ) .
165 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
166 $this->getLanguage()
167 ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
168 array( 'class' => 'mw-statistics-edits-average' )
169 );
170 }
171
172 private function getUserStats() {
173 global $wgActiveUserDays;
174
175 return Xml::openElement( 'tr' ) .
176 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
177 Xml::closeElement( 'tr' ) .
178 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
179 $this->getLanguage()->formatNum( $this->users ),
180 array( 'class' => 'mw-statistics-users' )
181 ) .
182 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
183 Linker::linkKnown(
184 SpecialPage::getTitleFor( 'Activeusers' ),
185 $this->msg( 'listgrouprights-members' )->escaped()
186 ),
187 $this->getLanguage()->formatNum( $this->activeUsers ),
188 array( 'class' => 'mw-statistics-users-active' ),
189 'statistics-users-active-desc',
190 $this->getLanguage()->formatNum( $wgActiveUserDays )
191 );
192 }
193
194 private function getGroupStats() {
195 global $wgGroupPermissions, $wgImplicitGroups;
196 $text = '';
197 foreach ( $wgGroupPermissions as $group => $permissions ) {
198 # Skip generic * and implicit groups
199 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
200 continue;
201 }
202 $groupname = htmlspecialchars( $group );
203 $msg = $this->msg( 'group-' . $groupname );
204 if ( $msg->isBlank() ) {
205 $groupnameLocalized = $groupname;
206 } else {
207 $groupnameLocalized = $msg->text();
208 }
209 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
210 if ( $msg->isBlank() ) {
211 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
212 } else {
213 $grouppageLocalized = $msg->text();
214 }
215 $linkTarget = Title::newFromText( $grouppageLocalized );
216 $grouppage = Linker::link(
217 $linkTarget,
218 htmlspecialchars( $groupnameLocalized )
219 );
220 $grouplink = Linker::linkKnown(
221 SpecialPage::getTitleFor( 'Listusers' ),
222 $this->msg( 'listgrouprights-members' )->escaped(),
223 array(),
224 array( 'group' => $group )
225 );
226 # Add a class when a usergroup contains no members to allow hiding these rows
227 $classZero = '';
228 $countUsers = SiteStats::numberingroup( $groupname );
229 if ( $countUsers == 0 ) {
230 $classZero = ' statistics-group-zero';
231 }
232 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
233 $this->getLanguage()->formatNum( $countUsers ),
234 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
235 }
236
237 return $text;
238 }
239
240 private function getViewsStats() {
241 return Xml::openElement( 'tr' ) .
242 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
243 Xml::closeElement( 'tr' ) .
244 $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
245 $this->getLanguage()->formatNum( $this->views ),
246 array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
247 $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
248 $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
249 $this->views / $this->edits : 0 ) ),
250 array( 'class' => 'mw-statistics-views-peredit' ) );
251 }
252
253 private function getMostViewedPages() {
254 $text = '';
255 $dbr = wfGetDB( DB_SLAVE );
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
274 if ( $res->numRows() > 0 ) {
275 $text .= Xml::openElement( 'tr' );
276 $text .= Xml::tags(
277 'th',
278 array( 'colspan' => '2' ),
279 $this->msg( 'statistics-mostpopular' )->parse()
280 );
281 $text .= Xml::closeElement( 'tr' );
282
283 foreach ( $res as $row ) {
284 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
285
286 if ( $title instanceof Title ) {
287 $text .= $this->formatRow( Linker::link( $title ),
288 $this->getLanguage()->formatNum( $row->page_counter ) );
289 }
290 }
291 $res->free();
292 }
293
294 return $text;
295 }
296
297 /**
298 * Conversion of external statistics into an internal representation
299 * Following a ([<header-message>][<item-message>] = number) pattern
300 *
301 * @param array $stats
302 * @return string
303 */
304 private function getOtherStats( array $stats ) {
305 $return = '';
306
307 foreach ( $stats as $header => $items ) {
308 // Identify the structure used
309 if ( is_array( $items ) ) {
310
311 // Ignore headers that are recursively set as legacy header
312 if ( $header !== 'statistics-header-hooks' ) {
313 $return .= $this->formatRowHeader( $header );
314 }
315
316 // Collect all items that belong to the same header
317 foreach ( $items as $key => $value ) {
318 $name = $this->msg( $key )->parse();
319 $number = htmlspecialchars( $value );
320
321 $return .= $this->formatRow(
322 $name,
323 $this->getLanguage()->formatNum( $number ),
324 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
325 );
326 }
327 } else {
328 // Create the legacy header only once
329 if ( $return === '' ) {
330 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
331 }
332
333 // Recursively remap the legacy structure
334 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
335 array( $header => $items ) ) );
336 }
337 }
338
339 return $return;
340 }
341
342 /**
343 * Format row header
344 *
345 * @param string $header
346 * @return string
347 */
348 private function formatRowHeader( $header ) {
349 return Xml::openElement( 'tr' ) .
350 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
351 Xml::closeElement( 'tr' );
352 }
353
354 protected function getGroupName() {
355 return 'wiki';
356 }
357 }