Debian Codegraph @

includes/ParamValidator/TypeDef/TitleDef.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\ParamValidator\TypeDef;
4
5 use MediaWiki\Linker\LinkTarget;
6 use MediaWiki\Title\TitleFactory;
7 use Wikimedia\Message\MessageValue;
8 use Wikimedia\ParamValidator\Callbacks;
9 use Wikimedia\ParamValidator\ParamValidator;
10 use Wikimedia\ParamValidator\TypeDef;
11
12 /**
13 * Type definition for page titles.
14 *
15 * Failure codes:
16 * - 'badtitle': invalid title (e.g. containing disallowed characters). No data.
17 * - 'missingtitle': the page with this title does not exist (when PARAM_MUST_EXIST
18 * was specified). No data.
19 *
20 * @since 1.36
21 */
22 class TitleDef extends TypeDef {
23
24 /**
25 * (bool) Whether the page with the given title needs to exist.
26 *
27 * Defaults to false.
28 */
29 public const PARAM_MUST_EXIST = 'param-must-exist';
30
31 /**
32 * (bool) Whether to return a LinkTarget.
33 *
34 * If false, the validated title is returned as a string (in getPrefixedText() format).
35 * Default is false.
36 *
37 * Avoid setting true with PARAM_ISMULTI, as it may result in excessive DB
38 * lookups. If you do combine them, consider setting low values for
39 * PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 to mitigate it.
40 */
41 public const PARAM_RETURN_OBJECT = 'param-return-object';
42
43 /** @var TitleFactory */
44 private $titleFactory;
45
46 /**
47 * @param Callbacks $callbacks
48 * @param TitleFactory $titleFactory
49 */
50 public function __construct( Callbacks $callbacks, TitleFactory $titleFactory ) {
51 parent::__construct( $callbacks );
52 $this->titleFactory = $titleFactory;
53 }
54
55 /**
56 * @inheritDoc
57 * @return string|LinkTarget Depending on the PARAM_RETURN_OBJECT setting.
58 */
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
82 /** @inheritDoc */
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
90 /** @inheritDoc */
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
124 /** @inheritDoc */
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
133 /** @inheritDoc */
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 }