TitleInputWidget: Add 'relative' option
authorKunal Mehta <legoktm@gmail.com>
Sat, 4 Jul 2015 07:39:41 +0000 (00:39 -0700)
committerKunal Mehta <legoktm@gmail.com>
Mon, 6 Jul 2015 06:40:18 +0000 (23:40 -0700)
Currently the JavaScript widget will return a value that is relative to
the namespace, so for "Category:Foo", it will return "Foo". This is
problematic for server-side forms that want a full title returned, so
make this configurable.

Change-Id: I605df2ca41831cae1c8f0a3331600d4487e7798f

includes/widget/TitleInputWidget.php
resources/src/mediawiki.widgets/mw.widgets.TitleInputWidget.js

index 173dbb0..7ddc67a 100644 (file)
@@ -15,21 +15,26 @@ use OOUI\TextInputWidget;
 class TitleInputWidget extends TextInputWidget {
 
        protected $namespace = null;
+       protected $relative = null;
 
        /**
         * @param array $config Configuration options
-        * @param number|null $config['namespace'] Namespace to prepend to queries
+        * @param int|null $config['namespace'] Namespace to prepend to queries
+        * @param bool|null $config['relative'] If a namespace is set, return a title relative to it (default; true)
         */
        public function __construct( array $config = array() ) {
                // Parent constructor
                parent::__construct( array_merge( $config, array( 'infusable' => true ) ) );
 
-               // Properties
+               // Properties, which are ignored in PHP and just shipped back to JS
                if ( isset( $config['namespace'] ) ) {
-                       // Actually ignored in PHP, we just ship it back to JS
                        $this->namespace = $config['namespace'];
                }
 
+               if ( isset( $config['relative'] ) ) {
+                       $this->relative = $config['relative'];
+               }
+
                // Initialization
                $this->addClasses( array( 'mw-widget-TitleInputWidget' ) );
        }
@@ -38,6 +43,9 @@ class TitleInputWidget extends TextInputWidget {
                if ( $this->namespace !== null ) {
                        $config['namespace'] = $this->namespace;
                }
+               if ( $this->relative !== null ) {
+                       $config['relative'] = $this->relative;
+               }
                return parent::getConfig( $config );
        }
 }
index 221de0f..df2f1a7 100644 (file)
@@ -16,6 +16,7 @@
         * @param {Object} [config] Configuration options
         * @cfg {number} [limit=10] Number of results to show
         * @cfg {number} [namespace] Namespace to prepend to queries
+        * @cfg {boolean} [relative=true] If a namespace is set, return a title relative to it
         * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
         * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
         * @cfg {boolean} [showImages] Show page images
@@ -37,6 +38,7 @@
                // Properties
                this.limit = config.limit || 10;
                this.namespace = config.namespace || null;
+               this.relative = config.relative !== undefined ? config.relative : true;
                this.showRedirectTargets = config.showRedirectTargets !== false;
                this.showRedlink = !!config.showRedlink;
                this.showImages = !!config.showImages;
        mw.widgets.TitleInputWidget.prototype.getOptionWidgetData = function ( title, data ) {
                var mwTitle = new mw.Title( title );
                return {
-                       data: this.namespace !== null ? mwTitle.getRelativeText( this.namespace ) : title,
+                       data: this.namespace !== null && this.relative
+                               ? mwTitle.getRelativeText( this.namespace )
+                               : title,
                        imageUrl: this.showImages ? data.imageUrl : null,
                        description: this.showDescriptions ? data.description : null,
                        missing: data.missing,