Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
68.66% |
46 / 67 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
SpecialSimilarEditors | |
68.66% |
46 / 67 |
|
75.00% |
3 / 4 |
21.93 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
5 | |||
onSubmit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onSuccess | |
36.36% |
12 / 33 |
|
0.00% |
0 / 1 |
24.49 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SimilarEditors; |
4 | |
5 | use Html; |
6 | use HTMLForm; |
7 | use OOUI\HtmlSnippet; |
8 | use OOUI\MessageWidget; |
9 | use SpecialPage; |
10 | use Status; |
11 | |
12 | class SpecialSimilarEditors extends SpecialPage { |
13 | |
14 | /** @var Client */ |
15 | private $similarEditorsClient; |
16 | |
17 | /** @var ResultsFormatterFactory */ |
18 | private $resultsFormatterFactory; |
19 | |
20 | /** @var HTMLForm */ |
21 | private $form; |
22 | |
23 | /** |
24 | * @param Client $similarEditorsClient |
25 | * @param ResultsFormatterFactory $resultsFormatterFactory |
26 | */ |
27 | public function __construct( |
28 | Client $similarEditorsClient, |
29 | ResultsFormatterFactory $resultsFormatterFactory |
30 | ) { |
31 | parent::__construct( 'SimilarEditors', 'similareditors', true ); |
32 | $this->similarEditorsClient = $similarEditorsClient; |
33 | $this->resultsFormatterFactory = $resultsFormatterFactory; |
34 | } |
35 | |
36 | /** |
37 | * @inheritDoc |
38 | */ |
39 | public function execute( $par ) { |
40 | $this->setHeaders(); |
41 | $this->checkPermissions(); |
42 | $this->outputHeader(); |
43 | $this->addHelpLink( 'Help:Extension:SimilarEditors' ); |
44 | |
45 | $out = $this->getOutput(); |
46 | $out->addModuleStyles( 'mediawiki.pager.styles' ); |
47 | $out->addModuleStyles( 'ext.similarEditors.styles' ); |
48 | |
49 | // Ensure the correct survey is added, in case multiple are enabled |
50 | $out->addHTML( Html::element( 'div', [ 'id' => 'similareditors-survey-embed' ] ) ); |
51 | |
52 | $fields = [ |
53 | 'Target' => [ |
54 | 'type' => 'user', |
55 | 'label-message' => 'similareditors-form-field-target-label', |
56 | 'placeholder-message' => 'similareditors-form-field-target-placeholder', |
57 | // TODO: revert as part of T309675 |
58 | 'exists' => false, |
59 | 'ipallowed' => true, |
60 | 'required' => true, |
61 | ], |
62 | ]; |
63 | |
64 | $this->form = HTMLForm::factory( 'ooui', $fields, $this->getContext() ); |
65 | $this->form |
66 | ->setMethod( 'post' ) |
67 | ->setWrapperLegendMsg( 'similareditors-form-legend' ) |
68 | ->setSubmitTextMsg( 'similareditors-form-submit' ) |
69 | ->setSubmitCallback( [ $this, 'onSubmit' ] ); |
70 | |
71 | if ( $this->getRequest()->getVal( 'wpTarget' ) === null ) { |
72 | $this->form->prepareForm() |
73 | ->displayForm( false ); |
74 | } else { |
75 | $status = $this->form->show(); |
76 | if ( $status === true || $status instanceof Status && $status->isGood() ) { |
77 | $this->onSuccess(); |
78 | } |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * Placeholder for the submit callback as required by HTMLForm |
84 | * |
85 | * @param array $formData |
86 | * @return bool |
87 | */ |
88 | public function onSubmit( $formData ) { |
89 | return true; |
90 | } |
91 | |
92 | /** |
93 | * Handle successful form submission. |
94 | * |
95 | * This includes handling errors from the Similarusers service. |
96 | */ |
97 | public function onSuccess() { |
98 | $out = $this->getOutput(); |
99 | |
100 | $target = $this->getRequest()->getVal( 'wpTarget' ); |
101 | $result = $this->similarEditorsClient->getSimilarEditors( $target ); |
102 | |
103 | if ( is_array( $result ) ) { |
104 | $this->form->displayForm( true ); |
105 | $out->addModules( 'ext.similarEditors' ); |
106 | if ( count( $result ) > 0 ) { |
107 | $resultsFormatter = $this->resultsFormatterFactory->createFormatter( |
108 | $this->getLanguage() |
109 | ); |
110 | $out->addHtml( $resultsFormatter->formatResults( $target, $result ) ); |
111 | } else { |
112 | $out->addHtml( $this->msg( 'similareditors-no-results' ) ); |
113 | } |
114 | } else { |
115 | switch ( $result ) { |
116 | // We encountered an error, but with no type or an unrecognized type. |
117 | // Display the default message instead of a customized message. |
118 | case 'database-refresh': |
119 | $message = $this->msg( 'similareditors-error-database-refresh' ); |
120 | break; |
121 | case 'user-no-account': |
122 | $message = $this->msg( 'similareditors-error-user-no-account', $target ); |
123 | break; |
124 | case 'user-bot': |
125 | $message = $this->msg( 'similareditors-error-user-bot', $target ); |
126 | break; |
127 | case 'user-no-edits': |
128 | $message = $this->msg( 'similareditors-error-user-no-edits', $target ); |
129 | break; |
130 | default: |
131 | $message = 'similareditors-error-default'; |
132 | } |
133 | $out->addHtml( |
134 | new MessageWidget( [ |
135 | 'type' => 'error', |
136 | 'label' => new HtmlSnippet( |
137 | $this->msg( $message )->parse() ), |
138 | ] ) |
139 | ); |
140 | $this->form->displayForm( false ); |
141 | } |
142 | } |
143 | } |