MediaWiki  master
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 }
Title\makeName
static makeName( $ns, $title, $fragment='', $interwiki='', $canonicalNamespace=false)
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:872
HTMLTitleTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLTitleTextField.php:111
MalformedTitleException\getErrorMessage
getErrorMessage()
Definition: MalformedTitleException.php:70
HTMLTitleTextField
Implements a text input field for page titles.
Definition: HTMLTitleTextField.php:21
HTMLTextField
<input> field.
Definition: HTMLTextField.php:15
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:96
HTMLTitleTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLTitleTextField.php:102
HTMLTitleTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLTitleTextField.php:106
$title
$title
Definition: testCompression.php:38
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:430
HTMLTitleTextField\__construct
__construct( $params)
Definition: HTMLTitleTextField.php:26
HTMLTitleTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLTitleTextField.php:40
MalformedTitleException\getErrorMessageParameters
getErrorMessageParameters()
Definition: MalformedTitleException.php:78
MediaWiki\Widget\TitleInputWidget
Title input widget.
Definition: TitleInputWidget.php:11
MalformedTitleException
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Definition: MalformedTitleException.php:26
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLTitleTextField\getDataAttribs
getDataAttribs()
Returns an array of data-* attributes to add to the field.
Definition: HTMLTitleTextField.php:119
HTMLTitleTextField\getInputWidget
getInputWidget( $params)
Definition: HTMLTitleTextField.php:94