Debian Codegraph @

includes/htmlform/fields/HTMLTitleTextField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\HTMLForm\Field;
4
5 use InvalidArgumentException;
6 use MediaWiki\Json\FormatJson;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Title\MalformedTitleException;
9 use MediaWiki\Title\Title;
10 use MediaWiki\Widget\TitleInputWidget;
11
12 /**
13 * Implements a text input field for page titles.
14 * Automatically does validation that the title is valid,
15 * as well as autocompletion if using the OOUI display format.
16 *
17 * Optional parameters:
18 * 'namespace' - Namespace the page must be in (use namespace constant; one of the NS_* constants may be used)
19 * 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
20 * 'creatable' - Whether to validate the title is creatable (not a special page)
21 * 'exists' - Whether to validate that the title already exists
22 * 'interwiki' – Tolerate interwiki links (other conditions such as 'namespace' or 'exists' will be
23 * ignored if the title is an interwiki title). Cannot be used together with 'relative'.
24 *
25 * @stable to extend
26 * @since 1.26
27 */
28 class HTMLTitleTextField extends HTMLTextField {
29 /**
30 * @stable to call
31 * @inheritDoc
32 */
33 public function __construct( $params ) {
34 $params += [
35 'namespace' => false,
36 'relative' => false,
37 'creatable' => false,
38 'exists' => false,
39 'interwiki' => false,
40 // This overrides the default from HTMLFormField
41 'required' => true,
42 ];
43
44 parent::__construct( $params );
45 }
46
47 public function validate( $value, $alldata ) {
48 if ( $this->mParams['interwiki'] && $this->mParams['relative'] ) {
49 // relative and interwiki cannot be used together, because we don't have a way to know about
50 // namespaces used by the other wiki (and it might actually be a non-wiki link, too).
51 throw new InvalidArgumentException( 'relative and interwiki may not be used together' );
52 }
53 // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
54 $value ??= '';
55
56 if ( !$this->mParams['required'] && $value === '' ) {
57 // If this field is not required and the value is empty, that's okay, skip validation
58 return parent::validate( $value, $alldata );
59 }
60
61 $titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
62 try {
63 if ( !$this->mParams['relative'] ) {
64 $title = $titleFactory->newFromTextThrow( $value );
65 } else {
66 // Can't use makeTitleSafe(), because it doesn't throw useful exceptions
67 $title = $titleFactory->newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
68 }
69 } catch ( MalformedTitleException $e ) {
70 return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
71 }
72
73 if ( $title->isExternal() ) {
74 if ( $this->mParams['interwiki'] ) {
75 // We cannot validate external titles, skip the rest of the validation
76 return parent::validate( $value, $alldata );
77 } else {
78 return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
79 }
80 }
81
82 $text = $title->getPrefixedText();
83 if ( $this->mParams['namespace'] !== false &&
84 !$title->inNamespace( $this->mParams['namespace'] )
85 ) {
86 return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
87 }
88
89 if ( $this->mParams['creatable'] && !$title->canExist() ) {
90 return $this->msg( 'htmlform-title-not-creatable', $text );
91 }
92
93 if ( $this->mParams['exists'] && !$title->exists() ) {
94 return $this->msg( 'htmlform-title-not-exists', $text );
95 }
96
97 return parent::validate( $value, $alldata );
98 }
99
100 protected function getInputWidget( $params ) {
101 if ( $this->mParams['namespace'] !== false ) {
102 $params['namespace'] = $this->mParams['namespace'];
103 }
104 $params['relative'] = $this->mParams['relative'];
105 return new TitleInputWidget( $params );
106 }
107
108 protected function shouldInfuseOOUI() {
109 return true;
110 }
111
112 protected function getOOUIModules() {
113 // FIXME: TitleInputWidget should be in its own module
114 return [ 'mediawiki.widgets' ];
115 }
116
117 public function getInputHtml( $value ) {
118 // add mw-searchInput class to enable search suggestions for non-OOUI, too
119 $this->mClass .= 'mw-searchInput';
120
121 // return the HTMLTextField html
122 return parent::getInputHTML( $value );
123 }
124
125 protected function getDataAttribs() {
126 return [
127 'data-mw-searchsuggest' => FormatJson::encode( [
128 'wrapAsLink' => false,
129 ] ),
130 ];
131 }
132 }
133
134 /** @deprecated class alias since 1.42 */
135 class_alias( HTMLTitleTextField::class, 'HTMLTitleTextField' );