MediaWiki 1.40.4
HTMLTitleTextField.php
Go to the documentation of this file.
1<?php
2
5
27 public function __construct( $params ) {
28 $params += [
29 'namespace' => false,
30 'relative' => false,
31 'creatable' => false,
32 'exists' => false,
33 'interwiki' => false,
34 // This overrides the default from HTMLFormField
35 'required' => true,
36 ];
37
38 parent::__construct( $params );
39 }
40
41 public function validate( $value, $alldata ) {
42 if ( $this->mParams['interwiki'] && $this->mParams['relative'] ) {
43 // relative and interwiki cannot be used together, because we don't have a way to know about
44 // namespaces used by the other wiki (and it might actually be a non-wiki link, too).
45 throw new InvalidArgumentException( 'relative and interwiki may not be used together' );
46 }
47 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
48 if ( $value === null ) {
49 $value = '';
50 }
51
52 if ( !$this->mParams['required'] && $value === '' ) {
53 // If this field is not required and the value is empty, that's okay, skip validation
54 return parent::validate( $value, $alldata );
55 }
56
57 try {
58 if ( !$this->mParams['relative'] ) {
59 $title = Title::newFromTextThrow( $value );
60 } else {
61 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
62 $title = Title::newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
63 }
64 } catch ( MalformedTitleException $e ) {
65 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
66 }
67
68 if ( $title->isExternal() ) {
69 if ( $this->mParams['interwiki'] ) {
70 // We cannot validate external titles, skip the rest of the validation
71 return parent::validate( $value, $alldata );
72 } else {
73 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
74 }
75 }
76
77 $text = $title->getPrefixedText();
78 if ( $this->mParams['namespace'] !== false &&
79 !$title->inNamespace( $this->mParams['namespace'] )
80 ) {
81 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
82 }
83
84 if ( $this->mParams['creatable'] && !$title->canExist() ) {
85 return $this->msg( 'htmlform-title-not-creatable', $text );
86 }
87
88 if ( $this->mParams['exists'] && !$title->exists() ) {
89 return $this->msg( 'htmlform-title-not-exists', $text );
90 }
91
92 return parent::validate( $value, $alldata );
93 }
94
95 protected function getInputWidget( $params ) {
96 if ( $this->mParams['namespace'] !== false ) {
97 $params['namespace'] = $this->mParams['namespace'];
98 }
99 $params['relative'] = $this->mParams['relative'];
100 return new TitleInputWidget( $params );
101 }
102
103 protected function shouldInfuseOOUI() {
104 return true;
105 }
106
107 protected function getOOUIModules() {
108 // FIXME: TitleInputWidget should be in its own module
109 return [ 'mediawiki.widgets' ];
110 }
111
112 public function getInputHtml( $value ) {
113 // add mw-searchInput class to enable search suggestions for non-OOUI, too
114 $this->mClass .= 'mw-searchInput';
115
116 // return the HTMLTextField html
117 return parent::getInputHTML( $value );
118 }
119
120 protected function getDataAttribs() {
121 return [
122 'data-mw-searchsuggest' => FormatJson::encode( [
123 'wrapAsLink' => false,
124 ] ),
125 ];
126 }
127}
msg( $key,... $params)
Get a translated interface message.
<input> field.
Implements a text input field for page titles.
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.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
shouldInfuseOOUI()
Whether the field should be automatically infused.
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Represents a title within MediaWiki.
Definition Title.php:82