Merge "New Hook rc/watchlist hook ChangesListBegin"
[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 $wgMemc, $wgDisableCounters, $wgMiserMode;
41
42 $this->setHeaders();
43 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
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 = $this->msg( 'statistics-footer' );
101 if ( !$footer->isBlank() ) {
102 $text .= "\n" . $footer->parse();
103 }
104
105 $this->getOutput()->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 array|string $descMsgParam Message parameters
115 * @return string table row in HTML format
116 */
117 private function formatRow( $text, $number, $trExtraParams = array(), $descMsg = '', $descMsgParam = '' ) {
118 if ( $descMsg ) {
119 $msg = $this->msg( $descMsg, $descMsgParam );
120 if ( $msg->exists() ) {
121 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
122 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc' ),
123 " $descriptionText" );
124 }
125 }
126
127 return 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 return Xml::openElement( 'tr' ) .
140 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
141 Xml::closeElement( 'tr' ) .
142 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
143 $this->msg( 'statistics-articles' )->parse() ),
144 $this->getLanguage()->formatNum( $this->good ),
145 array( 'class' => 'mw-statistics-articles' ) ) .
146 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
147 $this->getLanguage()->formatNum( $this->total ),
148 array( 'class' => 'mw-statistics-pages' ),
149 'statistics-pages-desc' ) .
150 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Listfiles' ),
151 $this->msg( 'statistics-files' )->parse() ),
152 $this->getLanguage()->formatNum( $this->images ),
153 array( 'class' => 'mw-statistics-files' ) );
154 }
155
156 private function getEditStats() {
157 return Xml::openElement( 'tr' ) .
158 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
159 Xml::closeElement( 'tr' ) .
160 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
161 $this->getLanguage()->formatNum( $this->edits ),
162 array( 'class' => 'mw-statistics-edits' ) ) .
163 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
164 $this->getLanguage()->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 $wgActiveUserDays;
170
171 return Xml::openElement( 'tr' ) .
172 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
173 Xml::closeElement( 'tr' ) .
174 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
175 $this->getLanguage()->formatNum( $this->users ),
176 array( 'class' => 'mw-statistics-users' ) ) .
177 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
178 Linker::linkKnown(
179 SpecialPage::getTitleFor( 'Activeusers' ),
180 $this->msg( 'listgrouprights-members' )->escaped()
181 ),
182 $this->getLanguage()->formatNum( $this->activeUsers ),
183 array( 'class' => 'mw-statistics-users-active' ),
184 'statistics-users-active-desc',
185 $this->getLanguage()->formatNum( $wgActiveUserDays ) );
186 }
187
188 private function getGroupStats() {
189 global $wgGroupPermissions, $wgImplicitGroups;
190 $text = '';
191 foreach ( $wgGroupPermissions as $group => $permissions ) {
192 # Skip generic * and implicit groups
193 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
194 continue;
195 }
196 $groupname = htmlspecialchars( $group );
197 $msg = $this->msg( 'group-' . $groupname );
198 if ( $msg->isBlank() ) {
199 $groupnameLocalized = $groupname;
200 } else {
201 $groupnameLocalized = $msg->text();
202 }
203 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
204 if ( $msg->isBlank() ) {
205 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
206 } else {
207 $grouppageLocalized = $msg->text();
208 }
209 $linkTarget = Title::newFromText( $grouppageLocalized );
210 $grouppage = Linker::link(
211 $linkTarget,
212 htmlspecialchars( $groupnameLocalized )
213 );
214 $grouplink = Linker::linkKnown(
215 SpecialPage::getTitleFor( 'Listusers' ),
216 $this->msg( 'listgrouprights-members' )->escaped(),
217 array(),
218 array( 'group' => $group )
219 );
220 # Add a class when a usergroup contains no members to allow hiding these rows
221 $classZero = '';
222 $countUsers = SiteStats::numberingroup( $groupname );
223 if ( $countUsers == 0 ) {
224 $classZero = ' statistics-group-zero';
225 }
226 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
227 $this->getLanguage()->formatNum( $countUsers ),
228 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
229 }
230
231 return $text;
232 }
233
234 private function getViewsStats() {
235 return Xml::openElement( 'tr' ) .
236 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
237 Xml::closeElement( 'tr' ) .
238 $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
239 $this->getLanguage()->formatNum( $this->views ),
240 array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
241 $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
242 $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
243 $this->views / $this->edits : 0 ) ),
244 array( 'class' => 'mw-statistics-views-peredit' ) );
245 }
246
247 private function getMostViewedPages() {
248 $text = '';
249 $dbr = wfGetDB( DB_SLAVE );
250 $res = $dbr->select(
251 'page',
252 array(
253 'page_namespace',
254 'page_title',
255 'page_counter',
256 ),
257 array(
258 'page_is_redirect' => 0,
259 'page_counter > 0',
260 ),
261 __METHOD__,
262 array(
263 'ORDER BY' => 'page_counter DESC',
264 'LIMIT' => 10,
265 )
266 );
267
268 if ( $res->numRows() > 0 ) {
269 $text .= Xml::openElement( 'tr' );
270 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-mostpopular' )->parse() );
271 $text .= Xml::closeElement( 'tr' );
272
273 foreach ( $res as $row ) {
274 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
275
276 if ( $title instanceof Title ) {
277 $text .= $this->formatRow( Linker::link( $title ),
278 $this->getLanguage()->formatNum( $row->page_counter ) );
279 }
280 }
281 $res->free();
282 }
283
284 return $text;
285 }
286
287 /**
288 * Conversion of external statistics into an internal representation
289 * Following a ([<header-message>][<item-message>] = number) pattern
290 *
291 * @param array $stats
292 * @return string
293 */
294 private function getOtherStats( array $stats ) {
295 $return = '';
296
297 foreach ( $stats as $header => $items ) {
298 // Identify the structure used
299 if ( is_array( $items ) ) {
300
301 // Ignore headers that are recursively set as legacy header
302 if ( $header !== 'statistics-header-hooks' ) {
303 $return .= $this->formatRowHeader( $header );
304 }
305
306 // Collect all items that belong to the same header
307 foreach ( $items as $key => $value ) {
308 $name = $this->msg( $key )->parse();
309 $number = htmlspecialchars( $value );
310
311 $return .= $this->formatRow( $name, $this->getLanguage()->formatNum( $number ), array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ) );
312 }
313 } else {
314 // Create the legacy header only once
315 if ( $return === '' ) {
316 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
317 }
318
319 // Recursively remap the legacy structure
320 $return .= $this->getOtherStats( array( 'statistics-header-hooks' => array( $header => $items ) ) );
321 }
322 }
323
324 return $return;
325 }
326
327 /**
328 * Format row header
329 *
330 * @param string $header
331 * @return string
332 */
333 private function formatRowHeader( $header ) {
334 return Xml::openElement( 'tr' ) .
335 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
336 Xml::closeElement( 'tr' );
337 }
338
339 protected function getGroupName() {
340 return 'wiki';
341 }
342 }