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