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
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' );
52 }
53 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
54 $value ??= '';
55
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 );
59 }
60
61 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
62 try {
63 if ( !$this->mParams['relative'] ) {
64 $title = $titleFactory->newFromTextThrow( $value );
65 } else {
66 // Can't use makeTitleSafe(), because it doesn't throw useful exceptions
67 $title = $titleFactory->newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
68 }
69 } catch ( MalformedTitleException $e ) {
70 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
71 }
72
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 );
77 } else {
78 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
79 }
80 }
81
82 $text = $title->getPrefixedText();
83 if ( $this->mParams['namespace'] !== false &&
84 !$title->inNamespace( $this->mParams['namespace'] )
85 ) {
86 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
87 }
88
89 if ( $this->mParams['creatable'] && !$title->canExist() ) {
90 return $this->msg( 'htmlform-title-not-creatable', $text );
91 }
92
93 if ( $this->mParams['exists'] && !$title->exists() ) {
94 return $this->msg( 'htmlform-title-not-exists', $text );
95 }
96
97 return parent::validate( $value, $alldata );
98 }
99
100 protected function getInputWidget( $params ) {
101 if ( $this->mParams['namespace'] !== false ) {
102 $params['namespace'] = $this->mParams['namespace'];
103 }
104 $params['relative'] = $this->mParams['relative'];
105 return new TitleInputWidget( $params );
106 }
107
108 protected function shouldInfuseOOUI() {
109 return true;
110 }
111
112 protected function getOOUIModules() {
113 // FIXME: TitleInputWidget should be in its own module
114 return [ 'mediawiki.widgets' ];
115 }
116
117 public function getInputHtml( $value ) {
118 // add mw-searchInput class to enable search suggestions for non-OOUI, too
119 $this->mClass .= 'mw-searchInput';
120
121 // return the HTMLTextField html
122 return parent::getInputHTML( $value );
123 }
124
125 protected function getDataAttribs() {
126 return [
127 'data-mw-searchsuggest' => FormatJson::encode( [
128 'wrapAsLink' => false,
129 ] ),
130 ];
131 }
132}
133
135class_alias( HTMLTitleTextField::class, 'HTMLTitleTextField' );
array $params
The job parameters.
Implements a text input field for page titles.
shouldInfuseOOUI()
Whether the field should be automatically infused.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
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.
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:78