Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.57% |
93 / 105 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
AbuseFilterViewHistory | |
88.57% |
93 / 105 |
|
50.00% |
1 / 2 |
18.48 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
show | |
87.76% |
86 / 98 |
|
0.00% |
0 / 1 |
17.53 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\AbuseFilter\View; |
4 | |
5 | use MediaWiki\Cache\LinkBatchFactory; |
6 | use MediaWiki\Context\IContextSource; |
7 | use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
8 | use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException; |
9 | use MediaWiki\Extension\AbuseFilter\FilterLookup; |
10 | use MediaWiki\Extension\AbuseFilter\Pager\AbuseFilterHistoryPager; |
11 | use MediaWiki\Extension\AbuseFilter\SpecsFormatter; |
12 | use MediaWiki\HTMLForm\HTMLForm; |
13 | use MediaWiki\Linker\Linker; |
14 | use MediaWiki\Linker\LinkRenderer; |
15 | use MediaWiki\Parser\ParserOptions; |
16 | use MediaWiki\User\UserNameUtils; |
17 | use OOUI; |
18 | |
19 | class AbuseFilterViewHistory extends AbuseFilterView { |
20 | |
21 | /** @var int|null */ |
22 | private $filter; |
23 | |
24 | /** @var FilterLookup */ |
25 | private $filterLookup; |
26 | |
27 | /** @var SpecsFormatter */ |
28 | private $specsFormatter; |
29 | |
30 | /** @var UserNameUtils */ |
31 | private $userNameUtils; |
32 | |
33 | /** @var LinkBatchFactory */ |
34 | private $linkBatchFactory; |
35 | |
36 | /** |
37 | * @param UserNameUtils $userNameUtils |
38 | * @param LinkBatchFactory $linkBatchFactory |
39 | * @param AbuseFilterPermissionManager $afPermManager |
40 | * @param FilterLookup $filterLookup |
41 | * @param SpecsFormatter $specsFormatter |
42 | * @param IContextSource $context |
43 | * @param LinkRenderer $linkRenderer |
44 | * @param string $basePageName |
45 | * @param array $params |
46 | */ |
47 | public function __construct( |
48 | UserNameUtils $userNameUtils, |
49 | LinkBatchFactory $linkBatchFactory, |
50 | AbuseFilterPermissionManager $afPermManager, |
51 | FilterLookup $filterLookup, |
52 | SpecsFormatter $specsFormatter, |
53 | IContextSource $context, |
54 | LinkRenderer $linkRenderer, |
55 | string $basePageName, |
56 | array $params |
57 | ) { |
58 | parent::__construct( $afPermManager, $context, $linkRenderer, $basePageName, $params ); |
59 | $this->userNameUtils = $userNameUtils; |
60 | $this->linkBatchFactory = $linkBatchFactory; |
61 | $this->filterLookup = $filterLookup; |
62 | $this->specsFormatter = $specsFormatter; |
63 | $this->specsFormatter->setMessageLocalizer( $context ); |
64 | $this->filter = $this->mParams['filter'] ?? null; |
65 | } |
66 | |
67 | /** |
68 | * Shows the page |
69 | */ |
70 | public function show() { |
71 | $out = $this->getOutput(); |
72 | $out->enableOOUI(); |
73 | $filter = $this->getRequest()->getIntOrNull( 'filter' ) ?: $this->filter; |
74 | $canViewPrivate = $this->afPermManager->canViewPrivateFilters( $this->getAuthority() ); |
75 | |
76 | if ( $filter ) { |
77 | $filterObj = null; |
78 | try { |
79 | $filterObj = $this->filterLookup->getFilter( $filter, false ); |
80 | } catch ( FilterNotFoundException $_ ) { |
81 | $filter = null; |
82 | } |
83 | if ( $filterObj && $filterObj->isHidden() && !$canViewPrivate ) { |
84 | $out->addWikiMsg( 'abusefilter-history-error-hidden' ); |
85 | return; |
86 | } |
87 | |
88 | if ( $filterObj && $filterObj->isProtected() ) { |
89 | $permStatus = $this->afPermManager |
90 | ->canViewProtectedVariablesInFilter( $this->getAuthority(), $filterObj ); |
91 | if ( !$permStatus->isGood() ) { |
92 | if ( $permStatus->getPermission() ) { |
93 | $out->addWikiMsg( $this->msg( |
94 | 'abusefilter-history-error-protected-due-to-permission', |
95 | $this->msg( "action-{$permStatus->getPermission()}" )->plain() |
96 | ) ); |
97 | return; |
98 | } |
99 | |
100 | // Add any messages in the status after a generic error message. |
101 | $additional = ''; |
102 | foreach ( $permStatus->getMessages() as $message ) { |
103 | $additional .= $this->msg( $message )->parseAsBlock(); |
104 | } |
105 | |
106 | $out->addWikiMsg( |
107 | $this->msg( 'abusefilter-history-error-protected' )->rawParams( $additional ) |
108 | ); |
109 | return; |
110 | } |
111 | } |
112 | } |
113 | |
114 | if ( $filter ) { |
115 | // Parse wikitext in this message to allow formatting of numero signs (T343994#9209383) |
116 | $out->setPageTitle( $this->msg( 'abusefilter-history' )->numParams( $filter )->parse() ); |
117 | } else { |
118 | $out->setPageTitleMsg( $this->msg( 'abusefilter-filter-log' ) ); |
119 | } |
120 | |
121 | // Useful links |
122 | $links = []; |
123 | if ( $filter ) { |
124 | $links['abusefilter-history-backedit'] = $this->getTitle( $filter )->getFullURL(); |
125 | } |
126 | |
127 | foreach ( $links as $msg => $title ) { |
128 | $links[$msg] = |
129 | new OOUI\ButtonWidget( [ |
130 | 'label' => $this->msg( $msg )->text(), |
131 | 'href' => $title |
132 | ] ); |
133 | } |
134 | |
135 | $backlinks = |
136 | new OOUI\HorizontalLayout( [ |
137 | 'items' => array_values( $links ) |
138 | ] ); |
139 | $out->addHTML( $backlinks ); |
140 | |
141 | // For user |
142 | $user = $this->userNameUtils->getCanonical( |
143 | $this->getRequest()->getText( 'user' ), |
144 | UserNameUtils::RIGOR_VALID |
145 | ); |
146 | if ( $user !== false ) { |
147 | $out->addSubtitle( |
148 | $this->msg( 'abusefilter-history-foruser' ) |
149 | // We don't really need to pass the real user ID |
150 | ->rawParams( Linker::userLink( 1, $user ) ) |
151 | // For GENDER |
152 | ->params( $user ) |
153 | ->parse() |
154 | ); |
155 | } else { |
156 | $user = null; |
157 | } |
158 | |
159 | $formDescriptor = [ |
160 | 'user' => [ |
161 | 'type' => 'user', |
162 | 'name' => 'user', |
163 | 'default' => $user, |
164 | 'size' => '45', |
165 | 'label-message' => 'abusefilter-history-select-user' |
166 | ], |
167 | 'filter' => [ |
168 | 'type' => 'int', |
169 | 'name' => 'filter', |
170 | 'default' => $filter ?: '', |
171 | 'size' => '45', |
172 | 'label-message' => 'abusefilter-history-select-filter' |
173 | ], |
174 | ]; |
175 | |
176 | $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ); |
177 | $htmlForm->setSubmitTextMsg( 'abusefilter-history-select-submit' ) |
178 | ->setWrapperLegendMsg( 'abusefilter-history-select-legend' ) |
179 | ->setTitle( $this->getTitle( 'history' ) ) |
180 | ->setMethod( 'get' ) |
181 | ->prepareForm() |
182 | ->displayForm( false ); |
183 | |
184 | $pager = new AbuseFilterHistoryPager( |
185 | $this->getContext(), |
186 | $this->linkRenderer, |
187 | $this->linkBatchFactory, |
188 | $this->filterLookup, |
189 | $this->specsFormatter, |
190 | $this->afPermManager, |
191 | $filter, |
192 | $user, |
193 | $canViewPrivate |
194 | ); |
195 | |
196 | $out->addParserOutputContent( |
197 | $pager->getFullOutput(), |
198 | ParserOptions::newFromContext( $this->getContext() ) |
199 | ); |
200 | } |
201 | } |