Merge "Http::getProxy() method to get proxy configuration"
[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 $miserMode = $this->getConfig()->get( 'MiserMode' );
40
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( 'small', [ 'class' => 'mw-statistic-desc' ],
101 " $descriptionHtml" );
102 }
103 }
104
105 return Html::rawElement( 'tr', $trExtraParams,
106 Html::rawElement( 'td', [], $text ) .
107 Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
108 );
109 }
110
111 /**
112 * Each of these methods is pretty self-explanatory, get a particular
113 * row for the table of statistics
114 * @return string
115 */
116 private function getPageStats() {
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( Linker::linkKnown( $specialAllPagesTitle,
123 $this->msg( 'statistics-articles' )->parse(), [], [ 'hideredirects' => 1 ] ),
124 $this->getLanguage()->formatNum( $this->good ),
125 [ 'class' => 'mw-statistics-articles' ],
126 'statistics-articles-desc' ) .
127 $this->formatRow( Linker::linkKnown( $specialAllPagesTitle,
128 $this->msg( 'statistics-pages' )->parse() ),
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 Linker::linkKnown( SpecialPage::getTitleFor( 'MediaStatistics' ),
137 $this->msg( 'statistics-files' )->parse() ),
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 Linker::linkKnown(
172 SpecialPage::getTitleFor( 'Activeusers' ),
173 $this->msg( 'listgrouprights-members' )->escaped()
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 $text = '';
184 foreach ( $this->getConfig()->get( 'GroupPermissions' ) as $group => $permissions ) {
185 # Skip generic * and implicit groups
186 if ( in_array( $group, $this->getConfig()->get( 'ImplicitGroups' ) ) || $group == '*' ) {
187 continue;
188 }
189 $groupname = htmlspecialchars( $group );
190 $msg = $this->msg( 'group-' . $groupname );
191 if ( $msg->isBlank() ) {
192 $groupnameLocalized = $groupname;
193 } else {
194 $groupnameLocalized = $msg->text();
195 }
196 $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
197 if ( $msg->isBlank() ) {
198 $grouppageLocalized = MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
199 } else {
200 $grouppageLocalized = $msg->text();
201 }
202 $linkTarget = Title::newFromText( $grouppageLocalized );
203
204 if ( $linkTarget ) {
205 $grouppage = Linker::link(
206 $linkTarget,
207 htmlspecialchars( $groupnameLocalized )
208 );
209 } else {
210 $grouppage = htmlspecialchars( $groupnameLocalized );
211 }
212
213 $grouplink = Linker::linkKnown(
214 SpecialPage::getTitleFor( 'Listusers' ),
215 $this->msg( 'listgrouprights-members' )->escaped(),
216 [],
217 [ 'group' => $group ]
218 );
219 # Add a class when a usergroup contains no members to allow hiding these rows
220 $classZero = '';
221 $countUsers = SiteStats::numberingroup( $groupname );
222 if ( $countUsers == 0 ) {
223 $classZero = ' statistics-group-zero';
224 }
225 $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
226 $this->getLanguage()->formatNum( $countUsers ),
227 [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
228 $classZero ] );
229 }
230
231 return $text;
232 }
233
234 /**
235 * Conversion of external statistics into an internal representation
236 * Following a ([<header-message>][<item-message>] = number) pattern
237 *
238 * @param array $stats
239 * @return string
240 */
241 private function getOtherStats( array $stats ) {
242 $return = '';
243
244 foreach ( $stats as $header => $items ) {
245 // Identify the structure used
246 if ( is_array( $items ) ) {
247
248 // Ignore headers that are recursively set as legacy header
249 if ( $header !== 'statistics-header-hooks' ) {
250 $return .= $this->formatRowHeader( $header );
251 }
252
253 // Collect all items that belong to the same header
254 foreach ( $items as $key => $value ) {
255 if ( is_array( $value ) ) {
256 $name = $value['name'];
257 $number = $value['number'];
258 } else {
259 $name = $this->msg( $key )->parse();
260 $number = $value;
261 }
262
263 $return .= $this->formatRow(
264 $name,
265 $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
266 [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
267 );
268 }
269 } else {
270 // Create the legacy header only once
271 if ( $return === '' ) {
272 $return .= $this->formatRowHeader( 'statistics-header-hooks' );
273 }
274
275 // Recursively remap the legacy structure
276 $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
277 [ $header => $items ] ] );
278 }
279 }
280
281 return $return;
282 }
283
284 /**
285 * Format row header
286 *
287 * @param string $header
288 * @return string
289 */
290 private function formatRowHeader( $header ) {
291 return Xml::openElement( 'tr' ) .
292 Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) .
293 Xml::closeElement( 'tr' );
294 }
295
296 protected function getGroupName() {
297 return 'wiki';
298 }
299 }