From d7d663bc5436b314ea9d35dd60c574668d540818 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Tue, 30 Jun 2015 22:27:23 -0700 Subject: [PATCH] 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 --- autoload.php | 1 + includes/htmlform/HTMLForm.php | 1 + includes/htmlform/HTMLTextField.php | 6 ++- includes/htmlform/HTMLTitleTextField.php | 66 ++++++++++++++++++++++++ languages/i18n/en.json | 3 ++ languages/i18n/qqq.json | 3 ++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 includes/htmlform/HTMLTitleTextField.php diff --git a/autoload.php b/autoload.php index 8e4da1e4c3..a1837f5af0 100644 --- a/autoload.php +++ b/autoload.php @@ -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', diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 702651ea83..1241114be4 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -153,6 +153,7 @@ class HTMLForm extends ContextSource { 'email' => 'HTMLTextField', 'password' => 'HTMLTextField', 'url' => 'HTMLTextField', + 'title' => 'HTMLTitleTextField', ); public $mFieldData; diff --git a/includes/htmlform/HTMLTextField.php b/includes/htmlform/HTMLTextField.php index 5c04ee2f19..40cff47e42 100644 --- a/includes/htmlform/HTMLTextField.php +++ b/includes/htmlform/HTMLTextField.php @@ -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 ); + } } diff --git a/includes/htmlform/HTMLTitleTextField.php b/includes/htmlform/HTMLTitleTextField.php new file mode 100644 index 0000000000..e1bc1a0cb3 --- /dev/null +++ b/includes/htmlform/HTMLTitleTextField.php @@ -0,0 +1,66 @@ + 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 ); + } +} diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 3a47bceca7..73332ee993 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -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", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index ca6d375737..598af80b5c 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -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]]}}", -- 2.20.1