HTMLForm: Add 'title' type

HTMLTitleTextField will automatically validate title input, can
optionally ensure the title is in a specific namespace, is creatable, or
already exists.

The field currently doesn't support GET requests since validation on
empty strings fails.

Bug: T104420
Change-Id: I45718462570d0a523a148c3830b1116b634df050
This commit is contained in:
Kunal Mehta 2015-06-30 22:27:23 -07:00 committed by Bartosz Dziewoński
parent 6a779b59a2
commit d7d663bc54
6 changed files with 79 additions and 1 deletions

View file

@ -509,6 +509,7 @@ $wgAutoloadLocalClasses = array(
'HTMLTextAreaField' => __DIR__ . '/includes/htmlform/HTMLTextAreaField.php',
'HTMLTextField' => __DIR__ . '/includes/htmlform/HTMLTextField.php',
'HTMLTextFieldWithButton' => __DIR__ . '/includes/htmlform/HTMLTextFieldWithButton.php',
'HTMLTitleTextField' => __DIR__ . '/includes/htmlform/HTMLTitleTextField.php',
'HWLDFWordAccumulator' => __DIR__ . '/includes/diff/DairikiDiff.php',
'HashBagOStuff' => __DIR__ . '/includes/libs/objectcache/HashBagOStuff.php',
'HashConfig' => __DIR__ . '/includes/config/HashConfig.php',

View file

@ -153,6 +153,7 @@ class HTMLForm extends ContextSource {
'email' => 'HTMLTextField',
'password' => 'HTMLTextField',
'url' => 'HTMLTextField',
'title' => 'HTMLTitleTextField',
);
public $mFieldData;

View file

@ -120,11 +120,15 @@ class HTMLTextField extends HTMLFormField {
$type = $this->getType( $attribs );
return new OOUI\TextInputWidget( array(
return $this->getInputWidget( array(
'id' => $this->mID,
'name' => $this->mName,
'value' => $value,
'type' => $type,
) + $attribs );
}
protected function getInputWidget( $params ) {
return new OOUI\TextInputWidget( $params );
}
}

View file

@ -0,0 +1,66 @@
<?php
use MediaWiki\Widget\TitleInputWidget;
/**
* Implements a text input field for page titles.
* Automatically does validation that the title is valid,
* as well as autocompletion if using the OOUI display format.
*
* FIXME: Does not work for forms that support GET requests.
*
* Optional parameters:
* 'namespace' - Namespace the page must be in
* 'creatable' - Whether to validate the title is creatable (not a special page)
* 'exists' - Whether to validate that the title already exists
*
* @since 1.26
*/
class HTMLTitleTextField extends HTMLTextField {
public function __construct( $params ) {
$params += array(
'namespace' => false,
'creatable' => false,
'exists' => false,
);
parent::__construct( $params );
}
public function validate( $value, $alldata ) {
try {
$title = Title::newFromTextThrow( $value );
} catch ( MalformedTitleException $e ) {
$msg = $this->msg( $e->getErrorMessage() );
$params = $e->getErrorMessageParameters();
if ( $params ) {
$msg->params( $params );
}
return $msg->parse();
}
$text = $title->getPrefixedText();
if ( $this->mParams['namespace'] !== false && !$title->inNamespace( $this->mParams['namespace'] ) ) {
return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
}
if ( $this->mParams['creatable'] && !$title->canExist() ) {
return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
}
if ( $this->mParams['exists'] && !$title->exists() ) {
return $this->msg( 'htmlform-title-not-exists', $text )->parse();
}
return parent::validate( $value, $alldata );
}
protected function getInputWidget( $params ) {
$this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
if ( $this->mParams['namespace'] !== false ) {
$params['namespace'] = $this->mParams['namespace'];
}
$params['relative'] = false;
return new TitleInputWidget( $params );
}
}

View file

@ -3535,6 +3535,9 @@
"htmlform-cloner-create": "Add more",
"htmlform-cloner-delete": "Remove",
"htmlform-cloner-required": "At least one value is required.",
"htmlform-title-badnamespace": "[[:$1]] is not in the \"{{ns:$2}}\" namespace.",
"htmlform-title-not-creatable": "\"$1\" is not a creatable page title",
"htmlform-title-not-exists": "[[:$1]] does not exist.",
"sqlite-has-fts": "$1 with full-text search support",
"sqlite-no-fts": "$1 without full-text search support",
"logentry-delete-delete": "$1 {{GENDER:$2|deleted}} page $3",

View file

@ -3706,6 +3706,9 @@
"htmlform-cloner-create": "Used as the text for the button that adds a row to a multi-input HTML form element.\n\nSee also:\n* {{msg-mw|htmlform-cloner-delete}}\n* {{msg-mw|htmlform-cloner-required}}",
"htmlform-cloner-delete": "Used as the text for the button that removes a row from a multi-input HTML form element\n\nSee also:\n* {{msg-mw|htmlform-cloner-create}}\n* {{msg-mw|htmlform-cloner-required}}\n{{Identical|Remove}}",
"htmlform-cloner-required": "Used as an error message in HTML forms.\n\nSee also:\n* {{msg-mw|htmlform-required}}\n* {{msg-mw|htmlform-cloner-create}}\n* {{msg-mw|htmlform-cloner-delete}}",
"htmlform-title-badnamespace": "Error message shown if the page title provided by the user is not in the required namespace. $1 is the page, $2 is the numerical namespace index.",
"htmlform-title-not-creatable": "Error message shown if the page title provided by the user is not creatable (a special page). $1 is the page title.",
"htmlform-title-not-exists": "Error message shown if the page title provided by the user does not exist. $1 is the page title.",
"sqlite-has-fts": "Shown on [[Special:Version]].\nParameters:\n* $1 - version",
"sqlite-no-fts": "Shown on [[Special:Version]].\nParameters:\n* $1 - version",
"logentry-delete-delete": "{{Logentry|[[Special:Log/delete]]}}",