Merge "mediawiki.api.watch: Use formatversion=2 for API requests"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.apisandbox.js
index 64cfcf5..43321fe 100644 (file)
@@ -1,4 +1,4 @@
-/*global OO */
+/* eslint-disable no-use-before-define */
 ( function ( $, mw, OO ) {
        'use strict';
        var ApiSandbox, Util, WidgetMethods, Validators,
@@ -51,9 +51,7 @@
                tokenWidget: {
                        alertTokenError: function ( code, error ) {
                                windowManager.openWindow( 'errorAlert', {
-                                       title: mw.message(
-                                               'apisandbox-results-fixtoken-fail', this.paramInfo.tokentype
-                                       ).parse(),
+                                       title: Util.parseMsg( 'apisandbox-results-fixtoken-fail', this.paramInfo.tokentype ),
                                        message: error,
                                        actions: [
                                                {
                                }
                        },
                        apiCheckValid: function () {
-                               var ok = this.getApiValue() !== undefined || suppressErrors;
+                               var ok = true,
+                                       pi = this.paramInfo;
+
+                               if ( !suppressErrors ) {
+                                       ok = this.getApiValue() !== undefined && !(
+                                               pi.allspecifier !== undefined &&
+                                               this.getItemsData().length > 1 &&
+                                               this.getItemsData().indexOf( pi.allspecifier ) !== -1
+                                       );
+                               }
+
                                this.setIcon( ok ? null : 'alert' );
                                this.setIconTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() );
                                return $.Deferred().resolve( ok ).promise();
        };
 
        /**
-        * @class mw.special.ApiSandbox.Utils
+        * @class mw.special.ApiSandbox.Util
         * @private
         */
        Util = {
                                        break;
 
                                case 'limit':
-                                       widget = new OO.ui.NumberInputWidget( {
-                                               required: Util.apiBool( pi.required ),
-                                               isInteger: true
+                                       widget = new OO.ui.TextInputWidget( {
+                                               required: Util.apiBool( pi.required )
                                        } );
-                                       widget.setIcon = widget.input.setIcon.bind( widget.input );
-                                       widget.setIconTitle = widget.input.setIconTitle.bind( widget.input );
-                                       widget.getValidity = widget.input.getValidity.bind( widget.input );
-                                       widget.input.setValidation( function ( value ) {
-                                               return value === 'max' || widget.validateNumber( value );
+                                       widget.setValidation( function ( value ) {
+                                               var n, pi = this.paramInfo;
+
+                                               if ( value === 'max' ) {
+                                                       return true;
+                                               } else {
+                                                       n = +value;
+                                                       return !isNaN( n ) && isFinite( n ) &&
+                                                               Math.floor( n ) === n &&
+                                                               n >= pi.min && n <= pi.apiSandboxMax;
+                                               }
                                        } );
+                                       pi.min = pi.min || 0;
+                                       pi.apiSandboxMax = mw.config.get( 'apihighlimits' ) ? pi.highmax : pi.max;
                                        widget.paramInfo = pi;
                                        $.extend( widget, WidgetMethods.textInputWidget );
-                                       widget.setRange( pi.min || 0, mw.config.get( 'apihighlimits' ) ? pi.highmax : pi.max );
                                        multiMode = 'enter';
                                        break;
 
                                                return a.data - b.data;
                                        } );
                                        if ( Util.apiBool( pi.multi ) ) {
+                                               if ( pi.allspecifier !== undefined ) {
+                                                       items.unshift( new OO.ui.MenuOptionWidget( {
+                                                               data: pi.allspecifier,
+                                                               label: mw.message( 'apisandbox-multivalue-all-namespaces', pi.allspecifier ).text()
+                                                       } ) );
+                                               }
+
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                                        menu: { items: items }
                                                } );
                                                return new OO.ui.MenuOptionWidget( { data: String( v ), label: String( v ) } );
                                        } );
                                        if ( Util.apiBool( pi.multi ) ) {
+                                               if ( pi.allspecifier !== undefined ) {
+                                                       items.unshift( new OO.ui.MenuOptionWidget( {
+                                                               data: pi.allspecifier,
+                                                               label: mw.message( 'apisandbox-multivalue-all-values', pi.allspecifier ).text()
+                                                       } ) );
+                                               }
+
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                                        menu: { items: items }
                                                } );
                },
 
                /**
-                * Parse an HTML string, adding target="_blank" to any links
+                * Parse an HTML string and call Util.fixupHTML()
                 *
                 * @param {string} html HTML to parse
                 * @return {jQuery}
                 */
                parseHTML: function ( html ) {
                        var $ret = $( $.parseHTML( html ) );
-                       $ret.filter( 'a' ).add( $ret.find( 'a' ) )
+                       return Util.fixupHTML( $ret );
+               },
+
+               /**
+                * Parse an i18n message and call Util.fixupHTML()
+                *
+                * @param {string} key Key of message to get
+                * @param {...Mixed} parameters Values for $N replacements
+                * @return {jQuery}
+                */
+               parseMsg: function () {
+                       var $ret = mw.message.apply( mw.message, arguments ).parseDom();
+                       return Util.fixupHTML( $ret );
+               },
+
+               /**
+                * Fix HTML for ApiSandbox display
+                *
+                * Fixes are:
+                * - Add target="_blank" to any links
+                *
+                * @param {jQuery} $html DOM to process
+                * @return {jQuery}
+                */
+               fixupHTML: function ( $html ) {
+                       $html.filter( 'a' ).add( $html.find( 'a' ) )
                                .filter( '[href]:not([target])' )
                                .attr( 'target', '_blank' );
-                       return $ret;
+                       return $html;
                }
        };
 
 
                        $content
                                .empty()
-                               .append( $( '<p>' ).append( mw.message( 'apisandbox-intro' ).parse() ) )
+                               .append( $( '<p>' ).append( Util.parseMsg( 'apisandbox-intro' ) ) )
                                .append(
                                        $( '<div>', { id: 'mw-apisandbox-ui' } )
                                                .append( $toolbar )
 
                /**
                 * Update the current query when the page hash changes
+                *
+                * @return {boolean} Successful
                 */
                loadFromHash: function () {
                        var params, m, re,
                        $.when.apply( $, deferreds ).done( function () {
                                if ( $.inArray( false, arguments ) !== -1 ) {
                                        windowManager.openWindow( 'errorAlert', {
-                                               title: mw.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
-                                               message: mw.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
+                                               title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
+                                               message: Util.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
                                                actions: [
                                                        {
                                                                action: 'accept',
                                                                readOnly: true,
                                                                value: mw.util.wikiScript( 'api' ) + '?' + query
                                                        } ), {
-                                                               label: mw.message( 'apisandbox-request-url-label' ).parse()
+                                                               label: Util.parseMsg( 'apisandbox-request-url-label' )
                                                        }
                                                ).$element,
                                                $result
 
                                                $result.empty();
                                                if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct ) ) {
-                                                       data = $.parseJSON( data );
+                                                       data = JSON.parse( data );
                                                        if ( data.modules.length ) {
                                                                mw.loader.load( data.modules );
                                                        }
                                                        if ( data.status && data.status !== 200 ) {
                                                                $( '<div>' )
                                                                        .addClass( 'api-pretty-header api-pretty-status' )
-                                                                       .append(
-                                                                               mw.message( 'api-format-prettyprint-status', data.status, data.statustext ).parse()
-                                                                       )
+                                                                       .append( Util.parseMsg( 'api-format-prettyprint-status', data.status, data.statustext ) )
                                                                        .appendTo( $result );
                                                        }
                                                        $result.append( Util.parseHTML( data.html ) );
                                                                                framed: false,
                                                                                icon: 'info',
                                                                                popup: {
-                                                                                       $content: $( '<div>' ).append( mw.message( 'apisandbox-continue-help' ).parse() ),
+                                                                                       $content: $( '<div>' ).append( Util.parseMsg( 'apisandbox-continue-help' ) ),
                                                                                        padded: true
                                                                                }
                                                                        } ).$element
 
                                if ( that.widgets[ name ] !== undefined ) {
                                        windowManager.openWindow( 'errorAlert', {
-                                               title: mw.message(
-                                                       'apisandbox-dynamic-error-exists', name
-                                               ).parse(),
+                                               title: Util.parseMsg( 'apisandbox-dynamic-error-exists', name ),
                                                actions: [
                                                        {
                                                                action: 'accept',
                                                                if ( pi.parameters[ i ].highmax !== undefined ) {
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
-                                                                               append: Util.parseHTML( mw.message(
-                                                                                       'api-help-param-limit2', pi.parameters[ i ].max, pi.parameters[ i ].highmax
-                                                                               ).parse() )
+                                                                               append: [
+                                                                                       Util.parseMsg(
+                                                                                               'api-help-param-limit2', pi.parameters[ i ].max, pi.parameters[ i ].highmax
+                                                                                       ),
+                                                                                       ' ',
+                                                                                       Util.parseMsg( 'apisandbox-param-limit' )
+                                                                               ]
                                                                        } ) );
                                                                } else {
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
-                                                                               append: Util.parseHTML( mw.message(
-                                                                                       'api-help-param-limit', pi.parameters[ i ].max
-                                                                               ).parse() )
+                                                                               append: [
+                                                                                       Util.parseMsg( 'api-help-param-limit', pi.parameters[ i ].max ),
+                                                                                       ' ',
+                                                                                       Util.parseMsg( 'apisandbox-param-limit' )
+                                                                               ]
                                                                        } ) );
                                                                }
                                                                break;
                                                                if ( tmp !== '' ) {
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
-                                                                               append: Util.parseHTML( mw.message(
+                                                                               append: Util.parseMsg(
                                                                                        'api-help-param-integer-' + tmp,
                                                                                        Util.apiBool( pi.parameters[ i ].multi ) ? 2 : 1,
                                                                                        pi.parameters[ i ].min, pi.parameters[ i ].max
-                                                                               ).parse() )
+                                                                               )
                                                                        } ) );
                                                                }
                                                                break;
                                        items.push( new OO.ui.FieldLayout(
                                                new OO.ui.Widget( {} ).toggle( false ), {
                                                        align: 'top',
-                                                       label: Util.parseHTML( mw.message( 'apisandbox-no-parameters' ).parse() )
+                                                       label: Util.parseMsg( 'apisandbox-no-parameters' )
                                                }
                                        ) );
                                }