Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / specials / SpecialStatistics.php
blob612ba84eca0e93f2e8ae2d85a940dbeb884c1348
1 <?php
2 /**
3 * Implements Special:Statistics
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page lists various statistics, including the contents of
26 * `site_stats`, plus page view details if enabled
28 * @ingroup SpecialPage
30 class SpecialStatistics extends SpecialPage {
32 private $views, $edits, $good, $images, $total, $users,
33 $activeUsers = 0;
35 public function __construct() {
36 parent::__construct( 'Statistics' );
39 public function execute( $par ) {
40 global $wgMemc, $wgDisableCounters, $wgMiserMode;
42 $this->setHeaders();
43 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
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 = '';
54 # Staticic - views
55 $viewsStats = '';
56 if ( !$wgDisableCounters ) {
57 $viewsStats = $this->getViewsStats();
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
71 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
73 # Statistic - pages
74 $text .= $this->getPageStats();
76 # Statistic - edits
77 $text .= $this->getEditStats();
79 # Statistic - users
80 $text .= $this->getUserStats();
82 # Statistic - usergroups
83 $text .= $this->getGroupStats();
84 $text .= $viewsStats;
86 # Statistic - popular pages
87 if ( !$wgDisableCounters && !$wgMiserMode ) {
88 $text .= $this->getMostViewedPages();
91 # Statistic - other
92 $extraStats = array();
93 if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
94 $text .= $this->getOtherStats( $extraStats );
97 $text .= Xml::closeElement( 'table' );
99 # Customizable footer
100 $footer = $this->msg( 'statistics-footer' );
101 if ( !$footer->isBlank() ) {
102 $text .= "\n" . $footer->parse();
105 $this->getOutput()->addHTML( $text );
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
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" );
126 return Html::rawElement( 'tr', $trExtraParams,
127 Html::rawElement( 'td', array(), $text ) .
128 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
133 * Each of these methods is pretty self-explanatory, get a particular
134 * row for the table of statistics
135 * @return string
137 private function getPageStats() {
138 return Xml::openElement( 'tr' ) .
139 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
140 Xml::closeElement( 'tr' ) .
141 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
142 $this->msg( 'statistics-articles' )->parse() ),
143 $this->getLanguage()->formatNum( $this->good ),
144 array( 'class' => 'mw-statistics-articles' ) ) .
145 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
146 $this->getLanguage()->formatNum( $this->total ),
147 array( 'class' => 'mw-statistics-pages' ),
148 'statistics-pages-desc' ) .
149 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Listfiles' ),
150 $this->msg( 'statistics-files' )->parse() ),
151 $this->getLanguage()->formatNum( $this->images ),
152 array( 'class' => 'mw-statistics-files' ) );
154 private function getEditStats() {
155 return Xml::openElement( 'tr' ) .
156 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
157 Xml::closeElement( 'tr' ) .
158 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
159 $this->getLanguage()->formatNum( $this->edits ),
160 array( 'class' => 'mw-statistics-edits' ) ) .
161 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
162 $this->getLanguage()->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
163 array( 'class' => 'mw-statistics-edits-average' ) );
166 private function getUserStats() {
167 global $wgActiveUserDays;
168 return Xml::openElement( 'tr' ) .
169 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
170 Xml::closeElement( 'tr' ) .
171 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
172 $this->getLanguage()->formatNum( $this->users ),
173 array( 'class' => 'mw-statistics-users' ) ) .
174 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
175 Linker::linkKnown(
176 SpecialPage::getTitleFor( 'Activeusers' ),
177 $this->msg( 'listgrouprights-members' )->escaped()
179 $this->getLanguage()->formatNum( $this->activeUsers ),
180 array( 'class' => 'mw-statistics-users-active' ),
181 'statistics-users-active-desc',
182 $this->getLanguage()->formatNum( $wgActiveUserDays ) );
185 private function getGroupStats() {
186 global $wgGroupPermissions, $wgImplicitGroups;
187 $text = '';
188 foreach ( $wgGroupPermissions as $group => $permissions ) {
189 # Skip generic * and implicit groups
190 if ( in_array( $group, $wgImplicitGroups ) || $group == '*' ) {
191 continue;
193 $groupname = htmlspecialchars( $group );
194 $msg = $this->msg( 'group-' . $groupname );
195 if ( $msg->isBlank() ) {
196 $groupnameLocalized = $groupname;
197 } else {
198 $groupnameLocalized = $msg->text();
200 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
201 if ( $msg->isBlank() ) {
202 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
203 } else {
204 $grouppageLocalized = $msg->text();
206 $linkTarget = Title::newFromText( $grouppageLocalized );
207 $grouppage = Linker::link(
208 $linkTarget,
209 htmlspecialchars( $groupnameLocalized )
211 $grouplink = Linker::linkKnown(
212 SpecialPage::getTitleFor( 'Listusers' ),
213 $this->msg( 'listgrouprights-members' )->escaped(),
214 array(),
215 array( 'group' => $group )
217 # Add a class when a usergroup contains no members to allow hiding these rows
218 $classZero = '';
219 $countUsers = SiteStats::numberingroup( $groupname );
220 if ( $countUsers == 0 ) {
221 $classZero = ' statistics-group-zero';
223 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
224 $this->getLanguage()->formatNum( $countUsers ),
225 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
227 return $text;
230 private function getViewsStats() {
231 return Xml::openElement( 'tr' ) .
232 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-views' )->parse() ) .
233 Xml::closeElement( 'tr' ) .
234 $this->formatRow( $this->msg( 'statistics-views-total' )->parse(),
235 $this->getLanguage()->formatNum( $this->views ),
236 array( 'class' => 'mw-statistics-views-total' ), 'statistics-views-total-desc' ) .
237 $this->formatRow( $this->msg( 'statistics-views-peredit' )->parse(),
238 $this->getLanguage()->formatNum( sprintf( '%.2f', $this->edits ?
239 $this->views / $this->edits : 0 ) ),
240 array( 'class' => 'mw-statistics-views-peredit' ) );
243 private function getMostViewedPages() {
244 $text = '';
245 $dbr = wfGetDB( DB_SLAVE );
246 $res = $dbr->select(
247 'page',
248 array(
249 'page_namespace',
250 'page_title',
251 'page_counter',
253 array(
254 'page_is_redirect' => 0,
255 'page_counter > 0',
257 __METHOD__,
258 array(
259 'ORDER BY' => 'page_counter DESC',
260 'LIMIT' => 10,
263 if ( $res->numRows() > 0 ) {
264 $text .= Xml::openElement( 'tr' );
265 $text .= Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-mostpopular' )->parse() );
266 $text .= Xml::closeElement( 'tr' );
267 foreach ( $res as $row ) {
268 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
269 if ( $title instanceof Title ) {
270 $text .= $this->formatRow( Linker::link( $title ),
271 $this->getLanguage()->formatNum( $row->page_counter ) );
275 $res->free();
277 return $text;
281 * Conversion of external statistics into an internal representation
282 * Following a ([<header-message>][<item-message>] = number) pattern
284 * @param array $stats
285 * @return string
287 private function getOtherStats( array $stats ) {
288 $return = '';
290 foreach ( $stats as $header => $items ) {
291 // Identify the structure used
292 if ( is_array( $items ) ) {
294 // Ignore headers that are recursively set as legacy header
295 if ( $header !== 'statistics-header-hooks' ) {
296 $return .= $this->formatRowHeader( $header );
299 // Collect all items that belong to the same header
300 foreach ( $items as $key => $value ) {
301 $name = $this->msg( $key )->parse();
302 $number = htmlspecialchars( $value );
304 $return .= $this->formatRow( $name, $this->getLanguage()->formatNum( $number ), array( 'class' => 'mw-statistics-hook' ) );
306 } else {
307 // Create the legacy header only once
308 if ( $return === '' ) {
309 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
312 // Recursively remap the legacy structure
313 $return .= $this->getOtherStats( array( 'statistics-header-hooks' => array( $header => $items ) ) );
317 return $return;
321 * Format row header
323 * @param string $header
324 * @return string
326 private function formatRowHeader( $header ) {
327 return Xml::openElement( 'tr' ) .
328 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
329 Xml::closeElement( 'tr' );
332 protected function getGroupName() {
333 return 'wiki';