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 $this->failIfNotString( $name, $value, $settings, $options );
39
40 // Validate the full list of tags at once, because the caller will
41 // *probably* stop at the first exception thrown.
42 if ( isset( $options['values-list'] ) ) {
43 $ret = $value;
44 $tagsStatus = ChangeTags::canAddTagsAccompanyingChange( $options['values-list'] );
45 } else {
46 // The 'tags' type always returns an array.
47 $ret = [ $value ];
48 $tagsStatus = ChangeTags::canAddTagsAccompanyingChange( $ret );
49 }
50
51 if ( !$tagsStatus->isGood() ) {
52 $msg = $this->messageConverter->convertMessage( $tagsStatus->getMessage() );
53 $data = [];
54 if ( $tagsStatus->value ) {
55 // Specific tags are not allowed.
56 $data['disallowedtags'] = $tagsStatus->value;
57 // @codeCoverageIgnoreStart
58 } else {
59 // All are disallowed, I guess
60 $data['disallowedtags'] = $settings['values-list'] ?? $ret;
61 }
62 // @codeCoverageIgnoreEnd
63
64 // Only throw if $value is among the disallowed tags
65 if ( in_array( $value, $data['disallowedtags'], true ) ) {
66 throw new ValidationException(
67 DataMessageValue::new( $msg->getKey(), $msg->getParams(), 'badtags', $data ),
68 $name, $value, $settings
69 );
70 }
71 }
72
73 return $ret;
74 }
75
76 public function getEnumValues( $name, array $settings, array $options ) {
77 return $this->changeTagsStore->listExplicitlyDefinedTags();
78 }
79
80}
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:76
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
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
Definition TypeDef.php:68
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21