Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CodeRepoStatsView
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
56
 writeStatusPathTable
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 writeAuthorStatusTable
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 writeAuthorTable
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\CodeReview\UI;
4
5use MediaWiki\Extension\CodeReview\Backend\CodeRevision;
6use MediaWiki\Extension\CodeReview\Backend\RepoStats;
7use MediaWiki\MediaWikiServices;
8use OutputPage;
9use SpecialPage;
10
11/**
12 * Special:Code/MediaWiki/stats
13 */
14class CodeRepoStatsView extends CodeView {
15    public function execute() {
16        global $wgOut, $wgLang;
17
18        // Todo inject instead of accessing the global
19        $output = $wgOut;
20
21        $stats = RepoStats::newFromRepo( $this->mRepo );
22        $repoName = $this->mRepo->getName();
23        $output->wrapWikiMsg( '<h2 id="stats-main">$1</h2>', [ 'code-stats-header', $repoName ] );
24        $output->addWikiMsg( 'code-stats-main',
25            $wgLang->timeanddate( $stats->time, true ),
26            $wgLang->formatNum( $stats->revisions ),
27            $repoName,
28            $wgLang->formatNum( $stats->authors ),
29            $wgLang->time( $stats->time, true ),
30            $wgLang->date( $stats->time, true )
31        );
32
33        if ( !empty( $stats->states ) ) {
34            $output->wrapWikiMsg( '<h3 id="stats-revisions">$1</h3>',
35                'code-stats-status-breakdown' );
36            $output->addHTML( '<table class="wikitable">'
37                . '<tr><th>' . wfMessage( 'code-field-status' )->escaped() . '</th><th>'
38                . wfMessage( 'code-stats-count' )->escaped() . '</th></tr>' );
39            $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
40            foreach ( CodeRevision::getPossibleStates() as $state ) {
41                $rawCount = $stats->states[$state] ?? 0;
42                $count = htmlspecialchars( $wgLang->formatNum( $rawCount ) );
43                $link = $linkRenderer->makeLink(
44                    SpecialPage::getTitleFor( 'Code', $repoName . '/status/' . $state ),
45                    $this->statusDesc( $state )
46                );
47                $output->addHTML( "<tr><td>$link</td>"
48                    . "<td class=\"mw-codereview-status-$state\">$count</td></tr>" );
49            }
50            $output->addHTML( '</table>' );
51        }
52
53        if ( !empty( $stats->fixmes ) ) {
54            $this->writeAuthorStatusTable( $output, 'fixme', $stats->fixmes );
55        }
56
57        if ( !empty( $stats->new ) ) {
58            $this->writeAuthorStatusTable( $output, 'new', $stats->new );
59        }
60
61        if ( !empty( $stats->fixmesPerPath ) ) {
62            $this->writeStatusPathTable( $output, 'fixme', $stats->fixmesPerPath );
63        }
64
65        if ( !empty( $stats->newPerPath ) ) {
66            $this->writeStatusPathTable( $output, 'new', $stats->newPerPath );
67        }
68    }
69
70    /**
71     * @param OutputPage $output
72     * @param string $status
73     * @param array $array
74     */
75    private function writeStatusPathTable( $output, $status, $array ) {
76        $output->wrapWikiMsg( "<h3 id=\"stats-$status-path\">$1</h3>",
77            "code-stats-$status-breakdown-path" );
78
79        foreach ( $array as $path => $news ) {
80            $output->wrapWikiMsg( "<h4 id=\"stats-$status-path\">$1</h4>",
81                [ "code-stats-$status-path", $path ] );
82            $this->writeAuthorTable( $output, $status, $news, [ 'path' => $path ] );
83        }
84    }
85
86    /**
87     * @param OutputPage $output
88     * @param string $status
89     * @param array $array
90     */
91    private function writeAuthorStatusTable( $output, $status, $array ) {
92        $output->wrapWikiMsg( "<h3 id=\"stats-{$status}\">$1</h3>",
93            "code-stats-{$status}-breakdown" );
94        $this->writeAuthorTable( $output, $status, $array );
95    }
96
97    /**
98     * @param OutputPage $output
99     * @param string $status
100     * @param array $array
101     * @param array $options
102     */
103    private function writeAuthorTable( $output, $status, $array, $options = [] ) {
104        global $wgLang;
105
106        $repoName = $this->mRepo->getName();
107        $output->addHTML( '<table class="wikitable">'
108            . '<tr><th>' . wfMessage( 'code-field-author' )->escaped() . '</th><th>'
109            . wfMessage( 'code-stats-count' )->escaped() . '</th></tr>' );
110        $title = SpecialPage::getTitleFor( 'Code', $repoName . "/status/{$status}" );
111
112        $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
113        foreach ( $array as $user => $count ) {
114            $count = htmlspecialchars( $wgLang->formatNum( $count ) );
115            $link = $linkRenderer->makeLink(
116                $title,
117                $user,
118                [],
119                array_merge( $options, [ 'author' => $user ] )
120            );
121            $output->addHTML( "<tr><td>$link</td>"
122                . "<td>$count</td></tr>" );
123        }
124        $output->addHTML( '</table>' );
125    }
126}