Merge "Fix the bug for dates between 1912 and 1941 in Thai language"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLTitleTextField.php
1 <?php
2
3 use MediaWiki\Widget\TitleInputWidget;
4
5 /**
6 * Implements a text input field for page titles.
7 * Automatically does validation that the title is valid,
8 * as well as autocompletion if using the OOUI display format.
9 *
10 * Note: Forms using GET requests will need to make sure the title value is not
11 * an empty string.
12 *
13 * Optional parameters:
14 * 'namespace' - Namespace the page must be in
15 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
16 * 'creatable' - Whether to validate the title is creatable (not a special page)
17 * 'exists' - Whether to validate that the title already exists
18 *
19 * @since 1.26
20 */
21 class HTMLTitleTextField extends HTMLTextField {
22 public function __construct( $params ) {
23 $params += [
24 'namespace' => false,
25 'relative' => false,
26 'creatable' => false,
27 'exists' => false,
28 // This overrides the default from HTMLFormField
29 'required' => true,
30 ];
31
32 parent::__construct( $params );
33 }
34
35 public function validate( $value, $alldata ) {
36 if ( $this->mParent->getMethod() === 'get' && $value === '' ) {
37 // If the form is a GET form and has no value, assume it hasn't been
38 // submitted yet, and skip validation
39 // TODO This doesn't look right, we should be able to tell the difference
40 // between "not submitted" (null) and "submitted but empty" (empty string).
41 return parent::validate( $value, $alldata );
42 }
43
44 if ( !$this->mParams['required'] && $value === '' ) {
45 // If this field is not required and the value is empty, that's okay, skip validation
46 return parent::validate( $value, $alldata );
47 }
48
49 try {
50 if ( !$this->mParams['relative'] ) {
51 $title = Title::newFromTextThrow( $value );
52 } else {
53 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
54 global $wgContLang;
55 $namespaceName = $wgContLang->getNsText( $this->mParams['namespace'] );
56 $title = Title::newFromTextThrow( $namespaceName . ':' . $value );
57 }
58 } catch ( MalformedTitleException $e ) {
59 $msg = $this->msg( $e->getErrorMessage() );
60 $params = $e->getErrorMessageParameters();
61 if ( $params ) {
62 $msg->params( $params );
63 }
64 return $msg;
65 }
66
67 $text = $title->getPrefixedText();
68 if ( $this->mParams['namespace'] !== false &&
69 !$title->inNamespace( $this->mParams['namespace'] )
70 ) {
71 return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text );
72 }
73
74 if ( $this->mParams['creatable'] && !$title->canExist() ) {
75 return $this->msg( 'htmlform-title-not-creatable', $text );
76 }
77
78 if ( $this->mParams['exists'] && !$title->exists() ) {
79 return $this->msg( 'htmlform-title-not-exists', $text );
80 }
81
82 return parent::validate( $value, $alldata );
83 }
84
85 protected function getInputWidget( $params ) {
86 if ( $this->mParams['namespace'] !== false ) {
87 $params['namespace'] = $this->mParams['namespace'];
88 }
89 $params['relative'] = $this->mParams['relative'];
90 return new TitleInputWidget( $params );
91 }
92
93 protected function shouldInfuseOOUI() {
94 return true;
95 }
96
97 protected function getOOUIModules() {
98 // FIXME: TitleInputWidget should be in its own module
99 return [ 'mediawiki.widgets' ];
100 }
101
102 public function getInputHtml( $value ) {
103 // add mw-searchInput class to enable search suggestions for non-OOUI, too
104 $this->mClass .= 'mw-searchInput';
105
106 // return the HTMLTextField html
107 return parent::getInputHTML( $value );
108 }
109
110 protected function getDataAttribs() {
111 return [
112 'data-mw-searchsuggest' => FormatJson::encode( [
113 'wrapAsLink' => false,
114 ] ),
115 ];
116 }
117 }