API: Display message-per-value style help for submodule parameters
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 25 May 2017 21:31:59 +0000 (17:31 -0400)
committerAnomie <bjorsch@wikimedia.org>
Tue, 20 Jun 2017 17:35:51 +0000 (17:35 +0000)
The module's summary will be used as the description of the value.

Since these are often very long lists, this also changes ApiSandbox to
collapse <dl> lists in parameter descriptions by default.

Bug: T123930
Change-Id: I205b68a52a94cae4c1cdf7ec9fd3e8a04d565919

includes/api/ApiBase.php
includes/api/ApiHelp.php
includes/api/ApiParamInfo.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
resources/Resources.php
resources/src/mediawiki.special/mediawiki.special.apisandbox.js

index aa970f4..3a9167f 100644 (file)
@@ -2241,7 +2241,49 @@ abstract class ApiBase extends ContextSource {
                        }
                        $msgs[$param] = [ $msg ];
 
-                       if ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
+                       if ( isset( $settings[ApiBase::PARAM_TYPE] ) &&
+                               $settings[ApiBase::PARAM_TYPE] === 'submodule'
+                       ) {
+                               if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
+                                       $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
+                               } else {
+                                       $prefix = $this->isMain() ? '' : ( $this->getModulePath() . '+' );
+                                       $map = [];
+                                       foreach ( $this->getModuleManager()->getNames( $param ) as $submoduleName ) {
+                                               $map[$submoduleName] = $prefix . $submoduleName;
+                                       }
+                               }
+                               ksort( $map );
+                               $submodules = [];
+                               $deprecatedSubmodules = [];
+                               foreach ( $map as $v => $m ) {
+                                       $arr = &$submodules;
+                                       $isDeprecated = false;
+                                       $summary = null;
+                                       try {
+                                               $submod = $this->getModuleFromPath( $m );
+                                               if ( $submod ) {
+                                                       $summary = $submod->getFinalSummary();
+                                                       $isDeprecated = $submod->isDeprecated();
+                                                       if ( $isDeprecated ) {
+                                                               $arr = &$deprecatedSubmodules;
+                                                       }
+                                               }
+                                       } catch ( ApiUsageException $ex ) {
+                                               // Ignore
+                                       }
+                                       if ( $summary ) {
+                                               $key = $summary->getKey();
+                                               $params = $summary->getParams();
+                                       } else {
+                                               $key = 'api-help-undocumented-module';
+                                               $params = [ $m ];
+                                       }
+                                       $m = new ApiHelpParamValueMessage( "[[Special:ApiHelp/$m|$v]]", $key, $params, $isDeprecated );
+                                       $arr[] = $m->setContext( $this->getContext() );
+                               }
+                               $msgs[$param] = array_merge( $msgs[$param], $submodules, $deprecatedSubmodules );
+                       } elseif ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
                                if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
                                        self::dieDebug( __METHOD__,
                                                'ApiBase::PARAM_HELP_MSG_PER_VALUE is not valid' );
