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