MediaWiki  master
TitleDef.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use TitleFactory;
11 
22 class TitleDef extends TypeDef {
23 
29  public const PARAM_MUST_EXIST = 'param-must-exist';
30 
41  public const PARAM_RETURN_OBJECT = 'param-return-object';
42 
44  private $titleFactory;
45 
51  parent::__construct( $callbacks );
52  $this->titleFactory = $titleFactory;
53  }
54 
59  public function validate( $name, $value, array $settings, array $options ) {
60  $mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
61  $returnObject = !empty( $settings[self::PARAM_RETURN_OBJECT] );
62 
63  $title = $this->titleFactory->newFromText( $value );
64 
65  if ( !$title ) {
66  $this->failure( 'badtitle', $name, $value, $settings, $options );
67  } elseif ( $mustExist && !$title->exists() ) {
68  $this->failure( 'missingtitle', $name, $value, $settings, $options );
69  }
70 
71  if ( $returnObject ) {
72  return $title->getTitleValue();
73  } else {
74  return $title->getPrefixedText();
75  }
76  }
77 
79  public function stringifyValue( $name, $value, array $settings, array $options ) {
80  if ( $value instanceof LinkTarget ) {
81  return $this->titleFactory->newFromLinkTarget( $value )->getPrefixedText();
82  }
83  return parent::stringifyValue( $name, $value, $settings, $options );
84  }
85 
87  public function checkSettings( string $name, $settings, array $options, array $ret ): array {
88  $ret = parent::checkSettings( $name, $settings, $options, $ret );
89 
90  $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
91  self::PARAM_MUST_EXIST, self::PARAM_RETURN_OBJECT,
92  ] );
93 
94  if ( !is_bool( $settings[self::PARAM_MUST_EXIST] ?? false ) ) {
95  $ret['issues'][self::PARAM_MUST_EXIST] = 'PARAM_MUST_EXIST must be boolean, got '
96  . gettype( $settings[self::PARAM_MUST_EXIST] );
97  }
98 
99  if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) {
100  $ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got '
101  . gettype( $settings[self::PARAM_RETURN_OBJECT] );
102  }
103 
104  if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
105  !empty( $settings[self::PARAM_RETURN_OBJECT] ) &&
106  (
107  ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
108  ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
109  )
110  ) {
111  $ret['issues'][] = 'Multi-valued title-type parameters with PARAM_RETURN_OBJECT '
112  . 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
113  . ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
114  . 'once we have a real use case to look at.)';
115  }
116 
117  return $ret;
118  }
119 
121  public function getParamInfo( $name, array $settings, array $options ) {
122  $info = parent::getParamInfo( $name, $settings, $options );
123 
124  $info['mustExist'] = !empty( $settings[self::PARAM_MUST_EXIST] );
125 
126  return $info;
127  }
128 
130  public function getHelpInfo( $name, array $settings, array $options ) {
131  $info = parent::getParamInfo( $name, $settings, $options );
132 
133  $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-title' );
134 
135  $mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
136  $info[self::PARAM_MUST_EXIST] = $mustExist
137  ? MessageValue::new( 'paramvalidator-help-type-title-must-exist' )
138  : MessageValue::new( 'paramvalidator-help-type-title-no-must-exist' );
139 
140  return $info;
141  }
142 
143 }
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\PARAM_RETURN_OBJECT
const PARAM_RETURN_OBJECT
(bool) Whether to return a LinkTarget.
Definition: TitleDef.php:41
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\checkSettings
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
Definition: TitleDef.php:87
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\PARAM_MUST_EXIST
const PARAM_MUST_EXIST
(bool) Whether the page with the given title needs to exist.
Definition: TitleDef.php:29
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI_LIMIT1
const PARAM_ISMULTI_LIMIT1
(int) Maximum number of multi-valued parameter values allowed
Definition: ParamValidator.php:119
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\$titleFactory
TitleFactory $titleFactory
Definition: TitleDef.php:44
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\getParamInfo
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition: TitleDef.php:121
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:19
MediaWiki\ParamValidator\TypeDef
Definition: NamespaceDef.php:3
$title
$title
Definition: testCompression.php:38
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition: TitleDef.php:130
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\__construct
__construct(Callbacks $callbacks, TitleFactory $titleFactory)
Definition: TitleDef.php:50
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
Definition: TitleDef.php:59
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef
Type definition for page titles.
Definition: TitleDef.php:22
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI_LIMIT2
const PARAM_ISMULTI_LIMIT2
(int) Maximum number of multi-valued parameter values allowed for users allowed high limits.
Definition: ParamValidator.php:127
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:35
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
Wikimedia\ParamValidator\ParamValidator::TypeDef\TitleDef\stringifyValue
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.This is intended as the inverse of getValue() and validate...
Definition: TitleDef.php:79
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
Wikimedia\ParamValidator\TypeDef\$callbacks
Callbacks $callbacks
Definition: TypeDef.php:22