MediaWiki master
TitleDef.php
Go to the documentation of this file.
1<?php
2
4
11
22class 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
50 public function __construct( Callbacks $callbacks, TitleFactory $titleFactory ) {
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 $this->failIfNotString( $name, $value, $settings, $options );
64
65 $title = $this->titleFactory->newFromText( $value );
66
67 if ( !$title ) {
68 // Message used: paramvalidator-badtitle
69 $this->failure( 'badtitle', $name, $value, $settings, $options );
70 } elseif ( $mustExist && !$title->exists() ) {
71 // Message used: paramvalidator-missingtitle
72 $this->failure( 'missingtitle', $name, $value, $settings, $options );
73 }
74
75 if ( $returnObject ) {
76 return $title->getTitleValue();
77 } else {
78 return $title->getPrefixedText();
79 }
80 }
81
83 public function stringifyValue( $name, $value, array $settings, array $options ) {
84 if ( $value instanceof LinkTarget ) {
85 return $this->titleFactory->newFromLinkTarget( $value )->getPrefixedText();
86 }
87 return parent::stringifyValue( $name, $value, $settings, $options );
88 }
89
91 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
92 $ret = parent::checkSettings( $name, $settings, $options, $ret );
93
94 $ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
95 self::PARAM_MUST_EXIST, self::PARAM_RETURN_OBJECT,
96 ] );
97
98 if ( !is_bool( $settings[self::PARAM_MUST_EXIST] ?? false ) ) {
99 $ret['issues'][self::PARAM_MUST_EXIST] = 'PARAM_MUST_EXIST must be boolean, got '
100 . gettype( $settings[self::PARAM_MUST_EXIST] );
101 }
102
103 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) {
104 $ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got '
105 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
106 }
107
108 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
109 !empty( $settings[self::PARAM_RETURN_OBJECT] ) &&
110 (
111 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
112 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
113 )
114 ) {
115 $ret['issues'][] = 'Multi-valued title-type parameters with PARAM_RETURN_OBJECT '
116 . 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
117 . ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
118 . 'once we have a real use case to look at.)';
119 }
120
121 return $ret;
122 }
123
125 public function getParamInfo( $name, array $settings, array $options ) {
126 $info = parent::getParamInfo( $name, $settings, $options );
127
128 $info['mustExist'] = !empty( $settings[self::PARAM_MUST_EXIST] );
129
130 return $info;
131 }
132
134 public function getHelpInfo( $name, array $settings, array $options ) {
135 $info = parent::getParamInfo( $name, $settings, $options );
136
137 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-title' );
138
139 $mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
140 $info[self::PARAM_MUST_EXIST] = $mustExist
141 ? MessageValue::new( 'paramvalidator-help-type-title-must-exist' )
142 : MessageValue::new( 'paramvalidator-help-type-title-no-must-exist' );
143
144 return $info;
145 }
146
147}
Type definition for page titles.
Definition TitleDef.php:22
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
Definition TitleDef.php:134
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition TitleDef.php:125
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:91
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
const PARAM_MUST_EXIST
(bool) Whether the page with the given title needs to exist.
Definition TitleDef.php:29
__construct(Callbacks $callbacks, TitleFactory $titleFactory)
Definition TitleDef.php:50
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:83
const PARAM_RETURN_OBJECT
(bool) Whether to return a LinkTarget.
Definition TitleDef.php:41
Creates Title objects.
Value object representing a message for i18n.
Service for formatting and validating API parameters.
Base definition for ParamValidator types.
Definition TypeDef.php:19
failIfNotString(string $name, $value, array $settings, array $options)
Fails if $value is not a string.
Definition TypeDef.php:68
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:121
Represents the target of a wiki link.
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21