Debian Codegraph @

extensions/AbuseFilter/includes/Pager/AbuseFilterHistoryPager.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Pager;
4
5 use HtmlArmor;
6 use MediaWiki\Cache\LinkBatchFactory;
7 use MediaWiki\Context\IContextSource;
8 use MediaWiki\Extension\AbuseFilter\AbuseFilter;
9 use MediaWiki\Extension\AbuseFilter\Filter\Flags;
10 use MediaWiki\Extension\AbuseFilter\FilterLookup;
11 use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseFilter;
12 use MediaWiki\Extension\AbuseFilter\SpecsFormatter;
13 use MediaWiki\Html\Html;
14 use MediaWiki\Linker\Linker;
15 use MediaWiki\Linker\LinkRenderer;
16 use MediaWiki\Pager\TablePager;
17 use MediaWiki\Title\Title;
18 use UnexpectedValueException;
19 use Wikimedia\Rdbms\IResultWrapper;
20
21 class AbuseFilterHistoryPager extends TablePager {
22
23 /** @var LinkBatchFactory */
24 private $linkBatchFactory;
25
26 /** @var FilterLookup */
27 private $filterLookup;
28
29 /** @var SpecsFormatter */
30 private $specsFormatter;
31
32 /** @var int|null The filter ID */
33 private $filter;
34
35 /** @var string|null The user whose changes we're looking up for */
36 private $user;
37
38 /** @var bool */
39 private $canViewPrivateFilters;
40
41 /** @var bool */
42 private $canViewProtectedVars;
43
44 /**
45 * @param IContextSource $context
46 * @param LinkRenderer $linkRenderer
47 * @param LinkBatchFactory $linkBatchFactory
48 * @param FilterLookup $filterLookup
49 * @param SpecsFormatter $specsFormatter
50 * @param ?int $filter
51 * @param ?string $user User name
52 * @param bool $canViewPrivateFilters
53 * @param bool $canViewProtectedVars
54 */
55 public function __construct(
56 IContextSource $context,
57 LinkRenderer $linkRenderer,
58 LinkBatchFactory $linkBatchFactory,
59 FilterLookup $filterLookup,
60 SpecsFormatter $specsFormatter,
61 ?int $filter,
62 ?string $user,
63 bool $canViewPrivateFilters = false,
64 bool $canViewProtectedVars = false
65 ) {
66 // needed by parent's constructor call
67 $this->filter = $filter;
68 parent::__construct( $context, $linkRenderer );
69 $this->linkBatchFactory = $linkBatchFactory;
70 $this->filterLookup = $filterLookup;
71 $this->specsFormatter = $specsFormatter;
72 $this->user = $user;
73 $this->canViewPrivateFilters = $canViewPrivateFilters;
74 $this->canViewProtectedVars = $canViewProtectedVars;
75 $this->mDefaultDirection = true;
76 }
77
78 /**
79 * Note: this method is called by parent::__construct
80 * @return array
81 * @see MediaWiki\Pager\Pager::getFieldNames()
82 */
83 public function getFieldNames() {
84 static $headers = null;
85
86 if ( $headers !== null ) {
87 return $headers;
88 }
89
90 $headers = [
91 'afh_timestamp' => 'abusefilter-history-timestamp',
92 'afh_user_text' => 'abusefilter-history-user',
93 'afh_public_comments' => 'abusefilter-history-public',
94 'afh_flags' => 'abusefilter-history-flags',
95 'afh_actions' => 'abusefilter-history-actions',
96 'afh_id' => 'abusefilter-history-diff',
97 ];
98
99 if ( !$this->filter ) {
100 // awful hack
101 $headers = [ 'afh_filter' => 'abusefilter-history-filterid' ] + $headers;
102 }
103
104 foreach ( $headers as &$msg ) {
105 $msg = $this->msg( $msg )->text();
106 }
107
108 return $headers;
109 }
110
111 /**
112 * @param string $name
113 * @param string|null $value
114 * @return string
115 */
116 public function formatValue( $name, $value ) {
117 $lang = $this->getLanguage();
118 $linkRenderer = $this->getLinkRenderer();
119
120 $row = $this->mCurrentRow;
121
122 switch ( $name ) {
123 case 'afh_filter':
124 $formatted = $linkRenderer->makeLink(
125 SpecialAbuseFilter::getTitleForSubpage( $row->afh_filter ),
126 $lang->formatNum( $row->afh_filter )
127 );
128 break;
129 case 'afh_timestamp':
130 $title = SpecialAbuseFilter::getTitleForSubpage(
131 'history/' . $row->afh_filter . '/item/' . $row->afh_id );
132 $formatted = $linkRenderer->makeLink(
133 $title,
134 $lang->userTimeAndDate( $row->afh_timestamp, $this->getUser() )
135 );
136 break;
137 case 'afh_user_text':
138 $formatted =
139 Linker::userLink( $row->afh_user ?? 0, $row->afh_user_text ) . ' ' .
140 Linker::userToolLinks( $row->afh_user ?? 0, $row->afh_user_text );
141 break;
142 case 'afh_public_comments':
143 $formatted = htmlspecialchars( $value, ENT_QUOTES, 'UTF-8', false );
144 break;
145 case 'afh_flags':
146 $formatted = $this->specsFormatter->formatFlags( $value, $lang );
147 break;
148 case 'afh_actions':
149 $actions = unserialize( $value );
150
151 $display_actions = '';
152
153 foreach ( $actions as $action => $parameters ) {
154 $displayAction = $this->specsFormatter->formatAction( $action, $parameters, $lang );
155 $display_actions .= Html::rawElement( 'li', [], $displayAction );
156 }
157 $display_actions = Html::rawElement( 'ul', [], $display_actions );
158
159 $formatted = $display_actions;
160 break;
161 case 'afh_id':
162 // Set a link to a diff with the previous version if this isn't the first edit to the filter.
163 // Like in AbuseFilterViewDiff, don't show it if:
164 // - the user cannot see private filters and any of the versions is hidden
165 // - the user cannot see protected variables and any of the versions is protected
166 $formatted = '';
167 if ( $this->filterLookup->getFirstFilterVersionID( $row->afh_filter ) !== (int)$value ) {
168 // @todo Should we also hide actions?
169 $prevFilter = $this->filterLookup->getClosestVersion(
170 $row->afh_id, $row->afh_filter, FilterLookup::DIR_PREV );
171 if (
172 ( $this->canViewPrivateFilters ||
173 (
174 !in_array( 'hidden', explode( ',', $row->afh_flags ) ) &&
175 !$prevFilter->isHidden()
176 )
177 ) &&
178 (
179 $this->canViewProtectedVars ||
180 (
181 !in_array( 'protected', explode( ',', $row->afh_flags ) ) &&
182 !$prevFilter->isProtected()
183 )
184 )
185 ) {
186 $title = SpecialAbuseFilter::getTitleForSubpage(
187 'history/' . $row->afh_filter . "/diff/prev/$value" );
188 $formatted = $linkRenderer->makeLink(
189 $title,
190 new HtmlArmor( $this->msg( 'abusefilter-history-diff' )->parse() )
191 );
192 }
193 }
194 break;
195 default:
196 throw new UnexpectedValueException( "Unknown row type $name!" );
197 }
198
199 return $formatted;
200 }
201
202 /**
203 * @return array
204 */
205 public function getQueryInfo() {
206 $info = [
207 'tables' => [ 'abuse_filter_history', 'abuse_filter', 'actor' ],
208 // All fields but afh_deleted on abuse_filter_history
209 'fields' => [
210 'afh_filter',
211 'afh_timestamp',
212 'afh_public_comments',
213 'afh_user' => 'actor_user',
214 'afh_user_text' => 'actor_name',
215 'afh_flags',
216 'afh_comments',
217 'afh_actions',
218 'afh_id',
219 'afh_changed_fields',
220 'afh_pattern',
221 'af_hidden'
222 ],
223 'conds' => [],
224 'join_conds' => [
225 'abuse_filter' =>
226 [
227 'LEFT JOIN',
228 'afh_filter=af_id',
229 ],
230 'actor' => [ 'JOIN', 'actor_id = afh_actor' ],
231 ]
232 ];
233
234 if ( $this->user !== null ) {
235 $info['conds']['actor_name'] = $this->user;
236 }
237
238 if ( $this->filter ) {
239 $info['conds']['afh_filter'] = $this->filter;
240 }
241
242 if ( !$this->canViewPrivateFilters ) {
243 // Hide data the user can't see.
244 $info['conds'][] = $this->mDb->bitAnd( 'af_hidden', Flags::FILTER_HIDDEN ) . ' = 0';
245 }
246
247 if ( !$this->canViewProtectedVars ) {
248 // Hide data the user can't see.
249 $info['conds'][] = $this->mDb->bitAnd( 'af_hidden', Flags::FILTER_USES_PROTECTED_VARS ) . ' = 0';
250 }
251
252 return $info;
253 }
254
255 /**
256 * @param IResultWrapper $result
257 */
258 protected function preprocessResults( $result ) {
259 if ( $this->getNumRows() === 0 ) {
260 return;
261 }
262
263 $lb = $this->linkBatchFactory->newLinkBatch();
264 $lb->setCaller( __METHOD__ );
265 foreach ( $result as $row ) {
266 $lb->add( NS_USER, $row->afh_user_text );
267 $lb->add( NS_USER_TALK, $row->afh_user_text );
268 }
269 $lb->execute();
270 $result->seek( 0 );
271 }
272
273 /**
274 * @codeCoverageIgnore Merely declarative
275 * @inheritDoc
276 */
277 public function getDefaultSort() {
278 return 'afh_timestamp';
279 }
280
281 /**
282 * @codeCoverageIgnore Merely declarative
283 * @inheritDoc
284 */
285 public function isFieldSortable( $field ) {
286 return $field === 'afh_timestamp';
287 }
288
289 /**
290 * @param string $field
291 * @param string $value
292 * @return array
293 * @see TablePager::getCellAttrs
294 */
295 public function getCellAttrs( $field, $value ) {
296 $row = $this->mCurrentRow;
297 $mappings = array_flip( AbuseFilter::HISTORY_MAPPINGS ) +
298 [ 'afh_actions' => 'actions', 'afh_id' => 'id' ];
299 $changed = explode( ',', $row->afh_changed_fields );
300
301 $fieldChanged = false;
302 if ( $field === 'afh_flags' ) {
303 // The field is changed if any of these filters are in the $changed array.
304 $filters = [ 'af_enabled', 'af_hidden', 'af_deleted', 'af_global' ];
305 if ( count( array_intersect( $filters, $changed ) ) ) {
306 $fieldChanged = true;
307 }
308 } elseif ( in_array( $mappings[$field], $changed ) ) {
309 $fieldChanged = true;
310 }
311
312 $class = $fieldChanged ? ' mw-abusefilter-history-changed' : '';
313 $attrs = parent::getCellAttrs( $field, $value );
314 $attrs['class'] .= $class;
315 return $attrs;
316 }
317
318 /**
319 * Title used for self-links.
320 *
321 * @return Title
322 */
323 public function getTitle() {
324 $subpage = $this->filter ? ( 'history/' . $this->filter ) : 'history';
325 return SpecialAbuseFilter::getTitleForSubpage( $subpage );
326 }
327 }