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
46 public function __construct( Callbacks $callbacks, TitleFactory $titleFactory ) {
47 parent::__construct( $callbacks );
48 $this->titleFactory = $titleFactory;
49 }
50
55 public function validate( $name, $value, array $settings, array $options ) {
56 $mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
57 $returnObject = !empty( $settings[self::PARAM_RETURN_OBJECT] );
58
59 $this->failIfNotString( $name, $value, $settings, $options );
60
61 $title = $this->titleFactory->newFromText( $value );
62
63 if ( !$title ) {
64 // Message used: paramvalidator-badtitle
65 $this->failure( 'badtitle', $name, $value, $settings, $options );
66 } elseif ( $mustExist && !$title->exists() ) {
67 // Message used: paramvalidator-missingtitle
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'][] = self::PARAM_MUST_EXIST;
91 $ret['allowedKeys'][] = self::PARAM_RETURN_OBJECT;
92
93 if ( !is_bool( $settings[self::PARAM_MUST_EXIST] ?? false ) ) {
94 $ret['issues'][self::PARAM_MUST_EXIST] = 'PARAM_MUST_EXIST must be boolean, got '
95 . gettype( $settings[self::PARAM_MUST_EXIST] );
96 }
97
98 if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) {
99 $ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got '
100 . gettype( $settings[self::PARAM_RETURN_OBJECT] );
101 }
102
103 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
104 !empty( $settings[self::PARAM_RETURN_OBJECT] ) &&
105 (
106 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
107 ( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
108 )
109 ) {
110 $ret['issues'][] = 'Multi-valued title-type parameters with PARAM_RETURN_OBJECT '
111 . 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
112 . ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
113 . 'once we have a real use case to look at.)';
114 }
115
116 return $ret;
117 }
118
120 public function getParamInfo( $name, array $settings, array $options ) {
121 $info = parent::getParamInfo( $name, $settings, $options );
122
123 $info['mustExist'] = !empty( $settings[self::PARAM_MUST_EXIST] );
124
125 return $info;
126 }
127
129 public function getHelpInfo( $name, array $settings, array $options ) {
130 $info = parent::getParamInfo( $name, $settings, $options );
131
132 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-title' );
133
134 $mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
135 $info[self::PARAM_MUST_EXIST] = $mustExist
136 ? MessageValue::new( 'paramvalidator-help-type-title-must-exist' )
137 : MessageValue::new( 'paramvalidator-help-type-title-no-must-exist' );
138
139 return $info;
140 }
141
142}
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:129
getParamInfo( $name, array $settings, array $options)
Describe parameter settings in a machine-readable format.Keys should be short strings using lowercase...
Definition TitleDef.php:120
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
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:55
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:46
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
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