Completed
Branch master (0c9f05)
by
unknown
29:21
created

HTMLTitleTextField::shouldInfuseOOUI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
		];
29
30
		parent::__construct( $params );
31
	}
32
33
	public function validate( $value, $alldata ) {
34
		if ( $this->mParent->getMethod() === 'get' && $value === '' ) {
35
			// If the form is a GET form and has no value, assume it hasn't been
36
			// submitted yet, and skip validation
37
			return parent::validate( $value, $alldata );
38
		}
39
		try {
40
			if ( !$this->mParams['relative'] ) {
41
				$title = Title::newFromTextThrow( $value );
42
			} else {
43
				// Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
44
				global $wgContLang;
45
				$namespaceName = $wgContLang->getNsText( $this->mParams['namespace'] );
46
				$title = Title::newFromTextThrow( $namespaceName . ':' . $value );
47
			}
48
		} catch ( MalformedTitleException $e ) {
49
			$msg = $this->msg( $e->getErrorMessage() );
50
			$params = $e->getErrorMessageParameters();
51
			if ( $params ) {
52
				$msg->params( $params );
53
			}
54
			return $msg->parse();
55
		}
56
57
		$text = $title->getPrefixedText();
58
		if ( $this->mParams['namespace'] !== false &&
59
			!$title->inNamespace( $this->mParams['namespace'] )
60
		) {
61
			return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
62
		}
63
64
		if ( $this->mParams['creatable'] && !$title->canExist() ) {
65
			return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
66
		}
67
68
		if ( $this->mParams['exists'] && !$title->exists() ) {
69
			return $this->msg( 'htmlform-title-not-exists', $text )->parse();
70
		}
71
72
		return parent::validate( $value, $alldata );
73
	}
74
75
	protected function getInputWidget( $params ) {
76
		$this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
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
	public function getInputHtml( $value ) {
89
		// add mw-searchInput class to enable search suggestions for non-OOUI, too
90
		$this->mClass .= 'mw-searchInput';
91
92
		// return the HTMLTextField html
93
		return parent::getInputHTML( $value );
94
	}
95
96
	protected function getDataAttribs() {
97
		return [
98
			'data-mw-searchsuggest' => FormatJson::encode( [
99
				'wrapAsLink' => false,
100
			] ),
101
		];
102
	}
103
}
104