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