SpecialStatsAddExtra: Format column label with msg
[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 ( Hooks::run( 'SpecialStatsAddExtra', array( &$extraStats, $this->getContext() ) ) ) {
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 $pageStatsHtml = 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
141 // Show the image row only, when there are files or upload is possible
142 if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
143 $pageStatsHtml .= $this->formatRow( Linker::linkKnown( SpecialPage::getTitleFor( 'MediaStatistics' ),
144 $this->msg( 'statistics-files' )->parse() ),
145 $this->getLanguage()->formatNum( $this->images ),
146 array( 'class' => 'mw-statistics-files' ) );
147 }
148
149 return $pageStatsHtml;
150 }
151
152 private function getEditStats() {
153 return Xml::openElement( 'tr' ) .
154 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( 'statistics-header-edits' )->parse() ) .
155 Xml::closeElement( 'tr' ) .
156 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
157 $this->getLanguage()->formatNum( $this->edits ),
158 array( 'class' => 'mw-statistics-edits' )
159 ) .
160 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
161 $this->getLanguage()
162 ->formatNum( sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) ),
163 array( 'class' => 'mw-statistics-edits-average' )
164 );
165 }
166
167 private function getUserStats() {
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 ) .
175 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
176 Linker::linkKnown(
177 SpecialPage::getTitleFor( 'Activeusers' ),
178 $this->msg( 'listgrouprights-members' )->escaped()
179 ),
180 $this->getLanguage()->formatNum( $this->activeUsers ),
181 array( 'class' => 'mw-statistics-users-active' ),
182 'statistics-users-active-desc',
183 $this->getLanguage()->formatNum( $this->getConfig()->get( 'ActiveUserDays' ) )
184 );
185 }
186
187 private function getGroupStats() {
188 $text = '';
189 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
190 # Skip generic * and implicit groups
191 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) || $group == '*' ) {
192 continue;
193 }
194 $groupname = htmlspecialchars( $group );
195 $msg = $this->msg( 'group-' . $groupname );
196 if ( $msg->isBlank() ) {
197 $groupnameLocalized = $groupname;
198 } else {
199 $groupnameLocalized = $msg->text();
200 }
201 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
202 if ( $msg->isBlank() ) {
203 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
204 } else {
205 $grouppageLocalized = $msg->text();
206 }
207 $linkTarget = Title::newFromText( $grouppageLocalized );
208
209 if ( $linkTarget ) {
210 $grouppage = Linker::link(
211 $linkTarget,
212 htmlspecialchars( $groupnameLocalized )
213 );
214 } else {
215 $grouppage = htmlspecialchars( $groupnameLocalized );
216 }
217
218 $grouplink = Linker::linkKnown(
219 SpecialPage::getTitleFor( 'Listusers' ),
220 $this->msg( 'listgrouprights-members' )->escaped(),
221 array(),
222 array( 'group' => $group )
223 );
224 # Add a class when a usergroup contains no members to allow hiding these rows
225 $classZero = '';
226 $countUsers = SiteStats::numberingroup( $groupname );
227 if ( $countUsers == 0 ) {
228 $classZero = ' statistics-group-zero';
229 }
230 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
231 $this->getLanguage()->formatNum( $countUsers ),
232 array( 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . $classZero ) );
233 }
234
235 return $text;
236 }
237
238 /**
239 * Conversion of external statistics into an internal representation
240 * Following a ([<header-message>][<item-message>] = number) pattern
241 *
242 * @param array $stats
243 * @return string
244 */
245 private function getOtherStats( array $stats ) {
246 $return = '';
247
248 foreach ( $stats as $header => $items ) {
249 // Identify the structure used
250 if ( is_array( $items ) ) {
251
252 // Ignore headers that are recursively set as legacy header
253 if ( $header !== 'statistics-header-hooks' ) {
254 $return .= $this->formatRowHeader( $header );
255 }
256
257 // Collect all items that belong to the same header
258 foreach ( $items as $key => $value ) {
259 if ( is_array( $value ) ) {
260 $name = $value['name'];
261 $number = $value['number'];
262 } else {
263 $name = $this->msg( $key )->parse();
264 $number = $value;
265 }
266
267 $return .= $this->formatRow(
268 $name,
269 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
270 array( 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key )
271 );
272 }
273 } else {
274 // Create the legacy header only once
275 if ( $return === '' ) {
276 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
277 }
278
279 // Recursively remap the legacy structure
280 $return .= $this->getOtherStats( array( 'statistics-header-hooks' =>
281 array( $header => $items ) ) );
282 }
283 }
284
285 return $return;
286 }
287
288 /**
289 * Format row header
290 *
291 * @param string $header
292 * @return string
293 */
294 private function formatRowHeader( $header ) {
295 return Xml::openElement( 'tr' ) .
296 Xml::tags( 'th', array( 'colspan' => '2' ), $this->msg( $header )->parse() ) .
297 Xml::closeElement( 'tr' );
298 }
299
300 protected function getGroupName() {
301 return 'wiki';
302 }
303 }