Merge "HTMLForm: Add 'title' type"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 16 Jul 2015 18:33:13 +0000 (18:33 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 16 Jul 2015 18:33:13 +0000 (18:33 +0000)
autoload.php
includes/htmlform/HTMLForm.php
includes/htmlform/HTMLTextField.php
includes/htmlform/HTMLTitleTextField.php [new file with mode: 0644]
languages/i18n/en.json
languages/i18n/qqq.json

index c443734..2c9dd8f 100644 (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',
index 43cac88..d49963d 100644 (file)
@@ -153,6 +153,7 @@ class HTMLForm extends ContextSource {
                'email' => 'HTMLTextField',
                'password' => 'HTMLTextField',
                'url' => 'HTMLTextField',
+               'title' => 'HTMLTitleTextField',
        );
 
        public $mFieldData;
index 5c04ee2..40cff47 100644 (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 );
+       }
 }
diff --git a/includes/htmlform/HTMLTitleTextField.php b/includes/htmlform/HTMLTitleTextField.php
new file mode 100644 (file)
index 0000000..e1bc1a0
--- /dev/null
@@ -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 );
+       }
+}
index 6001cd4..00bd980 100644 (file)
        "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",
index 0867cf7..cc9dcc6 100644 (file)
        "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]]}}",