MediaWiki  master
HTMLTitleTextField.php
Go to the documentation of this file.
1 <?php
2 
4 
20  /*
21  * @stable to call
22  */
23  public function __construct( $params ) {
24  $params += [
25  'namespace' => false,
26  'relative' => false,
27  'creatable' => false,
28  'exists' => false,
29  // This overrides the default from HTMLFormField
30  'required' => true,
31  ];
32 
33  parent::__construct( $params );
34  }
35 
36  public function validate( $value, $alldata ) {
37  // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
38  if ( $value === null ) {
39  $value = '';
40  }
41 
42  if ( !$this->mParams['required'] && $value === '' ) {
43  // If this field is not required and the value is empty, that's okay, skip validation
44  return parent::validate( $value, $alldata );
45  }
46 
47  try {
48  if ( !$this->mParams['relative'] ) {
49  $title = Title::newFromTextThrow( $value );
50  } else {
51  // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
52  $title = Title::newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
53  }
54  } catch ( MalformedTitleException $e ) {
55  return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
56  }
57 
58  $text = $title->getPrefixedText();
59  if ( $this->mParams['namespace'] !== false &&
60  !$title->inNamespace( $this->mParams['namespace'] )
61  ) {
62  return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
63  }
64 
65  if ( $this->mParams['creatable'] && !$title->canExist() ) {
66  return $this->msg( 'htmlform-title-not-creatable', $text );
67  }
68 
69  if ( $this->mParams['exists'] && !$title->exists() ) {
70  return $this->msg( 'htmlform-title-not-exists', $text );
71  }
72 
73  return parent::validate( $value, $alldata );
74  }
75 
76  protected function getInputWidget( $params ) {
77  if ( $this->mParams['namespace'] !== false ) {
78  $params['namespace'] = $this->mParams['namespace'];
79  }
80  $params['relative'] = $this->mParams['relative'];
81  return new TitleInputWidget( $params );
82  }
83 
84  protected function shouldInfuseOOUI() {
85  return true;
86  }
87 
88  protected function getOOUIModules() {
89  // FIXME: TitleInputWidget should be in its own module
90  return [ 'mediawiki.widgets' ];
91  }
92 
93  public function getInputHtml( $value ) {
94  // add mw-searchInput class to enable search suggestions for non-OOUI, too
95  $this->mClass .= 'mw-searchInput';
96 
97  // return the HTMLTextField html
98  return parent::getInputHTML( $value );
99  }
100 
101  protected function getDataAttribs() {
102  return [
103  'data-mw-searchsuggest' => FormatJson::encode( [
104  'wrapAsLink' => false,
105  ] ),
106  ];
107  }
108 }
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:820
HTMLTitleTextField\getInputHtml
getInputHtml( $value)
Definition: HTMLTitleTextField.php:93
MalformedTitleException\getErrorMessage
getErrorMessage()
Definition: MalformedTitleException.php:66
HTMLTitleTextField
Implements a text input field for page titles.
Definition: HTMLTitleTextField.php:19
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:115
HTMLTitleTextField\shouldInfuseOOUI
shouldInfuseOOUI()
Whether the field should be automatically infused.
Definition: HTMLTitleTextField.php:84
HTMLTitleTextField\getOOUIModules
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
Definition: HTMLTitleTextField.php:88
$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:364
HTMLTitleTextField\__construct
__construct( $params)
Stable to call.
Definition: HTMLTitleTextField.php:23
HTMLTitleTextField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLTitleTextField.php:36
MalformedTitleException\getErrorMessageParameters
getErrorMessageParameters()
Definition: MalformedTitleException.php:74
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:101
HTMLTitleTextField\getInputWidget
getInputWidget( $params)
Stable to override.
Definition: HTMLTitleTextField.php:76