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