MediaWiki master
TagsDef.php
Go to the documentation of this file.
1<?php
2
4
5use ChangeTags;
7use MediaWiki\Message\Converter as MessageConverter;
12
24class TagsDef extends EnumDef {
25
26 private ChangeTagsStore $changeTagsStore;
27
29 private $messageConverter;
30
31 public function __construct( Callbacks $callbacks, ChangeTagsStore $changeTagsStore ) {
32 parent::__construct( $callbacks );
33 $this->changeTagsStore = $changeTagsStore;
34 $this->messageConverter = new MessageConverter();
35 }
36
37 public function validate( $name, $value, array $settings, array $options ) {
38 // Validate the full list of tags at once, because the caller will
39 // *probably* stop at the first exception thrown.
40 if ( isset( $options['values-list'] ) ) {
41 $ret = $value;
42 $tagsStatus = ChangeTags::canAddTagsAccompanyingChange( $options['values-list'] );
43 } else {
44 // The 'tags' type always returns an array.
45 $ret = [ $value ];
46 $tagsStatus = ChangeTags::canAddTagsAccompanyingChange( $ret );
47 }
48
49 if ( !$tagsStatus->isGood() ) {
50 $msg = $this->messageConverter->convertMessage( $tagsStatus->getMessage() );
51 $data = [];
52 if ( $tagsStatus->value ) {
53 // Specific tags are not allowed.
54 $data['disallowedtags'] = $tagsStatus->value;
55 // @codeCoverageIgnoreStart
56 } else {
57 // All are disallowed, I guess
58 $data['disallowedtags'] = $settings['values-list'] ?? $ret;
59 }
60 // @codeCoverageIgnoreEnd
61
62 // Only throw if $value is among the disallowed tags
63 if ( in_array( $value, $data['disallowedtags'], true ) ) {
64 throw new ValidationException(
65 DataMessageValue::new( $msg->getKey(), $msg->getParams(), 'badtags', $data ),
66 $name, $value, $settings
67 );
68 }
69 }
70
71 return $ret;
72 }
73
74 public function getEnumValues( $name, array $settings, array $options ) {
75 return $this->changeTagsStore->listExplicitlyDefinedTags();
76 }
77
78}
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Gateway class for change_tags table.
Converter between Message and MessageValue.
Definition Converter.php:18
Type definition for tags type.
Definition TagsDef.php:24
__construct(Callbacks $callbacks, ChangeTagsStore $changeTagsStore)
Definition TagsDef.php:31
getEnumValues( $name, array $settings, array $options)
Get the values for enum-like parameters.
Definition TagsDef.php:74
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition TagsDef.php:37
Value object representing a message for i18n with alternative machine-readable data.
Type definition for enumeration types.
Definition EnumDef.php:32
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21