Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
14 / 16 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ChangeTagValidator | |
87.50% |
14 / 16 |
|
50.00% |
1 / 2 |
7.10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validateTag | |
86.67% |
13 / 15 |
|
0.00% |
0 / 1 |
6.09 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\AbuseFilter\ChangeTags; |
4 | |
5 | use ChangeTags; |
6 | use MediaWiki\Status\Status; |
7 | |
8 | /** |
9 | * Service for testing whether filters can tag edits and other changes |
10 | * with a specific tag |
11 | * @todo Use DI when available in core (T245964) |
12 | */ |
13 | class ChangeTagValidator { |
14 | |
15 | public const SERVICE_NAME = 'AbuseFilterChangeTagValidator'; |
16 | |
17 | /** @var ChangeTagsManager */ |
18 | private $changeTagsManager; |
19 | |
20 | /** |
21 | * @param ChangeTagsManager $changeTagsManager |
22 | */ |
23 | public function __construct( ChangeTagsManager $changeTagsManager ) { |
24 | $this->changeTagsManager = $changeTagsManager; |
25 | } |
26 | |
27 | /** |
28 | * Check whether a filter is allowed to use a tag |
29 | * |
30 | * @param string $tag Tag name |
31 | * @return Status |
32 | */ |
33 | public function validateTag( string $tag ): Status { |
34 | $tagNameStatus = ChangeTags::isTagNameValid( $tag ); |
35 | |
36 | if ( !$tagNameStatus->isGood() ) { |
37 | return $tagNameStatus; |
38 | } |
39 | |
40 | $canAddStatus = ChangeTags::canAddTagsAccompanyingChange( [ $tag ] ); |
41 | |
42 | if ( $canAddStatus->isGood() ) { |
43 | return $canAddStatus; |
44 | } |
45 | |
46 | if ( $tag === $this->changeTagsManager->getCondsLimitTag() ) { |
47 | return Status::newFatal( 'abusefilter-tag-reserved' ); |
48 | } |
49 | |
50 | // note: these are both local and global tags |
51 | $alreadyDefinedTags = $this->changeTagsManager->getTagsDefinedByFilters(); |
52 | if ( in_array( $tag, $alreadyDefinedTags, true ) ) { |
53 | return Status::newGood(); |
54 | } |
55 | |
56 | // note: this check is only done for the local wiki |
57 | // global filters could interfere with existing tags on remote wikis |
58 | $canCreateTagStatus = ChangeTags::canCreateTag( $tag ); |
59 | if ( $canCreateTagStatus->isGood() ) { |
60 | return $canCreateTagStatus; |
61 | } |
62 | |
63 | return Status::newFatal( 'abusefilter-edit-bad-tags' ); |
64 | } |
65 | } |