Merge "Make addedwatchtext less verbose"
[mediawiki.git] / includes / htmlform / HTMLTitleTextField.php
bloba225c67c392b712a39d686c96a0d2fab2c0e1aa9
1 <?php
3 use MediaWiki\Widget\TitleInputWidget;
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.
10 * Note: Forms using GET requests will need to make sure the title value is not
11 * an empty string.
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
19 * @since 1.26
21 class HTMLTitleTextField extends HTMLTextField {
22 public function __construct( $params ) {
23 $params += array(
24 'namespace' => false,
25 'relative' => false,
26 'creatable' => false,
27 'exists' => false,
30 parent::__construct( $params );
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 );
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 );
48 } catch ( MalformedTitleException $e ) {
49 $msg = $this->msg( $e->getErrorMessage() );
50 $params = $e->getErrorMessageParameters();
51 if ( $params ) {
52 $msg->params( $params );
54 return $msg->parse();
57 $text = $title->getPrefixedText();
58 if ( $this->mParams['namespace'] !== false && !$title->inNamespace( $this->mParams['namespace'] ) ) {
59 return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
62 if ( $this->mParams['creatable'] && !$title->canExist() ) {
63 return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
66 if ( $this->mParams['exists'] && !$title->exists() ) {
67 return $this->msg( 'htmlform-title-not-exists', $text )->parse();
70 return parent::validate( $value, $alldata );
73 protected function getInputWidget( $params ) {
74 $this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
75 if ( $this->mParams['namespace'] !== false ) {
76 $params['namespace'] = $this->mParams['namespace'];
78 $params['relative'] = $this->mParams['relative'];
79 return new TitleInputWidget( $params );