Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 157
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialStatistics
0.00% covered (danger)
0.00%
0 / 156
0.00% covered (danger)
0.00%
0 / 10
812
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 formatRow
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 getPageStats
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
20
 getEditStats
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getUserStats
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupStats
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
30
 getOtherStats
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
 formatRowHeader
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Specials;
22
23use MediaWiki\Html\Html;
24use MediaWiki\MainConfigNames;
25use MediaWiki\Parser\Sanitizer;
26use MediaWiki\SiteStats\SiteStats;
27use MediaWiki\SpecialPage\SpecialPage;
28use MediaWiki\Title\Title;
29use MediaWiki\User\UserGroupManager;
30use MediaWiki\User\UserGroupMembership;
31use MediaWiki\Xml\Xml;
32
33/**
34 * Special page lists various statistics, including the contents of
35 * `site_stats`, plus page view details if enabled
36 *
37 * @ingroup SpecialPage
38 */
39class SpecialStatistics extends SpecialPage {
40    private int $edits;
41    private int $good;
42    private int $images;
43    private int $total;
44    private int $users;
45    private int $activeUsers;
46
47    private UserGroupManager $userGroupManager;
48
49    /**
50     * @param UserGroupManager $userGroupManager
51     */
52    public function __construct( UserGroupManager $userGroupManager ) {
53        parent::__construct( 'Statistics' );
54        $this->userGroupManager = $userGroupManager;
55    }
56
57    public function execute( $par ) {
58        $this->setHeaders();
59        $this->outputHeader();
60        $this->getOutput()->addModuleStyles( 'mediawiki.special' );
61
62        $this->edits = SiteStats::edits();
63        $this->good = SiteStats::articles();
64        $this->images = SiteStats::images();
65        $this->total = SiteStats::pages();
66        $this->users = SiteStats::users();
67        $this->activeUsers = SiteStats::activeUsers();
68
69        $text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] );
70
71        # Statistic - pages
72        $text .= $this->getPageStats();
73
74        # Statistic - edits
75        $text .= $this->getEditStats();
76
77        # Statistic - users
78        $text .= $this->getUserStats();
79
80        # Statistic - usergroups
81        $text .= $this->getGroupStats();
82
83        # Statistic - other
84        $extraStats = [];
85        if ( $this->getHookRunner()->onSpecialStatsAddExtra(
86            $extraStats, $this->getContext() )
87        ) {
88            $text .= $this->getOtherStats( $extraStats );
89        }
90
91        $text .= Xml::closeElement( 'table' );
92
93        # Customizable footer
94        $footer = $this->msg( 'statistics-footer' );
95        if ( !$footer->isBlank() ) {
96            $text .= "\n" . $footer->parse();
97        }
98
99        $this->getOutput()->addHTML( $text );
100    }
101
102    /**
103     * Format a row
104     * @param string $text Description of the row
105     * @param float|string $number A statistical number
106     * @param array $trExtraParams Params to table row, see Html::element
107     * @param string $descMsg Message key
108     * @param array|string $descMsgParam Message parameters
109     * @return string Table row in HTML format
110     */
111    private function formatRow( $text, $number, $trExtraParams = [],
112        $descMsg = '', $descMsgParam = ''
113    ) {
114        if ( $descMsg ) {
115            $msg = $this->msg( $descMsg, $descMsgParam );
116            if ( !$msg->isDisabled() ) {
117                $descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() )
118                    ->escaped();
119                $text .= "<br />" . Html::rawElement(
120                    'small',
121                    [ 'class' => 'mw-statistic-desc' ],
122                    " $descriptionHtml"
123                );
124            }
125        }
126
127        return Html::rawElement( 'tr', $trExtraParams,
128            Html::rawElement( 'td', [], $text ) .
129            Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number )
130        );
131    }
132
133    /**
134     * Each of these methods is pretty self-explanatory, get a particular
135     * row for the table of statistics
136     * @return string
137     */
138    private function getPageStats() {
139        $linkRenderer = $this->getLinkRenderer();
140
141        $specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' );
142        $pageStatsHtml = Html::rawElement( 'tr', [],
143            Xml::tags( 'th', [ 'colspan' => '2' ],
144                $this->msg( 'statistics-header-pages' )->parse()
145            ) ) .
146                $this->formatRow(
147                    $this->getConfig()->get( MainConfigNames::MiserMode )
148                        ? $this->msg( 'statistics-articles' )->escaped()
149                        : $linkRenderer->makeKnownLink(
150                            $specialAllPagesTitle,
151                            $this->msg( 'statistics-articles' )->text(),
152                            [], [ 'hideredirects' => 1 ] ),
153                    $this->getLanguage()->formatNum( $this->good ),
154                    [ 'class' => 'mw-statistics-articles' ],
155                    'statistics-articles-desc' ) .
156                $this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle,
157                    $this->msg( 'statistics-pages' )->text() ),
158                    $this->getLanguage()->formatNum( $this->total ),
159                    [ 'class' => 'mw-statistics-pages' ],
160                    'statistics-pages-desc' );
161
162        // Show the image row only, when there are files or upload is possible
163        if ( $this->images !== 0 || $this->getConfig()->get( MainConfigNames::EnableUploads ) ) {
164            $pageStatsHtml .= $this->formatRow(
165                $linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ),
166                $this->msg( 'statistics-files' )->text() ),
167                $this->getLanguage()->formatNum( $this->images ),
168                [ 'class' => 'mw-statistics-files' ], 'statistics-files-desc' );
169        }
170
171        return $pageStatsHtml;
172    }
173
174    private function getEditStats() {
175        return Html::rawElement( 'tr', [],
176            Xml::tags( 'th', [ 'colspan' => '2' ],
177                $this->msg( 'statistics-header-edits' )->parse()
178            ) ) .
179            $this->formatRow( $this->msg( 'statistics-edits' )->parse(),
180                $this->getLanguage()->formatNum( $this->edits ),
181                [ 'class' => 'mw-statistics-edits' ]
182            ) .
183            $this->formatRow( $this->msg( 'statistics-edits-average' )->parse(),
184                $this->getLanguage()->formatNum(
185                    sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 )
186                ), [ 'class' => 'mw-statistics-edits-average' ]
187            );
188    }
189
190    private function getUserStats() {
191        return Html::rawElement( 'tr', [],
192            Xml::tags( 'th', [ 'colspan' => '2' ],
193                $this->msg( 'statistics-header-users' )->parse()
194            ) ) .
195            $this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' .
196                $this->getLinkRenderer()->makeKnownLink(
197                    SpecialPage::getTitleFor( 'Listusers' ),
198                    $this->msg( 'listgrouprights-members' )->text()
199                ),
200                $this->getLanguage()->formatNum( $this->users ),
201                [ 'class' => 'mw-statistics-users' ]
202            ) .
203            $this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' .
204                $this->getLinkRenderer()->makeKnownLink(
205                    SpecialPage::getTitleFor( 'Activeusers' ),
206                    $this->msg( 'listgrouprights-members' )->text()
207                ),
208                $this->getLanguage()->formatNum( $this->activeUsers ),
209                [ 'class' => 'mw-statistics-users-active' ],
210                'statistics-users-active-desc',
211                $this->getLanguage()->formatNum(
212                    $this->getConfig()->get( MainConfigNames::ActiveUserDays ) )
213            );
214    }
215
216    private function getGroupStats() {
217        $linkRenderer = $this->getLinkRenderer();
218        $lang = $this->getLanguage();
219        $text = '';
220        foreach ( $this->userGroupManager->listAllGroups() as $group ) {
221            $groupnameLocalized = $lang->getGroupName( $group );
222            $linkTarget = UserGroupMembership::getGroupPage( $group )
223                ?: Title::makeTitleSafe( NS_PROJECT, $group );
224
225            if ( $linkTarget ) {
226                $grouppage = $linkRenderer->makeLink(
227                    $linkTarget,
228                    $groupnameLocalized
229                );
230            } else {
231                $grouppage = htmlspecialchars( $groupnameLocalized );
232            }
233
234            $grouplink = $linkRenderer->makeKnownLink(
235                SpecialPage::getTitleFor( 'Listusers' ),
236                $this->msg( 'listgrouprights-members' )->text(),
237                [],
238                [ 'group' => $group ]
239            );
240            # Add a class when a usergroup contains no members to allow hiding these rows
241            $classZero = '';
242            $countUsers = SiteStats::numberingroup( $group );
243            if ( $countUsers == 0 ) {
244                $classZero = ' statistics-group-zero';
245            }
246            $text .= $this->formatRow( $grouppage . ' ' . $grouplink,
247                $this->getLanguage()->formatNum( $countUsers ),
248                [ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) .
249                    $classZero ] );
250        }
251
252        return $text;
253    }
254
255    /**
256     * Conversion of external statistics into an internal representation
257     * Following a ([<header-message>][<item-message>] = number) pattern
258     *
259     * @param array $stats
260     * @return string
261     */
262    private function getOtherStats( array $stats ) {
263        $return = '';
264
265        foreach ( $stats as $header => $items ) {
266            // Identify the structure used
267            if ( is_array( $items ) ) {
268                // Ignore headers that are recursively set as legacy header
269                if ( $header !== 'statistics-header-hooks' ) {
270                    $return .= $this->formatRowHeader( $header );
271                }
272
273                // Collect all items that belong to the same header
274                foreach ( $items as $key => $value ) {
275                    if ( is_array( $value ) ) {
276                        $name = $value['name'];
277                        $number = $value['number'];
278                    } else {
279                        $name = $this->msg( $key )->parse();
280                        $number = $value;
281                    }
282
283                    $return .= $this->formatRow(
284                        $name,
285                        $this->getLanguage()->formatNum( htmlspecialchars( $number ) ),
286                        [ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ]
287                    );
288                }
289            } else {
290                // Create the legacy header only once
291                if ( $return === '' ) {
292                    $return .= $this->formatRowHeader( 'statistics-header-hooks' );
293                }
294
295                // Recursively remap the legacy structure
296                $return .= $this->getOtherStats( [ 'statistics-header-hooks' =>
297                    [ $header => $items ] ] );
298            }
299        }
300
301        return $return;
302    }
303
304    /**
305     * Format row header
306     *
307     * @param string $header
308     * @return string
309     */
310    private function formatRowHeader( $header ) {
311        return Html::rawElement( 'tr', [],
312            Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() )
313        );
314    }
315
316    protected function getGroupName() {
317        return 'wiki';
318    }
319}
320
321/**
322 * Retain the old class name for backwards compatibility.
323 * @deprecated since 1.41
324 */
325class_alias( SpecialStatistics::class, 'SpecialStatistics' );