3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use HtmlFormatter\HtmlFormatter
;
24 use MediaWiki\MediaWikiServices
;
27 * Class to output help for an API module
29 * @since 1.25 completely rewritten
32 class ApiHelp
extends ApiBase
{
33 public function execute() {
34 $params = $this->extractRequestParams();
37 foreach ( $params['modules'] as $path ) {
38 $modules[] = $this->getModuleFromPath( $path );
42 $context = new DerivativeContext( $this->getMain()->getContext() );
43 $context->setSkin( SkinFactory
::getDefaultInstance()->makeSkin( 'apioutput' ) );
44 $context->setLanguage( $this->getMain()->getLanguage() );
45 $context->setTitle( SpecialPage
::getTitleFor( 'ApiHelp' ) );
46 $out = new OutputPage( $context );
47 $out->setRobotPolicy( 'noindex,nofollow' );
48 $out->setCopyrightUrl( 'https://www.mediawiki.org/wiki/Special:MyLanguage/Copyright' );
49 $context->setOutput( $out );
51 self
::getHelp( $context, $modules, $params );
53 // Grab the output from the skin
55 $context->getOutput()->output();
56 $html = ob_get_clean();
58 $result = $this->getResult();
59 if ( $params['wrap'] ) {
61 'mime' => 'text/html',
62 'filename' => 'api-help.html',
65 ApiResult
::setSubelementsList( $data, 'help' );
66 $result->addValue( null, $this->getModuleName(), $data );
69 $result->addValue( null, 'text', $html, ApiResult
::NO_SIZE_CHECK
);
70 $result->addValue( null, 'mime', 'text/html', ApiResult
::NO_SIZE_CHECK
);
71 $result->addValue( null, 'filename', 'api-help.html', ApiResult
::NO_SIZE_CHECK
);
76 * Generate help for the specified modules
78 * Help is placed into the OutputPage object returned by
79 * $context->getOutput().
81 * Recognized options include:
82 * - headerlevel: (int) Header tag level
83 * - nolead: (bool) Skip the inclusion of api-help-lead
84 * - noheader: (bool) Skip the inclusion of the top-level section headers
85 * - submodules: (bool) Include help for submodules of the current module
86 * - recursivesubmodules: (bool) Include help for submodules recursively
87 * - helptitle: (string) Title to link for additional modules' help. Should contain $1.
88 * - toc: (bool) Include a table of contents
90 * @param IContextSource $context
91 * @param ApiBase[]|ApiBase $modules
92 * @param array $options Formatting options (described above)
94 public static function getHelp( IContextSource
$context, $modules, array $options ) {
95 if ( !is_array( $modules ) ) {
96 $modules = [ $modules ];
99 $out = $context->getOutput();
100 $out->addModuleStyles( [
104 if ( !empty( $options['toc'] ) ) {
105 $out->addModules( 'mediawiki.toc' );
106 $out->addModuleStyles( 'mediawiki.toc.styles' );
108 $out->setPageTitle( $context->msg( 'api-help-title' ) );
110 $services = MediaWikiServices
::getInstance();
111 $cache = $services->getMainWANObjectCache();
113 if ( count( $modules ) == 1 && $modules[0] instanceof ApiMain
&&
114 $options['recursivesubmodules'] &&
115 $context->getLanguage()->equals( $services->getContentLanguage() )
117 $cacheHelpTimeout = $context->getConfig()->get( 'APICacheHelpTimeout' );
118 if ( $cacheHelpTimeout > 0 ) {
119 // Get help text from cache if present
120 $cacheKey = $cache->makeKey( 'apihelp', $modules[0]->getModulePath(),
121 (int)!empty( $options['toc'] ),
122 str_replace( ' ', '_', SpecialVersion
::getVersion( 'nodb' ) ) );
123 $cached = $cache->get( $cacheKey );
125 $out->addHTML( $cached );
130 if ( $out->getHTML() !== '' ) {
131 // Don't save to cache, there's someone else's content in the page
136 $options['recursivesubmodules'] = !empty( $options['recursivesubmodules'] );
137 $options['submodules'] = $options['recursivesubmodules'] ||
!empty( $options['submodules'] );
140 if ( empty( $options['nolead'] ) ) {
141 $msg = $context->msg( 'api-help-lead' );
142 if ( !$msg->isDisabled() ) {
143 $out->addHTML( $msg->parseAsBlock() );
148 $html = self
::getHelpInternal( $context, $modules, $options, $haveModules );
149 if ( !empty( $options['toc'] ) && $haveModules ) {
150 $out->addHTML( Linker
::generateTOC( $haveModules, $context->getLanguage() ) );
152 $out->addHTML( $html );
154 $helptitle = $options['helptitle'] ??
null;
155 $html = self
::fixHelpLinks( $out->getHTML(), $helptitle, $haveModules );
157 $out->addHTML( $html );
159 if ( $cacheKey !== null ) {
160 $cache->set( $cacheKey, $out->getHTML(), $cacheHelpTimeout );
165 * Replace Special:ApiHelp links with links to api.php
167 * @param string $html
168 * @param string|null $helptitle Title to link to rather than api.php, must contain '$1'
169 * @param array $localModules Keys are modules to link within the current page, values are ignored
172 public static function fixHelpLinks( $html, $helptitle = null, $localModules = [] ) {
173 $formatter = new HtmlFormatter( $html );
174 $doc = $formatter->getDoc();
175 $xpath = new DOMXPath( $doc );
176 $nodes = $xpath->query( '//a[@href][not(contains(@class,\'apihelp-linktrail\'))]' );
177 foreach ( $nodes as $node ) {
178 $href = $node->getAttribute( 'href' );
181 $href = rawurldecode( $href );
182 } while ( $old !== $href );
183 if ( preg_match( '!Special:ApiHelp/([^&/|#]+)((?:#.*)?)!', $href, $m ) ) {
184 if ( isset( $localModules[$m[1]] ) ) {
185 $href = $m[2] === '' ?
'#' . $m[1] : $m[2];
186 } elseif ( $helptitle !== null ) {
187 $href = Title
::newFromText( str_replace( '$1', $m[1], $helptitle ) . $m[2] )
190 $href = wfAppendQuery( wfScript( 'api' ), [
195 $node->setAttribute( 'href', $href );
196 $node->removeAttribute( 'title' );
200 return $formatter->getText();
204 * Wrap a message in HTML with a class.
206 * @param Message $msg
207 * @param string $class
211 private static function wrap( Message
$msg, $class, $tag = 'span' ) {
212 return Html
::rawElement( $tag, [ 'class' => $class ],
218 * Recursively-called function to actually construct the help
220 * @param IContextSource $context
221 * @param ApiBase[] $modules
222 * @param array $options
223 * @param array &$haveModules
226 private static function getHelpInternal( IContextSource
$context, array $modules,
227 array $options, &$haveModules
231 $level = empty( $options['headerlevel'] ) ?
2 : $options['headerlevel'];
232 if ( empty( $options['tocnumber'] ) ) {
233 $tocnumber = [ 2 => 0 ];
235 $tocnumber = &$options['tocnumber'];
238 foreach ( $modules as $module ) {
239 $tocnumber[$level]++
;
240 $path = $module->getModulePath();
241 $module->setContext( $context );
252 if ( empty( $options['noheader'] ) ||
!empty( $options['toc'] ) ) {
255 while ( isset( $haveModules[$anchor] ) ) {
256 $anchor = $path . '|' . ++
$i;
259 if ( $module->isMain() ) {
260 $headerContent = $context->msg( 'api-help-main-header' )->parse();
262 'class' => 'apihelp-header',
265 $name = $module->getModuleName();
266 $headerContent = $module->getParent()->getModuleManager()->getModuleGroup( $name ) .
268 if ( $module->getModulePrefix() !== '' ) {
269 $headerContent .= ' ' .
270 $context->msg( 'parentheses', $module->getModulePrefix() )->parse();
272 // Module names are always in English and not localized,
273 // so English language and direction must be set explicitly,
274 // otherwise parentheses will get broken in RTL wikis
276 'class' => 'apihelp-header apihelp-module-name',
282 $headerAttr['id'] = $anchor;
284 $haveModules[$anchor] = [
285 'toclevel' => count( $tocnumber ),
288 'line' => $headerContent,
289 'number' => implode( '.', $tocnumber ),
292 if ( empty( $options['noheader'] ) ) {
293 $help['header'] .= Html
::element(
294 'h' . min( 6, $level ),
300 $haveModules[$path] = true;
305 for ( $m = $module; $m !== null; $m = $m->getParent() ) {
306 $name = $m->getModuleName();
307 if ( $name === 'main_int' ) {
311 if ( count( $modules ) === 1 && $m === $modules[0] &&
312 !( !empty( $options['submodules'] ) && $m->getModuleManager() )
314 $link = Html
::element( 'b', [ 'dir' => 'ltr', 'lang' => 'en' ], $name );
316 $link = SpecialPage
::getTitleFor( 'ApiHelp', $m->getModulePath() )->getLocalURL();
317 $link = Html
::element( 'a',
318 [ 'href' => $link, 'class' => 'apihelp-linktrail', 'dir' => 'ltr', 'lang' => 'en' ],
323 array_unshift( $links, $link );
326 $help['header'] .= self
::wrap(
327 $context->msg( 'parentheses' )
328 ->rawParams( $context->getLanguage()->pipeList( $links ) ),
329 'apihelp-linktrail', 'div'
333 $flags = $module->getHelpFlags();
334 $help['flags'] .= Html
::openElement( 'div',
335 [ 'class' => 'apihelp-block apihelp-flags' ] );
336 $msg = $context->msg( 'api-help-flags' );
337 if ( !$msg->isDisabled() ) {
338 $help['flags'] .= self
::wrap(
339 $msg->numParams( count( $flags ) ), 'apihelp-block-head', 'div'
342 $help['flags'] .= Html
::openElement( 'ul' );
343 foreach ( $flags as $flag ) {
344 $help['flags'] .= Html
::rawElement( 'li', null,
345 self
::wrap( $context->msg( "api-help-flag-$flag" ), "apihelp-flag-$flag" )
348 $sourceInfo = $module->getModuleSourceInfo();
350 if ( isset( $sourceInfo['namemsg'] ) ) {
351 $extname = $context->msg( $sourceInfo['namemsg'] )->text();
353 // Probably English, so wrap it.
354 $extname = Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['name'] );
356 $help['flags'] .= Html
::rawElement( 'li', null,
358 $context->msg( 'api-help-source', $extname, $sourceInfo['name'] ),
363 $link = SpecialPage
::getTitleFor( 'Version', 'License/' . $sourceInfo['name'] );
364 if ( isset( $sourceInfo['license-name'] ) ) {
365 $msg = $context->msg( 'api-help-license', $link,
366 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $sourceInfo['license-name'] )
368 } elseif ( SpecialVersion
::getExtLicenseFileName( dirname( $sourceInfo['path'] ) ) ) {
369 $msg = $context->msg( 'api-help-license-noname', $link );
371 $msg = $context->msg( 'api-help-license-unknown' );
373 $help['flags'] .= Html
::rawElement( 'li', null,
374 self
::wrap( $msg, 'apihelp-license' )
377 $help['flags'] .= Html
::rawElement( 'li', null,
378 self
::wrap( $context->msg( 'api-help-source-unknown' ), 'apihelp-source' )
380 $help['flags'] .= Html
::rawElement( 'li', null,
381 self
::wrap( $context->msg( 'api-help-license-unknown' ), 'apihelp-license' )
384 $help['flags'] .= Html
::closeElement( 'ul' );
385 $help['flags'] .= Html
::closeElement( 'div' );
387 foreach ( $module->getFinalDescription() as $msg ) {
388 $msg->setContext( $context );
389 $help['description'] .= $msg->parseAsBlock();
392 $urls = $module->getHelpUrls();
394 $help['help-urls'] .= Html
::openElement( 'div',
395 [ 'class' => 'apihelp-block apihelp-help-urls' ]
397 $msg = $context->msg( 'api-help-help-urls' );
398 if ( !$msg->isDisabled() ) {
399 $help['help-urls'] .= self
::wrap(
400 $msg->numParams( count( $urls ) ), 'apihelp-block-head', 'div'
403 if ( !is_array( $urls ) ) {
406 $help['help-urls'] .= Html
::openElement( 'ul' );
407 foreach ( $urls as $url ) {
408 $help['help-urls'] .= Html
::rawElement( 'li', null,
409 Html
::element( 'a', [ 'href' => $url, 'dir' => 'ltr' ], $url )
412 $help['help-urls'] .= Html
::closeElement( 'ul' );
413 $help['help-urls'] .= Html
::closeElement( 'div' );
416 $params = $module->getFinalParams( ApiBase
::GET_VALUES_FOR_HELP
);
417 $dynamicParams = $module->dynamicParameterDocumentation();
419 if ( $params ||
$dynamicParams !== null ) {
420 $help['parameters'] .= Html
::openElement( 'div',
421 [ 'class' => 'apihelp-block apihelp-parameters' ]
423 $msg = $context->msg( 'api-help-parameters' );
424 if ( !$msg->isDisabled() ) {
425 $help['parameters'] .= self
::wrap(
426 $msg->numParams( count( $params ) ), 'apihelp-block-head', 'div'
429 $help['parameters'] .= Html
::openElement( 'dl' );
431 $descriptions = $module->getFinalParamDescription();
433 foreach ( $params as $name => $settings ) {
434 if ( !is_array( $settings ) ) {
435 $settings = [ ApiBase
::PARAM_DFLT
=> $settings ];
438 $help['parameters'] .= Html
::rawElement( 'dt', null,
439 Html
::element( 'span', [ 'dir' => 'ltr', 'lang' => 'en' ], $module->encodeParamName( $name ) )
444 if ( isset( $descriptions[$name] ) ) {
445 foreach ( $descriptions[$name] as $msg ) {
446 $msg->setContext( $context );
447 $description[] = $msg->parseAsBlock();
455 if ( !empty( $settings[ApiBase
::PARAM_REQUIRED
] ) ) {
456 $info[] = $context->msg( 'api-help-param-required' )->parse();
460 if ( !empty( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] ) ) {
461 foreach ( $settings[ApiBase
::PARAM_HELP_MSG_INFO
] as $i ) {
462 $tag = array_shift( $i );
463 $info[] = $context->msg( "apihelp-{$path}-paraminfo-{$tag}" )
464 ->numParams( count( $i ) )
465 ->params( $context->getLanguage()->commaList( $i ) )
466 ->params( $module->getModulePrefix() )
472 if ( !empty( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] ) ) {
474 $msg = 'api-help-param-templated-var-first';
475 foreach ( $settings[ApiBase
::PARAM_TEMPLATE_VARS
] as $k => $v ) {
476 $vars[] = $context->msg( $msg, $k, $module->encodeParamName( $v ) );
477 $msg = 'api-help-param-templated-var';
479 $info[] = $context->msg( 'api-help-param-templated' )
480 ->numParams( count( $vars ) )
481 ->params( Message
::listParam( $vars ) )
485 // Type documentation
486 if ( !isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
487 $dflt = $settings[ApiBase
::PARAM_DFLT
] ??
null;
488 if ( is_bool( $dflt ) ) {
489 $settings[ApiBase
::PARAM_TYPE
] = 'boolean';
490 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
491 $settings[ApiBase
::PARAM_TYPE
] = 'string';
492 } elseif ( is_int( $dflt ) ) {
493 $settings[ApiBase
::PARAM_TYPE
] = 'integer';
496 if ( isset( $settings[ApiBase
::PARAM_TYPE
] ) ) {
497 $type = $settings[ApiBase
::PARAM_TYPE
];
498 $multi = !empty( $settings[ApiBase
::PARAM_ISMULTI
] );
499 $hintPipeSeparated = true;
500 $count = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
501 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] +
1
502 : ApiBase
::LIMIT_SML2 +
1;
504 if ( is_array( $type ) ) {
505 $count = count( $type );
506 $deprecatedValues = $settings[ApiBase
::PARAM_DEPRECATED_VALUES
] ??
[];
507 $links = $settings[ApiBase
::PARAM_VALUE_LINKS
] ??
[];
508 $values = array_map( function ( $v ) use ( $links, $deprecatedValues ) {
511 // We can't know whether this contains LTR or RTL text.
512 $attr['dir'] = 'auto';
514 if ( isset( $deprecatedValues[$v] ) ) {
515 $attr['class'] = 'apihelp-deprecated-value';
517 $ret = $attr ? Html
::element( 'span', $attr, $v ) : $v;
518 if ( isset( $links[$v] ) ) {
519 $ret = "[[{$links[$v]}|$ret]]";
523 $i = array_search( '', $type, true );
524 if ( $i === false ) {
525 $values = $context->getLanguage()->commaList( $values );
527 unset( $values[$i] );
528 $values = $context->msg( 'api-help-param-list-can-be-empty' )
529 ->numParams( count( $values ) )
530 ->params( $context->getLanguage()->commaList( $values ) )
533 $info[] = $context->msg( 'api-help-param-list' )
534 ->params( $multi ?
2 : 1 )
537 $hintPipeSeparated = false;
543 if ( isset( $settings[ApiBase
::PARAM_SUBMODULE_MAP
] ) ) {
544 $map = $settings[ApiBase
::PARAM_SUBMODULE_MAP
];
547 $prefix = $module->isMain() ?
'' : ( $module->getModulePath() . '+' );
549 foreach ( $module->getModuleManager()->getNames( $name ) as $submoduleName ) {
550 $map[$submoduleName] = $prefix . $submoduleName;
552 $defaultAttrs = [ 'dir' => 'ltr', 'lang' => 'en' ];
557 $deprecatedSubmodules = [];
558 foreach ( $map as $v => $m ) {
559 $attrs = $defaultAttrs;
562 $submod = $module->getModuleFromPath( $m );
564 if ( $submod->isDeprecated() ) {
565 $arr = &$deprecatedSubmodules;
566 $attrs['class'] = 'apihelp-deprecated-value';
569 } catch ( ApiUsageException
$ex ) {
573 $v = Html
::element( 'span', $attrs, $v );
575 $arr[] = "[[Special:ApiHelp/{$m}|{$v}]]";
577 $submodules = array_merge( $submodules, $deprecatedSubmodules );
578 $count = count( $submodules );
579 $info[] = $context->msg( 'api-help-param-list' )
580 ->params( $multi ?
2 : 1 )
581 ->params( $context->getLanguage()->commaList( $submodules ) )
583 $hintPipeSeparated = false;
584 // No type message necessary, we have a list of values.
589 $namespaces = MWNamespace
::getValidNamespaces();
590 if ( isset( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] ) &&
591 is_array( $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] )
593 $namespaces = array_merge( $namespaces, $settings[ApiBase
::PARAM_EXTRA_NAMESPACES
] );
596 $count = count( $namespaces );
597 $info[] = $context->msg( 'api-help-param-list' )
598 ->params( $multi ?
2 : 1 )
599 ->params( $context->getLanguage()->commaList( $namespaces ) )
601 $hintPipeSeparated = false;
602 // No type message necessary, we have a list of values.
607 $tags = ChangeTags
::listExplicitlyDefinedTags();
608 $count = count( $tags );
609 $info[] = $context->msg( 'api-help-param-list' )
610 ->params( $multi ?
2 : 1 )
611 ->params( $context->getLanguage()->commaList( $tags ) )
613 $hintPipeSeparated = false;
618 if ( isset( $settings[ApiBase
::PARAM_MAX2
] ) ) {
619 $info[] = $context->msg( 'api-help-param-limit2' )
620 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
621 ->numParams( $settings[ApiBase
::PARAM_MAX2
] )
624 $info[] = $context->msg( 'api-help-param-limit' )
625 ->numParams( $settings[ApiBase
::PARAM_MAX
] )
631 // Possible messages:
632 // api-help-param-integer-min,
633 // api-help-param-integer-max,
634 // api-help-param-integer-minmax
637 if ( isset( $settings[ApiBase
::PARAM_MIN
] ) ) {
639 $min = $settings[ApiBase
::PARAM_MIN
];
641 if ( isset( $settings[ApiBase
::PARAM_MAX
] ) ) {
643 $max = $settings[ApiBase
::PARAM_MAX
];
645 if ( $suffix !== '' ) {
647 $context->msg( "api-help-param-integer-$suffix" )
648 ->params( $multi ?
2 : 1 )
649 ->numParams( $min, $max )
655 $info[] = $context->msg( 'api-help-param-upload' )
657 // No type message necessary, api-help-param-upload should handle it.
663 // Displaying a type message here would be useless.
669 // Add type. Messages for grep: api-help-param-type-limit
670 // api-help-param-type-integer api-help-param-type-boolean
671 // api-help-param-type-timestamp api-help-param-type-user
672 // api-help-param-type-password
673 if ( is_string( $type ) ) {
674 $msg = $context->msg( "api-help-param-type-$type" );
675 if ( !$msg->isDisabled() ) {
676 $info[] = $msg->params( $multi ?
2 : 1 )->parse();
682 $lowcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT1
] )
683 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT1
]
684 : ApiBase
::LIMIT_SML1
;
685 $highcount = !empty( $settings[ApiBase
::PARAM_ISMULTI_LIMIT2
] )
686 ?
$settings[ApiBase
::PARAM_ISMULTI_LIMIT2
]
687 : ApiBase
::LIMIT_SML2
;
689 if ( $hintPipeSeparated ) {
690 $extra[] = $context->msg( 'api-help-param-multi-separate' )->parse();
692 if ( $count > $lowcount ) {
693 if ( $lowcount === $highcount ) {
694 $msg = $context->msg( 'api-help-param-multi-max-simple' )
695 ->numParams( $lowcount );
697 $msg = $context->msg( 'api-help-param-multi-max' )
698 ->numParams( $lowcount, $highcount );
700 $extra[] = $msg->parse();
703 $info[] = implode( ' ', $extra );
706 $allowAll = $settings[ApiBase
::PARAM_ALL
] ??
false;
707 if ( $allowAll ||
$settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
708 if ( $settings[ApiBase
::PARAM_TYPE
] === 'namespace' ) {
709 $allSpecifier = ApiBase
::ALL_DEFAULT_STRING
;
711 $allSpecifier = ( is_string( $allowAll ) ?
$allowAll : ApiBase
::ALL_DEFAULT_STRING
);
713 $info[] = $context->msg( 'api-help-param-multi-all' )
714 ->params( $allSpecifier )
720 if ( isset( $settings[self
::PARAM_MAX_BYTES
] ) ) {
721 $info[] = $context->msg( 'api-help-param-maxbytes' )
722 ->numParams( $settings[self
::PARAM_MAX_BYTES
] );
724 if ( isset( $settings[self
::PARAM_MAX_CHARS
] ) ) {
725 $info[] = $context->msg( 'api-help-param-maxchars' )
726 ->numParams( $settings[self
::PARAM_MAX_CHARS
] );
730 $default = $settings[ApiBase
::PARAM_DFLT
] ??
null;
731 if ( $default === '' ) {
732 $info[] = $context->msg( 'api-help-param-default-empty' )
734 } elseif ( $default !== null && $default !== false ) {
735 // We can't know whether this contains LTR or RTL text.
736 $info[] = $context->msg( 'api-help-param-default' )
737 ->params( Html
::element( 'span', [ 'dir' => 'auto' ], $default ) )
741 if ( !array_filter( $description ) ) {
742 $description = [ self
::wrap(
743 $context->msg( 'api-help-param-no-description' ),
748 // Add "deprecated" flag
749 if ( !empty( $settings[ApiBase
::PARAM_DEPRECATED
] ) ) {
750 $help['parameters'] .= Html
::openElement( 'dd',
751 [ 'class' => 'info' ] );
752 $help['parameters'] .= self
::wrap(
753 $context->msg( 'api-help-param-deprecated' ),
754 'apihelp-deprecated', 'strong'
756 $help['parameters'] .= Html
::closeElement( 'dd' );
759 if ( $description ) {
760 $description = implode( '', $description );
761 $description = preg_replace( '!\s*</([oud]l)>\s*<\1>\s*!', "\n", $description );
762 $help['parameters'] .= Html
::rawElement( 'dd',
763 [ 'class' => 'description' ], $description );
766 foreach ( $info as $i ) {
767 $help['parameters'] .= Html
::rawElement( 'dd', [ 'class' => 'info' ], $i );
771 if ( $dynamicParams !== null ) {
772 $dynamicParams = ApiBase
::makeMessage( $dynamicParams, $context, [
773 $module->getModulePrefix(),
774 $module->getModuleName(),
775 $module->getModulePath()
777 $help['parameters'] .= Html
::element( 'dt', null, '*' );
778 $help['parameters'] .= Html
::rawElement( 'dd',
779 [ 'class' => 'description' ], $dynamicParams->parse() );
782 $help['parameters'] .= Html
::closeElement( 'dl' );
783 $help['parameters'] .= Html
::closeElement( 'div' );
786 $examples = $module->getExamplesMessages();
788 $help['examples'] .= Html
::openElement( 'div',
789 [ 'class' => 'apihelp-block apihelp-examples' ] );
790 $msg = $context->msg( 'api-help-examples' );
791 if ( !$msg->isDisabled() ) {
792 $help['examples'] .= self
::wrap(
793 $msg->numParams( count( $examples ) ), 'apihelp-block-head', 'div'
797 $help['examples'] .= Html
::openElement( 'dl' );
798 foreach ( $examples as $qs => $msg ) {
799 $msg = ApiBase
::makeMessage( $msg, $context, [
800 $module->getModulePrefix(),
801 $module->getModuleName(),
802 $module->getModulePath()
805 $link = wfAppendQuery( wfScript( 'api' ), $qs );
806 $sandbox = SpecialPage
::getTitleFor( 'ApiSandbox' )->getLocalURL() . '#' . $qs;
807 $help['examples'] .= Html
::rawElement( 'dt', null, $msg->parse() );
808 $help['examples'] .= Html
::rawElement( 'dd', null,
809 Html
::element( 'a', [ 'href' => $link, 'dir' => 'ltr' ], "api.php?$qs" ) . ' ' .
810 Html
::rawElement( 'a', [ 'href' => $sandbox ],
811 $context->msg( 'api-help-open-in-apisandbox' )->parse() )
814 $help['examples'] .= Html
::closeElement( 'dl' );
815 $help['examples'] .= Html
::closeElement( 'div' );
818 $subtocnumber = $tocnumber;
819 $subtocnumber[$level +
1] = 0;
821 'submodules' => $options['recursivesubmodules'],
822 'headerlevel' => $level +
1,
823 'tocnumber' => &$subtocnumber,
827 if ( $options['submodules'] && $module->getModuleManager() ) {
828 $manager = $module->getModuleManager();
830 foreach ( $groups as $group ) {
831 $names = $manager->getNames( $group );
833 foreach ( $names as $name ) {
834 $submodules[] = $manager->getModule( $name );
837 $help['submodules'] .= self
::getHelpInternal(
845 $module->modifyHelp( $help, $suboptions, $haveModules );
847 Hooks
::run( 'APIHelpModifyOutput', [ $module, &$help, $suboptions, &$haveModules ] );
849 $out .= implode( "\n", $help );
855 public function shouldCheckMaxlag() {
859 public function isReadMode() {
863 public function getCustomPrinter() {
864 $params = $this->extractRequestParams();
865 if ( $params['wrap'] ) {
869 $main = $this->getMain();
870 $errorPrinter = $main->createPrinterByName( $main->getParameter( 'format' ) );
871 return new ApiFormatRaw( $main, $errorPrinter );
874 public function getAllowedParams() {
877 ApiBase
::PARAM_DFLT
=> 'main',
878 ApiBase
::PARAM_ISMULTI
=> true,
880 'submodules' => false,
881 'recursivesubmodules' => false,
887 protected function getExamplesMessages() {
890 => 'apihelp-help-example-main',
891 'action=help&modules=query&submodules=1'
892 => 'apihelp-help-example-submodules',
893 'action=help&recursivesubmodules=1'
894 => 'apihelp-help-example-recursive',
895 'action=help&modules=help'
896 => 'apihelp-help-example-help',
897 'action=help&modules=query+info|query+categorymembers'
898 => 'apihelp-help-example-query',
902 public function getHelpUrls() {
904 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Main_page',
905 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:FAQ',
906 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Quick_start_guide',