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