Merge "objectcache: switch WANObjectCache process cache to MapCacheLRU"
[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 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
45 if ( $value === null ) {
46 $value = '';
47 }
48
49 if ( !$this->mParams['required'] && $value === '' ) {
50 // If this field is not required and the value is empty, that's okay, skip validation
51 return parent::validate( $value, $alldata );
52 }
53
54 try {
55 if ( !$this->mParams['relative'] ) {
56 $title = Title::newFromTextThrow( $value );
57 } else {
58 // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
59 global $wgContLang;
60 $namespaceName = $wgContLang->getNsText( $this->mParams['namespace'] );
61 $title = Title::newFromTextThrow( $namespaceName . ':' . $value );
62 }
63 } catch ( MalformedTitleException $e ) {
64 $msg = $this->msg( $e->getErrorMessage() );
65 $params = $e->getErrorMessageParameters();
66 if ( $params ) {
67 $msg->params( $params );
68 }
69 return $msg;
70 }
71
72 $text = $title->getPrefixedText();
73 if ( $this->mParams['namespace'] !== false &&
74 !$title->inNamespace( $this->mParams['namespace'] )
75 ) {
76 return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text );
77 }
78
79 if ( $this->mParams['creatable'] && !$title->canExist() ) {
80 return $this->msg( 'htmlform-title-not-creatable', $text );
81 }
82
83 if ( $this->mParams['exists'] && !$title->exists() ) {
84 return $this->msg( 'htmlform-title-not-exists', $text );
85 }
86
87 return parent::validate( $value, $alldata );
88 }
89
90 protected function getInputWidget( $params ) {
91 if ( $this->mParams['namespace'] !== false ) {
92 $params['namespace'] = $this->mParams['namespace'];
93 }
94 $params['relative'] = $this->mParams['relative'];
95 return new TitleInputWidget( $params );
96 }
97
98 protected function shouldInfuseOOUI() {
99 return true;
100 }
101
102 protected function getOOUIModules() {
103 // FIXME: TitleInputWidget should be in its own module
104 return [ 'mediawiki.widgets' ];
105 }
106
107 public function getInputHtml( $value ) {
108 // add mw-searchInput class to enable search suggestions for non-OOUI, too
109 $this->mClass .= 'mw-searchInput';
110
111 // return the HTMLTextField html
112 return parent::getInputHTML( $value );
113 }
114
115 protected function getDataAttribs() {
116 return [
117 'data-mw-searchsuggest' => FormatJson::encode( [
118 'wrapAsLink' => false,
119 ] ),
120 ];
121 }
122 }