1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<?php
namespace MediaWiki\ParamValidator\TypeDef;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\Title\TitleFactory;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\Callbacks;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef;
/**
* Type definition for page titles.
*
* Failure codes:
* - 'badtitle': invalid title (e.g. containing disallowed characters). No data.
* - 'missingtitle': the page with this title does not exist (when PARAM_MUST_EXIST
* was specified). No data.
*
* @since 1.36
*/
class TitleDef extends TypeDef {
/**
* (bool) Whether the page with the given title needs to exist.
*
* Defaults to false.
*/
public const PARAM_MUST_EXIST = 'param-must-exist';
/**
* (bool) Whether to return a LinkTarget.
*
* If false, the validated title is returned as a string (in getPrefixedText() format).
* Default is false.
*
* Avoid setting true with PARAM_ISMULTI, as it may result in excessive DB
* lookups. If you do combine them, consider setting low values for
* PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 to mitigate it.
*/
public const PARAM_RETURN_OBJECT = 'param-return-object';
/** @var TitleFactory */
private $titleFactory;
/**
* @param Callbacks $callbacks
* @param TitleFactory $titleFactory
*/
public function __construct( Callbacks $callbacks, TitleFactory $titleFactory ) {
parent::__construct( $callbacks );
$this->titleFactory = $titleFactory;
}
/**
* @inheritDoc
* @return string|LinkTarget Depending on the PARAM_RETURN_OBJECT setting.
*/
public function validate( $name, $value, array $settings, array $options ) {
$mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
$returnObject = !empty( $settings[self::PARAM_RETURN_OBJECT] );
$this->failIfNotString( $name, $value, $settings, $options );
$title = $this->titleFactory->newFromText( $value );
if ( !$title ) {
// Message used: paramvalidator-badtitle
$this->failure( 'badtitle', $name, $value, $settings, $options );
} elseif ( $mustExist && !$title->exists() ) {
// Message used: paramvalidator-missingtitle
$this->failure( 'missingtitle', $name, $value, $settings, $options );
}
if ( $returnObject ) {
return $title->getTitleValue();
} else {
return $title->getPrefixedText();
}
}
/** @inheritDoc */
public function stringifyValue( $name, $value, array $settings, array $options ) {
if ( $value instanceof LinkTarget ) {
return $this->titleFactory->newFromLinkTarget( $value )->getPrefixedText();
}
return parent::stringifyValue( $name, $value, $settings, $options );
}
/** @inheritDoc */
public function checkSettings( string $name, $settings, array $options, array $ret ): array {
$ret = parent::checkSettings( $name, $settings, $options, $ret );
$ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
self::PARAM_MUST_EXIST, self::PARAM_RETURN_OBJECT,
] );
if ( !is_bool( $settings[self::PARAM_MUST_EXIST] ?? false ) ) {
$ret['issues'][self::PARAM_MUST_EXIST] = 'PARAM_MUST_EXIST must be boolean, got '
. gettype( $settings[self::PARAM_MUST_EXIST] );
}
if ( !is_bool( $settings[self::PARAM_RETURN_OBJECT] ?? false ) ) {
$ret['issues'][self::PARAM_RETURN_OBJECT] = 'PARAM_RETURN_OBJECT must be boolean, got '
. gettype( $settings[self::PARAM_RETURN_OBJECT] );
}
if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
!empty( $settings[self::PARAM_RETURN_OBJECT] ) &&
(
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT1] ?? 100 ) > 10 ||
( $settings[ParamValidator::PARAM_ISMULTI_LIMIT2] ?? 100 ) > 10
)
) {
$ret['issues'][] = 'Multi-valued title-type parameters with PARAM_RETURN_OBJECT '
. 'should set low values (<= 10) for PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2.'
. ' (Note that "<= 10" is arbitrary. If something hits this, we can investigate a real limit '
. 'once we have a real use case to look at.)';
}
return $ret;
}
/** @inheritDoc */
public function getParamInfo( $name, array $settings, array $options ) {
$info = parent::getParamInfo( $name, $settings, $options );
$info['mustExist'] = !empty( $settings[self::PARAM_MUST_EXIST] );
return $info;
}
/** @inheritDoc */
public function getHelpInfo( $name, array $settings, array $options ) {
$info = parent::getParamInfo( $name, $settings, $options );
$info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-title' );
$mustExist = !empty( $settings[self::PARAM_MUST_EXIST] );
$info[self::PARAM_MUST_EXIST] = $mustExist
? MessageValue::new( 'paramvalidator-help-type-title-must-exist' )
: MessageValue::new( 'paramvalidator-help-type-title-no-must-exist' );
return $info;
}
}
|