| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class TitleDef extends TypeDef { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public const PARAM_MUST_EXIST = 'param-must-exist'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public const PARAM_RETURN_OBJECT = 'param-return-object'; |
| 42 |
|
| 43 |
|
| 44 |
private $titleFactory; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( Callbacks $callbacks, TitleFactory $titleFactory ) { |
| 51 |
parent::__construct( $callbacks ); |
| 52 |
$this->titleFactory = $titleFactory; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 69 |
$this->failure( 'badtitle', $name, $value, $settings, $options ); |
| 70 |
} elseif ( $mustExist && !$title->exists() ) { |
| 71 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |