MediaWiki master
HTMLTitleTextField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
11
33 public function __construct( $params ) {
34 $params += [
35 'namespace' => false,
36 'relative' => false,
37 'creatable' => false,
38 'exists' => false,
39 'interwiki' => false,
40 // This overrides the default from HTMLFormField
41 'required' => true,
42 ];
43
44 parent::__construct( $params );
45 }
46
48 public function validate( $value, $alldata ) {
49 if ( $this->mParams['interwiki'] && $this->mParams['relative'] ) {
50 // relative and interwiki cannot be used together, because we don't have a way to know about
51 // namespaces used by the other wiki (and it might actually be a non-wiki link, too).
52 throw new InvalidArgumentException( 'relative and interwiki may not be used together' );
53 }
54 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
55 $value ??= '';
56
57 if ( !$this->mParams['required'] && $value === '' ) {
58 // If this field is not required and the value is empty, that's okay, skip validation
59 return parent::validate( $value, $alldata );
60 }
61
62 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
63 try {
64 if ( !$this->mParams['relative'] ) {
65 $title = $titleFactory->newFromTextThrow( $value );
66 } else {
67 // Can't use makeTitleSafe(), because it doesn't throw useful exceptions
68 $title = $titleFactory->newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
69 }
70 } catch ( MalformedTitleException $e ) {
71 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
72 }
73
74 if ( $title->isExternal() ) {
75 if ( $this->mParams['interwiki'] ) {
76 // We cannot validate external titles, skip the rest of the validation
77 return parent::validate( $value, $alldata );
78 } else {
79 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
80 }
81 }
82
83 $text = $title->getPrefixedText();
84 if ( $this->mParams['namespace'] !== false &&
85 !$title->inNamespace( $this->mParams['namespace'] )
86 ) {
87 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
88 }
89
90 if ( $this->mParams['creatable'] && !$title->canExist() ) {
91 return $this->msg( 'htmlform-title-not-creatable', $text );
92 }
93
94 if ( $this->mParams['exists'] && !$title->exists() ) {
95 return $this->msg( 'htmlform-title-not-exists', $text );
96 }
97
98 return parent::validate( $value, $alldata );
99 }
100
102 protected function getInputWidget( $params ) {
103 if ( $this->mParams['namespace'] !== false ) {
104 $params['namespace'] = $this->mParams['namespace'];
105 }
106
107 if ( $this->mParams['creatable'] ) {
108 $params['creatable'] = $this->mParams['creatable'];
109 }
110
111 $params['relative'] = $this->mParams['relative'];
112 return new TitleInputWidget( $params );
113 }
114
116 protected function shouldInfuseOOUI() {
117 return true;
118 }
119
121 protected function getOOUIModules() {
122 // FIXME: TitleInputWidget should be in its own module
123 return [ 'mediawiki.widgets' ];
124 }
125
127 public function getInputHtml( $value ) {
128 // add mw-searchInput class to enable search suggestions for non-OOUI, too
129 $this->mClass .= 'mw-searchInput';
130
131 // return the HTMLTextField html
132 return parent::getInputHTML( $value );
133 }
134
136 protected function getDataAttribs() {
137 return [
138 'data-mw-searchsuggest' => FormatJson::encode( [
139 'wrapAsLink' => false,
140 ] ),
141 ];
142 }
143}
144
146class_alias( HTMLTitleTextField::class, 'HTMLTitleTextField' );
Implements a text input field for page titles.
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getDataAttribs()
Returns an array of data-* attributes to add to the field.to overridearray
msg( $key,... $params)
Get a translated interface message.
JSON formatter wrapper class.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Represents a title within MediaWiki.
Definition Title.php:69