Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialStatistics.php
blob1f339d1ed05e707633cf24bf2d711dbe05a723f2
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 {
31 private $edits, $good, $images, $total, $users,
32 $activeUsers = 0;
34 public function __construct() {
35 parent::__construct( 'Statistics' );
38 public function execute( $par ) {
39 global $wgMemc;
41 $miserMode = $this->getConfig()->get( 'MiserMode' );
43 $this->setHeaders();
44 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
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 # Set active user count
55 if ( !$miserMode ) {
56 $key = wfMemcKey( 'sitestats', 'activeusers-updated' );
57 // Re-calculate the count if the last tally is old...
58 if ( !$wgMemc->get( $key ) ) {
59 $dbw = wfGetDB( DB_MASTER );
60 SiteStatsUpdate::cacheUpdate( $dbw );
61 $wgMemc->set( $key, '1', 24 * 3600 ); // don't update for 1 day
65 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
67 # Statistic - pages
68 $text .= $this->getPageStats();
70 # Statistic - edits
71 $text .= $this->getEditStats();
73 # Statistic - users
74 $text .= $this->getUserStats();
76 # Statistic - usergroups
77 $text .= $this->getGroupStats();
79 # Statistic - other
80 $extraStats = array();
81 if ( Hooks::run( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
82 $text .= $this->getOtherStats( $extraStats );
85 $text .= Xml::closeElement( 'table' );
87 # Customizable footer
88 $footer = $this->msg( 'statistics-footer' );
89 if ( !$footer->isBlank() ) {
90 $text .= "\n" . $footer->parse();
93 $this->getOutput()->addHTML( $text );
96 /**
97 * Format a row
98 * @param string $text Description of the row
99 * @param float $number A statistical number
100 * @param array $trExtraParams Params to table row, see Html::elememt
101 * @param string $descMsg Message key
102 * @param array|string $descMsgParam Message parameters
103 * @return string Table row in HTML format
105 private function formatRow( $text, $number, $trExtraParams = array(),
106 $descMsg = '', $descMsgParam = ''
108 if ( $descMsg ) {
109 $msg = $this->msg( $descMsg, $descMsgParam );
110 if ( $msg->exists() ) {
111 $descriptionText = $this->msg( 'parentheses' )->rawParams( $msg->parse() )->escaped();
112 $text .= "<br />" . Xml::element( 'small', array( 'class' => 'mw-statistic-desc' ),
113 " $descriptionText" );
117 return Html::rawElement( 'tr', $trExtraParams,
118 Html::rawElement( 'td', array(), $text ) .
119 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
124 * Each of these methods is pretty self-explanatory, get a particular
125 * row for the table of statistics
126 * @return string
128 private function getPageStats() {
129 return Xml::openElement( 'tr' ) .
130 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-pages' )->parse() ) .
131 Xml::closeElement( 'tr' ) .
132 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'Allpages' ),
133 $this->msg( 'statistics-articles' )->parse() ),
134 $this->getLanguage()->formatNum( $this->good ),
135 array( 'class' => 'mw-statistics-articles' ) ) .
136 $this->formatRow( $this->msg( 'statistics-pages' )->parse(),
137 $this->getLanguage()->formatNum( $this->total ),
138 array( 'class' => 'mw-statistics-pages' ),
139 'statistics-pages-desc' ) .
140 $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'MediaStatistics' ),
141 $this->msg( 'statistics-files' )->parse() ),
142 $this->getLanguage()->formatNum( $this->images ),
143 array( 'class' => 'mw-statistics-files' ) );
146 private function getEditStats() {
147 return Xml::openElement( 'tr' ) .
148 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
149 Xml::closeElement( 'tr' ) .
150 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
151 $this->getLanguage()->formatNum( $this->edits ),
152 array( 'class' => 'mw-statistics-edits' )
154 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
155 $this->getLanguage()
156 ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
157 array( 'class' => 'mw-statistics-edits-average' )
161 private function getUserStats() {
162 return Xml::openElement( 'tr' ) .
163 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-users' )->parse() ) .
164 Xml::closeElement( 'tr' ) .
165 $this->formatRow( $this->msg( 'statistics-users' )->parse(),
166 $this->getLanguage()->formatNum( $this->users ),
167 array( 'class' => 'mw-statistics-users' )
169 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
170 Linker::linkKnown(
171 SpecialPage::getTitleFor( 'Activeusers' ),
172 $this->msg( 'listgrouprights-members' )->escaped()
174 $this->getLanguage()->formatNum( $this->activeUsers ),
175 array( 'class' => 'mw-statistics-users-active' ),
176 'statistics-users-active-desc',
177 $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
181 private function getGroupStats() {
182 $text = '';
183 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
184 # Skip generic * and implicit groups
185 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) || $group == '*' ) {
186 continue;
188 $groupname = htmlspecialchars( $group );
189 $msg = $this->msg( 'group-' . $groupname );
190 if ( $msg->isBlank() ) {
191 $groupnameLocalized = $groupname;
192 } else {
193 $groupnameLocalized = $msg->text();
195 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
196 if ( $msg->isBlank() ) {
197 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
198 } else {
199 $grouppageLocalized = $msg->text();
201 $linkTarget = Title::newFromText( $grouppageLocalized );
203 if ( $linkTarget ) {
204 $grouppage = Linker::link(
205 $linkTarget,
206 htmlspecialchars( $groupnameLocalized )
208 } else {
209 $grouppage = htmlspecialchars( $groupnameLocalized );
212 $grouplink = Linker::linkKnown(
213 SpecialPage::getTitleFor( 'Listusers' ),
214 $this->msg( 'listgrouprights-members' )->escaped(),
215 array(),
216 array( 'group' => $group )
218 # Add a class when a usergroup contains no members to allow hiding these rows
219 $classZero = '';
220 $countUsers = SiteStats::numberingroup( $groupname );
221 if ( $countUsers == 0 ) {
222 $classZero = ' statistics-group-zero';
224 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
225 $this->getLanguage()->formatNum( $countUsers ),
226 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
229 return $text;
233 * Conversion of external statistics into an internal representation
234 * Following a ([<header-message>][<item-message>] = number) pattern
236 * @param array $stats
237 * @return string
239 private function getOtherStats( array $stats ) {
240 $return = '';
242 foreach ( $stats as $header => $items ) {
243 // Identify the structure used
244 if ( is_array( $items ) ) {
246 // Ignore headers that are recursively set as legacy header
247 if ( $header !== 'statistics-header-hooks' ) {
248 $return .= $this->formatRowHeader( $header );
251 // Collect all items that belong to the same header
252 foreach ( $items as $key => $value ) {
253 $name = $this->msg( $key )->parse();
254 $number = htmlspecialchars( $value );
256 $return .= $this->formatRow(
257 $name,
258 $this->getLanguage()->formatNum( $number ),
259 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
262 } else {
263 // Create the legacy header only once
264 if ( $return === '' ) {
265 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
268 // Recursively remap the legacy structure
269 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
270 array( $header => $items ) ) );
274 return $return;
278 * Format row header
280 * @param string $header
281 * @return string
283 private function formatRowHeader( $header ) {
284 return Xml::openElement( 'tr' ) .
285 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
286 Xml::closeElement( 'tr' );
289 protected function getGroupName() {
290 return 'wiki';