3 use MediaWiki\Widget\TitleInputWidget
;
6 * Implements a text input field for page titles.
7 * Automatically does validation that the title is valid,
8 * as well as autocompletion if using the OOUI display format.
10 * Optional parameters:
11 * 'namespace' - Namespace the page must be in
12 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
13 * 'creatable' - Whether to validate the title is creatable (not a special page)
14 * 'exists' - Whether to validate that the title already exists
18 class HTMLTitleTextField
extends HTMLTextField
{
19 public function __construct( $params ) {
25 // This overrides the default from HTMLFormField
29 parent
::__construct( $params );
32 public function validate( $value, $alldata ) {
33 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
34 if ( $value === null ) {
38 if ( !$this->mParams
['required'] && $value === '' ) {
39 // If this field is not required and the value is empty, that's okay, skip validation
40 return parent
::validate( $value, $alldata );
44 if ( !$this->mParams
['relative'] ) {
45 $title = Title
::newFromTextThrow( $value );
47 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
48 $title = Title
::newFromTextThrow( Title
::makeName( $this->mParams
['namespace'], $value ) );
50 } catch ( MalformedTitleException
$e ) {
51 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
54 $text = $title->getPrefixedText();
55 if ( $this->mParams
['namespace'] !== false &&
56 !$title->inNamespace( $this->mParams
['namespace'] )
58 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams
['namespace'] );
61 if ( $this->mParams
['creatable'] && !$title->canExist() ) {
62 return $this->msg( 'htmlform-title-not-creatable', $text );
65 if ( $this->mParams
['exists'] && !$title->exists() ) {
66 return $this->msg( 'htmlform-title-not-exists', $text );
69 return parent
::validate( $value, $alldata );
72 protected function getInputWidget( $params ) {
73 if ( $this->mParams
['namespace'] !== false ) {
74 $params['namespace'] = $this->mParams
['namespace'];
76 $params['relative'] = $this->mParams
['relative'];
77 return new TitleInputWidget( $params );
80 protected function shouldInfuseOOUI() {
84 protected function getOOUIModules() {
85 // FIXME: TitleInputWidget should be in its own module
86 return [ 'mediawiki.widgets' ];
89 public function getInputHtml( $value ) {
90 // add mw-searchInput class to enable search suggestions for non-OOUI, too
91 $this->mClass
.= 'mw-searchInput';
93 // return the HTMLTextField html
94 return parent
::getInputHTML( $value );
97 protected function getDataAttribs() {
99 'data-mw-searchsuggest' => FormatJson
::encode( [
100 'wrapAsLink' => false,