Merge "HTML escape parameter 'text' of hook 'SkinEditSectionLinks'"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Special page lists various statistics, including the contents of
28 * `site_stats`, plus page view details if enabled
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialStatistics extends SpecialPage {
33 private $edits, $good, $images, $total, $users,
34 $activeUsers = 0;
35
36 public function __construct() {
37 parent::__construct( 'Statistics' );
38 }
39
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
43
44 $this->edits = SiteStats::edits();
45 $this->good = SiteStats::articles();
46 $this->images = SiteStats::images();
47 $this->total = SiteStats::pages();
48 $this->users = SiteStats::users();
49 $this->activeUsers = SiteStats::activeUsers();
50 $this->hook = '';
51
52 $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
53
54 # Statistic - pages
55 $text .= $this->getPageStats();
56
57 # Statistic - edits
58 $text .= $this->getEditStats();
59
60 # Statistic - users
61 $text .= $this->getUserStats();
62
63 # Statistic - usergroups
64 $text .= $this->getGroupStats();
65
66 # Statistic - other
67 $extraStats = [];
68 if ( Hooks::run( 'SpecialStatsAddExtra', [ &$extraStats, $this->getContext() ] ) ) {
69 $text .= $this->getOtherStats( $extraStats );
70 }
71
72 $text .= Xml::closeElement( 'table' );
73
74 # Customizable footer
75 $footer = $this->msg( 'statistics-footer' );
76 if ( !$footer->isBlank() ) {
77 $text .= "\n" . $footer->parse();
78 }
79
80 $this->getOutput()->addHTML( $text );
81 }
82
83 /**
84 * Format a row
85 * @param string $text Description of the row
86 * @param float $number A statistical number
87 * @param array $trExtraParams Params to table row, see Html::elememt
88 * @param string $descMsg Message key
89 * @param array|string $descMsgParam Message parameters
90 * @return string Table row in HTML format
91 */
92 private function formatRow( $text, $number, $trExtraParams = [],
93 $descMsg = '', $descMsgParam = ''
94 ) {
95 if ( $descMsg ) {
96 $msg = $this->msg( $descMsg, $descMsgParam );
97 if ( !$msg->isDisabled() ) {
98 $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
99 ->escaped();
100 $text .= "<br />" . Html::rawElement(
101 'small',
102 [ 'class' => 'mw-statistic-desc' ],
103 " $descriptionHtml"
104 );
105 }
106 }
107
108 return Html::rawElement( 'tr', $trExtraParams,
109 Html::rawElement( 'td', [], $text ) .
110 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
111 );
112 }
113
114 /**
115 * Each of these methods is pretty self-explanatory, get a particular
116 * row for the table of statistics
117 * @return string
118 */
119 private function getPageStats() {
120 $linkRenderer = $this->getLinkRenderer();
121
122 $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
123 $pageStatsHtml = Xml::openElement( 'tr' ) .
124 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( 'statistics-header-pages' )
125 ->parse() ) .
126 Xml::closeElement( 'tr' ) .
127 $this->formatRow( $linkRenderer->makeKnownLink(
128 $specialAllPagesTitle,
129 $this->msg( 'statistics-articles' )->text(),
130 [], [ 'hideredirects' => 1 ] ),
131 $this->getLanguage()->formatNum( $this->good ),
132 [ 'class' => 'mw-statistics-articles' ],
133 'statistics-articles-desc' ) .
134 $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
135 $this->msg( 'statistics-pages' )->text() ),
136 $this->getLanguage()->formatNum( $this->total ),
137 [ 'class' => 'mw-statistics-pages' ],
138 'statistics-pages-desc' );
139
140 // Show the image row only, when there are files or upload is possible
141 if ( $this->images !== 0 || $this->getConfig()->get( 'EnableUploads' ) ) {
142 $pageStatsHtml .= $this->formatRow(
143 $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
144 $this->msg( 'statistics-files' )->text() ),
145 $this->getLanguage()->formatNum( $this->images ),
146 [ 'class' => 'mw-statistics-files' ] );
147 }
148
149 return $pageStatsHtml;
150 }
151
152 private function getEditStats() {
153 return Xml::openElement( 'tr' ) .
154 Xml::tags( 'th', [ 'colspan' => '2' ],
155 $this->msg( 'statistics-header-edits' )->parse() ) .
156 Xml::closeElement( 'tr' ) .
157 $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
158 $this->getLanguage()->formatNum( $this->edits ),
159 [ 'class' => 'mw-statistics-edits' ]
160 ) .
161 $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
162 $this->getLanguage()->formatNum(
163 sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
164 ), [ 'class' => 'mw-statistics-edits-average' ]
165 );
166 }
167
168 private function getUserStats() {
169 return Xml::openElement( 'tr' ) .
170 Xml::tags( 'th', [ 'colspan' => '2' ],
171 $this->msg( 'statistics-header-users' )->parse() ) .
172 Xml::closeElement( 'tr' ) .
173 $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
174 $this->getLinkRenderer()->makeKnownLink(
175 SpecialPage::getTitleFor( 'Listusers' ),
176 $this->msg( 'listgrouprights-members' )->text()
177 ),
178 $this->getLanguage()->formatNum( $this->users ),
179 [ 'class' => 'mw-statistics-users' ]
180 ) .
181 $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
182 $this->getLinkRenderer()->makeKnownLink(
183 SpecialPage::getTitleFor( 'Activeusers' ),
184 $this->msg( 'listgrouprights-members' )->text()
185 ),
186 $this->getLanguage()->formatNum( $this->activeUsers ),
187 [ 'class' => 'mw-statistics-users-active' ],
188 'statistics-users-active-desc',
189 $this->getLanguage()->formatNum(
190 $this->getConfig()->get( 'ActiveUserDays' ) )
191 );
192 }
193
194 private function getGroupStats() {
195 $linkRenderer = $this->getLinkRenderer();
196 $text = '';
197 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
198 # Skip generic * and implicit groups
199 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) )
200 || $group == '*' ) {
201 continue;
202 }
203 $groupname = htmlspecialchars( $group );
204 $msg = $this->msg( 'group-' . $groupname );
205 if ( $msg->isBlank() ) {
206 $groupnameLocalized = $groupname;
207 } else {
208 $groupnameLocalized = $msg->text();
209 }
210 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
211 if ( $msg->isBlank() ) {
212 $grouppageLocalized = MediaWikiServices::getInstance()->getNamespaceInfo()->
213 getCanonicalName( NS_PROJECT ) . ':' . $groupname;
214 } else {
215 $grouppageLocalized = $msg->text();
216 }
217 $linkTarget = Title::newFromText( $grouppageLocalized );
218
219 if ( $linkTarget ) {
220 $grouppage = $linkRenderer->makeLink(
221 $linkTarget,
222 $groupnameLocalized
223 );
224 } else {
225 $grouppage = htmlspecialchars( $groupnameLocalized );
226 }
227
228 $grouplink = $linkRenderer->makeKnownLink(
229 SpecialPage::getTitleFor( 'Listusers' ),
230 $this->msg( 'listgrouprights-members' )->text(),
231 [],
232 [ 'group' => $group ]
233 );
234 # Add a class when a usergroup contains no members to allow hiding these rows
235 $classZero = '';
236 $countUsers = SiteStats::numberingroup( $groupname );
237 if ( $countUsers == 0 ) {
238 $classZero = ' statistics-group-zero';
239 }
240 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
241 $this->getLanguage()->formatNum( $countUsers ),
242 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
243 $classZero ] );
244 }
245
246 return $text;
247 }
248
249 /**
250 * Conversion of external statistics into an internal representation
251 * Following a ([<header-message>][<item-message>] = number) pattern
252 *
253 * @param array $stats
254 * @return string
255 */
256 private function getOtherStats( array $stats ) {
257 $return = '';
258
259 foreach ( $stats as $header => $items ) {
260 // Identify the structure used
261 if ( is_array( $items ) ) {
262 // Ignore headers that are recursively set as legacy header
263 if ( $header !== 'statistics-header-hooks' ) {
264 $return .= $this->formatRowHeader( $header );
265 }
266
267 // Collect all items that belong to the same header
268 foreach ( $items as $key => $value ) {
269 if ( is_array( $value ) ) {
270 $name = $value['name'];
271 $number = $value['number'];
272 } else {
273 $name = $this->msg( $key )->parse();
274 $number = $value;
275 }
276
277 $return .= $this->formatRow(
278 $name,
279 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
280 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
281 );
282 }
283 } else {
284 // Create the legacy header only once
285 if ( $return === '' ) {
286 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
287 }
288
289 // Recursively remap the legacy structure
290 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
291 [ $header => $items ] ] );
292 }
293 }
294
295 return $return;
296 }
297
298 /**
299 * Format row header
300 *
301 * @param string $header
302 * @return string
303 */
304 private function formatRowHeader( $header ) {
305 return Xml::openElement( 'tr' ) .
306 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
307 Xml::closeElement( 'tr' );
308 }
309
310 protected function getGroupName() {
311 return 'wiki';
312 }
313 }