index 192c039..3fd29ae 100644 (file)
@@ -529,23 +529,42 @@ class ApiHelp extends ApiBase {
                                                        switch ( $type ) {
                                                                case 'submodule':
                                                                        $groups[] = $name;
+
                                                                        if ( isset( $settings[ApiBase::PARAM_SUBMODULE_MAP] ) ) {
                                                                                $map = $settings[ApiBase::PARAM_SUBMODULE_MAP];
-                                                                               ksort( $map );
-                                                                               $submodules = [];
-                                                                               foreach ( $map as $v => $m ) {
-                                                                                       $submodules[] = "[[Special:ApiHelp/{$m}|{$v}]]";
-                                                                               }
+                                                                               $defaultAttrs = [];
                                                                        } else {
-                                                                               $submodules = $module->getModuleManager()->getNames( $name );
-                                                                               sort( $submodules );
-                                                                               $prefix = $module->isMain()
-                                                                                       ? '' : ( $module->getModulePath() . '+' );
-                                                                               $submodules = array_map( function ( $name ) use ( $prefix ) {
-                                                                                       $text = Html::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
-                                                                                       return "[[Special:ApiHelp/{$prefix}{$name}|{$text}]]";
-                                                                               }, $submodules );
+                                                                               $prefix = $module->isMain() ? '' : ( $module->getModulePath() . '+' );
+                                                                               $map = [];
+                                                                               foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
+                                                                                       $map[$submoduleName] = $prefix . $submoduleName;
+                                                                               }
+                                                                               $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
+                                                                       }
+                                                                       ksort( $map );
+
+                                                                       $submodules = [];
+                                                                       $deprecatedSubmodules = [];
+                                                                       foreach ( $map as $v => $m ) {
+                                                                               $attrs = $defaultAttrs;
+                                                                               $arr = &$submodules;
+                                                                               try {
+                                                                                       $submod = $module->getModuleFromPath( $m );
+                                                                                       if ( $submod ) {
+                                                                                               if ( $submod->isDeprecated() ) {
+                                                                                                       $arr = &$deprecatedSubmodules;
+                                                                                                       $attrs['class'] = 'apihelp-deprecated-value';
+                                                                                               }
+                                                                                       }
+                                                                               } catch ( ApiUsageException $ex ) {
+                                                                                       // Ignore
+                                                                               }
+                                                                               if ( $attrs ) {
+                                                                                       $v = Html::element( 'span', $attrs, $v );
+                                                                               }
+                                                                               $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
                                                                        }
+                                                                       $submodules = array_merge( $submodules, $deprecatedSubmodules );
                                                                        $count = count( $submodules );
                                                                        $info[] = $context->msg( 'api-help-param-list' )
                                                                                ->params( $multi ? 2 : 1 )
index 3be743d..4ce0e9f 100644 (file)
@@ -401,6 +401,25 @@ class ApiParamInfo extends ApiBase {
                                        if ( isset( $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX] ) ) {
                                                $item['submoduleparamprefix'] = $settings[ApiBase::PARAM_SUBMODULE_PARAM_PREFIX];
                                        }
+
+                                       $deprecatedSubmodules = [];
+                                       foreach ( $item['submodules'] as $v => $submodulePath ) {
+                                               try {
+                                                       $submod = $this->getModuleFromPath( $submodulePath );
+                                                       if ( $submod && $submod->isDeprecated() ) {
+                                                               $deprecatedSubmodules[] = $v;
+                                                       }
+                                               } catch ( ApiUsageException $ex ) {
+                                                       // Ignore
+                                               }
+                                       }
+                                       if ( $deprecatedSubmodules ) {
+                                               $item['type'] = array_merge(
+                                                       array_diff( $item['type'], $deprecatedSubmodules ),
+                                                       $deprecatedSubmodules
+                                               );
+                                               $item['deprecatedvalues'] = $deprecatedSubmodules;
+                                       }
                                } elseif ( $settings[ApiBase::PARAM_TYPE] === 'tags' ) {
                                        $item['type'] = ChangeTags::listExplicitlyDefinedTags();
                                } else {
index 2a4dc0b..9ce10b9 100644 (file)
        "api-help-title": "MediaWiki API help",
        "api-help-lead": "This is an auto-generated MediaWiki API documentation page.\n\nDocumentation and examples: https://www.mediawiki.org/wiki/API",
        "api-help-main-header": "Main module",
+       "api-help-undocumented-module": "No documentation for module $1.",
        "api-help-fallback-description": "$1",
        "api-help-fallback-parameter": "$1",
        "api-help-fallback-example": "$1",
index 9bcfdb0..bde0707 100644 (file)
        "api-help-title": "Page title for the auto-generated help output",
        "api-help-lead": "Text displayed at the top of the API help page",
        "api-help-main-header": "Text for the header of the main module",
+       "api-help-undocumented-module": "Text displayed for the summary of a submodule parameter when the module can't be loaded.\n\nParameters:\n* $1 - The module path.",
        "api-help-fallback-description": "{{notranslate}}",
        "api-help-fallback-parameter": "{{notranslate}}",
        "api-help-fallback-example": "{{notranslate}}",
index 864f93e..149bb0d 100644 (file)
@@ -1886,6 +1886,7 @@ return [
                        'oojs-ui.styles.icons-interactions',
                        'oojs-ui.styles.icons-moderation',
                        'mediawiki.widgets.datetime',
+                       'jquery.makeCollapsible',
                ],
                'messages' => [
                        'apisandbox-intro',
index c32f953..3bd3bc8 100644 (file)
                                                }
 
                                                descriptionContainer = $( '<div>' );
-                                               descriptionContainer.append( $( '<div>', {
-                                                       addClass: 'description',
-                                                       append: Util.parseHTML( pi.parameters[ i ].description )
-                                               } ) );
+
+                                               tmp = Util.parseHTML( pi.parameters[ i ].description );
+                                               tmp.filter( 'dl' ).makeCollapsible( {
+                                                       collapsed: true
+                                               } ).children( '.mw-collapsible-toggle' ).each( function () {
+                                                       var $this = $( this );
+                                                       $this.parent().prev( 'p' ).append( $this );
+                                               } );
+                                               descriptionContainer.append( $( '<div>', { addClass: 'description', append: tmp } ) );
+
                                                if ( pi.parameters[ i ].info && pi.parameters[ i ].info.length ) {
                                                        for ( j = 0; j < pi.parameters[ i ].info.length; j++ ) {
                                                                descriptionContainer.append( $( '<div>', {