Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialApiFeatureUsage
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 4
182
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 / 79
0.00% covered (danger)
0.00%
0 / 1
110
 onSubmit
0.00% covered (danger)
0.00%
0 / 4
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
3namespace MediaWiki\Extension\ApiFeatureUsage;
4
5use MediaWiki\Html\Html;
6use MediaWiki\HTMLForm\HTMLForm;
7use MediaWiki\SpecialPage\SpecialPage;
8use MediaWiki\Status\Status;
9use MediaWiki\Utils\MWTimestamp;
10
11class SpecialApiFeatureUsage extends SpecialPage {
12    /** @var ApiFeatureUsageQueryEngine */
13    private $engine;
14
15    public function __construct( ApiFeatureUsageQueryEngine $queryEngine ) {
16        parent::__construct( 'ApiFeatureUsage' );
17
18        $this->engine = $queryEngine;
19    }
20
21    /** @inheritDoc */
22    public function execute( $par ) {
23        $this->setHeaders();
24        $this->addHelpLink( 'Extension:ApiFeatureUsage' );
25        $this->checkPermissions();
26
27        $request = $this->getRequest();
28
29        [ $start, $end ] = $this->engine->suggestDateRange();
30
31        $form = HTMLForm::factory( 'ooui', [
32            'agent' => [
33                'type' => 'text',
34                'default' => '',
35                'label-message' => 'apifeatureusage-agent-label',
36                'required' => true,
37            ],
38            'startdate' => [
39                'type' => 'date',
40                'label-message' => 'apifeatureusage-startdate-label',
41                'required' => true,
42                'default' => $start->format( 'Y-m-d' ),
43            ],
44            'enddate' => [
45                'type' => 'date',
46                'label-message' => 'apifeatureusage-enddate-label',
47                'required' => true,
48                'default' => $end->format( 'Y-m-d' ),
49            ],
50        ], $this->getContext() );
51        $form->setMethod( 'get' );
52        $form->setSubmitCallback( [ $this, 'onSubmit' ] );
53        $form->setWrapperLegendMsg( 'apifeatureusage-legend' );
54        $form->addHeaderHtml( $this->msg( 'apifeatureusage-text' )->parseAsBlock() );
55        $form->setSubmitTextMsg( 'apifeatureusage-submit' );
56
57        $form->prepareForm();
58        if ( $request->getCheck( 'wpagent' ) || $request->getCheck( 'wpstartdate' ) ||
59            $request->getCheck( 'wpenddate' )
60        ) {
61            $status = $form->trySubmit();
62        } else {
63            $status = false;
64        }
65        $form->displayForm( $status );
66
67        if ( $status instanceof Status && $status->isOk() ) {
68            $out = $this->getOutput();
69            $out->addModuleStyles( 'ext.apifeatureusage' );
70
71            $warnings = [];
72            foreach ( $status->getMessages( 'warning' ) as $msg ) {
73                $warnings[] = $this->msg( $msg )->plain();
74            }
75            if ( $warnings ) {
76                if ( count( $warnings ) > 1 ) {
77                    $warnings = "\n* " . implode( "\n* ", $warnings );
78                } else {
79                    $warnings = $warnings[0];
80                }
81                $out->wrapWikiMsg( "<div class='error'>\n$1\n</div>",
82                    [ 'apifeatureusage-warnings', $warnings ]
83                );
84            }
85
86            $lang = $this->getLanguage();
87            $rows = [];
88            foreach ( $status->value as $row ) {
89                $cells = [];
90                $cells[] = Html::element( 'td', [], $row['feature'] );
91                $cells[] = Html::rawElement( 'td', [],
92                    Html::element( 'time', [], $row['date'] )
93                );
94                $cells[] = Html::element( 'td', [ 'class' => 'mw-apifeatureusage-count' ],
95                    $lang->formatNum( $row['count'] )
96                );
97
98                $rows[] = Html::rawElement( 'tr', [], implode( '', $cells ) );
99            }
100            $this->getOutput()->addHTML(
101                Html::rawElement( 'table', [ 'class' => 'wikitable sortable mw-apifeatureusage' ],
102                    Html::rawElement( 'thead', [],
103                        Html::rawElement( 'tr', [],
104                            Html::rawElement( 'th', [],
105                                $this->msg( 'apifeatureusage-column-feature' )->parse()
106                            ) .
107                            Html::rawElement( 'th', [],
108                                $this->msg( 'apifeatureusage-column-date' )->parse()
109                            ) .
110                            Html::rawElement( 'th', [],
111                                $this->msg( 'apifeatureusage-column-uses' )->parse()
112                            )
113                        )
114                    ) .
115                    Html::rawElement( 'tbody', [], implode( '', $rows ) )
116                )
117            );
118        }
119    }
120
121    /**
122     * @param array $data
123     * @param HTMLForm $form
124     * @return Status
125     */
126    public function onSubmit( $data, $form ) {
127        $agent = $data['agent'];
128        $start = new MWTimestamp( $data['startdate'] . 'T00:00:00Z' );
129        $end = new MWTimestamp( $data['enddate'] . 'T23:59:59Z' );
130
131        return $this->engine->enumerate( $agent, $start, $end );
132    }
133
134    /** @inheritDoc */
135    protected function getGroupName() {
136        return 'wiki';
137    }
138}