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