Merge "Don't try to verify XML well-formedness for partial SVG uploads"
[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 $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;
40
41 $miserMode = $this->getConfig()->get( 'MiserMode' );
42
43 $this->setHeaders();
44 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
45
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 # 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
62 }
63 }
64
65 $text = Xml::openElement( 'table', array( 'class' => 'wikitable mw-statistics-table' ) );
66
67 # Statistic - pages
68 $text .= $this->getPageStats();
69
70 # Statistic - edits
71 $text .= $this->getEditStats();
72
73 # Statistic - users
74 $text .= $this->getUserStats();
75
76 # Statistic - usergroups
77 $text .= $this->getGroupStats();
78
79 # Statistic - other
80 $extraStats = array();
81 if ( wfRunHooks( 'SpecialStatsAddExtra', array( &$extraStats ) ) ) {
82 $text .= $this->getOtherStats( $extraStats );
83 }
84
85 $text .= Xml::closeElement( 'table' );
86
87 # Customizable footer
88 $footer = $this->msg( 'statistics-footer' );
89 if ( !$footer->isBlank() ) {
90 $text .= "\n" . $footer->parse();
91 }
92
93 $this->getOutput()->addHTML( $text );
94 }
95
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
104 */
105 private function formatRow( $text, $number, $trExtraParams = array(),
106 $descMsg = '', $descMsgParam = ''
107 ) {
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" );
114 }
115 }
116
117 return Html::rawElement( 'tr', $trExtraParams,
118 Html::rawElement( 'td', array(), $text ) .
119 Html::rawElement( 'td', array( 'class' => 'mw-statistics-numbers' ), $number )
120 );
121 }
122
123 /**
124 * Each of these methods is pretty self-explanatory, get a particular
125 * row for the table of statistics
126 * @return string
127 */
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' ) );
144 }
145
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' )
153 ) .
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' )
158 );
159 }
160
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' )
168 ) .
169 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
170 Linker::linkKnown(
171 SpecialPage::getTitleFor( 'Activeusers' ),
172 $this->msg( 'listgrouprights-members' )->escaped()
173 ),
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' ) )
178 );
179 }
180
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;
187 }
188 $groupname = htmlspecialchars( $group );
189 $msg = $this->msg( 'group-' . $groupname );
190 if ( $msg->isBlank() ) {
191 $groupnameLocalized = $groupname;
192 } else {
193 $groupnameLocalized = $msg->text();
194 }
195 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
196 if ( $msg->isBlank() ) {
197 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
198 } else {
199 $grouppageLocalized = $msg->text();
200 }
201 $linkTarget = Title::newFromText( $grouppageLocalized );
202 $grouppage = Linker::link(
203 $linkTarget,
204 htmlspecialchars( $groupnameLocalized )
205 );
206 $grouplink = Linker::linkKnown(
207 SpecialPage::getTitleFor( 'Listusers' ),
208 $this->msg( 'listgrouprights-members' )->escaped(),
209 array(),
210 array( 'group' => $group )
211 );
212 # Add a class when a usergroup contains no members to allow hiding these rows
213 $classZero = '';
214 $countUsers = SiteStats::numberingroup( $groupname );
215 if ( $countUsers == 0 ) {
216 $classZero = ' statistics-group-zero';
217 }
218 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
219 $this->getLanguage()->formatNum( $countUsers ),
220 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
221 }
222
223 return $text;
224 }
225
226 /**
227 * Conversion of external statistics into an internal representation
228 * Following a ([<header-message>][<item-message>] = number) pattern
229 *
230 * @param array $stats
231 * @return string
232 */
233 private function getOtherStats( array $stats ) {
234 $return = '';
235
236 foreach ( $stats as $header => $items ) {
237 // Identify the structure used
238 if ( is_array( $items ) ) {
239
240 // Ignore headers that are recursively set as legacy header
241 if ( $header !== 'statistics-header-hooks' ) {
242 $return .= $this->formatRowHeader( $header );
243 }
244
245 // Collect all items that belong to the same header
246 foreach ( $items as $key => $value ) {
247 $name = $this->msg( $key )->parse();
248 $number = htmlspecialchars( $value );
249
250 $return .= $this->formatRow(
251 $name,
252 $this->getLanguage()->formatNum( $number ),
253 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
254 );
255 }
256 } else {
257 // Create the legacy header only once
258 if ( $return === '' ) {
259 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
260 }
261
262 // Recursively remap the legacy structure
263 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
264 array( $header => $items ) ) );
265 }
266 }
267
268 return $return;
269 }
270
271 /**
272 * Format row header
273 *
274 * @param string $header
275 * @return string
276 */
277 private function formatRowHeader( $header ) {
278 return Xml::openElement( 'tr' ) .
279 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
280 Xml::closeElement( 'tr' );
281 }
282
283 protected function getGroupName() {
284 return 'wiki';
285 }
286 }