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