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 MediaWiki\ChangeTags\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 | public function __construct( private readonly ChangeTagsManager $changeTagsManager ) { |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Check whether a filter is allowed to use a tag |
| 22 | * |
| 23 | * @param string $tag Tag name |
| 24 | * @return Status |
| 25 | */ |
| 26 | public function validateTag( string $tag ): Status { |
| 27 | $tagNameStatus = ChangeTags::isTagNameValid( $tag ); |
| 28 | |
| 29 | if ( !$tagNameStatus->isGood() ) { |
| 30 | return $tagNameStatus; |
| 31 | } |
| 32 | |
| 33 | $canAddStatus = ChangeTags::canAddTagsAccompanyingChange( [ $tag ] ); |
| 34 | |
| 35 | if ( $canAddStatus->isGood() ) { |
| 36 | return $canAddStatus; |
| 37 | } |
| 38 | |
| 39 | if ( $tag === $this->changeTagsManager->getCondsLimitTag() ) { |
| 40 | return Status::newFatal( 'abusefilter-tag-reserved' ); |
| 41 | } |
| 42 | |
| 43 | // note: these are both local and global tags |
| 44 | $alreadyDefinedTags = $this->changeTagsManager->getTagsDefinedByFilters(); |
| 45 | if ( in_array( $tag, $alreadyDefinedTags, true ) ) { |
| 46 | return Status::newGood(); |
| 47 | } |
| 48 | |
| 49 | // note: this check is only done for the local wiki |
| 50 | // global filters could interfere with existing tags on remote wikis |
| 51 | $canCreateTagStatus = ChangeTags::canCreateTag( $tag ); |
| 52 | if ( $canCreateTagStatus->isGood() ) { |
| 53 | return $canCreateTagStatus; |
| 54 | } |
| 55 | |
| 56 | return Status::newFatal( 'abusefilter-edit-bad-tags' ); |
| 57 | } |
| 58 | } |