3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Widget\TitleInputWidget
;
7 * Implements a text input field for page titles.
8 * Automatically does validation that the title is valid,
9 * as well as autocompletion if using the OOUI display format.
11 * Note: Forms using GET requests will need to make sure the title value is not
14 * Optional parameters:
15 * 'namespace' - Namespace the page must be in
16 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
17 * 'creatable' - Whether to validate the title is creatable (not a special page)
18 * 'exists' - Whether to validate that the title already exists
22 class HTMLTitleTextField
extends HTMLTextField
{
23 public function __construct( $params ) {
29 // This overrides the default from HTMLFormField
33 parent
::__construct( $params );
36 public function validate( $value, $alldata ) {
37 if ( $this->mParent
->getMethod() === 'get' && $value === '' ) {
38 // If the form is a GET form and has no value, assume it hasn't been
39 // submitted yet, and skip validation
40 // TODO This doesn't look right, we should be able to tell the difference
41 // between "not submitted" (null) and "submitted but empty" (empty string).
42 return parent
::validate( $value, $alldata );
45 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
46 if ( $value === null ) {
50 if ( !$this->mParams
['required'] && $value === '' ) {
51 // If this field is not required and the value is empty, that's okay, skip validation
52 return parent
::validate( $value, $alldata );
56 if ( !$this->mParams
['relative'] ) {
57 $title = Title
::newFromTextThrow( $value );
59 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
60 $namespaceName = MediaWikiServices
::getInstance()->getContentLanguage()->
61 getNsText( $this->mParams
['namespace'] );
62 $title = Title
::newFromTextThrow( $namespaceName . ':' . $value );
64 } catch ( MalformedTitleException
$e ) {
65 $msg = $this->msg( $e->getErrorMessage() );
66 $params = $e->getErrorMessageParameters();
68 $msg->params( $params );
73 $text = $title->getPrefixedText();
74 if ( $this->mParams
['namespace'] !== false &&
75 !$title->inNamespace( $this->mParams
['namespace'] )
77 return $this->msg( 'htmlform-title-badnamespace', $this->mParams
['namespace'], $text );
80 if ( $this->mParams
['creatable'] && !$title->canExist() ) {
81 return $this->msg( 'htmlform-title-not-creatable', $text );
84 if ( $this->mParams
['exists'] && !$title->exists() ) {
85 return $this->msg( 'htmlform-title-not-exists', $text );
88 return parent
::validate( $value, $alldata );
91 protected function getInputWidget( $params ) {
92 if ( $this->mParams
['namespace'] !== false ) {
93 $params['namespace'] = $this->mParams
['namespace'];
95 $params['relative'] = $this->mParams
['relative'];
96 return new TitleInputWidget( $params );
99 protected function shouldInfuseOOUI() {
103 protected function getOOUIModules() {
104 // FIXME: TitleInputWidget should be in its own module
105 return [ 'mediawiki.widgets' ];
108 public function getInputHtml( $value ) {
109 // add mw-searchInput class to enable search suggestions for non-OOUI, too
110 $this->mClass
.= 'mw-searchInput';
112 // return the HTMLTextField html
113 return parent
::getInputHTML( $value );
116 protected function getDataAttribs() {
118 'data-mw-searchsuggest' => FormatJson
::encode( [
119 'wrapAsLink' => false,