MediaWiki master
HTMLTitleTextField.php
Go to the documentation of this file.
1<?php
2
4
5use FormatJson;
6use 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 if ( $value === null ) {
55 $value = '';
56 }
57
58 if ( !$this->mParams['required'] && $value === '' ) {
59 // If this field is not required and the value is empty, that's okay, skip validation
60 return parent::validate( $value, $alldata );
61 }
62
63 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
64 try {
65 if ( !$this->mParams['relative'] ) {
66 $title = $titleFactory->newFromTextThrow( $value );
67 } else {
68 // Can't use makeTitleSafe(), because it doesn't throw useful exceptions
69 $title = $titleFactory->newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
70 }
71 } catch ( MalformedTitleException $e ) {
72 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
73 }
74
75 if ( $title->isExternal() ) {
76 if ( $this->mParams['interwiki'] ) {
77 // We cannot validate external titles, skip the rest of the validation
78 return parent::validate( $value, $alldata );
79 } else {
80 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
81 }
82 }
83
84 $text = $title->getPrefixedText();
85 if ( $this->mParams['namespace'] !== false &&
86 !$title->inNamespace( $this->mParams['namespace'] )
87 ) {
88 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
89 }
90
91 if ( $this->mParams['creatable'] && !$title->canExist() ) {
92 return $this->msg( 'htmlform-title-not-creatable', $text );
93 }
94
95 if ( $this->mParams['exists'] && !$title->exists() ) {
96 return $this->msg( 'htmlform-title-not-exists', $text );
97 }
98
99 return parent::validate( $value, $alldata );
100 }
101
102 protected function getInputWidget( $params ) {
103 if ( $this->mParams['namespace'] !== false ) {
104 $params['namespace'] = $this->mParams['namespace'];
105 }
106 $params['relative'] = $this->mParams['relative'];
107 return new TitleInputWidget( $params );
108 }
109
110 protected function shouldInfuseOOUI() {
111 return true;
112 }
113
114 protected function getOOUIModules() {
115 // FIXME: TitleInputWidget should be in its own module
116 return [ 'mediawiki.widgets' ];
117 }
118
119 public function getInputHtml( $value ) {
120 // add mw-searchInput class to enable search suggestions for non-OOUI, too
121 $this->mClass .= 'mw-searchInput';
122
123 // return the HTMLTextField html
124 return parent::getInputHTML( $value );
125 }
126
127 protected function getDataAttribs() {
128 return [
129 'data-mw-searchsuggest' => FormatJson::encode( [
130 'wrapAsLink' => false,
131 ] ),
132 ];
133 }
134}
135
137class_alias( HTMLTitleTextField::class, 'HTMLTitleTextField' );
array $params
The job parameters.
JSON formatter wrapper class.
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.
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