3 namespace MediaWiki\HTMLForm\Field
;
5 use InvalidArgumentException
;
6 use MediaWiki\Json\FormatJson
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Title\MalformedTitleException
;
9 use MediaWiki\Title\Title
;
10 use MediaWiki\Widget\TitleInputWidget
;
13 * Implements a text input field for page titles.
14 * Automatically does validation that the title is valid,
15 * as well as autocompletion if using the OOUI display format.
17 * Optional parameters:
18 * 'namespace' - Namespace the page must be in (use namespace constant; one of the NS_* constants may be used)
19 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
20 * 'creatable' - Whether to validate the title is creatable (not a special page)
21 * 'exists' - Whether to validate that the title already exists
22 * 'interwiki' – Tolerate interwiki links (other conditions such as 'namespace' or 'exists' will be
23 * ignored if the title is an interwiki title). Cannot be used together with 'relative'.
28 class HTMLTitleTextField
extends HTMLTextField
{
33 public function __construct( $params ) {
40 // This overrides the default from HTMLFormField
44 parent
::__construct( $params );
47 public function validate( $value, $alldata ) {
48 if ( $this->mParams
['interwiki'] && $this->mParams
['relative'] ) {
49 // relative and interwiki cannot be used together, because we don't have a way to know about
50 // namespaces used by the other wiki (and it might actually be a non-wiki link, too).
51 throw new InvalidArgumentException( 'relative and interwiki may not be used together' );
53 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
56 if ( !$this->mParams
['required'] && $value === '' ) {
57 // If this field is not required and the value is empty, that's okay, skip validation
58 return parent
::validate( $value, $alldata );
61 $titleFactory = MediaWikiServices
::getInstance()->getTitleFactory();
63 if ( !$this->mParams
['relative'] ) {
64 $title = $titleFactory->newFromTextThrow( $value );
66 // Can't use makeTitleSafe(), because it doesn't throw useful exceptions
67 $title = $titleFactory->newFromTextThrow( Title
::makeName( $this->mParams
['namespace'], $value ) );
69 } catch ( MalformedTitleException
$e ) {
70 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
73 if ( $title->isExternal() ) {
74 if ( $this->mParams
['interwiki'] ) {
75 // We cannot validate external titles, skip the rest of the validation
76 return parent
::validate( $value, $alldata );
78 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
82 $text = $title->getPrefixedText();
83 if ( $this->mParams
['namespace'] !== false &&
84 !$title->inNamespace( $this->mParams
['namespace'] )
86 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams
['namespace'] );
89 if ( $this->mParams
['creatable'] && !$title->canExist() ) {
90 return $this->msg( 'htmlform-title-not-creatable', $text );
93 if ( $this->mParams
['exists'] && !$title->exists() ) {
94 return $this->msg( 'htmlform-title-not-exists', $text );
97 return parent
::validate( $value, $alldata );
100 protected function getInputWidget( $params ) {
101 if ( $this->mParams
['namespace'] !== false ) {
102 $params['namespace'] = $this->mParams
['namespace'];
104 $params['relative'] = $this->mParams
['relative'];
105 return new TitleInputWidget( $params );
108 protected function shouldInfuseOOUI() {
112 protected function getOOUIModules() {
113 // FIXME: TitleInputWidget should be in its own module
114 return [ 'mediawiki.widgets' ];
117 public function getInputHtml( $value ) {
118 // add mw-searchInput class to enable search suggestions for non-OOUI, too
119 $this->mClass
.= 'mw-searchInput';
121 // return the HTMLTextField html
122 return parent
::getInputHTML( $value );
125 protected function getDataAttribs() {
127 'data-mw-searchsuggest' => FormatJson
::encode( [
128 'wrapAsLink' => false,
134 /** @deprecated class alias since 1.42 */
135 class_alias( HTMLTitleTextField
::class, 'HTMLTitleTextField' );